

// default function for pausing and resuming functionality
// see no frizz landing page slide show for example use
var windowPauseResume = function(str) {}


/* see header_ajax for use */
function ConfirmLogoutText(orderItemsCount){
 
    return (orderItemsCount > 0) ? "You have items in your cart. Logging out will empty your cart\nAre you sure want to continue?" :
                                    "Do you really want to logout?";
                                    
}



// Makes a GA call.
// @by marram
function analytics(page){
    if (typeof (pageTracker) != 'undefined'){
        // pageTracker._trackPageview(page);
    }
};

function initMailList(){
    
    /* TODO Validate */
    
	if ($j.browser.msie && ($j.browser.version) < 7 ) {
		$j("#signup").submit(function(){
		    $j(this).ajaxSubmit();
			$j('#enterform').hide();
			$j('#thankyouform').show();
			$j(this).clearForm();
			return false;
		});
	}else {
	
	
	   $j("#signup").submit(function(){
			if ($j(this).validate(
			 {
                        rules: {
                            email: {
                                required: true,
                                email: true
                            }
                        },
                        messages: {
                            email: "Please enter a valid email address"
                        }
                    }
			).form()) {
				$j(this).ajaxSubmit(function(){
					$j('#enterform').hide();
					$j('#thankyouform').show();
					$j(this).clearForm();
				});
			}
			return false;
	});
	}
	$j('.closesignup a').click(function(){
		$j('#enterform').show();
		$j('#thankyouform').hide();
		$j('#mailingform').hide();
		$j('#clickaway').hide();
		return false;
	});
	$j('#clickaway').click(function(){
		$j('#enterform').show();
		$j('#thankyouform').hide();
		$j('#mailingform').hide();
		$j('#clickaway').hide();
	})
	

	
}

/*** MAIN NAV ***/

var navEventLock = false;
var menuTimer = -1;
var isMouseOver = false;
var isNavOpen = false;
var showingDefault = false;
var inOutCount = 0;
var emptySkirt = "0";
var navHeightClosed = 28;
var navHeightOpen = 184;


function initNavigation(){
     
    $j("div#navScrimShade").css("height", ($j("div.container").height() + navHeightClosed)+"px"); 

    $j('.ulTabs').hover(function(){
        setInOut(true);
        if (navEventLock) return;
        menuTimer = window.setTimeout("presentNav()",220);
        
    },function(){
        setInOut(false);
        if (navEventLock) return;        
        menuTimer = window.setTimeout("dismissNav()",80);   
    });
    
/*

    $j('.navBoxNav').hover(function(){
        setInOut(true);
        if (navEventLock) return;
        menuTimer = window.setTimeout("presentNav()",220);
        
    },function(){
        setInOut(false);
        if (navEventLock) return;        
        menuTimer = window.setTimeout("dismissNav()",80);   
    });
    
*/
    
     
    // SUB Navs
    $j("ul.ulTabs li.liTab").hover(function() { 
        isMouseOver = true;
        
        $j(this).addClass('hovering');
        
        $j("ul.ulTabs li.liTab").find("div.subNavDiv").fadeOut(10);
        showingDefault = false;
        
        $j(this).find("div.subNavDiv").hide().css('left',"0px").fadeIn(250);
        
        //Show the subnav
    
    } , function() {
        $j(this).removeClass('hovering');
        // hover out...
        // always hide the sub nav 
        $j(this).find("div.subNavDiv").css('left',"-1999px"); //Hide the subnav
    });
    
    moniterInterval = setInterval(navMonitor,100);
}

function setInOut(bool){
    window.clearTimeout(menuTimer);    
    isMouseOver = bool;
    // debug
    inOutCount++;
}

function presentNav(){
    lockNav();
    $j("div#navBoxAndScrim").css("height", ($j("div.container").height() + navHeightClosed)+"px"); 
    $j('div#navScrimShade').css("left","0px").fadeIn(250);
    $j('.navBoxNav').css("height",navHeightOpen+"px");
    unlockNav();
    isNavOpen = true;
    windowPauseResume('pause');
}

