var oWin, oImg;

function init (e) {
	preloadImages(Array("/images/backgrounds/menu-welcome.png",
						"/images/backgrounds/menu-car.png",
						"/images/backgrounds/menu-discover.png",
						"/images/backgrounds/menu-fuel.png"));
	addMenu();
	adjustExternalLinks ();
	adjustSearchBox ();
}

function preloadImages(paths) {
	for (n = 0; n < paths.length; n++) {
		image = new Image();
		image.src = paths[n];
	}
}

function addMenu() {
	var map_welcome  = document.getElementById("menu-welcome");
	var map_car      = document.getElementById("menu-car");
	var map_discover = document.getElementById("menu-discover");
	var map_travel   = document.getElementById("menu-travel");
	var map_fuel     = document.getElementById("menu-fuel");

	var nav_welcome  = document.getElementById("nav-takeanaudiotour").firstChild;
	var nav_car      = document.getElementById("nav-home").firstChild;
	var nav_discover = document.getElementById("nav-discoveryourbag").firstChild;
	var nav_travel   = document.getElementById("nav-travelblog").firstChild;
	var nav_fuel     = document.getElementById("nav-getsomefuel").firstChild;

	map_welcome.onmouseover = nav_welcome.onmouseover = function() {swapBackground("banner-welcome.png");};
	map_car.onmouseover = nav_car.onmouseover = function() {swapBackground("banner-car.png");};
	map_discover.onmouseover = nav_discover.onmouseover = function() {swapBackground("banner-discover.png");};
	map_travel.onmouseover = nav_travel.onmouseover = function() {swapBackground("banner-travel.png");};
	map_fuel.onmouseover = nav_fuel.onmouseover = function() {swapBackground("banner-fuel.png");};
	
	map_welcome.onmouseout = map_car.onmouseout = map_discover.onmouseout = map_travel.onmouseout = map_fuel.onmouseout = function() {swapBackground("banner.png");};
	nav_welcome.onmouseout = nav_car.onmouseout = nav_discover.onmouseout = nav_travel.onmouseout = nav_fuel.onmouseout = function() {swapBackground("banner.png");};
}

function swapBackground(filename) {
	var path   = "/images/backgrounds/";
	var banner = document.getElementById("top-banner");
	
	banner.src = path + filename;
}

function disableFinishButton() {
	var finish_btn = document.getElementById("btnFinish");
	var finish_div = finish_btn.parentNode;
	var finish_p = document.createElement("p");

	finish_btn.disabled = true;
	
	finish_p.className = "error";
	finish_p.innerHTML = "Please wait while your order is being processed. Refreshing this page may result in duplicate payments.";
	
	finish_div.insertBefore(finish_p, finish_btn);
}

function fillShipping() {
	var oFirstName = document.getElementById("txtFirstName");
	var oLastName = document.getElementById("txtLastName");
	var oAddress = document.getElementById("txtAddress");
	var oCity = document.getElementById("txtCity");
	var oState = document.getElementById("ddlState");
	var oCountry = document.getElementById("ddlCountry");
	var oZip = document.getElementById("txtZip");

	var oShipFirstName = document.getElementById("txtShipFirstName");
	var oShipLastName = document.getElementById("txtShipLastName");
	var oShipAddress = document.getElementById("txtShipAddress");
	var oShipCity = document.getElementById("txtShipCity");
	var oShipState = document.getElementById("ddlShipState");
	var oShipCountry = document.getElementById("ddlShipCountry");
	var oShipZip = document.getElementById("txtShipZip");
	
	if (document.getElementById("sameShipping").checked) {
		oShipFirstName.value = oFirstName.value;
		oShipLastName.value = oLastName.value;
		oShipAddress.value = oAddress.value;
		oShipCity.value = oCity.value;
		oShipState.selectedIndex = oState.selectedIndex;
		oShipCountry.selectedIndex = oCountry.selectedIndex;
		oShipZip.value = oZip.value;
	} else {
		oShipFirstName.value = "";
		oShipLastName.value = "";
		oShipAddress.value = "";
		oShipCity.value = "";
		oShipState.value = "";
		oShipCountry.value = "";
		oShipZip.value = "";
	}
}

