// variable for minicart show/hide functionality
var trackerID, minicart_norecords;


// Make an object pointing to the location of the Flash movie on your web server.
// Try using the font name as the variable name, makes it easy to remember which
// object you're using. 
jenkins = { src: 'js/jenkins.swf' };

/**** READY() FUNCTION ****/
jQuery(document).ready(function() {	
	
	/*** content CSS workarounds ***/
	if(jQuery("#contentwrapper").html() == null)
	{
		//we have two layouts, one has the contentwrapper, the others don't
		//if the contentwrapper doesn't exist, add these classes to the divs
		jQuery("#content").addClass("content_nowrapper");
		jQuery("#breadcrumbs").addClass("brdcrmb_nowrapper");
	}
	else
	{
		//append the div contentfullheight at the end of the div main
		//or add that div to every program...
		jQuery("#main").append("<div id=\"contentfullheight\"></div>");
	}
	

	//there's a bug in msie, that doesn't resize the div contentfullheight, even though it has height 100% specified 
	//in screen css. this is a work around, maybe we find a better solution for this.
	if(jQuery.browser.msie)
	{
		//function that is called, when the window was resized
		function resize_cntheight() {    
			
			jQuery("#contentfullheight").css("height", document.getElementById("main").offsetHeight);
		}; 
		
		var resizeTimer = null;
		
		//binding the resize event to the window and defining a callback function after resizing
		jQuery(window).bind('resize', function() {    
			if (resizeTimer) 
			{
				clearTimeout(resizeTimer);    
			}
			resizeTimer = setTimeout(resize_cntheight, 100);
		});
	}

	/*** END - content CSS workarounds ***/
	
	/*** sIFR configuration ***/
	
	if(typeof sIFR != "undefined")
	{
		// Next, activate sIFR:
		sIFR.activate(jenkins);
	}
	
	/*** end sIFR configuration ***/
	
	
	// set Shopping Bag total items
	jQuery.ajax({
		type: "get",
		url: "WGCART1.PGM", 
		data: "task=wrtitemcount", 
		cache: false,
		success: 
			function (numitems) {
				// update shopping cart item count
				jQuery('#cartcount').html(numitems);
				
				//if the text is replaced by the sifr flash object, the original text is inside the following 
				//selector. we need to get the content, before removing the flash object.
				var shopbag = jQuery(".sifr-shopping").children(".sIFR-alternate").html();
				//first remove the class (to be on the save side)
				jQuery(".sifr-shopping").removeClass("sIFR-replaced");
				//reset the html content of the div to the original content (removes the flash object)
				jQuery(".sifr-shopping").html(shopbag);
				
				//now set the details for the flash object style
				var shoppingbag = {
					selector: '.sifr-shopping',
					css: [
					'.sIFR-root { font-size:22px; font-weight:normal; color:#ffffff; }'
					],
					wmode: 'transparent'
				};
				
				if(typeof sIFR != "undefined")
				{
					if(sIFR.isActive == true)
					{
						jQuery(".sifr-shopping").css({"position":"relative", "top":"10px"});
						//replace the text with the flash object
						sIFR.replace(jenkins, shoppingbag);
					}
					else
					{
						jQuery("#shopbag span").css({"visibility" : "visible"});
					}
				}
			}
	});
	
	
	/**** Start site search  ****/
	var site = "";
	var entry = "";
	
	// get the current site, replace the value in the search box if it has a site: in it
	// if this wgw.js is only being used in 1921, remove the ajax call and just set site = " site:1921jeans.com"
	jQuery.get(
	"wgsitesrch.pgm",
	{
		task: 'getDiv',
		rand: (Math.random()*100)
	},
	function (result)
	{
		if(result == "02") {
			site = " site:silverjeans.com";
		}
		if(result == "13") {
			site = " site:1921jeans.com";
		}
		
		entry = jQuery("#cse-search-box :text[value$='"+site+"']").val();
		if(entry != undefined){
			entry = entry.replace(site, "");
			jQuery("#cse-search-box :text[value$='"+site+"']").val(entry);
		}
	}
	);	
	
	// adds the current site to the search query
	jQuery('#cse-search-box').submit(function() {
		
		jQuery('#cse-search-box :text').val(jQuery('#cse-search-box :text').val()+site);
		return true;
	});
	
	/**** end site search ****/
	
	// get the user name
	jQuery.get(
	"wgmyacct.pgm",
	{
		task: 'name',
		rand: (Math.random()*100)
	},
	function (returnhtml)
	{
		jQuery('#welcome').html(returnhtml);
	}
	);
	
	// get the current country browsing
	jQuery.get(
	"wgmyacct.pgm",
	{
		task: 'country',
		rand: (Math.random()*100)
	},
	function (returnhtml)
	{
		if (returnhtml == "CA")
		{
			jQuery('#canada_site_link').css("display", "none");
			jQuery('#us_site_link').css("display", "inline");
			jQuery('#us_site_link').find("a").bind("click", function () { setCountry('US'); });
		}
		else
		{
			jQuery('#us_site_link').css("display", "none");
			jQuery('#canada_site_link').css("display", "inline");				
			jQuery('#canada_site_link').find("a").bind("click", function () { setCountry('CA'); });
		}
	}
	);
	
	if ( jQuery().live ) {
		jQuery(".popupSubscribe").live("click", function(e){	   
			doPopUp(e);
		});
	} else {
		jQuery(".popupSubscribe").bind("click", function(e){	   
			doPopUp(e);
		});
	}
	
	var defEmail = "email@domain.com";
	jQuery('#elistName').val(defEmail);
	
	jQuery('#elistName').focus(function(e){
		if (jQuery('#elistName').val() == defEmail) {
			jQuery('#elistName').val("").css("color", "black");
		}
	});
	
	jQuery('#elistName').blur(function(e){
		if (jQuery('#elistName').val() == "") {
			jQuery('#elistName').val(defEmail).css("color", "gray");
		}
	});
	
	
	// add link to Shopping Bag link
	jQuery('#shopbag a').click( function()
	{
		document.location = 'WGCART1.PGM';
	});
	
	
	/**** START MINICART SETUP ****/
	
	// add cart mouseover/mouseout functionality
	jQuery('#shopbag').mouseover(function () 
	{
		// clear any existing timed events, and start the "show cart" timed event
		window.clearTimeout(trackerID);
		trackerID = window.setTimeout('showCart()',350);
		
		// populate with current cart contents
		jQuery.get(
		"WGCART1.pgm",
		{
			task: 'wrtminicart',
			rnd: Math.random()
		},
		function (returnhtml)
		{
			jQuery('#minicart').html(returnhtml);
			
		}
		);		
		
	}).mouseout(function ()
	{
		// clear any existing timed events, and start the "hide cart" timed event
		window.clearTimeout(trackerID);
		trackerID = window.setTimeout('hideCart()',250);
	}
	);
	
	// add minicart container mouseover/mouseout functionality
	jQuery('#minicart').mouseover(function () 
	{
		// clear any existing timed events, specifically any mouseout events
		window.clearTimeout(trackerID);
	}).mouseout(function ()
	{
		// clear any existing timed events, and start the "hide cart" timed event
		window.clearTimeout(trackerID);
		trackerID = window.setTimeout('hideCart()',250);
	}
	);
	
	
	/**** END MINICART SETUP ****/
	
	
});		



