// JavaScript Document

var highlights = [],
	index = 0,
	width = 221,
	delay = 9000,
	suunta = 1,
	interval,
	canClick = true;
	
var brands = [],
	brandsIndex = 0,
	brandsWidth = 137,
	brandsDelay = 7000,
	brandsSuunta = 1,
	brandsInterval,
	brandsCanClick = true;
	
var fpHighlights = [],
	fpHighlightsIndex = 1,
	fpHighlightsHeight = 382,
	fpHighlightsDelay = 8000,
	fpHighlightsInterval,
	fpHighlightsCanClick = true;

$(document).ready(function(){
	/* Get highlight objects and links */
	highlights = $('#highlightCarousel .highlightContainer .highlightElements').children('a,object');
	
	/* Animate, if there is more than four highlight */
	if (highlights.length > 4)
	{
		$('#highlightCarousel .previousHighlight').css( 'visibility', 'visible' );
		$('#highlightCarousel .nextHighlight').css( 'visibility', 'visible' );
		
		$('#highlightCarousel .previousHighlight').click(function() {
			if ( canClick )
			{
				clearTimeout(interval);
				suunta = -1;
				loopHighlights();
			}
			return false;
		});
	
		$('#highlightCarousel .nextHighlight').click(function() {
			if ( canClick )
			{
				clearTimeout(interval);
				suunta = 1;
				loopHighlights();
			}
			return false;
		});
		
		/* Clone first four highlight as the last highlight */
		$('#highlightCarousel .highlightContainer .highlightElements').append( highlights.slice(0,4).clone() );
		
		/* Get updated highlights and remove all other elements */
		highlights = $('#highlightCarousel .highlightContainer .highlightElements').children('a,object');
		$('#highlightCarousel .highlightContainer .highlightElements').html(highlights);
		
		/* Calculate container width */
		$('#highlightCarousel .highlightContainer .highlightElements').css('width', highlights.length * width + 'px');
		
		/* Start animation */
		interval = setTimeout(loopHighlights, delay);
	}
	
	/* Get brands objects and links */
	brands = $('#brandCarousel .brandContainer .brandElements').children('a,object');
	
	/* Animate, if there is more than three brands */
	if (brands.length > 3)
	{
		$('#brandCarousel .previousBrand').css( 'visibility', 'visible' );
		$('#brandCarousel .nextBrand').css( 'visibility', 'visible' );
		
		$('#brandCarousel .previousBrand').click(function() {
			if ( brandsCanClick )
			{
				clearTimeout( brandsInterval );
				brandsSuunta = -1;
				loopBrands();
			}
			return false;
		});
	
		$('#brandCarousel .nextBrand').click(function() {
			if ( brandsCanClick )
			{
				clearTimeout( brandsInterval );
				brandsSuunta = 1;
				loopBrands();
			}
			return false;
		});
		
		/* Clone first four brands as the last brands */
		$('#brandCarousel .brandContainer .brandElements').append( brands.slice(0,3).clone() );
		
		/* Get updated brands and remove all other elements */
		brands = $('#brandCarousel .brandContainer .brandElements').children('a,object');
		$('#brandCarousel .brandContainer .brandElements').html( brands );
		
		/* Calculate container width */
		$('#brandCarousel .brandContainer .brandElements').css('width', brands.length * brandsWidth + 'px');
		
		/* Start animation */
		brandsInterval = setTimeout(loopBrands, brandsDelay);
	}
	
	/* Get fpHighlights objects and links and remove other elements */
	fpHighlights = $('#fpHighlightContent .fpHighlightElements').children('a,object');
	$('#fpHighlightContent .fpHighlightElements').html( fpHighlights );
	
	/* Animate, if there is one or more than an one fpHighlights */
	if (fpHighlights.length > 1)
	{
		var i = 1;
		for ( i = 1; i <= fpHighlights.length; i++ )
		{
			$( '#fpHighlight' + i ).show();
			
			if ( i == 1 )
				$( '#fpHighlight' + i ).addClass( 'fpHighlightBtnCurrent' );
				
			$( '#fpHighlight' + i ).click(function() {
				if ( fpHighlightsCanClick )
				{
					clearTimeout( fpHighlightsInterval );
					showFpHighlight( this );
				}
				return false;
			});
		}
		
		/* Calculate container height */
		$('#fpHighlightContent .fpHighlightElements').css('height', fpHighlights.length * fpHighlightsHeight + 'px');
		
		/* Start animation */
		fpHighlightsInterval = setTimeout(loopFpHighlights, fpHighlightsDelay);
	}
	
	$("a.ingr, a.nutr").click( function()
	{
	 	var tmpName = "div_" + $( this ).attr( 'name' );
		showMore( tmpName );
 
		return false;
	});
});

