﻿String.prototype.bool = function () { return (/^true$/i).test(this); }; 

function browser_IsIE() {
	return (jQuery.browser.msie);
}
function opacityRemove(oObjRemoveFilter) {
	if (oObjRemoveFilter && oObjRemoveFilter.length) {
		oObjRemoveFilter.css({ opacity: 1 });
		if (browser_IsIE()) {
			oObjRemoveFilter.get(0).style.removeAttribute('filter');
		}
	}
}

function getNegativeMargin(iHeight) {
	return (0 - iHeight - 1)
}

//PNG
function initPngSwitch(sPathToRoot) {
	if (browser_IsIE() && jQuery.browser.version <= 6) {
//		var olnkPng = jQuery(document.createElement('link'));
//		var sPath = sPathToRoot + 'ui/css/png.css';
//		olnkPng.attr({ href: sPath, type: 'text/css', rel: 'stylesheet' });
//		jQuery('head').append(olnkPng);
		jQuery('body').addClass('require_png');

		//inline images
		jQuery.each(jQuery(document).find('img.png'), function (indexPng, valuePng) {
			jQuery(this).attr("src", jQuery(this).attr("src").replace(/\.png$/i, ".gif"));
		});
	}
}
//End PNG

//Clean Root
function setPathToRoot(sInput, sPathToRoot) {
	//%%PATH_TO_ROOT%%
	return sInput.replace(/\%\%PATH\_TO\_ROOT\%\%/gi, sPathToRoot);
}

// Left Callout fine tuning
function setLeftCallouts(sLeftCallContainerId, sPathToRoot) {
	var oContainer = jQuery('#' + sLeftCallContainerId);
	if (oContainer) {
		//Order by Z-Index [bubble]
//		jQuery.each(oContainer.find('tout'), function () {
//			var oCurr = jQuery(this);
//			var oPrev = oCurr.prev();
//			var iCurrZindex = parseInt(oCurr.css('z-index'));
//			var iPrevZindex = parseInt(oPrev.css('z-index'));

//			while (iPrevZindex > iCurrZindex) {
//				oCurr.css('z-index') = iPrevZindex;
//				oPrev.css('z-index') = iCurrZindex;

//				var iSwap = iPrevZindex;
//				iPrevZindex = iCurrZindex;
//				iCurrZindex = iSwap;

//				//Switch Html
//			}

//			if (iCurrZindex > 0 && iPrevZindex > 0) {
//				if (iCurrIndex < iPrevZindex) {

//				}
//			}
		//		});
		//Order by reassembly
		var oNewContainer = jQuery(document.createElement('div'));
		var iNumItems = oContainer.children().length;
		var iCount = 0;
		var iCurrIndex = 0;
		var iMaxIndex = 30;
		while (iCount < iNumItems && iCurrIndex < iMaxIndex) {
			var bFound = false;
			jQuery.each(oContainer.children('.tout'), function () {
				var iFoundIndex = parseInt(jQuery(this).css('z-index'));

				iFoundIndex = (!iFoundIndex) ? 0 : iFoundIndex;
				//alert(iFoundIndex + ' ' + iCurrIndex + ' ' + iCount);
				if (iFoundIndex >= 0 && !bFound) {

					if (iFoundIndex <= iCurrIndex) {
						if (jQuery(this).css('display') != 'none') {
							//If First set green
							if (iCount == 0) {
								jQuery(this).css('background-image', 'url(' + sPathToRoot + 'ui/image/leftcall_green_top.gif)');
							}
							else {
								jQuery(this).css('background-image', 'none');
							}
							oNewContainer.append(jQuery(this));
							bFound = true;
						}
					}
					else {
						if (iFoundIndex > iMaxIndex)
							iMaxIndex = iFoundIndex;
					}
				}
			});
			if (bFound) {
				iCount++;
			}
			else {
				iCurrIndex++;
			}
		}

		//Last rounded bottom if not smiley
		var oLastItem = oNewContainer.find('.tout:last-child');
		var bCommit = false;
		while (!bCommit && oLastItem != null) {
			if (oLastItem.css('display') == 'none') {
				oLastItem = oLastItem.prev();
			}
			else {
				bCommit = true;
//				if (!oLastItem.hasClass('smiley_bottom')) {
//					var oBottomContainer = document.createElement('div');
//					var oBottomImage = jQuery(document.createElement('img'));
//					oBottomImage.attr('src', sPathToRoot + 'ui/image/leftcall_bottom.gif').attr('alt', '');
//					oLastItem.append(jQuery(oBottomContainer).append(oBottomImage));
//				}
//				else {
//					
//				}
				var oBottomContainer = jQuery(document.createElement('div'));
				oBottomContainer.addClass('img_bottom');
				//var oBottomImage = jQuery(document.createElement('img'));
				//oBottomImage.addClass('img_bottom');
				//oBottomImage.attr('src', sPathToRoot + 'ui/image/leftcall_bottom.gif').attr('alt', '');
				oLastItem.append(oBottomContainer);//.append(oBottomImage));
			}
		}

		//Add
		oContainer.replaceWith(oNewContainer);
	}
}
// End Left Callouts

