// jCarousellite has been modified - please do not upgrade script unless you take this into consideration... :-)

if(typeof console === "undefined") {
	console = { log: function() {}};
}

// ENDORSEMENTS GALLERY
function endorsementsGall(){
	var stay_time = 5000; //in ms
	var change_speed = 600; //in ms
	$('ul.endorsements-hold').each(function(){
		var _hold = $(this);
		var _list = _hold.children();
		var _a = _list.index(_list.filter('.active:eq(0)'));
		if(_a == -1) _a = 0;
		var _t, _f = true;
		
		_list.removeClass('active').css('opacity', 0).eq(_a).addClass('active').css('opacity', 1);
		_hold.mouseenter(function(){
			_f = false;
			if(_t) clearTimeout(_t);
		}).mouseleave(function(){
			_f = true;
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time);
			}
		});
		if(_f && stay_time){
			_t = setTimeout(function(){
				if(_a < _list.length - 1) changeEl(_a + 1);
				else changeEl(0);
			}, stay_time);
		}
		function changeEl(_ind){
			if(_t) clearTimeout(_t);
			if(_ind != _a){
				_hold.stop().height(_hold.height());
				_list.eq(_a).stop().removeClass('active').animate({opacity: 0}, change_speed);
				_list.eq(_ind).stop().addClass('active').animate({opacity: 1}, change_speed);
				_hold.animate({height: _list.eq(_ind).outerHeight()}, change_speed/2, function(){ $(this).height('auto');});
				_a = _ind;
			}
			if(_f && stay_time){
				_t = setTimeout(function(){
					if(_a < _list.length - 1) changeEl(_a + 1);
					else changeEl(0);
				}, stay_time + change_speed);
			}
		}
	});
}

// ACCORDION
jQuery.fn.acc = function(_options){
	var _options = jQuery.extend({
		speed: 400,
		active: 'active',
		list: '.children()',
		opener: 'a.opener',
		slide: 'div.slide'
	}, _options);
	return this.each(function(){
		var _list = eval('$(this)' + _options.list);
		var _active = _options.active;
		var _speed = _options.speed;
		var _a = _list.index(_list.filter('.' + _active + ':eq(0)'));
		if(_a != -1) _list.removeClass(_active).eq(_a).addClass(_active);
		for(var i = 0; i < _list.length; i++){
			_list.eq(i).data('btn', _list.eq(i).find(_options.opener).eq(0));
			_list.eq(i).data('box', _list.eq(i).find(_options.slide).eq(0));
			if(i == _a) _list.eq(i).data('box').css('display', 'block');
			else _list.eq(i).data('box').css('display', 'none');
			_list.eq(i).data('btn').data('ind', i);
			_list.eq(i).data('btn').click(function(){
				changeEl($(this).data('ind'));
				return false;
			});
		}
		var anim_f = true;
		var a_h, ind_h, _k;
		function changeEl(_ind){
			if(anim_f){
				anim_f = false;
				if(_a == _ind){
					_list.eq(_a).removeClass(_active).data('box').animate({height: 0}, {
						duration: _speed,
						complete: function(){
							$(this).css({display:'none', height:'auto'});
							_a = -1;
							anim_f = true;
						}
					});
				}
				else{
					_list.eq(_ind).data('box').css('display', 'block');
					ind_h = _list.eq(_ind).data('box').outerHeight();
					_list.eq(_ind).data('box').height(0);
					if(_a != -1){
						a_h = _list.eq(_a).removeClass(_active).data('box').outerHeight();
						_k = a_h/ind_h;
					}
					_list.eq(_ind).addClass(_active).data('box').animate({height: ind_h}, {
						duration: _speed,
						step: function(t_h){
							if(_a != -1) _list.eq(_a).data('box').height(a_h - t_h*_k);
						},
						complete: function(){
							_list.eq(_ind).data('box').height('auto');
							if(_a != -1) _list.eq(_a).data('box').css({display:'none', height: 'auto'});
							_a = _ind;
							anim_f = true;
						}
					});
				}
			}
		}
	});
}


$(function() {

	
	var $ = jQuery;
	
	var CT	=	{

		interface : function(){
			$('#header .region').hover(function() {
				$(this).addClass('over');
			}, function() {
				$(this).removeClass('over');
			});
			
			if ($('div.expandable').length > 0)
			{
				try{
					$('div.expandable').expander({
						expandText:				'read more',
						userCollapseText:	'[^]'
					});	
				}catch(e){}
			}
			
			if ($('.datepicker').length > 0)
			{
				$('.datepicker').datepicker({ dateFormat: 'yy-mm-dd' });
			}			
		},

		init : function(){
			//videoGallery();
			endorsementsGall();
			
			if ($('ul.accordion').length > 0)
			{
				$('ul.accordion').acc({
					speed: 300,
					active: 'opened',
					opener: 'a.opener',
					slide: 'div.slide'
				});			
			}
			
			CT.interface();		
		}

	};
	
	CT.init();
	
	
	if ($('#video-gallery > div').length > 0) {
		
		//TRACK SOME STATE
		var isPlaying = false,
			shouldPause = false;
		
		//SETUP SLIDESHOW
		$('#video-gallery > div').jCarouselLite({
		    btnNext: ".link-next",
		    btnPrev: ".link-prev",
			beforeStart: function(a) {
				if (shouldPause) {
					$(a).find('object').each(function() {
						this.api_pause();
					});
					shouldPause = false;
				}
			},
			onClick: function() {
				if (isPlaying) {
					shouldPause = true;
				}
			},
			auto: 1400,
			speed: 3000
		});
		
		//make some corrections
		$('#video-gallery > div').css('width', 1200);
		$('#video-gallery > div').css('left', -300);
		
		//VIDEO STUFF
		window.on_play = function() {
			isPlaying = true;
			toggleCarouselState();
		};
		
		window.on_pause = function() {
			isPlaying = false;
			toggleCarouselState();
		}
		
		var toggleCarouselState = function() {
			if ($('#video-gallery > div').data('disabled') == 'yes' && isPlaying == false) {
				$('#video-gallery > div').data('disabled', 'no');
			} else {
				$('#video-gallery > div').data('disabled', 'yes');
			}
		};
		
		window.vimeo_player_loaded = function(swf_id) {
			var player = document.getElementById(swf_id);
			player.api_addEventListener('onPlay', 'on_play');
			player.api_addEventListener('onPause', 'on_pause');
		};
		
		var players = $('#video-gallery > div > ul li');
					
		for (var i = players.length - 1; i >= 0; i--){
			
			var _el = $(players[i]);
			_el.css("zIndex", 1);
			_el.css("position", 'relative');
			var clip_id = parseInt(_el.text());
			var video_id = 'video_'+i+'_'+clip_id;
			_el.html('<div id="'+video_id+'"></div>');
			_el.data('video_id', video_id);
			
			var swfUrl = "http://vimeo.com/moogaloop.swf",
				jsParams = {
					js_api: 1, 
					clip_id: clip_id,
					js_onLoad: 'vimeo_player_loaded',
					js_swf_id: video_id
				},
				flashParams = {
					allowscriptaccess: 'always',
					allowfullscreen: 'true',
					wmode: 'transparent'
				};
			
			swfobject.embedSWF(swfUrl, video_id, $('#'+video_id).outerWidth(), $('#'+video_id).outerHeight(), "9.0.0", "swf/expressInstall.swf", jsParams, flashParams, {});
		};
		
	}
	
	
	

});
