// JavaScript Document for products page functionality

// this function will determine the correct list of colors to display based on the selected size
function getColors(a,b,c){
	cGroup = new Array();
	cGroup = b.split("|");
	colors = "<select name=\"color\" size=\"1\" id=\"color\" onchange=\"$.post('libraries/php/library.ajaxtools.php?tool=skuimage',{pid:"+c+",sid: document.getElementById('size').value,cid: this.value},function(data){if(data != '|'){result=data.split('|'); swapImage(result[0],result[1]);}});\"><option value=\"\">Select Your Color</option>";
	for(x in cGroup){
		parts = new Array();
		parts = cGroup[x].split(",");
		if(parts[0] == a){
			colors += "<option value=\""+parts[1]+"\">"+parts[2]+"</option>";
		}
	}
	colors += "</select>";
	document.getElementById("colorchoice").innerHTML = colors;	
}

// this function will open a centered popup with a product zoom image in it
// a = the large size filename
function showZoom(a){
	PositionX = 10;
	PositionY = 10;
	defaultWidth  = 600;
	defaultHeight = 500;
	var AutoClose = true;
	var imageTitle = "";
	var imgWin = window.open('','_blank','menubar=no,location=no,status=no,scrollbars=yes,resizable=yes,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
	if( !imgWin ) { return true; } //popup blockers should not cause errors
	imgWin.document.write('<html><head><title>'+a+'<\/title><script type="text\/javascript">\n'+
	'function resizeWinTo() {\n'+
	'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
	'var oH = document.images[0].height, oW = document.images[0].width;\n'+
	'if( !oH || window.doneAlready ) { return; }\n'+ //in case images are disabled
	'window.doneAlready = true;\n'+ //for Safari and Opera
	'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
	'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
	'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
	'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
	'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
	'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
	'if((oW >= 900) || (oH >= 700)){x.resizeTo( 800, 600 );}else{x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );}\n'+
	'var scW = screen.availWidth ? screen.availWidth : screen.width;\n'+
	'var scH = screen.availHeight ? screen.availHeight : screen.height;\n'+
	'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
	'}\n'+
	'<\/script>'+
	'<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>'+
	(document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;display:table;">'))+
	'<img src="uploads/products/zoom/'+a+'" alt="Loading image ..." title="" onload="resizeWinTo();">'+
	(document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
	imgWin.document.close();
	if( imgWin.focus ) { imgWin.focus(); }
	return false;
}

// this function will swap images in the main viewer
// a = id of the image to switch to
// b = the zoom size filename
function swapImage(a,b){
	document.getElementById("large").src="uploads/products/large/"+a;
	//alert(document.getElementById("zoomlink").innerHTML);
	//newInner = "<a href=\"javascript:;\" title=\"Click here to view zoom image\" onclick=\"return showZoom('"+b+"');\"><div class=\"zoombutton\">closer view</div></a>";
	newInner = "<div class=\"zoombutton\"><a href=\"javascript:;\" title=\"Click here to view zoom image\" onclick=\"return showZoom('"+b+"');\"><img src=\"images/shop_icons_magnify.png\" alt=\"zoom\" /> closer view</a></div>";
	//alert(newInner);
	document.getElementById("zoomlink").innerHTML = newInner;
}