//function $(element) {
//	if (typeof element == 'string' && element.length > 0) {
//		element = document.getElementById(element);
//	}
//	return element;
//}
function $t(str) {
	return document.createTextNode(str);
}

window.addEvent('domready', function() {
	new SelectLink();
	
	$$('#productParade li:nth-child(3n)').each(function(e) {
		e.addClass('noMargin');
	});
});

document.write('<style type="text/css">');
document.write(".submit { position: absolute; left: -3000px; top: -3000px; }");
document.write('</style>');

function submit_cart() {
	var quantity = $('quantity');
	var sizes = $('sizes');

	if ((typeof(variations)).toLowerCase() != 'object' || !sizes || !quantity) {
		alert('There has been an error, please refersh the page');
		return;
	}

	if (sizes.value == 0) {
		alert('Please select a size and quantity');
	}
	else if (sizes.value > 0 && quantity.value == 0) {
		alert('Please select a quantity');
	}
	else {
		document.forms['cart_quantity'].submit();
	}
}

function optionsChange() {
	var quantity = $('quantity');
	var sizes = $('sizes');
	if ((typeof(variations)).toLowerCase() != 'object' || !sizes || !quantity) {
		alert('There has been an error, please refersh the page');
		return;
	}

	while(quantity.childNodes.length > 0) {
		quantity.removeChild(quantity.childNodes[0]);
	}
	if (sizes.value == 0) {
		var option = document.createElement('option');
		option.value = '0';
		option.appendChild($t('Select a size')); 
		quantity.appendChild(option);
		return;
	}

	var quantities = new Array();
	/*
	quantities[quantities.length] = {
		value: 0,
		text: 1
	}
	*/
	for (var i = 0; i < variations.length; i++) {
		var variation = variations[i];
		if (variation.attributes[0].val == sizes.value) {
			for (var j = 1; j <= variation.quantity && j < 5; j++) {
				quantities[quantities.length] = {
					value: j,
					text: j
				}
			}
		}
		/*
		for (var j = 0; j < variation.attributes.length; j++) { 
		}
		*/
	}
	if (quantities.length == 0) {
		quantities[quantities.length] = {
			value: 0,
			text: 'Out of stock'
		}
	}

	for (i = 0; i < quantities.length; i++) {
		var option = document.createElement('option');
		option.value = quantities[i].value;
		option.appendChild($t(quantities[i].text)); 
		quantity.appendChild(option);
	}
}

