$(document).ready(
	function()
	{
		$("#carouselItems").homepageCarousel();
	}
);

function embedVimeoVideos() {
	$("#carouselItems .video div").each(function(i,v) {
		
		playerId = $(this).attr('id')
		$('#' + playerId).parent().children('.afterVid').hide();
		videoId = playerId.replace('vimeo', '').replace('Clone', '');
	 	var flashvars = {
	        clip_id: videoId,
	        show_portrait: 0,
	        show_byline: 0,
	        show_title: 0,
	        js_api: 1, 
			color: '56B19B',
			js_onLoad: 'videoLoaded',
			js_swf_id: playerId
	    };
		var params = {
			allowscriptaccess: 'always',
			allowfullscreen: 'true'
		};
		var attributes = {};
		
		swfobject.embedSWF("http://vimeo.com/moogaloop.swf", playerId, "790", "445", "9.0.0","expressInstall.swf", flashvars, params, attributes);
		$('#carouselItems a.playAgain').click(videoPlayAgain);
		
	});
}

function videoLoaded(playerId) {
	video = $('#' + playerId).get(0);
	video.api_addEventListener('onFinish', 'videoFinished');
}
	
function videoFinished(playerId) {
	$('#' + playerId).parent().children('object').css({'z-index':'1'});
	$('#' + playerId).parent().children('.afterVid').show();
	$('#' + playerId).parent().children('.static').show();
}

function videoPlayAgain(playAgainId) {
	$(this).parent().parent().children('object').get(0).api_seekTo(0);
	$(this).parent().hide();   //hide playagain controls
	$(this).parent().parent().children('.static').hide();
	$(this).parent().parent().children('object').css({'z-index':'4'});
	console.debug($(this).parent().parent().children('object').get(0));
	$(this).parent().parent().children('object').get(0).api_play();
}

(function($) {
	$.fn.homepageCarousel = function(o) {
		return this.each(function() {
		
			var autoInterval = 6000;
			var changeSpeed = 1500;
			
			var running = false;
			var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size();
			
			beforeClone = tLi.slice(tl-1).clone();
			afterClone = tLi.slice(0,1).clone();
			
			beforeClone.attr('id', beforeClone.attr('id') + "Clone");
			afterClone.attr('id', afterClone.attr('id') + "Clone");
			
			beforeClone.children('div').attr('id',beforeClone.children('div').attr('id') + "Clone");
			afterClone.children('div').attr('id',afterClone.children('div').attr('id') + "Clone");			
			
			ul.prepend(beforeClone).append(afterClone);
			
			var li = $("li", ul), itemLength = li.size(), curr = 1;
			var autoIntervalId = 0;
			div.css("visibility", "visible");
			
			li.css({overflow: "hidden", float: "left"});
			ul.css({margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1"});
			div.css({overflow: "hidden", position: "relative", "z-index": "2", left: "0px"});
			
			var liSize = width(li);
			var ulSize = liSize * itemLength;
			var divSize = liSize;
			
			li.css({width: li.width(), height: li.height()});
			ul.css("width", ulSize+"px").css("left", -(curr*liSize));
			
			div.css("width", divSize+"px");
			
			$.each($("#carouselItems").find("li.video"), function(i, val) {
				$(val).click(function() {
					clearInterval(autoIntervalId);
				});
			});
			
			$.each($("#carouselControls").find("li"), function(i, val) {
				$(val).click(function() {
					clearInterval(autoIntervalId);
					return go(1+i);
				});
			});
			
			embedVimeoVideos();	
			
			autoIntervalId = setInterval(function() {go(curr+1);}, autoInterval + changeSpeed);
			
			
			function go(to) {
				if(to<=-1) {
					ul.css("left", -((itemLength-(2))*liSize)+"px");
					curr = to==-1 ? itemLength-3 : itemLength-3;
				} else if(to>=itemLength) {
					ul.css("left", -( liSize ) + "px" );
					curr = to==itemLength ? 2 : 2;
				} else curr = to;
			
				running = true;
			
				ul.animate({left: -(curr*liSize)}, changeSpeed, "easeinout", function() {running = false;});
			
				var activeIndex;
				activeIndex = (to % tl == 0) ? tl : to % tl;
				$("#carouselControls").find('a').removeClass("active");
				$("#carouselControls").find('a[rel="' + (activeIndex)  + '"]').addClass("active");
			
				return false;
			};
		});
	};
	
	function css(el, prop) {
		return parseInt($.css(el[0], prop)) || 0;
	};
	
	function width(el) {
		return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
	};
	
	function height(el) {
		return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
	};

})(jQuery);

jQuery.easing = {
	easeinout: function(x, t, b, c, d) {
		if (t < d/2) return 2*c*t*t/(d*d) + b;
		var ts = t - d/2;
		return -2*c*ts*ts/(d*d) + 2*c*ts/d + c/2 + b;
	}
};
