var oWin, oImg;

function init (e) {
	adjustExternalLinks ();
	adjustSearchBox ();
	preloadImages();
	rotateHighlights(0);
}

function preloadImages() {
	var aImages = [];
	
	aImages[0] = '/images/highlights/logo-belief-net.png';
	aImages[1] = '/images/highlights/cover-quilting.jpg';
	aImages[2] = '/images/highlights/cover-people.jpg';
	aImages[3] = '/images/highlights/logo-inc.png';
	aImages[4] = '/images/highlights/logo-hollywood-news.png';
	aImages[5] = '/images/highlights/logo-home-journal-opi.png';
	aImages[6] = '/images/highlights/logo-mashable.png';
	aImages[7] = '/images/highlights/logo-pinesol.jpg';
	aImages[8] = '/images/highlights/wetv.gif';
	aImages[9] = '/images/highlights/cover-ladies-home-journal.jpg';
	aImages[10] = '/images/highlights/cover-womens-world.jpg';
	aImages[11] = '/images/highlights/pillowcase-program.png';
	aImages[12] = '/images/highlights/logo-dreamers-into-doers.png';
	aImages[13] = '/images/highlights/logo-nature-made.png';
	aImages[14] = '/images/highlights/logo-dreamers-into-doers.png';
	aImages[15] = '/images/highlights/cover-country-almanac.png';
	aImages[16] = '/images/highlights/terry-at-harpo-studios.png';
	aImages[17] = '/images/highlights/cover-cottages-and-bungalows.png';
	aImages[18] = '/images/highlights/cover-paint-magic.png';
	
	for (var n = 0, sUrl; sUrl = aImages[n++];) {
		var temp = new Image();
		temp.src = sUrl;
	}
}

function rotateHighlights(nCtr) {
	var hl_div = document.getElementById('highlights-rotator');
	var aHighlights = [];
	
	aHighlights[0] = '<img src="/images/highlights/logo-belief-net.png" alt="beliefnet - Inspiration. Spirituality. Faith." />';
	aHighlights[1] = '<img src="/images/highlights/cover-quilting.jpg" alt="American Patchwork &amp; Quilting" />';
	aHighlights[2] = '<div style="text-align:center"><img src="/images/highlights/cover-people.jpg" alt="People Magazine" /></div><p style="text-align:center">August 2, 2010</p>';
	aHighlights[3] = '<img src="/images/highlights/logo-inc.png" alt="inc." />';
	aHighlights[4] = '<img src="/images/highlights/logo-hollywood-news.png" alt="HollywoodNews.com" />';
	aHighlights[5] = '<div style="text-align:center"><img src="/images/highlights/logo-home-journal-opi.png" alt="Ladies Home Journal - OPI" /></div><p style="text-align:center">Top 10 national winner for making a powerful difference in our community and nationwide</p>';
	aHighlights[6] = '<img src="/images/highlights/logo-mashable.png" alt="Mashable Social Networking News" />';
	aHighlights[7] = '<div style="text-align:center"><img src="/images/highlights/logo-pinesol.jpg" alt="Powerful Difference" /></div><p style="text-align:center">2010 National Top 10 Winner!</p>';
	aHighlights[8] = '<img src="/images/highlights/wetv.gif" alt="We TV" /><p style="text-align:center">Featured on Website</p>';
	aHighlights[9] = '<div style="margin-top:10px;text-align:center"><img src="/images/highlights/cover-ladies-home-journal.jpg" alt="Ladies\' Home Journal" /><p>Featured in the May 2010 issue</p></div>';
	aHighlights[10] = '<img src="/images/highlights/cover-womans-world.jpg" alt="Woman\'s World" /><p style="text-align:center">March 15th, 2010 issue</p>';
	aHighlights[11] = '<img src="/images/highlights/pillowcase-program.png" alt="1 Million Pillowcase Challenge" /><p style="text-align:center">Better Homes and Gardens</p>';
	aHighlights[12] = '<img src="/images/highlights/logo-dreamers-into-doers.png" alt="Martha Stewart\'s Dreamers into Doers" /><p style="text-align:center">2009 Winner of the Martha Stewart Dreamers into Doers People\'s Choice Award</p>';
	aHighlights[13] = '<img src="/images/highlights/logo-nature-made.png" alt="Nature Made" /><p style="text-align:center">2009 National Winner</p>';
	aHighlights[14] = '<img src="/images/highlights/logo-dreamers-into-doers.png" alt="Martha Stewart\'s Dreamers into Doers" /><p style="text-align:center">Top Ten Winner of Martha Stewart\'s Doer of the Week Contest in 2009</p>';
	aHighlights[15] = '<img src="/images/highlights/cover-country-almanac.png" alt="Country Almanac" />';
	aHighlights[16] = '<img src="/images/highlights/terry-at-harpo-studios.png" alt="Terry at Harpo Studios" /><p style="text-align:center">As Seen On The Oprah Winfrey Show March 10, 2008</p>';
	aHighlights[17] = '<img src="/images/highlights/cover-cottages-and-bungalows.png" alt="Cottages and Bungalows" />';
	aHighlights[18] = '<img src="/images/highlights/cover-paint-magic.png" alt="Paint Magic" />';
	
	if (hl_div) {
		hl_div.innerHTML = aHighlights[nCtr];
		nCtr = (nCtr < aHighlights.length - 1) ? nCtr + 1 : 0;
		
		window.setTimeout('rotateHighlights(' + nCtr + ')', 7000);
	}
}

function adjustSearchBox () {
	var aInputs, oSearchBox;
	
	if (document.getElementsByTagName) {
		aInputs = document.getElementsByTagName("input");
		
		for (var n = 0; n < aInputs.length; n++) {
			if (aInputs[n].id.indexOf("txtSearch") > 0) {
				oSearchBox = aInputs[n];
			}
		}

		if (oSearchBox) {
			if (window.addEventListener) {
				oSearchBox.addEventListener("focus", clearInput, false);
			} else {
				oSearchBox.attachEvent("onfocus", clearInput);
			}
		}
	}
}

function clearInput (e) {
	var oTarget = (e.target) ? e.target : event.srcElement;
	
	if (oTarget.value == "Search our shop") oTarget.value = "";
}

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 to your total .";
	} else {
		oLabel.innerHTML = "";	
	}
}

function disableImageClick(e) {
	var sMsg, dDate, nYear;
	var oSource;
	var aAllow = new Array("/links/", "page=94");

	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=640, height=480, scrollbars=yes, resizable=yes"); return false;};
			}
		}
	}
}

function popUp(sURL) {
	var sFeatures, sHTML;
	oImage = new Image();
	
	oImage.src = sURL;
	
	sFeatures = "width=320, height=240, status=no";
	
	sHTML = "<html><head><title>Product Viewer</title>";
	sHTML += "<meta http-equiv='imagetoolbar' content='no'>";
	sHTML += "<script type='text/javascript' src='include/scripts.js'></script>";
	sHTML += "</head><body style='margin:0;padding:0'>";
	sHTML += "<img src='" + sURL + "' alt='Product Image'>";
	sHTML += "</body></html>";

	oWin = window.open("", "", sFeatures);
	oWin.document.open();
	oWin.document.write(sHTML);
	oWin.document.close();
	
	if (oImage.complete) {
		oWin.resizeTo(oImage.width, oImage.height + 50);
	} else {
		oImage.onload = resizePopUp;
	}
}

function resizePopUp() {
	oWin.resizeTo(oImage.width, oImage.height + 50);
}

function openWindow(sUrl, nWidth, nHeight) {
	var sFeatures = "width=" + nWidth + ", height=" + nHeight;
	
	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);
}

