/* 
* The home page loads, 
* The Carousel slides up and then slides down
* The links in .new_product are shown and cycle randomly
* When the mouse passes on region where carousel should be it appears and stays untils the mouse quits the carousel
*/

$(document).ready(function() {
    var linkCount = $(".carousel_container a").length;
    var state="";
    var scrollState="";
    var newProductIndex = 1;
    var shownIndex = 1;
    var isIe7 = ($.browser.msie && $.browser.version.substr(0,1)<8);
    var isIphone = (navigator.platform.indexOf('iPhone')==0);
    var isIpad = (navigator.platform.indexOf('iPad')==0);
    var isIpod = (navigator.platform == 'iPod');
    var isIos = (isIphone || isIpad  || isIpod);

    //get the width and margin of the first link, set in pages.css and used for scroll calculations
    var firstLink = $(".carousel_container a").first();
    var linkWidth = parseInt(firstLink.css("width"));
    var linkMarginRight = parseInt(firstLink.css("margin-right"));
    //get marginstart from css because it is different for ie7
    var marginStart  = parseInt($(".carousel_container").css("margin-left"));
    if($.isNaN(marginStart)) {
	marginStart=0;
    } else {
	if(isIe7) {
	    //ie7 reports margins start differently
	    marginStart += (linkWidth+linkMarginRight);
	    $(".carousel_container").css("margin-left", marginStart);
	}
    }
    


    //resize prev and next links for iphone
    if(isIphone) {
	$(".front .next img, .front .previous img").css(
	    {"width": "68px", "height": "76px", "margin-top": "-40px"});
	$(".front .next img").css("margin-left", "-34px");
    }
    function hideLinks() {
	if(state=="hiding") { return; }
	state="hiding";
	var completeFunc = function() {
	    state='hidden';
	    //show the div which shows carousel on mouseover
	    $('.mouseover_menu_bottom').show();
	    //hide the div which would hide the menu onmousover
	    $('.menu_bottom_mouseover_active').hide();
	};
	//hide immediately if ie7
	if (isIe7) {
	    $('.carousel_bottom_homepage').hide();
	    completeFunc();
	} else {
	    $('.carousel_bottom_homepage').hide("slide", { direction: "down" }, 1000, completeFunc);
 	}

    }
    function showLinks(afterFunction) {
	if(state=="showing") { return; }
	state="showing";
	$(".new_product").hide();
	var completeFunc = function() {
	    state='shown';
	    //hide the div which shows carousel on mouseover
	    $('.mouseover_menu_bottom').hide();
	    //show the link so that it is visible when the carousel slides back down
	    $(".new_product").show();

	    //show the div which will hide the menu onmousover
	    $('.menu_bottom_mouseover_active').show();
	    //should we do something else after showing?
	    if(typeof(afterFunction)=="function") {
		afterFunction();
	    }
	};
	//hide immediately without animation if ie7
	if (isIe7) {
	    $('.carousel_bottom_homepage').show();
	    completeFunc();
	} else {
	    $('.carousel_bottom_homepage').show("slide",{direction: "down"}, 1000, completeFunc);	    
	}
    }

    function scrollToLink(linkIndex, immediately) {
	if(scrollState=="scrolling") {
	    return;
	}
	var firstVisibleIndex = linkIndex;
	if(firstVisibleIndex<1) {
	    firstVisibleIndex=1;
	}
	//don't scroll pass the last image
	if(firstVisibleIndex>linkCount-3) {
	    firstVisibleIndex=linkCount-3;
	}

	
	scrollState="scrolling";
	var duration=200; //equivalent of "fast"
	var cssChange = {"margin-left": (marginStart + (-1 * (firstVisibleIndex - 1) * (linkWidth + linkMarginRight))) + "px"};

	//we  hide the previous and next links after the scroll
	var completeFunc = function() {
	    scrollState=""; 
	    shownIndex = firstVisibleIndex;
	    //can we scroll to the right?
	    if(shownIndex >= (linkCount-3)) {
		$(".next").hide();
	    } else {
		$(".next").show();
	    }
	    //can we scroll to the left?
	    if(shownIndex < 2) {
		$(".previous").hide();
	    } else {
		$(".previous").show();
	    };
	};
	
	if(typeof(immediately)=="undefined" || !immediately) {
	    //animate the change of margin
	    $(".carousel_container").animate(
		cssChange,
		{"duration": duration,
		 "complete": completeFunc
		});
	} else {
	    //change css immediately without animation
	    $(".carousel_container").css(cssChange);
	    completeFunc();
	}
    }

    function scrollToLinkAndShowLinks() {
	//scroll without animation
	scrollToLink(newProductIndex, true);	
	showLinks();
    }


    function showLinksAndScrollToLink() {
	showLinks(function() {
	    //scroll to the currently shown link in new_product once the carousel is shown
	    scrollToLink(newProductIndex);	
	});
    }

    //previous link is only shown when there is a previous link to show
    $(".previous").hide();
    
    //show and hide the MEA at the load of the homepage
    showLinks();
    //hide after 2 seconds if not iOs
    var timeout_MEA_load ="";
    if(!isIos) {
	timeout_MEA_load = setTimeout(function(){
	    hideLinks();
	},2000);
    }
    
    //change text of new product link every 4 seconds with text from one random link
    /*
    $(".new_product").everyTime(4000,function() {
	newProductIndex = (Math.floor((linkCount)*Math.random()+1));
	$('.new_product').html('+ '+$("#imagelink_"+ newProductIndex + " span").html());
    });
*/
    $(".new_product").click(function() {
	if(state=="shown") {
	    hideLinks();
	} else {
	    scrollToLinkAndShowLinks();
	} 
	
    });

    //on mousover of bottom region: show the links
    $(".mouseover_menu_bottom").mouseover(function() {
	if(state!='shown') {
	    //start by scrolling to the link which is currently shown in new_product heading
	    //then show the carousel
	    scrollToLinkAndShowLinks();
	}
    });
    
    //hide the carousel after 2 seconds on mouseover of the region surrounding it
    var timeout_MEA_hide = "";
    $(".menu_bottom_mouseover_active").mouseover(function() {
	//are we arlready waiting to hide the carousel?
	if(timeout_MEA_hide!="") {
	    clearTimeout(timeout_MEA_hide);
	    timeout_MEA_hide="";
	}
	//wait 2 seconds before hiding
	timeout_MEA_hide = setTimeout(function(){
	    hideLinks();
	    timeout_MEA_hide ="";
	},2000);
    });

    //set action on next and previous links
    $(".next").click(function(event) {
	scrollToLink(shownIndex+1);
    });
    $(".previous").click(function(event) {
	scrollToLink(shownIndex-1);
    });
    //cancel the hiding of carousel if we mouseover it
    $(".carousel_bottom_homepage").mouseover(function() {
	if(timeout_MEA_hide!="") {
	    clearTimeout(timeout_MEA_hide);
	    timeout_MEA_hide="";
	} 
	
	if(timeout_MEA_load!="") {
	    //cancel the hiding 2 seconds after page load
	    clearTimeout(timeout_MEA_load);
	    timeout_MEA_load="";
	}  
	if(state=="hiding") { 
	    showLinks();
	}
    });

});





