﻿/*按比例生成缩略图*/
function DrawImage(ImgD,W,H)
{ 
	  var flag=false; 
	  var image=new Image(); 
	  image.src=ImgD.src; 
	  if(image.width>0 && image.height>0)
	  { 
		    flag=true; 
		    if(image.width>W)
		    {
			
		    	ImgD.height=image.height*W/image.width;
		    	ImgD.width=W;
		    }
		    else
		    {
			ImgD.height=image.height;
			ImgD.width=image.width;
		    }
			
		    if(ImgD.height>H)
		    {
		    	ImgD.width=ImgD.width*H/ImgD.height;
		    	ImgD.height=H;
		    }
		
		   ImgD.alt="";   
		   
	  }
}


function constrainPhoto(img) {
    if(img.src.indexOf("loading.gif")>0){return;}
	//img.style.display = 'none';				
	var testImg = new Image();
	testImg.src = img.src;
	var maxWidth = parseInt(img.getAttribute("constrainWidth"));
	var maxHeight = parseInt(img.getAttribute("constrainHeight"));	
	if (maxWidth == 0)
		maxWidth = testImg.width;	
	if (maxHeight == 0)
		maxHeight = testImg.height;	
	var heightScale = maxHeight / Math.max(testImg.height, maxHeight);
	var widthScale = maxWidth / Math.max(testImg.width, maxWidth);	
	var scaleFactor = Math.min(heightScale, widthScale);	
	img.width = testImg.width * scaleFactor;
	img.height = testImg.height * scaleFactor;
	var photoBox=document.getElementById('photoBox');
	if(photoBox) photoBox.style.background='none';
	img.style.display = '';	
	img.style.cursor='pointer';
	//img.onclick=open(img);
}

function replacePhoto(photoUrl, index){
    var tmpstrs = photoUrl.split("_"); 
    var tmpstr = tmpstrs[0];
	var postfix;
    if (tmpstrs[1]!=null) {
        var aa = tmpstrs[1].split(".");
		if(aa[1] != null){
		    postfix = aa[1];
		}
    }
    return tmpstr + index + "." + postfix;
}
