

// 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 ($.browser.msie && ($.browser.version) < 7 ) {
		$("#signup").submit(function(){
		    $(this).ajaxSubmit();
			$('#enterform').hide();
			$('#thankyouform').show();
			$(this).clearForm();
			return false;
		});
	}else {
	
	
	   $("#signup").submit(function(){
			if ($(this).validate(
			 {
                        rules: {
                            email: {
                                required: true,
                                email: true
                            }
                        },
                        messages: {
                            email: "Please enter a valid email address"
                        }
                    }
			).form()) {
				$(this).ajaxSubmit(function(){
					$('#enterform').hide();
					$('#thankyouform').show();
					$(this).clearForm();
				});
			}
			return false;
	});
	}
	$('.closesignup a').click(function(){
		$('#enterform').show();
		$('#thankyouform').hide();
		$('#mailingform').hide();
		$('#clickaway').hide();
		return false;
	});
	$('#clickaway').click(function(){
		$('#enterform').show();
		$('#thankyouform').hide();
		$('#mailingform').hide();
		$('#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(){
     
    $("div#navScrimShade").css("height", ($("div.container").height() + navHeightClosed)+"px"); 

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

    $('.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
    $("ul.ulTabs li.liTab").hover(function() { 
        isMouseOver = true;
        
        $(this).addClass('hovering');
        
        $("ul.ulTabs li.liTab").find("div.subNavDiv").fadeOut(10);
        showingDefault = false;
        
        $(this).find("div.subNavDiv").hide().css('left',"0px").fadeIn(250);
        
        //Show the subnav
    
    } , function() {
        $(this).removeClass('hovering');
        // hover out...
        // always hide the sub nav 
        $(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();
    $("div#navBoxAndScrim").css("height", ($("div.container").height() + navHeightClosed)+"px"); 
    $('div#navScrimShade').css("left","0px").fadeIn(250);
    $('.navBoxNav').css("height",navHeightOpen+"px");
    unlockNav();
    isNavOpen = true;
    windowPauseResume('pause');
}

function dismissNav(){
    lockNav();
    $('div#navScrimShade').css("left","-999em");
    isNavOpen = false;
    $('div#navBoxAndScrim').css("height",navHeightClosed+"px"); 
    unlockNav();
    $('.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 ($("ul.ulTabs li.hovering").length > 0 ) return;
        
        menuTimer = window.setTimeout("dismissNav()",500);  
        return;
       
    }
     // should we open it ? ... 
    if ( !navEventLock && isMouseOver && !isNavOpen  ){
         if ($("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 ( $('div#debugState').length == 0) {
        $(document.body).append("<div id='debugState'>Word</div>");
        $('div#debugState').css("position","absolute").css("top","1px");
    }
    $('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){

	}
}
