/*
	Pass in path to file from the assets/images folder (do not includes assets or images in the file path) 
	ex: home\image.gif - see if image assets/images/home/image.gif exists
	If there are multiple files to check, pass in filenames/paths separated by commas
	ex: home\image.gif,swatches\spacer.gif,logo.jpg

	Fills filesExist array with true/false values
	Returns true if file exists and false if it does not.  
	Will fill an element in array of true/false, one result in array for each filename passed in
*/
var filesExist = new Array(); /* Array used to hold results from doFilesExist function */

//needed to put this in since the doesFileExist takes longer than the popup window appeared.
checkJumbo();
function doFilesExist(stringOfFiles)
{
	if (document.implementation && document.implementation.createDocument)
	{
		textDoc = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		textDoc = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	
	//call to checking file
    textDoc.open('GET', 'doesfileexist.asp?files='+stringOfFiles, true);
	
	textDoc.onreadystatechange = 
		function(){
			try{
				if(textDoc.readyState == 4){	// 4 = complete
					//if ERROR is NOT contained in the returned message...
					if(textDoc.responseText.search(/ERROR/i)==-1){
						//alert(textDoc.responseText);
						
						//fill array with results
						filesExist = textDoc.responseText.split(",");
																		
					}//end if no ERROR
					else 
					{
						//error, return null
						return null;
					}
				}
				else {
					// So IE won't throw an error
					return;
				}
			}catch(e){
				// improperly formed json
			}			};
	
	
	textDoc.send(null);
}

var winPhoto2;

function checkJumbo(){
    var att_string;
    att_string = product_var[0].attr_value2.replace(/ /g, "_");
    doFilesExist( 'jumbo\\' + pf_id + '_' + att_string + '_j.jpg' );
}
    
function makeNewWindow2(path,title)
{
	var fullpath = "";
	var extension = "";
	
      
        
       if ( filesExist[0] == "true" ){
		fullpath="assets/images/jumbo/";
		extension="_j";
	} else {
		fullpath="assets/images/swatches/";
		extension="_lg";
	}
		
	winPhoto2 = window.open("","AltView","height=300,width=300,status=0,scrollbars=0,location=0");
	winPhoto2.document.open();
	winPhoto2.document.write('<html>');
	winPhoto2.document.write('    <head>');
	winPhoto2.document.write('        <title>');
	winPhoto2.document.write(title);
	winPhoto2.document.write('        </title>');
	winPhoto2.document.write('<script lang=javascript>');
	winPhoto2.document.write('var largeimage = new Image();');
	winPhoto2.document.write('largeimage.src ="'+ fullpath + pf_id + '_' + path + extension + '.jpg"');
	winPhoto2.document.write('<'+'/'+'script>');
	winPhoto2.document.write('    </head>');
	winPhoto2.document.write('    <body marginwidth="0" marginheight="0" topmargin="0" leftmargin="0">');
	winPhoto2.document.write('    <center><font size=4><b>'+title+'</b></font></center><br>');
	winPhoto2.document.write('        <center><img align=center src="' + fullpath + pf_id + '_' + path + extension + '.jpg" onLoad="window.resizeTo(largeimage.width+60,largeimage.height+150);" ></center>');
	winPhoto2.document.write('    </body>');
	winPhoto2.document.write('</html>');
	winPhoto2.document.close();
	winPhoto2.focus();
	return false;
}




function draw_image_2(){
	 if (product_image[1].scr=="PRODUCT" && product_image[1].image!="" ) {
		if (product_image[1].image_show=="TARGET REG" || product_image[1].image_show=="TARGET" ){
			if (product_image[1].image_show=="TARGET REG"){
				if (user_guid==""){
					document.write("<A HREF=\"shopper_lookup.asp?target=" + product_image[1].target + "&\" onClick=return(visitargs('shopper_lookup.asp',\"target=" + product_image[1].target + "&\")); target=\"_top\" >");
				}else {
					document.write("<A HREF=\"" + product_image[1].target + "\" onClick=return(visitargs('" + product_image[1].target + "')); target=\"_top\" >");
				}
			}else {
				document.write("<a HREF=\"" + product_image[1].target + "\" target=\"_top\">");
			}
			document.write("<img name=\"mainimg\" id=\"mainimg\" ALIGN =" + product_image[1].align + " ");
			if (product_image[1].width!=""){document.write("WIDTH =" + product_image[1].width + " ");}
			document.write(" BORDER=0  VSPACE=5  HSPACE=5 ALT=\"" + product_image[1].alt_text + "\" ");
			document.write(" SRC=\"assets/product_images/" + product_image[1].image +"\"></a>");
		}else{
			document.write("<img name=\"mainimg\" id=\"mainimg\" ALIGN =\"" + product_image[1].align + "\" ");
			if (product_image[1].width!=""){document.write("WIDTH =" + product_image[1].width + " ");}
			document.write(" BORDER=0  VSPACE=5  HSPACE=5 ALT=\"" + product_image[1].alt_text + "\" ");
			document.write(" SRC=\"assets/product_images/" + product_image[1].image +"\">");
		}
	}
	else{
		document.write("<img src=\"assets/product_images/product.gif\">");
	}
}

function imgOn(imgName) {
        document.getElementById("mainimg").src="assets/images/swatches/" + pf_id + "_" + imgName + "_lg.jpg";
}

function display_thumbnails2()
{
	if (product_var.length >= 1 )
	{
	
		var firstTime=true;
		var array_image;
		var lastImage="";
		var imagefilename;
		var columns=6;
		var counter=1;
		var n=0;
		var message ="";
		
		product_var.sort(UUSort);
		if (typeof info_attributes!="undefined" && info_attributes != "no swatch")
		{
		
			for (atn=0;atn< product_var.length ;atn++)
			{
				if (product_var[atn].avail!="0")
				{
					if (product_var[atn].attr_value2!="" && product_var[atn].attr_value2!=lastImage)
					{
						imagefilename = product_var[atn].attr_value2.split(" ").join("_");
						if (counter==1 && firstTime==true)
						{
							document.write("<div id=swatchMsg></div>");
							document.write("<br><table border=0><tr><td><div align=center width=100%><table border=0><tr valign=top>");
							firstTime=false;
						}
						document.write("<td align=center valign=top><table border=0><tr><td align=center valign=top width=60><A HREF=\"#\" onClick=\"return(makeNewWindow2('" + imagefilename + "',''));\" onmouseover=\"imgOn('"+imagefilename+"')\" onmouseout=\"document.getElementById('mainimg').src='assets/product_images/"+product_image[1].image+"'\"><img NAME=\""+imagefilename+"\" id=\""+imagefilename+"\" src=\"assets/images/swatches/" + pf_id + "_" + imagefilename + "_sm.jpg\" class=swatchImage width=60></A>");
						document.write("<br>"+product_var[atn].attr_value2+"</td></tr></table></td>");
						if (counter==columns){document.write("</tr><tr>");counter=0}
						counter = counter+1;
						n=n+1;
						lastImage = product_var[atn].attr_value2;
					}
				}
				
			}
			if (n > 0){document.write("</tr></table></div></td></tr></table>")}
			message = ( n <= 2 ) ? "" : "Available in these colors.";
			message += " Mouse over to see alternate color photo and click to enlarge.";
			if (n > 0){document.getElementById('swatchMsg').innerHTML = message;}
			
		}
	} 
}//end display_thumbnails
