// JavaScript Document
if(ess) {
}else{
	var ess={};
}

/* generic custom popup window */
ess.showCustomPopUp = function(thisUrl,thisName,theseParams) {
	remote = open(thisUrl,thisName,theseParams);
}

/* email a friend popup window */
ess.emailAFriend = function(target) {
    remote = open(target, 'email_a_friend', 'width=430,height=626,top=0,left=0,resizable=no,scrollbars=yes');
}

/* DO NOT REMOVE: this function provides cart ship-to-store popup and other global page compatibility */
function showCustomPopUp(thisUrl,thisName,theseParams) {
    ess.showCustomPopUp(thisUrl,thisName,theseParams);
}

/*********Navigation Init*****************/
Event.observe(window,'load',function(e){
    new Df.Dropnav($('mainNav'));
});

/*********RecentlyView Product Slider Init*****************/
Event.observe(window,'load',function(e)
{
    // Fix the width of the slider element
    if(document.getElementById("rv-products")!=null && document.getElementById("rv-product-slider")!=null)
    {
        var rvProds = document.getElementById("rv-products");
        var rvProdListItems = rvProds.getElementsByTagName("li");
        var width = 0;
        for(var i = 0; i<rvProdListItems.length; i++) {
           width = width+rvProdListItems[i].offsetWidth;
        }
		var productSlider = document.getElementById("rv-product-slider");
		productSlider.style.width = ""+width+"px";
	
	
		var ins3 = new Df.Slider('rv-products')
		ins3.pars.animate = {
			time: 100,
			pause: 20
		}
		ins3.pars.iterateBy = 'li'
		ins3.set();
		
    }
    
    /* edit: add support for gracefully degrading focus/blur text and style for input boxes */
    //Only operates on inputs with title attribute; uses content of title attribute for "off" state text when empty.
	$$('input[type="text"][title]').each(function (elem) { 
		var orig = elem.getAttribute('title');
		Element.observe(elem, 'focus', function (e) {
			if (elem.value==orig) elem.value='';
			//Element.addClassName(elem, "inputFocused");
		});
		Element.observe(elem, 'blur', function (e) {
			if (elem.value=='') elem.value=orig;
			//Element.removeClassName(elem, "inputFocused");
		});
		elem.value=orig;
	});
});

/* the following replaces - $('rvLink').observe('click' - to provide ADA JavaScript disabled support */
function showRecentlyViewed()
{
    if ($('recentlyView') && $('rvLink') && $('recent-history'))
    {
        if ($('reviewStarsBlock')) Element.setStyle('reviewStarsBlock',{zIndex: -1});
        if (Prototype.Browser.ie6)
            $$('select').each(function (el) { el.style.visibility = 'hidden'; });
            $('rvLink').addClassName('rvLinkOn');
            $('recent-history').addClassName('recent-history-show');
    }
}

/* the following replaces - $('rvClose').observe('click' - to provide ADA JavaScript disabled support */
function hideRecentlyViewed()
{
    if ($('recentlyView') && $('rvLink') && $('recent-history'))
    {
        if ($('reviewStarsBlock')) Element.setStyle('reviewStarsBlock',{zIndex: ''});
        if (Prototype.Browser.ie6)
            $$('select').each(function (el) { el.style.visibility = 'visible'; });
            $('rvLink').removeClassName('rvLinkOn');
            $('recent-history').removeClassName('recent-history-show');
    }
}



function showCustomPopUp(thisUrl,thisName,theseParams)
{
	remote = open(thisUrl, thisName, theseParams);
}

/* common utility function: is provided value numeric */
function isNumeric(v) {
    var isNumeric = false;
    if (notEmpty(v)) {
        var vChars = '0123456789';
        isNumeric = true;
        var Char;
        for (var i = 0; i < v.length && isNumeric; i++) {
            Char = v.charAt(i);
            if (vChars.indexOf(Char) == -1) {
                isNumeric = false;
                break;
            }
        }
    }
    return isNumeric;
}

/* common utility function: is provided value not empty / not undefined */
function isEmpty(v) {
    return !notEmpty(v);
}

function notEmpty(v) {
    var notEmpty = notNull(v);
    if (notEmpty) {
        try {
            if (v.length == 0) notEmpty = false;
        }
        catch(e1) {
            try {
                if (v.size == 0) notEmpty = false;
            } catch(e2) {
            }
        }
    }
    return notEmpty;
}

/* common utility function: is provided value not undefined / not null */
function notNull(v) {
    return v != undefined && v != null;
}

function isIE()
{
    var ie = false;
    try {
        if (document.all && navigator.appVersion.toLowerCase().indexOf('msie ') != -1) ie = true;
    } catch(e) {
        /* swallow */
    }
    return ie;
}

function isIE6()
{
    var ie6 = false;
    try {
        if (document.all && navigator.appVersion.toLowerCase().indexOf('msie 6.') != -1) ie6 = true;
    } catch(e) {
        /* swallow */
    }
    return ie6;
}

