window.onDomReady						 =	function(action) {
	var done							 =	false;
	window.onload						 =	loaded;
	if (document.addEventListener)			document.addEventListener("DOMContentLoaded", loaded, false);
	if ((/WebKit|KHTML|MSIE/i).test(navigator.userAgent))		check();
	
	function check() {
		if (document.body !== null && document.getElementsByTagName) {
			if (typeof document.fileSize != "undefined") {
				try {
					document.documentElement.doScroll("left");
					loaded();
				} catch (err) {}
			}
			if (typeof document.readyState != "undefined" && (/loaded|complete/).test(document.readyState))
				loaded();
		}
		if (!done)							setTimeout(check, 10);
	}
	
	function loaded() {
		if (!done) {
			done						 =	true;
			action();
		}
	}
}
window.onDomReady(function() { if (window.SOAPI) SOAPI.setup(false, "setup()", false); });

window.setup							 =	function() {
	
	SOAPI.configureWidgetPieces({
		Panel							 :	4095,
		Button							 :	4291,
		Textbox							 :	0,
		Textarea						 :	0,
		Tooltip							 :	8191
	});
	
	SOAPI.action						 =	false;
	SOAPI.buildWidgets();
	
	if (window.setup2) window.setup2();
	
}

//	String.pad() is still needed here because product pages do not include SOAPI
String.prototype.pad = function(n, c) {
	for (var t = "", i = this.length; i < n; i++) t += c;
	return t + this;
}

window.command							 =	function(query) {
	var xhr								 =	new XMLHttpRequest();
	xhr.open("GET", query, true);
	xhr.send(null);
}

window.showProductImage = function(id) {
	for (var i = 0, len = ProductImages.length; i < len; i++) {
		document.getElementById("main_product_image_" + ProductImages[i]).style.visibility = "hidden";
		document.getElementById("main_product_image_" + ProductImages[i]).style.display    = "none";
	}
	document.getElementById("main_product_image_" + id).style.visibility = "visible";
	document.getElementById("main_product_image_" + id).style.display    = "block";
}

window.updateProductOptions = function() {
	var price = parseInt(BasePrice);
	for (i in ProductOptionTypes) {
		var select = document.getElementById("options_" + i);
		if (!select) continue;
		var option = ProductOptions[select.value];
		document.getElementById("options_thumb_" + i).src       = option.thumb;
		document.getElementById("options_thumb_" + i).alt       = option.title;
		document.getElementById("options_link_"  + i).href      = option.image;
		document.getElementById("options_link_"  + i).title     = option.title;
		//document.getElementById("options_title_" + i).innerHTML = option.title;
		price += parseInt(option.price);
	}
	price = String(price).pad(3, "0");
	price = "£" + price.substr(0, price.length - 2) + "." + price.substr(price.length - 2);
	document.getElementById("total_price").innerHTML = price;
}