function adjustSearchBox () {
	var oSearchBox = document.getElementById("keywords");

	if (window.addEventListener) {
		oSearchBox.addEventListener("focus", clearInput, false);
		oSearchBox.addEventListener("blur", setInput, false);
	} else {
		oSearchBox.attachEvent("onfocus", clearInput);
		oSearchBox.attachEvent("onblur", setInput);
	}
}

function clearInput (e) {
	var oTarget = (e.target) ? e.target : event.srcElement;
	
	if (oTarget.value == "Search our shop") oTarget.value = "";
}

function setInput (e) {
	var oTarget = (e.target) ? e.target : event.srcElement;
	
	if (oTarget.value == "") oTarget.value = "Search our shop";
}

function checkTax(sState, nAmount, nTotal) {
	var oDdl = document.getElementById("ddlState");
	var oLabel = document.getElementById("taxLabel");
	var nTaxAmount = nAmount * nTotal;
	
	if (oDdl.options[oDdl.selectedIndex].value == sState) {
		oLabel.innerHTML = "$" + nTaxAmount.toFixed(2) + " sales tax will be added.";
	} else {
		oLabel.innerHTML = "";	
	}
}

function disableImageClick(e) {
	var sMsg, dDate, nYear;
	var oSource;
	var aAllow = new Array("/links/");

	for (var n = 0; n < aAllow.length; n++) {
		if (window.location.href.indexOf(aAllow[n]) > -1) return true;
	}

	dDate = new Date();
	nYear = dDate.getFullYear();
	sMsg = "This image is copyright " + nYear;
	
	if (document.all)
		oSource = event.srcElement;
	else {
		oSource = e.target;
	}
	
	if (oSource.nodeName == "IMG") {
		alert(sMsg);
		e.cancelBubble = true;
		
		if (e.stopPropagation) {
			e.stopPropagation();
			e.preventDefault();
		}
		return false;
	}
	
	return true;
}

function adjustExternalLinks() {
	if (document.getElementsByTagName) {
		var n, hyperlink;
		var linkArray = document.getElementsByTagName("a");

		for (n = 0; n < linkArray.length; n++) {
			hyperlink = linkArray[n];

			if (hyperlink.className == "external-link") {
				hyperlink.onclick = function () {window.open(this.href, "_blank", "width=800, height=600, menubar=yes, toolbar=yes, location=yes, scrollbars=yes, resizable=yes"); return false;};
			}
		}
	}
}

function popUp(sURL) {
	var sFeatures, sHTML;
	oImage = new Image();
	
	oImage.src = sURL;
	
	if (oImage.complete) {
		resizePopUp();
	} else {
		oImage.onload = resizePopUp;
	}
}

function resizePopUp() {
	var nWidth, nHeight;
	
	nWidth = (parseInt(oImage.width) + 50 > screen.width) ? screen.width - 50 : parseInt(oImage.width) + 25;
	nHeight = (parseInt(oImage.height) + 200 > screen.height) ? screen.height - 200 : parseInt(oImage.height) + 110;
	
	sFeatures = "width=" + nWidth + ", height=" + nHeight + ", status=no, scrollbars=yes";
	
	sHTML = "<html><head><title>Image Viewer</title>";
	sHTML += "<meta http-equiv='imagetoolbar' content='no'>";
	sHTML += "<script type='text/javascript' src='/scripts/scripts.js'></script>";
	sHTML += "</head><body style='margin:5px 0 0;padding:0;text-align:center;font:12px sans-serif'>";
	sHTML += "<img src='" + oImage.src + "' alt='Product Image' onclick='window.close();'>";
	sHTML += "<p><a href='javascript:window.close();'>Close Window</a></p>";
	sHTML += "</body></html>";

	oWin = window.open("", "", sFeatures);
	oWin.document.open();
	oWin.document.write(sHTML);
	oWin.document.close();
}

function openWindow(sUrl, nWidth, nHeight) {
	var sFeatures = "width=" + nWidth + ", height=" + nHeight + "menubar=yes, toolbar=yes, location=yes, resizable=yes scrollbars=yes";
	
	window.open(sUrl, "_blank", sFeatures);
}

if (window.addEventListener) {
	window.addEventListener("load", init, false);
	document.addEventListener("contextmenu", disableImageClick, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", init);
	document.attachEvent("oncontextmenu", disableImageClick);
}
