// JavaScript Document
if(!window.ess) window.ess={};

ess.showCustomPopUp = window.showCustomPopUp = function showCustomPopup(thisUrl,thisName,theseParams) {
	if (thisName == '') thisName='popup';
	try { remote = window.open(thisUrl, thisName, theseParams); } 
	catch (e) { /* Eat it */ }
}

Element.addMethods({
	fireNativeEvent: function fireNativeEvent(element, eventName) {
		var e;
		if (document.createEventObject)
			return element.fireEvent('on'+eventName,document.createEventObject())
		return !element.dispatchEvent((e = document.createEvent("HTMLEvents")).initEvent(eventName, true, true) || e);
	}
});

Event.observe(window, 'load', function(e){
	if (!$('nav1')) return;
	new Df.Dropnav($('nav1'), {
		animate: {
			time: 200,
			opacity: 1,
			height:function(obj){
				return obj.getElement().scrollHeight +7;
			}	
		}	
	});
	$$('#nav1 ul.cHeaderNavUl').each(function(v) {
		v.setStyle({height:"0px",opacity:0}); 
	});

	//Rotating header image
	if (document.body.className != 'home') {
		var 
			how_many_items = 12,
			item = (new Date()).getSeconds() % how_many_items + 1;
		$$('#brand').each(function (brand) { 
			brand.src='../images/header' + item + '.jpg' 
		});
	}
});
Event.observe(window, 'load', function(e){
	$$('.slider').each(function(v) {
		v.calculatedWidth = 0;
		v.select('li').each(function(vv) {
			v.calculatedWidth += vv.offsetWidth;
		});
		v.style.width = ""+v.calculatedWidth+"px";
	});	
});
Event.observe(window, 'load', function() {
	if ($$('body.cart, body.checkout, body.help').length>0) return;
	if (!Df.Combo) {
		if ($$('script[src$="Df.Combo.js"]').length==0)
			$$('head').pop().appendChild(new Element('script', {src: '/js/df/Df.Combo.js', type:'text/javascript'}));
		return setTimeout(arguments.callee, 10);
	}
	$$('select').each(function (el) {
		var 
			data = [],
			combo = new Element('div'),
			width = Math.round(el.offsetWidth*1.25);
		el.select('option').each(function (opt) { 
			data.push({label: opt.innerHTML, value: opt.value}); 
		});
		combo.addClassName('combo-box');
		el.parentNode.insertBefore(combo, el);
		el.hide();
		combo.setStyle({width: width+"px"});
		var dfCombo = new Df.Combo(combo, {
			data: data,
			animate: {
				ease: Df.Transitions.quadInOut,
				time: 400
			},
			listWidth: width,
			leftEdgeClassName: 'combo-left',
			listClassName: 'combo-list'
		});
		dfCombo.setCurrentValue(el.options[el.selectedIndex].value);
		combo.observe(':update', function () {
			//Keep the select box in synch
			var check = dfCombo.getCurrentValue();
			el.value = check;
			Element.fireNativeEvent(el, 'change');
		});
	});
});

Event.observe(window, 'load',function(e){
	$$('.slider').each(function(v) {
		v.calculatedWidth = 0;
		// Fix the width of the slider element
		v.select('li').each(function(vv) {
			v.calculatedWidth += vv.offsetWidth;
		});
		v.style.width = ""+v.calculatedWidth+"px";
		var sliderEl = v.up('.product-slider');
		(function () {
			if (!Df.Slider) {
				$$('head').pop().appendChild(new Element('script', {type: 'text/javascript', src:Df.classPath+'Df.Slider.js'}));
				return setTimeout(arguments.callee, 10);
			}
			var sliderIns = new Df.Slider(sliderEl);
			sliderIns.pars.animate = {time:100,pause:20};
			sliderIns.pars.iterateBy = "li";
			sliderIns.set();
		})();
	});
});
Event.observe(document, 'dom:loaded', function (e) {
	$$('#mainContent .wrap table table tr:nth-child(1)').invoke('addClassName','order_status_header');
	$$('#mainContent .wrap table table tr:nth-child(2n+2)').invoke('addClassName','order_status_odd order_status_row');
	$$('#mainContent .wrap table table tr:nth-child(2n+3)').invoke('addClassName','order_status_even order_status_row');
	
	//Duplicating functionality of ETYOS PS 93952 
	$$('form#search, form#body-search').invoke('observe', 'submit', function (e) {		
		this.select('input#search-box, input#body-search-box').each(function (input) {
			input.value = input.value
				.replace(/^ +| +$/g,'')
				.replace(/[^\w\d-]+/g, ' ')
				.replace(/\s+[^\w\d]+\s+/g, ' ');
		});
	});
});
