var isIE6		= (/msie|MSIE 6/.test(navigator.userAgent));
var isSafari		= (/Safari/.test(navigator.userAgent));
var iComScreenDiv	= null;
var iComImage		= null;
var oldOverflow		= document.documentElement.style.overflow;
var imageFrame;

function displayImage(id, size, width, height) {
	var linkURL = 'http://www.opusdesign.com.au//index.php?option=com_icommerce&task=image&format=raw&prod_image_id=' + id + '&size=' + size;
	setupImageDiv(width, height, linkURL);
	toggleImage(width, height);
	return false;
}

function setupElement(e, index) {
	e.style.position	= (isIE6) ? "absolute" : "fixed";
	e.style.top		= "0px";
	e.style.left		= "0px";
	e.style.zIndex		= index;
	if (isIE6) {
		e.style.cursor	= "hand";
		e.style.width	= "100%";
		e.style.height	= docHeight();
	} else {
		e.style.pointer	= "hand";
		e.style.right	= "0px";
		e.style.bottom	= "0px";
	}
}

function docWidth()  {return ((window.innerWidth ) ? window.innerWidth  : document.documentElement.clientWidth );}
function docHeight() {return ((window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight);}
function setupImageDiv(width, height, linkURL) {
	if (iComScreenDiv == null) {
		iComScreenDiv		= document.createElement('div');
		iComScreenDiv.onclick	= toggleImage;
		document.body.appendChild(iComScreenDiv);
		iComImage		= document.createElement('img');
		iComImage.onclick	= toggleImage;
		document.body.appendChild(iComImage);
	}

	setupElement(iComScreenDiv, 1000);
	iComScreenDiv.style.display		= "none";
	iComScreenDiv.style.backgroundColor	= "#fff";
	iComScreenDiv.style.opacity		= 0.8;
	iComScreenDiv.style.filter		= "alpha(opacity=80)";

	setupElement(iComImage, 1001);
	iComImage.style.display			= "none";
	iComImage.style.backgroundColor		= "transparent";
	iComImage.style.width			= width  + "px";
	iComImage.style.height			= height + "px";
	iComImage.style.borderWidth		= "1px";
	iComImage.style.borderColor		= "#000";
	iComImage.style.borderStyle		= "solid";
	iComImage.src				= linkURL;
}

function toggleImage(width, height) {
	var show = iComScreenDiv.style.display == "none";
	if (show) {
		if (!isSafari)
			document.documentElement.style.overflow = "hidden";
		if (isIE6) scrollTo(0, 0);
		iComImage.style.top	= ((docHeight() / 2) - (height / 2)) + "px";
		iComImage.style.left	= ((docWidth () / 2) - (width  / 2)) + "px";
		iComImage.style.display	= iComScreenDiv.style.display = "";
	} else {
		if (!isSafari)
			document.documentElement.style.overflow = oldOverflow;
		iComImage.style.display = iComScreenDiv.style.display = "none";
		iComImage.src = null;
	}
	return false;
}

function checkQty(form) {
	for (var i = 0; i < form.elements.length; ++i) {
		var element = form.elements[i];
		var name = element.name.split('[', 2);
		if ((name[0] == 'qty') && (element.value != "") && (parseInt(element.value) > 0))
			return true;
	}
	alert('You must enter a quantity before you can continue');
	return false;
}
