$(document).ready(function() {

//top navigation button animation
	$('#global-nav').find('a').mouseenter(function(){
		$(this).animate({marginTop:'13px'},{queue:false,duration:150});
	}).mouseleave(function(){
		$(this).animate({marginTop:'22px'},{queue:false,duration:400});
	});
	
//srotiment ingredienser overlay box
	$("a.button-pinfo").click(function(){
		$(this).parent("div").parent("div").fadeOut();
		return false;
	});
	
	$("a.button-ingredienser").click(function(){
		$(this).parent("div").find("div.prod-overlay").fadeIn();
		return false;
	});	

//top slide trigger	
	$('#slides').slides({
		preload: true,
		generateNextPrev: false,
    play: 5000,
    pause: 2500,
    hoverPause: true
	});
	
	$('#slides-browser').slides({
		preload: true,
		generateNextPrev: false,
		generatePagination: false		
	});
	
//front page ice cream picker forward/rewind buttons

	//define maximum number of full screens you can forward, that is overall items number / 7, because there are 7 items on the screen at one time
	var icItems = $(".ic-items li").size();
	//round the number to get an useful value
	var icMaxForward = (Math.ceil(icItems/7)-1);	

	//define the length of the list and add 400 of margin for safety 
	var icItemsWidth = icItems * 93;
	$(".ic-items").css({'width': icItemsWidth+400});
	
	var icClick = 0;
	$(".ic-next").click(function() {
		if (icClick < icMaxForward) {
			//651px is a width of 7 list items that are visible
			$(".ic-items").animate({marginLeft:'-=651px'},{queue:true,duration:400});
			icClick++;
		}
		return false;
	});
	$(".ic-prev").click(function() {
		if (icClick > 0) {
			$(".ic-items").animate({marginLeft:'+=651px'},{queue:true,duration:400});
			icClick--;
		}
		return false;
	});
	
//front page ice cream picker expand details on mouse enter
	$(".ic-items li").mouseenter(function(){
		var element = (($(this).index()+1)%7);
		//add 20px to overflow so the whole list item is visible, it's because list items don't have nice equal sizes
		if (element == 5) $("div.ic-holder").css({'width':'670px'});
		
		//if element is different than 6th and 7th visible on the screen
		if((element != 6) && (element != 0)) {
			//expand info and show background
			$(this).animate({width:'263px'},{queue:false,duration:200});
			$(this).addClass("hover");
		}
		else {
			//expand and add negative margin for last 2 visible elements
			$(this).animate({width:'263px'},{queue:false,duration:200});
			$(this).parent("ul").animate({marginLeft: -((icClick*650)+202)},{queue:false,duration:200});
			$(this).addClass("hover");
		}
	}).mouseleave(function(){
		var element = (($(this).index()+1)%7);
		if (element == 5) $("div.ic-holder").css({'width':'650px'});

		if((element == 6) || (element == 0)) {
			$(this).animate({width:'61px'},{queue:false,duration:200});
			$(this).parent("ul").animate({marginLeft: -(icClick*650)},{queue:false,duration:200});
			$(this).removeClass("hover");
		}
		else {
			$(this).animate({width:'61px'},{queue:false,duration:200});
			$(this).removeClass("hover");
		}
	});
});
