<!-- By Matt Spinelli -->


var screenDpi = 96;
var diagLen=0;
var currentScreenResW=screen.width;
var currentScreenResH=screen.height;
var slideOn = false;
var slideEverStarted = false;
var counter = 0;

function findChecked(formobj){
    var checkedLoc = 0;
    if(formobj.item_name.length!=undefined) 
    {
    	for(i = 0; i < formobj.item_name.length; i++){
    		if (formobj.item_name[i].checked)
  			checkedLoc = i;
    	} 
    	return formobj.item_name[checkedLoc].value;
    }
    else
    {
    	return formobj.item_name.value;
    }
}

function zoom(divid,imageid,imagefile,zoomlevel,orgLen,aspect)
{
    if (slideOn == true){stopSlide();}
    
    getDpi(zoomlevel);
    var image1=new Image();
    
    
    //set the current image as background 
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")"; 
     
    //make image transparent 
    changeOpac(0, imageid); 
     
    if(zoomlevel!=0){document.getElementById('cropProgress').innerHTML="<img src='zoom/loading.gif'/>";    };   

     
    //make new image 
    image1.src=imagefile;

    if(zoomlevel!=0)
    {   
	document.getElementById('infodiv').innerHTML='Crop views are lossless images (saved as PNG instead of JPEG) made from the final edited photograph. The file size averages about 350KB per crop so there may be some delay after you press the crop buttons.<br><br>';  	
    }
    
    schedule(image1,imageid,imagefile,zoomlevel,orgLen,aspect);   
  
}

function imgDefined(imagefile)
{
    var image2=new Image();
    image2.src=imagefile;
    return image2;
}

function schedule(image,imageid,imagefile,zoomlevel,orgLen,aspect)
{
        var speed = Math.round(1175 / 100); 
        var timer = 0;
        
        if (image.src == undefined)
        {
        	image = imgDefined(imagefile);
        }
        
	if (image.complete == true)
	{
    	    document.getElementById('cropProgress').innerHTML="(Crop view loading done.)";
	    
	    if(zoomlevel!=0)
	    {   
		    if(zoomlevel==100)
		    {
			document.getElementById('infodiv').innerHTML='Crop views are lossless images (saved as PNG instead of JPEG) made from the final edited photograph. The file size averages about 350KB per crop so there may be some delay after you press the crop buttons.<br><br>You may be interested in buying a print. To help you determine if the quality of the photograph is acceptable to you, the above crop is the same size on your monitor as the corresponding area would be on a <b>'+ String(Math.round(orgLen/screenDpi/aspect*10)/10)+ '" x ' + String(Math.round(orgLen/screenDpi*10)/10)+'"</b> print.<br><br>';
		    }
		    if(zoomlevel==50)
		    {
			document.getElementById('infodiv').innerHTML='Crop views are lossless images (saved as PNG instead of JPEG) made from the final edited photograph. The file size averages about 350KB per crop so there may be some delay after you press the crop buttons.<br><br>You may be interested in buying a print. To help you determine if the quality of the photograph is acceptable to you, the above crop is the same size on your monitor as the corresponding area would be on a <b>'+ String(Math.round(orgLen/2/screenDpi/aspect*10)/10)+ '" x ' + String(Math.round(orgLen/2/screenDpi*10)/10)+'"</b> print.<br><br>';  	
		    }    
	    }
	    else 
	    {
		document.getElementById('infodiv').innerHTML="";
		document.getElementById('cropProgress').innerHTML="";	    	
	    }       	
	    
	    //fade in image 
	    for(i = 0; i <= 100; i++) { 
	        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed)); 
	        timer++; 
	    }    	
	    document.getElementById(imageid).src = imagefile; 	    
	}
	else
	{
		setTimeout("schedule('"+ image + "', '" + imageid + "', '" + imagefile  + "', '" + zoomlevel + "', '" + orgLen + "', '" + aspect + "')", 1000);
	}
}

function getDpi(zoomlevel)
{	
	var aplusb;
	isNumber = new Boolean(false);	
	if (currentScreenResW != screen.width || currentScreenResH != screen.height)
	{
		alert('You have switch screen resolutions and will need to enter the screen size again in case you are using multiple monitors');
		diagLen=0;
		currentScreenResW=screen.width;
		currentScreenResH=screen.height;
	}
	if (diagLen == 0){
		while (Number(diagLen)<1 || Number(diagLen)>150 || isNumber==false)
		{
			diagLen=prompt("Please enter the viewable diagonal screen size of your monitor in inches (i.e. 17, 19, etc.). Acceptable rage from 1 to 150.","");
			if (isNaN(diagLen))
			{
				isNumber=false;
			}
			else
			{
				isNumber=true;
			}
			if (diagLen!="")
			{
				if (!diagLen)
				{
					alert("You chose not to enter your screen size. Equivalent print size for the crops will be calculated using the standard of 96 dpi.")
					diagLen=-999
					break
				}
			}
		}

	}
	if (diagLen != -999)
	{	
		aplusb=(screen.width * screen.width) + (screen.height * screen.height);
		screenDpi= Math.sqrt(aplusb) / diagLen;
		currentScreenResW=screen.width;
		currentScreenResH=screen.height;
	}		
}

function blendimage(divid, imageid, imagefile, millisec, titleCaption, captionText, titleBuy, buyText,zoomImage50,zoomImage100,normalImage,orgLen,aspect,cancelSlide) { 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 
  
    //turn off slideshow if it is one if they user pressed one of the thumbnail buttons
    if (slideOn == true && cancelSlide == true){
        stopSlide();
    }
       
    //set the current image as background 
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")"; 
     
    //make image transparent 
    changeOpac(0, imageid); 
     
    //make new image 
    document.getElementById(imageid).src = imagefile; 
    
    //fade in image 
    for(i = 0; i <= 100; i++) { 
        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed)); 
        timer++; 
    } 
        
    document.getElementById(titleCaption).innerHTML=captionText;
    document.getElementById(titleBuy).innerHTML=buyText;
    document.getElementById('zoom').innerHTML="<br><a href=\"javascript:zoom('blenddiv','blendimage','" + zoomImage50 +"',50,"+orgLen+","+aspect+")\">Zoom to 50% Crop</a>&nbsp&nbsp<a href=\"javascript:zoom('blenddiv','blendimage','"+zoomImage100+"',100,"+orgLen+","+aspect+")\">Zoom to 100% Crop</a>&nbsp&nbsp<a href=\"javascript:zoom('blenddiv','blendimage','"+normalImage+"',0,"+orgLen+","+aspect+")\">Show All</a>"
    //clear the crop view information if it exists
    document.getElementById('cropProgress').innerHTML="";	 
    document.getElementById('infodiv').innerHTML="";
}

function enlargeimage(divid, imageid, imagefile, millisec) { 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    document.getElementById(divid).style.position = 'absolute';
    document.getElementById(divid).style.left = '70px';
  
    //turn off slideshow if it is one if they user pressed one of the thumbnail buttons
    if (slideOn == true && cancelSlide == true){
        stopSlide();
    }
       
    //set the current image as background 
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")"; 
     
    //make image transparent 
    changeOpac(0, imageid); 
     
    //make new image 
    document.getElementById(imageid).src = imagefile; 
    
    //fade in image 
    for(i = 0; i <= 100; i++) { 
        setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed)); 
        timer++; 
    } 
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function stopSlide(){
	slideOn = false;
	counter = 0;
	document.getElementById('stopwatch').innerHTML='<a href="#slideshow" onclick="javascript:slideshow()">PLAY</a><br><br>';
}
