///////////////////////////
//GENERAL IMAGE PRELOADER//
///////////////////////////
if(document.images)
{
	var pic1 = new Image(51, 29);
	pic1.src = "/images/bags-on.jpg";
	
	var pic2 = new Image(49, 29);
	pic2.src = "/images/kayak-on.jpg";
	
	var pic3 = new Image(52, 29);
	pic3.src = "/images/canoe-on.jpg";
	
	var pic4 = new Image(65, 29);
	pic4.src = "/images/camping-on.jpg";
	
	var pic5 = new Image(51, 29);
	pic5.src = "/images/store-on.jpg";
	
	var pic6 = new Image(39, 29);
	pic6.src = "/images/sale-on.jpg";
	
	var pic7 = new Image(166, 16);
	pic7.src = "/images/home3-on.jpg";
	
	var pic8 = new Image(166, 16);
	pic8.src = "/images/home3-on-ie6.jpg";
	
	var pic9 = new Image(512, 315);
	pic9.src = "/images/popup.png";
	
	var pic10 = new Image(64, 65);
	pic10.src = "/images/gib-on.jpg";
	
	var pic11 = new Image(85, 72);
	pic11.src = "/images/no-worries-on.jpg";
	
	var pic12 = new Image(75, 28);
	pic12.src = "/images/minicart-link-on.jpg";
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
//HIDE SELECT BOXES AS FIX FOR IE6 (POSITIONED ELEMENTS WILL ALWAYS BE BEHIND SELECT BOXES IN < IE7)//
//////////////////////////////////////////////////////////////////////////////////////////////////////
function hideSelects(ans)
{
	switch(ans)
	{
		case "yes":
		$("#productDetails select").hide();
		break;
		
		case "no":
		$("#productDetails select").show();
		break;
	}
}

///////////////
//SWAP IMAGES//
///////////////
function swapImg(caller, state, type)
{
	caller.src = "/images/"+caller.id+"-"+state+"."+type;
}

///////////////////////////////////
//DISPLAY SUBLINKS ON SUBLINKS BG//
///////////////////////////////////
function displaySublinks(state, caller)
{	
	$("#sublinkswrap").css("display", state);
	
	if(caller)
	{
		$.get("/sublinks/"+caller.id+"_sublinks.html", function(data)
		{
			$("#sublinks").html(data);
		});
	}
	else
	{
		$("#sublinks").html("");
	}
	
	if(navigator.appVersion.indexOf("MSIE 6") > -1)
	{
		switch(state)
		{
			case "block":
			hideSelects("yes");
			break;
			
			case "none":
			hideSelects("no");
			break;
		}
	}
}

//////////////////////////////////////////////////
//SET AND UNSET login AND retailer LINKS' EVENTS//
//////////////////////////////////////////////////
function setLoginRetailerEvents(ans)
{
	switch(ans)
	{
		case "yes":
		$("#loginPopup").click(function(){displayPopup(this)});
		$("#retailersPopup").click(function(){displayPopup(this); retailersSelect()});
		$(".closePopupLink").click(hidePopup);
		break;
		
		case "no":
		$("#loginPopup").unbind();
		$("#retailersPopup").unbind();
		$(".closePopupLink").unbind();
		break;
	}
}

//////////////////////////////////////
//SHOW POPUP FOR LOGIN AND RETAILERS//
//////////////////////////////////////
var popupArr = new Array();
popupArr["loginPopup"] = "loginform";
popupArr["retailersPopup"] = "retailers";

function displayPopup(caller)
{
	var browser = navigator.appName;
	
	setLoginRetailerEvents("no");
	
	$("#popupwrap").hide().css("top", "150px");
	$("#popup").html("<div id=\""+popupArr[caller.id]+"\">"+$("#"+popupArr[caller.id]).html()+"</div>");
	
	if(browser != "Microsoft Internet Explorer")
	{
		$("#popupwrap").fadeIn(1000, function(){setLoginRetailerEvents("yes")});
	}
	else
	{
		$("#popupwrap").show();
		setLoginRetailerEvents("yes");
	}
	
	if(caller.id == "loginPopup")
	{
		$("#loginPopup").hide();
		$("#registerLink").hide();
	}
	else
	{
		$("#loginPopup").css("display", "inline-block");
		$("#registerLink").css("display", "inline-block");
	}
	
	if(navigator.appVersion.indexOf("MSIE 6") > -1)
	{
		hideSelects("yes");
	}
}

//////////////////////////////////////
//HIDE POPUP FOR LOGIN AND RETAILERS//
//////////////////////////////////////
function hidePopup()
{
	var browser = navigator.appName;
	
	setLoginRetailerEvents("no");
	
	if(browser != "Microsoft Internet Explorer")
	{
		$("#popupwrap").animate({top: "100px", opacity: "0"}, 1000, function()
		{
			$("#popupwrap").hide().css("opacity", "1");
			$("#popup").html("");
			
			setLoginRetailerEvents("yes");
			
			$("#loginPopup").css("display", "inline-block");
			$("#registerLink").css("display", "inline-block");
		});
	}
	else
	{
		$("#popupwrap").hide()
		$("#popup").html("");
		
		setLoginRetailerEvents("yes");
		
		$("#loginPopup").css("display", "inline-block");
		$("#registerLink").css("display", "inline-block");
	}
	
	if(navigator.appVersion.indexOf("MSIE 6") > -1)
	{
		hideSelects("no");
	}
}

////////////////////////////
//SHOW SOCIAL NETWORK INFO//
////////////////////////////
function socialFade(caller)
{	
	var index = $("#socialLinks > a").index(caller);
	
	if($("#socialBoxes > div:eq("+index+")").css("display") == "none")
	{
		var fadein;
		var upOpac;
		var fadeout;
		var downOpac;
		
		for(i=0;i<2;i++)
		{
			if($("#socialLinks a").index($("#socialLinks > a:eq("+i+")")) == index)
			{
				var upOpac = $("#socialLinks > a:eq("+i+") > img");
				var fadein = $("#socialBoxes > div:eq("+i+")");
			}
			else
			{
				var downOpac = $("#socialLinks > a:eq("+i+") > img");
				var fadeout = $("#socialBoxes > div:eq("+i+")");
			}
		}
		
		$(fadein).fadeIn(1000);
		$(fadeout).fadeOut(1000);
		$(upOpac).css("opacity", "1");
		$(downOpac).css("opacity", "0.7");
	}
	else
	{
		$("#socialLinks > a:eq("+index+") > img").css("opacity", "0.7");
		$("#socialBoxes > div:eq("+index+")").fadeOut(1000);
	}
}

////////////////////////////////////////////
//FUNCTION TO SHOW GIB AND NO WORRIES INFO//
////////////////////////////////////////////
function showInfo(caller, ans)
{
	var theId = caller.firstChild.id;
	
	if(navigator.appVersion.indexOf("MSIE 6") > -1)
	{
		var locBottom = (theId == "gib") ? 72 : 79;
	}
	else if(navigator.appVersion.indexOf("MSIE 7") > -1)
	{
		var locBottom = (theId == "gib") ? 73 : 80;
	}
	else
	{
		var locBottom = (theId == "gib") ? 70 : 77;		
	}
	
	var locRight = (theId == "gib") ? 110 : 6;
		
	switch(ans)
	{
		case "yes":
		swapImg(caller.firstChild, "on", "jpg");
		formatInfoCaller(caller, "on");
		getInfo(caller);
		$("#info").css({right : locRight+"px", bottom : locBottom+"px"}).show();
		eventifyInfo(caller);
		break;
		
		case "no":
		swapImg(caller.firstChild, "off", "jpg");
		formatInfoCaller(caller, "off");
		hideInfo(caller, true);
		break;
	}
}

//////////////////////////////////
//FUNCTION TO GET INFO FOR #INFO//
//////////////////////////////////
function getInfo(caller)
{
	var infos = new Array();
	infos["gib"] = "Green is Better&reg;<br />We at GAIA&reg; Sports believe that protecting our planet and keeping our environment green is a top priority.<br /><br />All products bearing the Green is Better&reg; seal are made with environmentally friendly materials.  These products are guaranteed PVC free! So ease your conscience when buying these products because we've got your carbon footprints covered!";
	infos["no-worries"] = "No Worries Guarantee<br />Freedom on land, water, or mountain can only come from peace of mind. Worrying about your gear can distract you from the challenge at hand or the beauty around you.<br /><br />At GAIA&reg; Sports we recognize that this peace of mind can only come from quality gear. Our No Worries guarantee provides that all GAIA&reg; products are guaranteed to the owner against defects in material and/or workmanship. So, No Worries! We've got you covered!";
	
	$("#info").html(infos[caller.firstChild.id]);
}

///////////////////////////////////////////////
//FORMAT BORDER AND BACKGROUND ON INFO CALLER//
///////////////////////////////////////////////
function formatInfoCaller(caller, state)
{
	switch(state)
	{
		case "on":
		$(caller).css({background : "#FFFFFF", border : "1px solid #999999", padding : "0 4px 5px 4px", zIndex : "102"})
		break;
		
		case "off":
		$(caller).css({background : "none", border : "0 none", borderBottom : "1px solid #CCCCCC", padding : "0 5px 5px 5px", zIndex : "101"})
		break;
	}
}

/////////////////////////////////////////////////////////
//FUNCTION TO ADD MOUSEOVER AND MOUSEOUT EVENTS TO #INFO//
/////////////////////////////////////////////////////////
function eventifyInfo(caller)
{
	$("#info").mouseover(function()
	{
		$("#info").show();
		getInfo(caller);
		swapImg(caller.firstChild, "on", "jpg");
		formatInfoCaller(caller, "on");
	});
	$("#info").mouseout(function()
	{
		swapImg(caller.firstChild, "off", "jpg");
		formatInfoCaller(caller, "off");
		hideInfo();
	});
}

////////////////////////////////////
//FUNCTION TO CLEAR AND HIDE #INFO//
////////////////////////////////////
function hideInfo(caller, keepEvents)
{
	$("#info").unbind();
	$("#info").html("").hide();
	if(caller && keepEvents)
	{
		eventifyInfo(caller);
	}
}

////////////////////////
//GET RETAILERS SELECT//
////////////////////////
function retailersSelect()
{
	$.post("/retailers/retailers.php", {retailersSelect: "retailersselect"}, function(data)
	{
		$("#retailersSelectWrap").html(data);
	});
}

/////////////////////////////////////////////////////////
//FUNCTION TO DISPLAY AND HIDE CUSTOMER SERVICE OPTIONS//
/////////////////////////////////////////////////////////
function showCustServOpts()
{
	$("#custServiceOpts").css("display") == "none" ? $("#custServiceOpts").show() : $("#custServiceOpts").hide();
}

////////////////////////
//FUNCTION TO MAIL SAM//
////////////////////////
function someThing()
{
	var one = "ma";
	var two = "il";
	var three = "to";
	var col = ":";
	var name = "sam";
	var at = "@";
	var dom1 = "gaia";
	var dom2 = "sports";
	var dot = ".";
	var ext = "com";
	var url = one + two + three + col + name + at + dom1 + dom2 + dot + ext;
	window.location.href = url;
}

////////////////////////////////////////////
//FUNCTION TO HANDLE EMPTY REQUIRED FIELDS//
////////////////////////////////////////////
function emptyFields(caller, state)
{
	$(caller).attr("class", "");
}

/////////////////////////////////////////////
//FUNCTION TO GET CONTENTS OF SHOPPING CART//
/////////////////////////////////////////////
function getCartContents(doOpen)
{
	$.get("pages.php?pageid=24", function(data)
	{
		var mcLinkStart = data.indexOf("<!--MINICARTLINK-->");
		var mcLinkStop = data.indexOf("<!--ENDLINK-->");
		var mcLink = data.slice(mcLinkStart+19, mcLinkStop);
		
		var mcStart = data.indexOf("<!--MINICART-->");
		var mcStop = data.indexOf("<!--ENDCART-->");
		var mc = data.slice(mcStart+15, mcStop);
		
		$("#minicartLink").html(mcLink);
		$("#minicart").html(mc);
		
		if (doOpen == true)
		{
			$("#minicart").show();
			$("#minicartLink a").attr("class", "minicartOn");
		}
	});
}

/////////////////////////////////////////
//FUNCTION TO REMOVE ITEM FROM MINICART//
/////////////////////////////////////////
function removeProduct(url)
{
	$.get(url, function()
	{
		if(window.location.href.indexOf("cart.php") > -1) window.location.href = window.location.href;
		getCartContents(true);
	});
}

////////////////////////////////////
//FUNCTION TO EDIT PRODUCT OPTIONS//
////////////////////////////////////
function editProduct(state, id, product, quant, opts)
{
	switch(state)
	{
		case "open":
			optsval = (opts == true) ? "Y" : "N";
			$.get("popup_poptions.php?target=cart&id="+id+"&theProd="+product+"&quant="+quant+"&opts="+optsval, function(data)
			{
				var stt = data.indexOf("<form");
				var stp = data.indexOf("</form>");
				var updateForm = data.slice(stt, stp+6);
				
				var theWidth = document.body.clientWidth;
				var theHeight = document.body.clientHeight;
				
				$(document.body).append('<div id="editProdOptsOverlay"></div>');
				$("#editProdOptsOverlay").css({width : theWidth+"px", height : theHeight+"px"});
				$(document.body).append('<div id="editProdOptsWrap"><div id="editProdOpts">'+data+'</div></div>');
			});
		break;
		
		case "update":
			var quantDat = $("form[name=quantform]").serialize();
			if($("form[name=orderform]").length > 0) var optsDat = $("form[name=orderform]").serialize();
			$.post("cart.php", quantDat, function()
			{
				if($("form[name=orderform]").length > 0)
				{
					$.post("popup_poptions.php", optsDat, function()
					{
						if(window.location.href.indexOf("cart.php") > -1) window.location.href = window.location.href;
						removeEditProd();
						getCartContents(true);
					});
				}
				else
				{
					if(window.location.href.indexOf("cart.php") > -1) window.location.href = window.location.href;
					removeEditProd();
					getCartContents(true);
				}
			});
		break;
	}
}

/////////////////////////////////////////////////
//FUNCTION TO CLOSE EDIT PRODUCTION OPTIONS BOX//
/////////////////////////////////////////////////
function removeEditProd()
{
	$("#editProdOptsWrap").remove();
	$("#editProdOptsOverlay").remove();
}

//////////////////////////////////
//FUNCTION TO ADD COUPON TO CART//
//////////////////////////////////
function addCoupon()
{
	var coupon = $("form[name=couponform]").serialize();
	$.post("cart.php", coupon, function()
	{
		if(window.location.href.indexOf("cart.php") > -1) window.location.href = window.location.href;
		getCartContents(true);
	});
}

function removeCoupon()
{
	$.get("cart.php?mode=unset_coupons", function()
	{
		if(window.location.href.indexOf("cart.php") > -1) window.location.href = window.location.href;
		getCartContents(true);
	});
}

///////////////////////////////////////////////////
//FUNCTION TO HANDLE OPENING AND CLOSING MINICART//
///////////////////////////////////////////////////
function miniCart(update)
{
	if(update == true)
	{
		getCartContents(true);
	}
	else
	{
		if($("#minicart").css("display") == "none")
		{
			$("#minicart").show();
			$("#minicartLink a").attr("class", "minicartOn")
		}
		else
		{
			$("#minicart").hide();
			$("#minicartLink a").attr("class", "minicartOff")
		}	
	}
}

///////////
//SECRETS//
///////////
function openEE()
{
	$("#header").append('<div id="ee" style="width:100%;position:absolute;top:50px;z-index:1000"><div style="position:relative;width:475px;height:384px;margin:0 auto;background:#FFFFFF;border:2px solid #999999;"><div style="float:right;margin:2px 8px 0 0"><a href="javascript:void(0)" onclick="document.getElementById(\'header\').removeChild(document.getElementById(\'ee\'))">X</a></div><div style="width:425px;height:344px;margin:20px auto;"><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/mbs64GvGgPU&hl=en&fs=1&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/mbs64GvGgPU&hl=en&fs=1&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></div></div></div>');
}

/////////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function()
{
	//////////////////////////////////////////////////////////
	//SET LINKS TO SWAP IMAGES ON HOVER AND DISPLAY SUBLINKS//
	//////////////////////////////////////////////////////////
	var links1;
	var links2;
	var t;
	
	$("a.imgSwapLink").mouseover(function()
	{		
		if(typeof(links2) != "undefined" && typeof(t) != "undefined")
		{
			clearTimeout(t);
			swapImg(links2, "off", "jpg");
		}
		
		links1 = this.firstChild;
		swapImg(links1, "on", "jpg");
		displaySublinks("block", links1);
		swapImg(document.getElementById("home3"), "on", "jpg");
	});
	
	$("a.imgSwapLink").mouseout(function()
	{		
		links2 = this.firstChild;
		t = setTimeout(function()
		{
			swapImg(links2, "off", "jpg");
			swapImg(document.getElementById("home3"), "off", "jpg");
			displaySublinks("none");
		}, 500);
	});
	
	$("#sublinkswrap").mouseover(function()
	{
		clearTimeout(t);
	});
	
	$("#sublinkswrap").mouseout(function()
	{
		t = setTimeout(function()
		{
			swapImg(links1, "off", "jpg");
			swapImg(document.getElementById("home3"), "off", "jpg");
			displaySublinks("none");
		}, 500);
	});
	
	/////////////////////////////////////////
	//SHOW POPUP BOX FOR LOGIN OR RETAILERS//
	/////////////////////////////////////////
	setLoginRetailerEvents("yes");
	
	///////////////////////////////////////////////////////////////
	//SHOW SOCIAL BOXES (LINK TO FACEBOOK FAN PAGE FOR OPERA)//
	///////////////////////////////////////////////////////////////
	if(navigator.appName == "Opera")
	{
		$("#fbpage").attr("href", "http://www.facebook.com/pages/Gaia-Sports/119548746712");
		$("#fbFanBox").remove();
		$("#twitterpage").toggle(
		function()
		{
			$("#twitterpage > img").css("opacity", "1");
			$("#twitterBox").fadeIn(1000);
		},
		
		function()
		{
			$("#twitterpage > img").css("opacity", "0.7");
			$("#twitterBox").fadeOut(1000);
		});
	}
	else
	{
		$("#socialLinks > a").click(
		function()
		{
			socialFade(this);
		});
	}
	
	////////////////////////////////
	//SHOW GIB AND NO WORRIES INFO//
	////////////////////////////////
	$("#footerLogos > a").mouseover(function(){swapImg(this.firstChild, "on", "jpg");showInfo(this, "yes")}).mouseout(function(){swapImg(this.firstChild, "off", "jpg");showInfo(this, "no")});
	
	//////////////////////////////////////////
	//SHOW AND HIDE CUSTOMER SERVICE OPTIONS//
	//////////////////////////////////////////
	$("#custServiceLink").mouseover(showCustServOpts).mouseout(showCustServOpts);
	$("#custServiceOpts").mouseover(showCustServOpts).mouseout(showCustServOpts);
	
	//////////////////////////
	//SETUP MAIL LINK TO SAM//
	//////////////////////////
	if($(".mailLinkSam").length > 0)
	{
		$(".mailLinkSam").each(function()
		{
			$(this).html('<div class="floatClear"></div><div class="mailLinkSamImg floatLeft"><a href="javascript:void(0)" onclick="someThing()"/></div><div class="mailLinkSamText floatLeft"><a href="javascript:void(0)" onclick="someThing()">Email</a></div><div class="floatClear"></div>');
		});
	}
	
	////////////////////////////////
	//HANDLE EMPTY REQUIRED FIELDS//
	////////////////////////////////
	if($(".forgotField:eq(0)").length > 0)
	{
		$(".forgotField").each(function()
		{
			$(this).focus(function(){emptyFields(this)});
		});
	}
	
	//////////////////////
	//FILL MINICART HTML//
	//////////////////////
	getCartContents();
	
	//////
	//EE//
	//////
	if (window.addEventListener)
	{  
		var state = 0, konami = [38,38,40,40,37,39,37,39,66,65];  
		window.addEventListener("keydown", function(e)
		{  
			if (e.keyCode == konami[state]) state++;
			else state = 0;
			if (state == 10) openEE();
		}, true);
	}
});