// hide the minicart container
function hideCart()
{
	jQuery('#minicart').fadeOut('slow');
}

// show the minicart container
function showCart()
{
	jQuery('#minicart').fadeIn('slow');		
}


/*** email list signup ***/

// show/hide popup box and position it
function doPopUp(e) {
	var epopup = jQuery("#ePopUp");
	
	if (epopup.css("display") == "none" || epopup.css("display") == "") {
		jQuery.get(
		"WGEMALLIST.pgm",
		{
			task: 'DEFAULT'
		},
		function (returnhtml)
		{
			epopup.html(returnhtml);
			
		}
		);
		
		//var poplocX = e.pageX;
	    //var poplocY = e.pageY;

	    //poplocX = poplocX - (epopup.width()/1.7);
	    //poplocY = poplocY + 15;// - (epopup.height()/2)
          
	    //epopup.css("left", (poplocX)+"px");
	    //epopup.css("top", (poplocY)+"px");
		
		epopup.fadeIn("slow");
		//jQuery('#elistName').focus();
		
	} else {
		epopup.fadeOut("fast");
	}
}


// email list ajax call and response
function elistSignup() {
	var newEmail = jQuery('#elistName').val();
	
	jQuery.ajax({
		type: "GET",
		url: "WGEMALLIST.pgm",
		dataType: "html",
		data: {email: newEmail, task: "signup"},
		success: function(data) {
			
			jQuery('#ePopUp').html("<table>" + data + "</table>");
			if ( !jQuery().live ) {
				
				jQuery(".popupSubscribe").unbind("click").bind("click", function(e){	   
					doPopUp(e);
				});
			}
			
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			alert("XMLHttpRequest- readystate: " + XMLHttpRequest.readyState +
			" status: " + XMLHttpRequest.status + "\ntextStatus: " + textStatus +
			"\nerrorThrown: " + errorThrown);
		}
	});		// end of ajax 
}		// end of function

function createaccount()
{
	var url;
	
	badChar = /^(\w)+([\.\w_-])*@([\w])+(\.[\w_-]+)+$/g     // this means anything not matched by this is 'bad'
	var value = jQuery("#elistName").val();
	var result = value.match(badChar); // result will be null if no match is found, which means 'value' is not an email
	
	if(jQuery("#elistName").val() == "" || result == null)
	{
		url = 'wglogin.pgm';
	}
	else
	{
		url = 'wgcrtacct.pgm?cuemail=' + jQuery('#elistName').val(); 
	}
	
	window.location = url;
}

/*** end of email list signup ***/


/*** SITE MAP ***/
function prods(ul)
{
	jQuery(".stmp_prods").children("ul.prods").hide("slow");
	jQuery(".stmp_prods").css("list-style-image", "url(img/stmp_rightarrow.gif)");
	
	if(jQuery("#" + ul).css("display") == "none")
	{
		jQuery("#" + ul).show("slow");
		jQuery("#" + ul).parent("li").css("list-style-image", "url(img/stmp_downarrow.gif)");
	}
	else
	{
		jQuery("#" + ul).hide("slow");
		jQuery("#" + ul).parent("li").css("list-style-image", "url(img/stmp_rightarrow.gif)");
	}
}
/*** SITE MAP - END ***/


//set the country and redirect to the home page
function setCountry(countrytoset)
{
	jQuery.get(
	"wgmyacct.pgm",
	{
		task: 'setcountry',
		country: countrytoset,
		rand: (Math.random()*100)
	},
	function (returnhtml)
	{
		window.location = 'wghome.pgm';
	}
	);
}
