﻿// JScript 文件
function ImageInfo(_objImg,_width,_height)
{
	this.height=_height;
    this.width=_width;
	this.image=new Image();
	this.image.src=_objImg.src;
	this.id=_objImg.id;
}

var objImgs = new Object();

//預加載圖片
function loadImage(objImg){
    objImg.style.display="none";
    objImgs[objImg.id] = new Image();
    objImgs[objImg.id].src=objImg.src;
}

//height要指定的高度
//width要指定的寬度
//imgID圖片對象ID
function ResizeImage(height,width,imgID)
{
	var objImg=document.getElementById(imgID);
    var image=objImgs[imgID];
    
    if(image.height==0 || image.width==0)
    {
        objImg.style.display="none";
        //objImg.height=height;
        //objImg.width=width;
        return;
    }
	if(height<=0 || width<=0) return;
	var hDiff=height-image.height;         //相框高度減去圖像實際高度
	var wDiff=width-image.width;         //相框寬度減去圖像實際寬度
    var useheight=true;
    //圖片實際高、寬 都大於 相框
    if(	hDiff<=0 && wDiff<=0)  
    {
        if(Math.abs(hDiff)<=Math.abs(wDiff)) useheight=false;
    }
    else if(hDiff<=0 && wDiff>=0)
    {
        useheight=true;
    }
    else if(hDiff>=0 && wDiff<=0)
    {
        useheight=false;
    }
    else
    {
        if(Math.abs(hDiff)>=Math.abs(wDiff)) useheight=false;
    }
    
	if(useheight)
	{
		objImg.height=height;
		objImg.width=image.width*(height/image.height);
	}
	else
	{
		objImg.width=width;
		objImg.height=image.height*(width/image.width);
	}
	objImg.style.display="";
	//objImg.alt=objImg.width+"×"+objImg.height;
}

//預加載圖片
function loadImageInfo(objImg,width,height){
    objImgs[objImg.id] = new ImageInfo(objImg,width,height);
}

function ResizeImages()
{
	for ( var imgID in objImgs ) ResizeImageByObj(imgID);
}

//height要指定的高度
//width要指定的寬度
//imgID圖片對象ID
function ResizeImageByObj(imgID)
{
    var height=objImgs[imgID].height;
    var width=objImgs[imgID].width;
	var objImg=document.getElementById(imgID);
    var image=objImgs[imgID].image;
    
    if(image.height==0 || image.width==0)
    {
        objImg.style.display="none";
        //objImg.height=height;
        //objImg.width=width;
        return;
    }
	if(height<=0 || width<=0) return;
	var hDiff=height-image.height;         //相框高度減去圖像實際高度
	var wDiff=width-image.width;         //相框寬度減去圖像實際寬度
    var useheight=true;
    //圖片實際高、寬 都大於 相框
    if(	hDiff<=0 && wDiff<=0)  
    {
        if(Math.abs(hDiff)<=Math.abs(wDiff)) useheight=false;
    }
    else if(hDiff<=0 && wDiff>=0)
    {
        useheight=true;
    }
    else if(hDiff>=0 && wDiff<=0)
    {
        useheight=false;
    }
    else
    {
        if(Math.abs(hDiff)>=Math.abs(wDiff)) useheight=false;
    }
    
	if(useheight)
	{
		objImg.height=height;
		objImg.width=image.width*(height/image.height);
	}
	else
	{
		objImg.width=width;
		objImg.height=image.height*(width/image.width);
	}
	objImg.style.display="";
	//objImg.alt=objImg.width+"×"+objImg.height;
}

