/*
 * Rexcraft Common Javascript
 * dsbeumer@jamestower.com - 06/01/07
 *
 * [Revision History]
 * 06/01/07 - dsbeumer - Created.
 * 06/06/07 - dsbeumer - Added IE hover fix code & objectified it.
 * 06/12/07 - dsbeumer - Added the ability to override the default popup parameters for popMe links.
 * 06/13/07 - dsbeumer - Added homepage newsletter form submit popup overrides.
 * 				Added compatibility hover-text-underlining for e-mail a friend submit button.
 * 06/15/07 - dsbeumer - Added shipping charges popup parameters.
 * 				Added thank you page print-friendly popup parameters.
 * 06/25/07 - dsbeumer - Added mythsFacts popup properties.
 * 06/26/07 - dsbeumer - Changed popMe replacement href value from '#' to '#nogo'.
 * 06/28/07 - dsbeumer - Added swapMe & initMe areas, and tweaked the popMe code.
 * 06/29/07 - dsbeumer - Removed swapMe onmouseout method so that images will stick once swapped.
 * 				Changed > in popMe init for loop to < as it should be.
 * 07/03/07 - dsbeumer - Added support for swapping link titles & hrefs to swapMe routine.
 * 07/10/08 - asbaumgart - Added link attributes for monogram and design popups
 * 01/30/09 - lbmanthei - 7074 - Made the email signup popup longer to accomodate new partner checkbox.
 * 02/19/09 - lbmanthei - 7119 - Added code do the email signup popup just like the home page signup form is done if the coming soon sale banner is clicked (frmSaleSignup).
 * 03/26/09 - lbmanthei - 7397 - Made the email signup popup longer to accomodate new rexcraft & image gallery checkboxes.
 */

// initMe Window Onload Handler

// Add initMe to the window scope itself for ease of access
window.initMe = function (fnc) {
	if (typeof fnc == 'function') {
		// Optimally use addEventListener, which does our job for us but isn't universally supported
		if (window.addEventListener) {
			window.addEventListener('load', fnc, false);
		// Attempt to use the IE-proprietary attachEvent method alternately
		} else if (window.attachEvent) {
			window.attachEvent('onload', fnc);
		// Finally, use the standard window.onload with some hooking to preserve any previously assigned functionality
		} else {
			if (window.onload != null) {
				var onloadHook = window.onload;
				window.onload = function (e) {
					onloadHook(e);
					fnc();
				};
			} else {
				window.onload = fnc;
			}
		}
	}
};

