var carouselItems = 0;
var carouselScenes = 0;
var currentScene = 1;
var carouselNextInterval = "";
var carousel = "";
var carouselAnimationTime = 1000;
var carouselPausTime = 10000;
var carouselItemWidth = 313;
var carouselSceneWidth = 626;
var index;

$(document).ready(function()
{
	$("body").data('promotionHover', false);
	setupCarousel();
	
	$("#promotion .menu .hover").hoverIntent({
		sensitivity: 20,
		interval: 75,
		timeout: 0,
		over: menuOver,
		out: menuOut
	});
	
	$("#promotion .media").hover(
	function (event)
	{
		target = ($(event.target).is("div.object")) ? $(event.target) : $(event.target).closest("div.object");
		
		if(target.is(".default"))
		{
			showPromotion("default")
		}
		else
		{
			$("body").data('promotionHover', true);
			showPromotion(index)
		}
	},
	function (event)
	{
		$("body").data('promotionHover', false);
		$("#promotion .menu a").removeClass("active");
		showPromotion("default")
	});
	
	$("#promotion .menu .hover").click(function (event)
	{
		window.location = $(event.target).find("a:first").attr("href");
	});
});

function menuOver(event)
{
	target = ($(event.target).is(".hover")) ? event.target : $(event.target).closest(".hover");
	index = parseInt($("#promotion .menu div.hover").index(target)) + 1;
	
	$("a", target).addClass("active");
	showPromotion(index)
}

function menuOut(event)
{
	target = ($(event.target).is(".hover")) ? event.target : $(event.target).closest(".hover");
	showPromotion("default");
	window.setTimeout('deHoverMenu()', 100);
}

function deHoverMenu()
{
	if($("body").data('promotionHover') != true)
		$("#promotion .menu a").removeClass("active");
}
		
function showPromotion(indexToShow)
{
	var promotion = getPromotionToShow(indexToShow);
	$("#promotion .media div.object").stop(true, false).animate({left: "-646px"}, {queue:true, duration:250, easing:'swing'});
	promotion.stop(true, false).animate({left: "0px"}, {queue:false, duration:250, easing:'swing'});
}

function getPromotionToShow(indexToShow)
{
	if(indexToShow == 'default')
		return $("#promotion .media .default");
	
	return $("#promotion .media .slide-" + indexToShow);
}

function setupCarousel()
{
	carouselItems = $("#carousel li").length;
	carouselScenes = Math.ceil(parseInt(carouselItems) / 2);
	carousel = $("#carousel");
		
	carousel.css("width", carouselItems * carouselItemWidth);
	
	var sceneIndicatorHtml = '';
	for(i=0; i<carouselScenes; i++)
	{
		if(i == 0)
			sceneIndicatorHtml += '<img src="img/scene_active.gif" width="6" height="6" class="active" />';
		else
			sceneIndicatorHtml += '<img src="img/scene_passive.gif" width="6" height="6" />';
	}
	$("#carousel-container .scene-indicator").append(sceneIndicatorHtml);
		
	//User have clicked on a scene selector, scroll to this scene
	$("#carousel-container .scene-indicator img").click(function (event)
	{
		currentScene = parseInt($("#carousel-container .scene-indicator img").index(event.target)) + 1;
		carouselAnimate(currentScene);
	});
	
	$("#carousel-container .next").click(carouselNext);
	$("#carousel-container .prev").click(carouselPrev);
	
	carouselNextInterval = window.setTimeout(carouselNext, carouselPausTime);	
}

function carouselAnimate(sceneToShow)
{
	window.clearTimeout(carouselNextInterval);
	
	if(sceneToShow > carouselScenes)
		currentScene = 1;
	else if(sceneToShow < 1)
		currentScene = carouselScenes;
	else
		currentScene = sceneToShow;
	
	marginLeft = (currentScene == carouselScenes && carouselItems % 2 == 1) ? carouselSceneWidth * (currentScene - 1) - carouselItemWidth : carouselSceneWidth * (currentScene - 1);
	carousel.stop(true,false).animate({marginLeft: "-" + marginLeft}, carouselAnimationTime, 'swing');
	carouselNextInterval = window.setTimeout(carouselNext, carouselPausTime);
	carouselUpdateSelector();
}

function carouselNext()
{
	carouselAnimate(currentScene+1);
}

function carouselPrev()
{
	carouselAnimate(currentScene-1);
}

function carouselUpdateSelector()
{
	newActive = "#carousel-container .scene-indicator img:nth-child(" + currentScene +")";
	oldActive = $("#carousel-container .scene-indicator img.active");
	oldIndex = parseInt($("#carousel-container .scene-indicator img").index(oldActive)) + 1;
	
	if(currentScene != oldIndex)
	{
		(currentScene > oldIndex) ? oldActive.insertAfter(newActive) : oldActive.insertBefore(newActive);
	}
}