if (typeof(fos) == "undefined") var fos = {};
if (typeof(fos.Bag) == "undefined") fos.Bag = {};
//Global Bag Constructor
fos.Bag = function(){
	//private variables
	var tab = document.getElementById('global_bag');
	var flyout = document.getElementById('global_bag_flyout');
	if($().bgiframe){$(flyout).bgiframe();}
	var last_prod = document.getElementById('last_prod');
	var no_prod = document.getElementById('no_prod');
	var qty = document.getElementById('global_bag_qty');
	var delay = 300;
	var scrollTime = 1000;
	var extendedDelay = 15000;
	var hideTimer;
	var showTimer;
	
	//privileged methods
	this.EnterTab = enterTab;
	this.LeaveTab = leaveTab;
	this.EnterFlyout = enterFlyout;
	this.LeaveFlyout = leaveFlyout;
	this.AddToCart = addToCart;
	
	//setup user events
	$(tab).bind('mouseenter', this.EnterTab);
	$(tab).bind('mouseleave', this.LeaveTab);
	$(flyout).bind('mouseenter', this.EnterFlyout);
	$(flyout).bind('mouseleave', this.LeaveFlyout);


	function showBag(){
		var pageName = document.location.href;
		if(pageName.indexOf("FSGiftCardApply") == -1 && pageName.indexOf("PIAdd") == -1 && pageName.indexOf("FSSBAddressProcess") == -1 && pageName.indexOf("OrderProcess") == -1 && pageName.indexOf("FSPromoCodeApply") == -1 && pageName.indexOf("OrderItemDisplay") == -1 && pageName.indexOf("ShippingAndBillingView") == -1 && pageName.indexOf("OrderDisplay") == -1 && pageName.indexOf("OrderOKView") ==-1)
		{
			//check to see what qty is and toggle panels as necessary
			if(getNumItems() > 0){
				$(last_prod).show();
				$(no_prod).hide();
			}
			else{
				$(last_prod).hide();
				$(no_prod).show();
			}
			//change global bag class then slide out
			$(tab).addClass('show_bag');
			$(flyout).slideDown();
		}
	}
	
	function hideBag(){
		$(flyout).slideUp(function(){
			$(tab).removeClass('show_bag');
		});
	}
	
	function scrollToTop(){
	    $('html,body').animate({
	        scrollTop: 0
	    }, scrollTime);
	}
	
	function startShowTimer(){
		showTimer = setTimeout(function(){
				showBag();
			}, delay);
	}
	
	function clearShowTimer(){
		clearTimeout(showTimer);
	}
	
	function startHideTimer(time){
		hideTimer = setTimeout(function(){
				hideBag();
			}, time);
	}
	
	function clearHideTimer(){
		clearTimeout(hideTimer);
	}
	
	function getNumItems (){
		return parseInt(removeSpaces(qty.innerHTML));
	}
	
	function enterTab (){
		clearHideTimer();
		startShowTimer();
	}
	
	function leaveTab (){
		clearShowTimer();
		startHideTimer(delay);
	}
	
	function enterFlyout(){
		clearHideTimer();
	}
	
	function leaveFlyout(){
		startHideTimer(delay);
	}
	
		function removeSpaces(string){
		var strOut = new String(string);
		strOut = strOut.replace(/[^0-9]/g,'');
		if(strOut == null)
		{
			return "0";
		}else
		{
			return strOut;
		}
	}
	
	function addToCart(){
		//add something to cart via ajax if necessary
		
		/*************** call back***************/
				//on success of ajax call load in new last prod content
				// and update qty
					
				//then set up extended display of widget
				scrollToTop();
				showBag();
				startHideTimer(extendedDelay);
		/************* end call back ************/
	}
};



//read the specified cookie
function readCookie(name) 
{
	 var nameEQ = name + "=";
 	var ca = document.cookie.split(';');
 	for(var i=0;i < ca.length;i++) 
 	{
  		var c = ca[i];
 		 while (c.charAt(0)==' ') c = c.substring(1,c.length);
  		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 	}
 return null;
}

function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function initGovCookies() {
  if (readCookie('CLICK_CT') == null) {
    setCookie('CLICK_CT', '1', 30 );
  }
  if (readCookie('VISIT_CT') == null) {
    setCookie('VISIT_CT', '0', 30 );
  }
  if (readCookie('LAST_SESSONID') == null) {
    setCookie('LAST_SESSONID', 'X', 30 );
  }
}


/* Function to display welcome message and user status
*/
function welcomeMessage(logonURL, logoffURL){
	var displayNameCookie = "DISPLAY_NAME";
	var userTypeCookie = "USER_TYPE";
	
	var displayName = readCookie(displayNameCookie);
	var userType = readCookie(userTypeCookie);
	
	/* Logic to display welcome message*/
	/* Logic to display SignIn, SignOut & Not You links*/
	if(userType == 'G')
	{
		document.getElementById('myAccount').innerHTML = "<a href="+logonURL+">My Account</a>";
		document.getElementById('userStatus').innerHTML = "<a href="+logonURL+">Sign In</a>";
	}
	else if(userType == 'R')
	{
		if(displayName != null && displayName.length > 0 && displayName != "\"\""){
			document.getElementById('myAccount').innerHTML = "<a href="+logonURL+">"+displayName+"'s Account</a>";			
		}
		else{
			document.getElementById('myAccount').innerHTML = "<a href="+logonURL+">My Account</a>";	
		}
		document.getElementById('userStatus').innerHTML = "<a href="+logoffURL+">Sign Out</a>";
	}
	else if(userType == 'M') //For remembered User
	{
		if(displayName != null && displayName.length > 0 && displayName != "\"\""){
			document.getElementById('myAccount').innerHTML = "<a href="+logonURL+">"+displayName+"'s Account</a>";			
		}
		else{
			document.getElementById('myAccount').innerHTML = "<a href="+logonURL+">My Account</a>";	
		}
		document.getElementById('userStatus').innerHTML = "<a href=Logoff?URL="+logonURL+">Not you?</a>";
	}
	else
	{
		document.getElementById('myAccount').innerHTML = "<a href="+logonURL+">My Account</a>";
		document.getElementById('userStatus').innerHTML = "<a href="+logonURL+">Sign In</a>";
	}
}

function parentShowResponse(catId, storeId){
   $.ajax({
    url:'/webapp/wcs/stores/servlet/MiniCartView?catalogId='+catId+'&storeId='+storeId,
    cache:false,
    success: function(html){
        document.getElementById("global_bag_wrapper").innerHTML = html;
		fos.bag = new fos.Bag();
        fos.bag.AddToCart();
        }
    });    
}

if(document.domain.indexOf('fossil.com') > 0){
	document.domain = 'fossil.com';
}
/*quick view rollover*/
$(document).ready(function(){
	$(".quickView a:first-child, .quickView a.quickViewIcon").bind("mouseenter",function(e){
		$(this).parent().children(".quickViewIcon").css("display", "inline");
	});
	$(".quickView a:first-child, .quickView a.quickViewIcon").bind("mouseleave",function(e){
		$(this).parent().children(".quickViewIcon").css("display", "none");
	});
});