function searchValidation(form) {
    if(!notEmpty(form.kw.value)) {
        alert("Enter a search keyword");
        return false;
    }
    return true;
}

/* returns the value of an xml node "nodeName" within "inText" */
function getXmlValue(inText, nodeName)
{
	var ret = '';
	var n = '<' + nodeName + '>';
	var n2 = '</' + nodeName + '>';
	var f = inText.indexOf(n);
	if (f > -1)
	{
		ret = inText.substring(f + n.length, inText.indexOf(n2));
	}
	return ret;
}

Event.observe(window,'load',function(){
	if($('product-search')){
		document.getElementById("product-search").onchange = function() {
		 // get the current URL
			var sParam = this.value;
			if(sParam!=""){
				var url = window.location.toString(); //get the parameters 
				url.match(/\?(.+)$/); 
				var base = url.substring(0,url.indexOf("?")+1);
				var params = RegExp.$1;
				// split up the query string and store in an 
				// associative array 
				var params = params.split("&"); 
				var queryStringList = {}; 
				for(var i=0;i<params.length;i++) { 
					var tmp = params[i].split("=");
					if(i!=0 && tmp[0]!="s") {
						base = base + "&";
						
					}
					if(tmp[0]!="s") {
						base = base + params[i];
					}
				}
				$('product-search').value = $('product-search').selectedIndex;
				
				base = base + "&s=" + sParam;
				window.location.href = base;
			}
		}
	}
},false);

Element.setAttributes = function (elem, atts) {
	for (var name in atts)
		elem.setAttribute(name, atts[name]);
	return elem;
}
//Check if sIFR is needed, and load sifr.js if so.
Event.observe(document, 'dom:loaded', function () {
	$(document.body).observe("click", function(ee) {
		Df.Anchor.rewriteHandler(ee);
	})
	var 	jsDir = 'js/',
		cssDir = 'css/',
		akurl = '/',
		docBody = document.body,
		i=0,
		scripts = $$('script'),
		sel, tmp,
		load=[];
	
	//Find the Akamai URL by locating the Prototype script
	
	for (i=0; i<scripts.length; i++)
		if (/prototype/i.test(scripts[i].src))
			akurl = scripts[i].src.toString().replace(/^(https?:\/\/[^\/]+\/|\/).*$/,'$1');
	for (sel in sIFRStyled)
		if ($$(sel).length>0) {
			tmp = Object.extend(sIFRDefaults, Object.extend({sSelector:sel},sIFRStyled[sel]));
			tmp.sFlashSrc = akurl+tmp.sFlashSrc;
			if (typeof tmp.sFlashVars != 'string' && typeof tmp.sFlashVars != 'undefined') {
				var tmp1 = [];
				for (i in tmp.sFlashVars)
					tmp1.push(escape(i)+'='+escape(tmp.sFlashVars[i]));
				tmp.sFlashVars = tmp1.join('&');
			}
			load.push(tmp);
		}
	if (load.length>0) {
		//Add the sIFR JS
		/*
		docBody.appendChild(tmp=Element.setAttributes(
			document.createElement('script'), {
				type: 'text/javascript',
				src: akurl+jsDir+'sifr.js'
			}
		));*/
		var start = (new Date()).getTime();
		//sleep momentarily to let the script load, then run the replace
		setTimeout(function () {
			/*console.log(((new Date()).getTime() - start)/1000);
			if (((new Date()).getTime() - start)/1000 > sIFRTimeLimit) return;*/
			//If sIFR isn't here, sleep another 50ms
			if (!window.sIFR || !window.sIFR.replaceElement)
					return setTimeout(arguments.callee, 50);
			//Add the sIFR CSS
			/*
			docBody.appendChild(Element.setAttributes(
				document.createElement('link'), {
					rel: 'stylesheet',
					type: 'text/css',
					href: akurl+cssDir+'sifr.css'
				}
			));*/
			setTimeout(function () {
				//Run the replace for extant elements only
				for(i=0; i<load.length; i++) try {
					sIFR.replaceElement(named(load[i]));
				} catch (e) {
					alert(e.message);
				}
			},50);
		}, 50);
	}
});

/* SIFR_HEAD */
/* sIFR headline configuration */
var	sIFRDefaults = { //applied to ALL sIFR elements
		sWmode: 'transparent',
		nPaddingTop: 0,
		nPaddingBottom: 0,
		//URLs are relative to CLIENT-SIDE SITE ROOT, which is auto-detected
		sFlashSrc: 'js/ultramagnetic.swf'
	},
	sIFRStyled = { //per-selector configuration
		"#product-detail h1": {
			sColor:"#5381AC", 
			sBgColor:"#FFFFFF", 
			
			sFlashVars:{
				textalign: 'left',
				offsetTop: 0
			},
			css: [
				'.sIFR-root { font-size: 24px; }'
			]
		}
	},
	sIFRTimeLimit = 5.0;
	