function loopHighlights ()
{
	/*Disable link operations*/
	canClick = false;
	
	//suunta = suunta || 1;
	index = index + suunta;
	/* Loops index between 0 and the amount of highlights */
	if( index + 3 >= ( highlights.length ) ) {
		index = 1;
		$('#highlightCarousel .highlightContainer .highlightElements').css({ left: '0px' });  
	}
	else if ( index < 0 ) {
		index = highlights.length - 5;
		$('#highlightCarousel .highlightContainer .highlightElements').css({ left: '-' + ((highlights.length - 4) * width) + 'px' });
	}
	
	/* Do the slide animation */
	$('#highlightCarousel .highlightContainer .highlightElements').animate({ left: '-' + (index * width) + 'px' } , 500, function(){ canClick = true; });
	suunta = 1;
	interval = setTimeout(loopHighlights, delay);
}

function loopBrands ()
{
	/*Disable link operations*/
	brandsCanClick = false;
	
	//suunta = suunta || 1;
	brandsIndex = brandsIndex + brandsSuunta;
	/* Loops index between 0 and the amount of highlights */
	if( brandsIndex + 2 >= ( brands.length ) ) {
		brandsIndex = 1;
		$('#brandCarousel .brandContainer .brandElements').css({ left: '0px' });  
	}
	else if ( brandsIndex < 0 ) {
		brandsIndex = brands.length - 4;
		$('#brandCarousel .brandContainer .brandElements').css({ left: '-' + ((brands.length - 3) * brandsWidth) + 'px' });
	}
	
	/* Do the slide animation */
	$('#brandCarousel .brandContainer .brandElements').animate({ left: '-' + (brandsIndex * brandsWidth) + 'px' } , 500, function(){ brandsCanClick = true; });
	brandsSuunta = 1;
	brandsInterval = setTimeout(loopBrands, brandsDelay);
}

function loopFpHighlights ()
{
	fpHighlightsIndex += 1;
	showFpHighlight( fpHighlightsIndex, true );
}

function showFpHighlight ( highLightNumber, animation )
{
	fpHighlightsCanClick = false;
	
	if ( !animation )
	{
		btnId = $( highLightNumber ).attr( 'id' );
		fpHighlightsIndex = parseInt( btnId.replace( 'fpHighlight', '' ) );
	}
	
	if( fpHighlightsIndex > fpHighlights.length )
		fpHighlightsIndex = 1;
		
	$( '.fpHighlightBtn' ).removeClass( 'fpHighlightBtnCurrent' );
	$('#fpHighlightContent .fpHighlightElements').fadeOut( 500, function(){
		$('#fpHighlightContent .fpHighlightElements').css({ top: (fpHighlightsHeight - (fpHighlightsIndex * fpHighlightsHeight)) + 'px' });
		$( '#fpHighlight' + fpHighlightsIndex ).addClass( 'fpHighlightBtnCurrent' );
		$('#fpHighlightContent .fpHighlightElements').fadeIn( 500, function(){
			fpHighlightsCanClick = true;
		});
	});
	
	fpHighlightsInterval = setTimeout(loopFpHighlights, fpHighlightsDelay);
}

function showMore ( scrollToId )
{
	var scroller = $( '#' + scrollToId )[0];
			
	if ( $( scroller ).is(":hidden") )
		$( scroller ).slideDown();
	else
		$( scroller ).slideUp();

}

function showAdvertise ( adType, videoUrl, videoRuntime )
{
	swfobject.embedSWF( "/files/truly/flash/video_fi.swf", "video", "700", "400", "8", "/files/truly/flash/expressInstall.swf", { importType: adType, contentUrl: videoUrl, contentRuntime: videoRuntime }, { menu: 'false', wmode: 'transparent', allowScriptAccess: "always" }, { id: "video" });
	
	return false;
}

function voteProductAdjective ( linkElement, productNumber, adjective )
{
	$.post("/karkit/karkit-adjektiiviaanestys", { id: productNumber, word: adjective }, function( data, textStatus )
	{
		if ( textStatus == 'success' )
		{
			/*$( linkElement ).fadeTo( 1000, 0.33, function () { $( linkElement ).fadeTo( 1000, 1 ); } );*/
			/*alert( 'Äänesi on rekisteröity' );*/
		}
	});
	
	return false;
}