//height要指定的高度
//width要指定的寬度
//objImg圖片對象ID
function ResizeImageByObj2(objImg,width,height)
{
    var  image=new  Image();  
    image.src=objImg.src;
    
    if(image.height==0 || image.width==0)
    {
        objImg.style.display="none";
        //objImg.height=height;
        //objImg.width=width;
        return;
    }
	if(height<=0 || width<=0) return;
	var hDiff=height-image.height;         //相框高度減去圖像實際高度
	var wDiff=width-image.width;         //相框寬度減去圖像實際寬度
    var useheight=true;
    //圖片實際高、寬 都大於 相框
    if(	hDiff<=0 && wDiff<=0)  
    {
        if(Math.abs(hDiff)<=Math.abs(wDiff)) useheight=false;
    }
    else if(hDiff<=0 && wDiff>=0)
    {
        useheight=true;
    }
    else if(hDiff>=0 && wDiff<=0)
    {
        useheight=false;
    }
    else
    {
        if(Math.abs(hDiff)>=Math.abs(wDiff)) useheight=false;
    }
    
	if(useheight)
	{
		objImg.height=height;
		objImg.width=image.width*(height/image.height);
	}
	else
	{
		objImg.width=width;
		objImg.height=image.height*(width/image.width);
	}
	objImg.style.display="";
	//objImg.alt=objImg.width+"×"+objImg.height;
}

//height要指定的高度
//width要指定的寬度
//objImg圖片對象->this 
function ResizeImage1(height,width,objImg)
{
	var  image=new  Image();  
    image.src=objImg.src;  
	if(height<=0 || width<=0 || (image.height<=height && image.width<=width)) return;
	var hDiff=height-image.height;
	var wDiff=width-image.width;
	if(Math.abs(hDiff)>Math.abs(wDiff))
	{
		objImg.height=height;
		objImg.width=image.width*(height/image.height);
	}
	else
	{
		objImg.width=width;
		objImg.height=image.height*(width/image.width);
	}	
	//objImg.alt=objImg.width+"×"+objImg.height;
}

//height要指定的高度
//width要指定的寬度
//imgID圖片對象ID
//strUrl圖片路徑
function ResizeImage2(height,width,imgID,strUrl)
{
	var objImg=document.getElementById(imgID);
	var  image=new  Image();  
    image.src=strUrl;  
    if(image.height==0 || image.width==0)
    {
        //objImg.style.display="none";
	objImg.height=height;
	objImg.width=width;
	objImg.src=strUrl;
        return;
    }
	if(height<=0 || width<=0) return;
	var hDiff=height-image.height;         //相框高度減去圖像實際高度
	var wDiff=width-image.width;         //相框寬度減去圖像實際寬度
    var useheight=true;
    //圖片實際高、寬 都大於 相框
    if(	hDiff<=0 && wDiff<=0)  
    {
        if(Math.abs(hDiff)<=Math.abs(wDiff)) useheight=false;
    }
    else if(hDiff<=0 && wDiff>=0)
    {
        useheight=true;
    }
    else if(hDiff>=0 && wDiff<=0)
    {
        useheight=false;
    }
    else
    {
        if(Math.abs(hDiff)>=Math.abs(wDiff)) useheight=false;
    }
    
	if(useheight)
	{
		objImg.height=height;
		objImg.width=image.width*(height/image.height);
	}
	else
	{
		objImg.width=width;
		objImg.height=image.height*(width/image.width);
	}
	objImg.src="";
	objImg.src=strUrl;
	//objImg.style.display="";
	//objImg.alt=objImg.width+"×"+objImg.height;
}


//objImg圖片對象->this
function ResizeImage3(objImg)
{
    //var objImg=document.getElementById(objID);
	var  image=new  Image();  
	var height=objImg.height;
	var width=objImg.width;
    image.src=objImg.src;
	
	if(height<=0 || width<=0 || (image.height<=height && image.width<=width)) return;
	var hDiff=height-image.height;
	var wDiff=width-image.width;
	if(Math.abs(hDiff)>Math.abs(wDiff))
	{
		objImg.height=height;
		objImg.width=image.width*(height/image.height);
	}
	else
	{
		objImg.width=width;
		objImg.height=image.height*(width/image.width);
	}	
	//objImg.alt=objImg.width+"×"+objImg.height;
}

function ResizeText(width,divID)
{
	
	var strWidth=width.split("@");
	var strdivID=divID.split("@");
	
	for(i=0; i<strdivID.length-1; i++)
	{
	   var objTmp=document.getElementById( strdivID[i]);
	   objTmp.style.width=strWidth[i];
	}	
	//objTmp.innerText=strText;  
}