// Home Navigator items
function initHomeNavigatorButtons() {
	var oContainer = jQuery('#home_navigator');
	if (oContainer) {
		jQuery.each(oContainer.find('.tout'), function (indexTout, valueTout) {
			var sHoverClass = 'hover' + ((jQuery(this).hasClass('tout_long')) ? ' hover_long' : '');
			jQuery(this).hover(function () {
				jQuery(this).addClass(sHoverClass);
			}, function () {
				jQuery(this).removeClass(sHoverClass);
			});
		});
	}
}
// End Home Navigator items

//Smart Tips
function initSmartTips() {
	jQuery.each(jQuery(document).find('div.smart_tip'), function () {
		var oTip = jQuery(this);
		oTip.find('img.icon').click(function () {
			oTip.find('.smart_tip_hover').css({ display: 'block' });
		});

		oTip.find('.smart_tip_hover').click(function () {
			jQuery(this).css({ display: 'none' });
		});
	});
}
//End Smart Tips

//SmartStopHandlers
function processHandlerReturnXml(xml, fnOnSuccess, fnOnError) {
	var oRespXml = jQuery(xml).find('Response');
	if (oRespXml.attr('Success').bool()) {
		fnOnSuccess(oRespXml.find('ResponseXml'));
	}
	else {
		if (fnOnError) {
			fnOnError(oRespXml.find('Message').text());
		}
		else {
			//Default Error
			//alert(oRespXml.find('Message').text());
			alert('An unexpected error occurred, and we were unable to process your request. Our staff has been notified of the problem.');
		}
	}
	remove_Processing_Overlay();
}
//End Handlers

//Cart SubTotal
function registerCartSubTotal(sContainerIdentifier) {
	jQuery(document).bind('update_cart_subtotal', function (event, fNewSubTotal) {
		jQuery(sContainerIdentifier).text(fNewSubTotal);
	});
}
//End Cart SubTotal

//Login Status
function registerLoginChanged(sContainerIdentifier) {
	jQuery(document).bind('update_login_status', function (event, bNewStatus) {
		jQuery(sContainerIdentifier).css({ visibility: (bNewStatus) ? 'hidden' : 'visible' });
	});
}
//End Login Status

//Site Map
function init_SiteMap() {
	var oContainer = jQuery('#footer .tools .sitemap .smap_container').css({ display: 'none' });
	var oLink = jQuery('#footer .tools .sitemap p.link a');

	oLink.hover(function () {
			if (oContainer.css("display") == "none") {
				oContainer.css({ display: 'block' });
			}
			
		});
	oContainer.mouseleave(function () {
		if (oContainer.css("display") == "block") {
			oContainer.css({ display: 'none' });
		}
	});
	oContainer.find('div.close a').click(function () {
		oContainer.css({ display: 'none' });
	});
}
//End Site Map

//Langs
function init_Langs() {
	jQuery('#lang_selector_lnk').click(function () {
		var oLangSelector = jQuery('#lang_container');
		if (jQuery(this).hasClass('open')) {
			jQuery(this).removeClass('open');
			oLangSelector.css({ display: 'none' });
		}
		else {
			jQuery(this).addClass('open');
			oLangSelector.css({ display: 'block' });
		}
	});
}
//End Langs

//Search Input
function register_SearchForm(sButtonId, sInputId, sDefaultText, sValidatorId, sPathToSearchUrl, sTypeId) {
	jQuery('#' + sButtonId).click(function () {

		var sValue = jQuery('#' + sInputId).val();
		var bIsValid = (sValue && sValue.length > 0 && sValue.length != sDefaultText);
		var oValidator = jQuery('#' + sValidatorId);

		if (bIsValid) {
			oValidator.css({ display: 'none' });
			//Get Redirect URL
			jQuery.ajax({
				type: "GET",
				url: sPathToSearchUrl,
				data: { srch: sValue, type: jQuery('#' + sTypeId).val() },
				dataType: "text",
				error: function (jqXHR, textStatus, errorThrown) {
					alert('ajax returned error. ' + errorThrown);
				},
				success: function (sResponseText) {
					window.location.href = sResponseText;
				}
			});
		}
		else {
			//Trigger validation
			oValidator.css({ display: 'block' });
		}
	});
}
//End Serach Input