function dismissNav(){
    lockNav();
    $j('div#navScrimShade').css("left","-999em");
    isNavOpen = false;
    $j('div#navBoxAndScrim').css("height",navHeightClosed+"px"); 
    unlockNav();
    $j('.navBoxNav').css("height",navHeightClosed+"px");
    windowPauseResume('resume');
    
}

function unlockNav() {
    navEventLock = false;
}
function lockNav() {
    navEventLock = true;
}

function navMonitor() {

    //updateDebugStateStr();

    // is it open and showing no children ?
    // todo show selected stated tab's children
    if ( isNavOpen && isMouseOver)
    {
        if ($j("ul.ulTabs li.hovering").length > 0 ) return;
        
        menuTimer = window.setTimeout("dismissNav()",500);  
        return;
       
    }
     // should we open it ? ... 
    if ( !navEventLock && isMouseOver && !isNavOpen  ){
         if ($j("ul.ulTabs li.hovering").length == 0 ) {
            return;
        }
         menuTimer = window.setTimeout("presentNav()",200);
         return;
    }
    // should we close it ? ... 
    if ( !navEventLock && !isMouseOver && isNavOpen  ){
         menuTimer = window.setTimeout("dismissNav()",300);
         return;
    }
    
}

function updateDebugStateStr() {
    if ( $j('div#debugState').length == 0) {
        $j(document.body).append("<div id='debugState'>Word</div>");
        $j('div#debugState').css("position","absolute").css("top","1px");
    }
    $j('div#debugState').text(
        inOutCount + ": " + 
        ( navEventLock ? " animating ":" at rest ") +
        ((isMouseOver)?"IN":"OUT") +
        "  empty skirt:" + emptySkirt +
         " isOpen:" +isNavOpen  
     );
}


/*** end MAIN NAV ***/

function recordOutboundLink(anchor, category, action) {
	try {
		pageTracker._trackEvent(category, action);
		setTimeout('document.location = "' + anchor.href + '"', 100)
	}catch(err){

	}
}
// How to use videos
function showHowToUse(sku, extra){
	var options = $H({width: 900, height: 450, title: "How to use."})
	options = options.merge(extra);
	var url = "/product/data/how_to_use/how_to_use_"+sku+".phtml";
	Modalbox.show(url, options.toObject());
}

function showHowToUseTB(videoUrl, fullStyle) {
    if(!docReady) { return; }
    
    var fullHowToInsructionsPathName = "howto_instruction_full_";
    var style = "";
    if(fullStyle != undefined)
    {
        fullHowToInsructionsPathName += fullStyle + ".tpl";
        
        $j('#fullHowToSteps').load('_include/howto_instruction_full_long.tpl');
        $j('#fullHowToSteps').hide();
        $j('#fullHowToSteps').load('/howToVideoPopSteps.php',
          {stepsURL: '_include/'+fullHowToInsructionsPathName}, function(data){
          $j('#fullHowToSteps').show();
        });
        style = "/" + fullStyle;
    }
    
    if ('/flash/howto/1_SS_420.flv' == '' ){    
        // no video
        tb_show("medium","#TB_inline?height=428&width=640&inlineId=how_to_use",null);
    } else {
        // with video
     
        tb_show("cool","#TB_inline?height=440&width=900&inlineId=how_to_use",null);
        var flashvars={};
        if(videoUrl == undefined){
            flashvars.playSt = '/flash/howto/1_SS_420.flv';
        }
        else {
            flashvars.playSt =  videoUrl;
        }
        
        // note that the player ratio is 705/453=1.556
        swfobject2.embedSWF("/flash/general/video_medium.swf", 
                            "howto_video", "400px", "257px", "9", 
                            "expressInstall.swf", flashvars, {wmode:'transparent'}, null);
    }
}


function showIngredients(sku){
	var url = "/product/data/ingredients/"+sku+".html";
	Modalbox.show(url, {width: 600, title: "Ingredients."});
}