Popup = {
	container: 'detailcontainer',
	created: false,
	detailimg: 'detailimg',
	xml: '',
	create: function () {

		var sizes = Popup.getSizes();
		/*
		overlay.style.height = sizes.page.height + 'px';
		if (/msie/gi.test(navigator.userAgent.toLowerCase())) {
			overlay.style.width = sizes.page.width + 'px';
		}
		var outer = document.createElement('div');
		outer.setAttribute('id', Popup.outerCont);
		outer.onclick = function (event) {
			Popup.hide();
		}
		var inner = document.createElement('div');
		inner.setAttribute('id', Popup.innerCont);
		inner.onclick = function (event) {
			Popup.cancelBubble(event);
		}
		*/

		Popup.created = true;
	},
	load:	function (href) {
		if (Popup.created) {
			Popup.show(Popup.xml);
			return;
		}
		var con = Popup.xhcon();
		if (!con) {
			return;
		}
		con.connect(href, 'GET', Math.random(), Popup.show);
	},
	showImage: function(src) {
		Popup.detailimg = $('detailimg');
		setTimeout('eval(\'Popup.detailimg.src = "' + src + '"\');', 1);
		//$('detailimg').src = src;
		$('detailimg').style.display = '';
		$('detailsizeguide').style.display = 'none';
	},
	showSizeGuide: function () {
		$('detailimg').style.display = 'none';
		$('detailsizeguide').style.display = 'block';
	},
	show: function (oXML) {
		Popup.xml = oXML;
		var str = oXML.responseText;
		if (/^\s$/.test(str) || str == '') {
			alert('Page not found');
			return;
		}

		$(Popup.container).style.display = 'none';
		$(Popup.container).innerHTML = str;

		var overlay = $('detailoverlay');
		if (overlay) {
			var sizes = Popup.getSizes();
			overlay.style.height = sizes.page.height + 'px';
			if (/msie/gi.test(navigator.userAgent.toLowerCase())) {
				overlay.style.filter = 'alpha(opacity=50)';
				overlay.style.width = sizes.page.width + 'px';
			}
			overlay.onclick = function (event) {
				Popup.hide();
			}
		}

		$(Popup.container).style.display = 'block';
		$('productInfo').style.visibility = 'hidden';
	},
	hide: function () {
		var container = $(Popup.container);
		$('productInfo').style.visibility = 'visible';
		container.style.display = 'none';
		// remove all children
		while (container.childNodes.length > 0) {
			container.removeChild(container.childNodes[0]);
		}
	},
	getSizes: function () {
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		}
		else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		}
		else {
			pageWidth = xScroll;
		}
	
	
		return { page: {width: pageWidth, height: pageHeight}, window: {width: windowWidth, height: windowHeight }};
	},
	cancelBubble:	function (eObj) {
		if (!eObj) var eObj = window.event;
		if (!eObj) return;
		eObj.cancelBubble = true;
		if (eObj.stopPropagation) {
			eObj.stopPropagation();
		}
	},
	xhcon:		function () {
		var xmlhttp, bComplete = false;
		try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch (e) { try { xmlhttp = new XMLHttpRequest(); }
		catch (e) { xmlhttp = false; }}}
		if (!xmlhttp) {
			return null;
		}
		this.connect = function(sURL, sMethod, sVars, fnDone) {
			if (!xmlhttp) {
				return false;
			}
			bComplete = false;
			sMethod = sMethod.toUpperCase();
	
			try {
				if (sMethod == "GET") {
					xmlhttp.open(sMethod, sURL+"?"+sVars, true);
					sVars = "";
				}
				else {
					xmlhttp.open(sMethod, sURL, true);
					xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
					xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				}
				xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && !bComplete) {
						bComplete = true;
						fnDone(xmlhttp);
					}
				};
				xmlhttp.send(sVars);
			}
			catch(z) { return false; }
			return true;
		};
		return this;
	}
}

function showTerms(n) {
	var pages = [1, 2, 3, 4, 5];
	var e = $('page' + n);
	if (e) {
		for (var i = 0; i < pages.length; i++) {
			if ($('page' + pages[i])) {
				$('page' + pages[i]).style.display = 'none';
				$('pageLink' + pages[i]).className = '';
			}
		}
		e.style.display = 'block';
		$('pageLink' + n).className = 'on';
	}
}


//go to the url given by a select input when changing value
var SelectLink = new Class({
	Implements: Options,
	options: {
		className: 'goto',
		saveLinkClass: 'saveLink'
	},
	initialize: function(options) {
		this.setOptions(options);
		$$('select.' + this.options.className).each(function(e) {
			this.setAction(e);
			var selectedValue = this.getSelectedValue(e.get('name'));
			if (selectedValue) {
//				if (IS_INDEX && selectedValue.indexOf('html/') == -1) {
//					selectedValue = 'html/'. selectedValue;
//				}
//				if (!IS_INDEX) {
//					selectedValue = selectedValue.replace(/^html\//, '');
//				}
				e.set('value', selectedValue);
			}
		}, this);
	},
	setAction: function(e) {
		e = $(e);
		if (!e) return;
		
		e.addEvent('change', function(evt) {
			this.handleChange(e);
		}.bindWithEvent(this, e));
	},
	handleChange: function(e) {
		e = $(e);
		if (!e) return;
		
		var value = e.get('value');
		if (e.hasClass(this.options.saveLinkClass)) {
			this.save(e.get('name'), value);
		}
		if (value) {
			e.setStyle('opacity', 0.3);
			//the delay is for ie to have time to select the selected value
			(function() {window.location = value}).delay(2);
		}
	},
	save: function(name, value) {
		Cookie.write(name + 'Select', value, {path: '/'});
	},
	getSelectedValue: function(name) {
		return Cookie.read(name + 'Select');
	}
});