var myWindow;

function openPopup(url,width, height) {  

    // default 450/450 width/height when no value sent
    if (width == '0' || width == '')
    	width = 450;
    if (height == '0' || height == '')
    	height = 450;
    	 
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));

    // KSA/SLD 04/08/2008 Create window to display image so the image will not be resized.  Also add Close link.
	var windowFeatures = "width=" + width + ",height=" + height + ",status,galleryimg=no,scrollbars=yes,resizable=yes,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
 
    myWindow = window.open("", "imageWindow", windowFeatures);
    myWindow.document.write("<HTML>");
    myWindow.document.write("<HEAD>");
    myWindow.document.write("<style type='text/css'>");
 	myWindow.document.write("a:link,a:hover,a:visited,a:active{text-decoration:none;color:#001852;}a:hover{text-decoration:underline;}");
 	myWindow.document.write("</style>");
    myWindow.document.write("</HEAD>");
	myWindow.document.write("<BODY>");
	myWindow.document.write("<span style='float: right;'><a href='javascript:window.close();'>&lt; Close &gt;</a> </span>");
 	myWindow.document.write("<img src='"+url+"' galleryimg='no'>");
	myWindow.document.write("</BODY>");
    myWindow.document.write("</HTML>");	
    
    if( myWindow)
    	myWindow.focus();
}
function openNonImagePopup(url,width, height) {   
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    
    // default 450/450 width/height when no value sent
    if (width == '0' || width == '')
    	width = 450;
    if (height == '0' || height == '')
    	height = 450;

	var windowFeatures = "width=" + width + ",height=" + height + ",status,galleryimg=no,scrollbars=yes,resizable=yes,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    myWindow = window.open(url, "imageWindow", windowFeatures);    
    if( myWindow)
    	myWindow.focus();
}


	/*
	var products = $A(document.getElementsByClassName('availableColor'));
	products.each (function (el){ Event.observe(el,'click',availableColorClickEvent)});
	
	function availableColorClickEvent(evt){
	//alert('hi');
	var prod = Event.element(evt);
	var title = prod.title;
	var src = prod.src;
	var index = src.lastIndexOf('.');
	var targetSrc = src.substring(0, index) + '2' + src.substring(index, src.length);
	//alert(targetSrc);
	swapImage(targetSrc,title);
	Event.stop(evt); 
	}
	
	*/
	
	function swapImage(src,title ){
	
		// KSA/SLD 04/30/2008 Make sure image exist before swap
		ajaxCall("/catalog/multiTasker.do",{action:'isFileExisting',file:src},swapCallBack);

		function swapCallBack(transport){
			
			var res = transport.responseText;
			res = res.strip();
			if(res.indexOf("error") != -1)
				return;
			if(res=="true"){
				$('imageNote').style.display = 'block';
				$('productImage').src = src;
				$('imageNote').innerHTML = title;
			}
		}		
	}	
	
	function swapImage2(src,title ){
		var index = src.lastIndexOf('.');
		var newSrc = src.substring(0, index) + '2' + src.substring(index, src.length);
		ajaxCall("/catalog/multiTasker.do",{action:'isFileExisting',file:newSrc},swapCallBack2);
		
		function swapCallBack2(transport){
			
			var res = transport.responseText;
			res = res.strip();
			if(res.indexOf("error") != -1)
			return;
//			alert(res == "true");
//			alert(res == true);
			if(res=="true"){
				
				$('productImage').src =newSrc;
				$('imageNote').innerHTML = title;
			}
		}
		
		
	}
	