currentAnimation = 1;
defaultSize = 120;

function setup()
{
	var initialAnimation = animations[0];

	$("#panel").hide();
	
	$(".mood-text").not(initialAnimation.text).hide();

	$(".mood-text").css("position", "absolute");
	
	$(".mood-product").not(initialAnimation.product).hide();
}

setup();

$(document).ready(
function()
{
	
	setup();
	
	$("#panel").fadeIn(1000);

	values = {};

	var url = window.location.toString();
	
	var queryString = url.split("?");

	if (queryString[1]!=undefined)
	{
		var params = queryString[1].split("&");
		
		if (params[0]!=undefined)
		{
			for( var i=0, j=params.length; i < j; i++ )
			{
				var item = params[i].split("=");
		
				values[item[0]]=item[1];
			}
		}
	}
	
	fadeIn = (values.fadein>0)?Number(values.fadein):400;
	fadeOut = (values.fadeout>0)?Number(values.fadeout):200;

	productIn = (values.productin>0)?Number(values.productin):400;
	productOut = (values.productout>0)?Number(values.productout):200;

	textIn = (values.textin>0)?Number(values.textin):400;
	textOut = (values.textout>0)?Number(values.textout):200;
		
	zoomEnabled = (values.zoomenabled)?values.zoomenabled!=0:true;

	animationEnabled = (values.animationenabled)?values.animationenabled!=0:true;

	zoom = (values.zoom>0)?Number(values.zoom):18;

	zoomSize = "" + (zoom)?defaultSize + Number(zoom):130;
	zoomOffset = zoom / 2 + "";

	zoomIn = (values.zoomin>0)?Number(values.zoomin):400;
	zoomOut = (values.zoomout>0)?Number(values.zoomout):200;
	
	startAnimation(5000);
	
	$(".mood-cell").hover(
		function()
		{

			imageID = $(this).attr("id");
			
			hover = hovers[imageID];

			show( "#" + imageID, hover.product, hover.text );
			
		},
		function()
		{
		}
	);

// if we hover over an empty cell restore images

	$(".mood-empty").hover(
		function()
		{
			resetImages();
		},
		function()
		{
		}
	);

	
	$("#mood-grid").hover(
		function()
		{
			stopAnimation();
		},
		function()
		{
			resetImages();
			startAnimation(5000);
		}
	);

	$("#mood-text").hover(
		function()
		{
			stopAnimation();
		},
		function()
		{
			startAnimation(5000);
		}
	);

});

function animate( index )
{

	currentAnimation = index;
	
	animation = animations[index];
	
	show( animation.image, animation.product, animation.text );
	
}

function isZoomed(index)
{
	return $(this).width()!=defaultSize;
}

function isNotZoomed(index)
{
	return $(this).width()==defaultSize;
}

function isFullyZoomed(index)
{
	return $(this).width()==zoom;
}

function show( image, product, text )
{
	
	var focus = $(image + " .mood-image");
	var blur = $(image + " .mood-image-blur");
	
	focus.add(blur).stop(true, true);
	
	if (zoomEnabled)
	{
		var inTime = (fadeIn>zoomIn)?fadeIn:zoomIn;
		var outTime = (fadeOut>zoomOut)?fadeOut:zoomOut;
		
		otherFocus = $(".mood-image").not( focus );
		otherBlur= $(".mood-image-blur").not( blur );

		otherFocus.filter( isNotZoomed ).fadeOut(outTime);
		
		otherFocus.filter( isZoomed).animate( { opacity: 0, filter:"alpha(opacity = 0)", width:defaultSize, height:defaultSize, top:"0", left:"0"}, { queue: true, duration: zoomOut, easing: "swing" } );
		otherBlur.filter( isZoomed).animate( { width:defaultSize, height:defaultSize, top:"0", left:"0"}, { queue: true, duration: zoomOut, easing: "swing" } );

		otherFocus.filter( isZoomed ).parent().css("zIndex","100");
		// zoom focus
		
		focus.parent().css("zIndex","500");
		focus.add(blur).animate( { opacity:"1", filter:"alpha(opacity = 100)", width:zoomSize, height:zoomSize, top:"-" + zoomOffset, left:"-" + zoomOffset}, { queue: true, duration: zoomIn, easing: "swing" });
		
		focus.queue( function()
		{
			otherBlur.filter( isZoomed).animate( { width:defaultSize, height:defaultSize, top:"0", left:"0"}, { queue: true, duration: zoomOut, easing: "swing" } );
			$(this).dequeue();	
		});

	}
	else
	{
		$(".mood-image").not( focus ).stop(true, true).fadeOut(fadeOut);
		focus.fadeIn(fadeIn);
	}
	
	highlightText(text)
	
	showProduct(product);
	
}

function showProduct( product )
{
	
	var current = $( product );
	
	current.fadeIn(productIn);
	
	$(".mood-product").not(current).fadeOut(productOut);
		
}

function highlightText( text )
{
	var current = $(text);
	
	current.stop(true, true).fadeIn(textIn);
	
	var hides = $(".mood-text").not(current);
	
	hides.stop(true, true).fadeOut(textOut);
		
}

function animateTimer()
{

	animate(currentAnimation);

	animation = animations[currentAnimation];

	currentAnimation++;
	
	if (currentAnimation >= animations.length)
	{
		currentAnimation = 0;
	}
	
	startAnimation(animation.timing * 1000);
}

function startAnimation(timing)
{
	if (animationEnabled)
	{
		animationTimer = setTimeout("animateTimer()", timing);
	}
	else
	{
		animationTimer = null;
	}
}

function stopAnimation()
{
	clearTimeout( animationTimer );
}

function resetImages()
{

//	$(".mood-image, .mood-image-blur").stop(true, true);

	$(".mood-image").fadeIn(fadeIn);

	if (zoomEnabled)
	{
		$(".mood-image, .mood-image-blur").filter( isZoomed ).animate( { width:"120", height:"120", left:"0", top:"0"}, { queue: true, duration: zoomOut, easing: "swing"} );
		$(".mood-image").parent().css("zIndex","100");
	}
}
