// variable for minicart show/hide functionality
var trackerID, minicart_norecords;


/**** 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>");
	}
	
	//jQuery("body").append("<div id=\"bluebar\"></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 ***/
	
	// set Shopping Bag total items, do this first
	jQuery.get("WGCART1.PGM", { task: "wrtitemcount", rnd: Math.random() },
		function (numitems) {
			// update shopping cart item count
			jQuery('#cartcount').html(numitems);
		}
	);


	/**** 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
	//SSR - moved to footer
/*	jQuery.ajax({
		url: "wgmyacct.pgm",
		data: "task=country&rand=" + (Math.random()*100),
		cache: false,
		success: function (returnhtml)
			{
				if (returnhtml == "CA")
				{
					jQuery('#canada_site_link').css("display", "none");
					jQuery('#us_site_link').css("display", "inline");
					//jQuery('#us_site_img').css("display", "none");
					//jQuery('#canada_site_img').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_img').css("display", "none");
					//jQuery('#us_site_img').css("display", "inline");				
					jQuery('#canada_site_link').find("a").bind("click", function () { setCountry('CA'); });
				}
			}
	
	});
		*/
		
	var defEmail = "email@domain.com";
		
	if ( jQuery().live ) {
		jQuery(".popupSubscribe").live("click", function(e){	   
			doPopUp(e);
		});
	} else {
		jQuery(".popupSubscribe").bind("click", function(e){	   
			doPopUp(e);
		});
	}
		
	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

/*** 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)
		{
			//alert('country set');
			window.location = 'wghome.pgm?rand=' + (Math.random()*100);
		}
	);
}