/* Global Variables */
var jumbosExist = new Array(); /* stores true/false to determine if jumbo exists */
var altGood = false; /* used to determine if alternate images loaded properly */
var jumboGood = false; /* used to determine if jumbo image loaded properly */
var recallFill = false; /* used to determine if fill_alternates function needs to be recalled */


/* called from product page to kick off image check and draw out hidden alternate popup for later use */
function drawJumboImage() 
{ 
	checkJumbosExist();
	
}


/* Draws product image
   -adds comingsoon.jpg as missing image and mainimg as id */
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_name + "\" title=\"" + product_name + "\" ");         
			document.write(" SRC=\"assets/product_images/" + product_image[1].image +"\" onerror=\"this.src='assets/images/comingsoon.jpg';\"></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_name + "\" title=\"" + product_name + "\" ");         
			document.write(" SRC=\"assets/product_images/" + product_image[1].image +"\" onerror=\"this.src='assets/images/comingsoon.jpg';\">");
		}  		
	}
	else{
		document.write("<img src=\"assets/product_images/product.gif\">");
	}
}

/* called from product page to add alternates holder div to page
   -altImages div is filled when alternate images are determined to exist or not.
*/
function display_alternates()
{
	document.write("<div id=\"altImages\"></div>");
	if(recallFill==true) fill_alternates();
}

/* fills altImages div with alternate images */
function fill_alternates()
{	
	//if there are any alternates to show...
	if(altGood == true)
	{
		//see if the container to fill exists
		if(!document.getElementById("altImages"))
		{
			recallFill = true;
		}
		else
		{
			//the number below changes the number of columns for the swatches
			var columns=5;
			var atn;
			atn=0;
			var myImage;
			var counter=1;	
			
			var output = "";
			
			output += "<br><div id=\"alternates_container\" class=\"add_images_container\"><div class=\"add_images\"><A HREF=\"javascript:void(0);\" onClick=\"popJumboImage(0);return false;\" >Additional Views</a><br>";
			
			for (var atn=1; atn<8; atn++)
			{
				if(jumbosExist[atn]=="true")
				{
					output += "<A HREF=\"javascript:void(0);\" onClick=\"popJumboImage("+atn+");return false;\" >" + getAltImage(atn) + "</A>";
				}
				counter = counter+1;
			}
			output += "</div></div>";
			
			document.getElementById("altImages").innerHTML = output;
		}//if the container to fill exists
	}//end if altGood		
} //end display_thumbnails

/* draw out the alternate image tag*/
function getAltImage(atn){
	return "<img NAME=\"img_"+atn+"\" id=\"img_"+atn+"\" src=\"assets/images/alternates/" + pf_id + "_sm_" + atn + ".jpg\" border=\"0\" class=\"add_image\">";
}


/* sets which alternate's lg version to show */
function setAltJumbo(atn){
	var output = "";
	if(atn==0 && jumboGood==true)
		output = "<img src=\"assets/images/alternates/"+pf_id+"_xl.jpg\" alt=\""+product_name+"\" title=\""+product_name+"\" id=\"JumboImg\">";
	if(atn==0 && jumboGood==false)
		output = "<img src=\"assets/product_images/"+product_image[1].image+"\" border=0 onError=\"this.src='assets/images/spacer.gif'; this.width='1';\" alt=\""+product_name+"\" title=\""+product_name+"\">";
	if(altGood==true && atn!=0)
		output = "<img src=\"assets/images/alternates/" + pf_id + "_lg_" + atn + ".jpg\" border=0 onError=\"this.src='assets/images/spacer.gif'; this.width='1';\" alt=\""+product_name+"\" title=\""+product_name+"\">";
	
	
	document.getElementById("JIContainer").innerHTML = output;
}


/* draw out hidden alternate/jumbo popup*/
function fillJumboPop(shownAtn)
{
	var image_pop_fill = ""; 
	image_pop_fill += "<div style=\"text-align:right;display:block;\"><a href=\"javascript:void(0);\" onClick=\"document.getElementById('imagepop').style.visibility = 'hidden'; return false;\" ><img src=\"assets/images/btnclose.gif\" border=\"0\" alt=\"Close\" title=\"Close\"></a></div><br>"; 
    image_pop_fill += "<div style=\"display:block;width:510px;\"><div style='float:left; width:75px;'>";
	
	for (var atn=1; atn<8; atn++)
	{
		if(jumbosExist[atn]=="true")
		{
			altGood = true;
			image_pop_fill += "<a href='javascript:void(0);' onclick='setAltJumbo("+atn+");'>"+getAltImage(atn,"")+"</a>";
		}
	}
	image_pop_fill += "</div>";
	
	//jumbo image
	image_pop_fill += "<div style='float:right; width:425px;' id=\"JIContainer\"><img src=\"assets/images/";
	if(jumbosExist[0]=="true")
	{
		jumboGood = true;
		image_pop_fill += "alternates/"+pf_id+"_xl.jpg";
	}
	else
	{
		image_pop_fill += "comingsoon.jpg";
	}
	image_pop_fill += "\" alt=\""+product_name+"\" title=\""+product_name+"\" id=\"JumboImg\"><div>";
	
	image_pop_fill += "<br class='clear' /></div>";
	
    document.getElementById('imagepop').style.visibility = 'hidden'; 
    document.getElementById("imagepop").innerHTML = image_pop_fill; 
	
	if(shownAtn != null) setAltJumbo(shownAtn);
}

/* draws out the jumbo image link */
function drawJumboLink(){ 
    /*if(jumboGood==true) document.write("<br><a href=\"javascript:void(0);\" onclick=\"popJumboImage(); return false;\" id=\"enlarge_link\">+ Enlarge Image</a>");  */
}

function popJumboImage (showAtn){ 
    if(showAtn != null) setAltJumbo(showAtn);
	document.getElementById('imagepop').style.visibility = 'visible';	
}

/* gathers string of images to check for existence */
function checkJumbosExist()
{
	var jumboFiles = "";
	jumboFiles = "alternates\\" + pf_id + "_xl.jpg";
	
	//check to see if there are alternates to show
	//go through 0-7 to see if alternates exist
	for (var atn=1; atn<8; atn++)
	{
		jumboFiles += ",alternates\\" + pf_id + "_sm_" + atn + ".jpg"; 
	}	
	
	//see if files exist - WILL FILL THE jumbosExist ARRAY
	doFilesExist(jumboFiles);
	
	/*
	 Element 0 in jumbosExist = main product image jumbo
	 Element 1 in jumbosExist  = first alts's jumbo (alternate 0)
	*/	
}


/*
	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 jumbosExist array with true/false values
	Returns true if file exists and false if it does not.  
	Will return an array of true/false, one result in array for each filename passed in
*/
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);
						
						//return results
						jumbosExist = textDoc.responseText.split(",");

						//fill popup with images
						fillJumboPop();

						//display alternate images
						fill_alternates();
					}//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);
}