// Rexcraft-specific namespace
var rr = {
	isEmail: function (strValue) {
		// Short JavaScript regexp syntax, negates need for escaping. Equivalent to:
		// new RegExp("^[-!#\\$%\\*\\+\\/\\?\\ ... etc");
		return /^[-!#\$%\*\+\/\?\|\^&{}`~\w]+(\.[-!#\$%\*\+\/\?\|\^&{}`~\w]+)*@[-\w]+(\.[-\w]+)+$/.test(strValue);
	},

	/* IE Hover Fix */
	ieHoverFix: {
		setHover: function (nav) {
			var ieULs = nav.getElementsByTagName('ul');
			var ieLIs = null;
			if (navigator.appVersion.substr(22,3)!="5.0") {
				var ieMat = null;
				// IE script to cover <select> elements with <iframe>s
				for (j = 0; j < ieULs.length; j++) {
					ieMat=document.createElement('iframe');
					ieMat.src="javascript:false";
					ieMat.scrolling="no";
					ieMat.frameBorder="0";
					ieMat.style.width=ieULs[j].offsetWidth+"px";
					ieMat.style.height=ieULs[j].offsetHeight+"px";
					ieMat.style.zIndex="0";
					ieULs[j].insertBefore(ieMat, ieULs[j].childNodes[0]);
					ieULs[j].style.zIndex="101";
				}
				// IE script to change class on mouseover
				ieLIs = nav.getElementsByTagName('li');
				for (var i = 0; i <ieLIs.length; i++) {
					if (ieLIs[i]) {
						// Does this LI have children? If so add a sfnode class.
						if (ieLIs[i].getElementsByTagName("UL").length>0) {
							ieLIs[i].className+=" sfnode";
						}

						// Add a sfhover class to the li.
						ieLIs[i].onmouseover = function() {this.className+=" sfhover";};
						ieLIs[i].onmouseout = function() {this.className=this.className.replace(' sfhover', '');};
					}
				}
			}
			else
			{
				// IE 5.0 doesn't support iframes so hide the select statements on hover and show on mouse out.
				// IE script to change class on mouseover
				ieLIs = document.getElementById('topnav').getElementsByTagName('li');
				for (i = 0; i < ieLIs.length; i++) {
					if (ieLIs[i]) {
						ieLIs[i].onmouseover = function() {this.className += " sfhover"; rr.ieHoverFix.hideSelects();};
						ieLIs[i].onmouseout = function() {this.className = this.className.replace(' sfhover', ''); rr.ieHoverFix.showSelects();};
					}
				}
			}
		},
		hideSelects: function () {
			var oSelects = document.getElementsByTagName('select');
			for(var i = 0; i < oSelects.length; i++)
				oSelects[i].className += ' hide';
		},
		showSelects: function () {
			var oSelects = document.getElementsByTagName('select');
			for(var i = 0; i < oSelects.length; i++)
				oSelects[i].className = oSelects[i].className.replace(' hide','');
		}
	},

	/* Initialization */
	init: function () {

		// popMe Popup Links

		var strPopupArgs = 'toolbar=no,location=no,directories=no,menubar=no,status=no,scrollbars=no,resizable=no,replace=no';

		// Target-specific arguments which override the default (keys = target property values)
		var arrOverrideArgs = {
			liveHelp: 'width=410,height=300,' + strPopupArgs,
			freeCatalog: 'width=600,height=500,resizable=1,scrollbars=1,top=30,left=50,location=no,directories=no,toolbar=no,menubar=no',
			invInkColors: 'width=370,height=350,resizable=1,scrollbars=1,top=30,left=50,location=no,directories=no,toolbar=no,menubar=no',
			invEnvLiners: 'width=370,height=360,resizable=1,scrollbars=1,top=30,left=50,location=no,directories=no,toolbar=no,menubar=no',
			wedVerses: 'width=430,height=560,resizable=1,scrollbars=1,top=30,left=50,location=no,directories=no,toolbar=no,menubar=no',
			invTypestyles: 'width=420,height=500,resizable=1,scrollbars=1,top=30,left=50,location=no,directories=no,toolbar=no,menubar=no',
			customDesigns: 'width=400,height=560,resizable=1,scrollbars=1,top=30,left=50,location=no,directories=no,toolbar=no,menubar=no',
			customMonograms: 'width=450,height=560,resizable=1,scrollbars=1,top=30,left=50,location=no,directories=no,toolbar=no,menubar=no',
			shippingCharges: 'width=550,height=400,resizable=0,scrollbars=0,top=30,left=50,location=no,directories=no,toolbar=no,menubar=no',
			thankYouPrintable: 'width=650,height=500,toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes',
			mythsFacts: 'width=400,height=340,resizable=1,scrollbars=1'
		};
		var arrAnchorTags = document.getElementsByTagName('a');
		var objAnchor = null;

		// Loop through anchors for any with the popMe class and compatible href property
		for (var i = 0; i < arrAnchorTags.length; i++) {
			objAnchor = arrAnchorTags[i];

			if (objAnchor.className && objAnchor.className.indexOf('popMe') >= 0 && objAnchor.href !== '' && objAnchor.href.charAt(0) != '#') {
				// Get the link destination to use for the popup
				objAnchor.popMeUrl = objAnchor.href;
				// Reset the link href so that it doesn't go anywhere
				objAnchor.href = '#_';

				// If a target was supplied, take that value as the popup window name
				if (objAnchor.target && objAnchor.target !== '') {
					objAnchor.popMeName = objAnchor.target;
					objAnchor.target = '';

					// If the name was found in our overrides array, use the supplied arguments
					if (arrOverrideArgs[objAnchor.popMeName]) {
						objAnchor.popMeArgs = arrOverrideArgs[objAnchor.popMeName];
					} else {
						objAnchor.popMeArgs = strPopupArgs;
					}
				} else {
					objAnchor.popMeName = '_blank';
					objAnchor.popMeArgs = strPopupArgs;
				}

				objAnchor.onclick = function () {
					window.open(this.popMeUrl, this.popMeName, this.popMeArgs);
				};
			}
		}

		// Search Box

		var objSearchTextInput = document.frmSearch.strSearchDesc;
		objSearchTextInput.strDefaultText = objSearchTextInput.value;
		objSearchTextInput.strDefaultColor = '#888';
		objSearchTextInput.strNormalColor = (objSearchTextInput.style.color !== '') ? objSearchTextInput.style.color : '#000';

		objSearchTextInput.style.color = objSearchTextInput.strDefaultColor;

		objSearchTextInput.onfocus = function () {
			if (this.value == this.strDefaultText) {
				this.value = '';
				this.style.color = this.strNormalColor;
			}
		};
		objSearchTextInput.onblur = function () {
			if (this.value === '') {
				this.value = this.strDefaultText;
				this.style.color = this.strDefaultColor;
			}
		};

		// Homepage Signup Form
		if (document.frmSignup && document.frmSignup.strEmail) {
			var objSignupEmailInput = document.frmSignup.strEmail;
			objSignupEmailInput.strDefaultText = objSignupEmailInput.value;
			objSignupEmailInput.strDefaultColor = '#666';
			objSignupEmailInput.strNormalColor = (objSearchTextInput.style.color !== '') ? objSearchTextInput.style.color : '#000';

			objSignupEmailInput.style.color = objSignupEmailInput.strDefaultColor;

			objSignupEmailInput.onfocus = function () {
				if (this.value == this.strDefaultText) {
					this.value = '';
					this.style.color = this.strNormalColor;
				}
			};
			objSignupEmailInput.onblur = function () {
				if (this.value === '') {
					this.value = this.strDefaultText;
					this.style.color = this.strDefaultColor;
				}
			};

			document.frmSignup.onsubmit = function () {
				if (rr.isEmail(document.frmSignup.strEmail.value)) {
					winNewsletter = window.open(document.frmSignup.action, 'NewsletterSignup', 'height=450,width=300,top=30,left=50,location=no,directories=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,menubar=no');
					winNewsletter.strEmail = document.frmSignup.strEmail.value;
				}
				else {
					alert('Please enter a valid e-mail address.')
				}
				return false;
			};
		}

		// Homepage sale signup form
		if (document.frmSaleSignup) {
			document.frmSaleSignup.onsubmit = function () {
				winNewsletter = window.open(document.frmSaleSignup.action, 'NewsletterSignup', 'height=400,width=300,top=30,left=50,location=no,directories=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,menubar=no');
				return false;
			};
		}

		if (document.frmEmailFriend && document.frmEmailFriend.btnSubmit) {
			var objEmailFriendSubmit = document.frmEmailFriend.btnSubmit;

			objEmailFriendSubmit.onmouseover = function () {
				this.style.textDecoration = 'underline';
			};
			objEmailFriendSubmit.onmouseout = function () {
				this.style.textDecoration = 'none';
			};
		}

		// IE Hover Fix

		if (navigator.appVersion.indexOf('MSIE 6') >= 0) {
			// Find all unordered lists.
			var ieNavs = document.getElementsByTagName('ul');
			for (i = 0; i < ieNavs.length; i++) {
				var ul = ieNavs[i];
				// If they have a class of nav add the menu hover.
				if (ul.className == 'nav')
					rr.ieHoverFix.setHover(ul);
			}
		}

		// swapMe Image Swapper

		var objSwapMeTarget = document.getElementById('swapMeTarget');
		var objSwapMeLinkTarget = null;

		// Requires that a target image be setup properly
		if (objSwapMeTarget) {
			if (objSwapMeTarget.parentNode.tagName.toLowerCase() == 'a') {
				objSwapMeLinkTarget = objSwapMeTarget.parentNode;
			}

			arrAnchorTags = document.getElementsByTagName('a');
			objAnchor = null;

			// Loop through anchors for any with the swapMe class and required id
			for (var i = 0; i < arrAnchorTags.length; i++) {
				objAnchor = arrAnchorTags[i];

				if (objAnchor.className && objAnchor.className.indexOf('swapMe') >= 0 && objAnchor.id !== '') {
					// Create new image with path pulled from the anchor's ID property
					objAnchor.swapMeImg = new Image();
					objAnchor.swapMeImg.src = objAnchor.id;

					// Apply mouse events
					objAnchor.onmouseover = function () {
						objSwapMeTarget.src = this.swapMeImg.src;
						objSwapMeTarget.alt = this.title;

						if (objSwapMeLinkTarget != null) {
							objSwapMeLinkTarget.title = this.title;
							objSwapMeLinkTarget.href = this.href;
						}
					};
				}
			}
		}
	}
};

window.initMe(rr.init);