﻿/**
* 重新设定图片大小
*@param strPicUrl 图片路径
*@param width_v  目标宽度
*@param height_v 目标高度
*@param strPicAlt 图片alt
*@param strPicLinkUrl 图片链接地址
*/
function resizeImage(strPicUrl,width_v,height_v,strPicAlt,strPicLinkUrl,strHrefAttributes)
{
    if(isFirefox=navigator.userAgent.indexOf("Firefox")>0)
    {
        var backw=width_v;
        var backh=height_v;
    }
    else
    {
        var backw=width_v;
        var backh=height_v;
    }
    var i = String(Math.floor(Math.random()*10000)+1);  
    document.writeln("<div style='width:"+backw+"px;height:"+backh+"px;background:#fff;'>");
    document.writeln("<div style='width:"+width_v+"px;height:"+height_v+"px;overflow:hidden;'>");
    if(strPicLinkUrl != null && strPicLinkUrl != "")
    {
        document.writeln("<a href='"+strPicLinkUrl+"' target='_blank' " + strHrefAttributes + "><img src='"+strPicUrl+"' id='img"+i+"' alt='"+strPicAlt+"' /></a>");
    }
    else
    {
        document.writeln("<img src='"+strPicUrl+"' id='img"+i+"' alt='"+strPicAlt+"' />");
    }
    document.writeln("</div></div>");
    var pic=document.getElementById("img"+i);
    if((width_v/height_v)>(pic.offsetWidth/pic.offsetHeight))
    {
        pic.width=width_v;
    }
    else
    {
        pic.height=height_v;
    }
}



/**
* 重新设定图片大小
*@param strPicUrl 图片路径
*@param width_v  目标宽度
*@param height_v 目标高度
*@param strPicAlt 图片alt
*@param strPicLinkUrl 图片链接地址
*/
function resizeImageMore(strPicUrl,width_v,height_v,strPicAlt,strPicLinkUrl,strHrefAttributes)
{

    if(isFirefox=navigator.userAgent.indexOf("Firefox")>0)
    {
        var backw=width_v;
        var backh=height_v;
    }
    else
    {
        var backw=width_v;
        var backh=height_v;
    }
    var i = String(Math.floor(Math.random()*10000)+1);  
    document.writeln("<div style='width:"+backw+"px;height:"+backh+"px;background:#fff;'>");
    document.writeln("<div style='width:"+width_v+"px;height:"+height_v+"px;overflow:hidden;'>");
    if(strPicLinkUrl != null && strPicLinkUrl != "")
    {
        document.writeln("<a href='"+strPicLinkUrl+"' target='_blank' " + strHrefAttributes + "><img src='"+strPicUrl+"' id='img"+i+"' alt='"+strPicAlt+"' /></a>");
    }
    else
    {
        document.writeln("<img src='"+strPicUrl+"' id='img"+i+"' alt='"+strPicAlt+"' />");
    }
    document.writeln("</div></div>");
    var pic=document.getElementById("img"+i);

    var image=new Image(); 
    image=pic; 
//    debugger;
//    if(image.width/image.height>= iwidth/height_v)
//    { 
//       if(image.width>width_v)
//       { 
//            pic.width=width_v; 
//            pic.height=(image.height*width_v)/image.width; 
//       }
//       else
//       { 
//            pic.width=image.width; 
//            pic.height=image.height; 
//       } 
//       pic.alt=image.width+"×"+image.height; 
//    } 
//    else
//    { 
//        if(image.height>height_v)
//        { 
//            pic.height=height_v; 
//            pic.width=(image.width*height_v)/image.height; 
//        }
//        else
//        { 
//            pic.width=image.width; 
//            pic.height=image.height; 
//        } 
//        pic.alt=image.width+"×"+image.height; 
//    }
}


function resizeimg(ImgD,iwidth,iheight) { 
     var image=new Image(); 
     image.src=ImgD.src; 
     if(image.width>0 && image.height>0){ 
        if(image.width/image.height>= iwidth/iheight){ 
           if(image.width>iwidth){ 
               ImgD.width=iwidth; 
               ImgD.height=(image.height*iwidth)/image.width; 
           }else{ 
                  ImgD.width=image.width; 
                  ImgD.height=image.height; 
                } 
               ImgD.alt=image.width+"×"+image.height; 
        } 
        else{ 
                if(image.height>iheight){ 
                       ImgD.height=iheight; 
                       ImgD.width=(image.width*iheight)/image.height; 
                }else{ 
                        ImgD.width=image.width; 
                        ImgD.height=image.height; 
                     } 
                ImgD.alt=image.width+"×"+image.height; 
            } 
　　　　　ImgD.style.cursor= "pointer"; //改变鼠标指针 
//　　　　　ImgD.onclick = function() { window.open(this.src);} //点击打开大图片 
//　　　　    if (navigator.userAgent.toLowerCase().indexOf("ie") > -1) { //判断浏览器，如果是IE 
//　　　　　　ImgD.title = "请使用鼠标滚轮缩放图片，点击图片可在新窗口打开"; 
//　　　　　　ImgD.onmousewheel = function img_zoom() //滚轮缩放 
//　　　　　 { 
//　　　　　　　　　　var zoom = parseInt(this.style.zoom, 10) || 100; 
//　　　　　　　　　　zoom += event.wheelDelta / 12; 
//　　　　　　　　　　if (zoom> 0)　this.style.zoom = zoom + "%"; 
//　　　　　　　　　　return false; 
//　　　　　 } 
//　　　      } else 
//　　　      { //如果不是IE 
//　　　　　　　     ImgD.title = "点击图片可在新窗口打开"; 
//            } 
    } 
}


function preloadImages() {
  var d=document;   
  if(d.images)
  {
     if(!d.domimages) d.domimages=new Array();
     
     var i,j=d.domimages.length,domimagessrc=preloadImages.arguments; 
     
     for(i=0; i<domimagessrc.length; i++)
     {
         if (domimagessrc[i].indexOf("#")!=0)
         {
            d.domimages[j] = new Image;
            d.domimages[j++].src=domimagessrc[i];
         }
     }
  }
}

function preloadAllImages() 
{ 
  var imgs = document.getElementsByTagName("img");
   for(var i=0;i<imgs.length;i++)
   {
        if(imgs[i].src){
            var img = new Image();
            img.src=imgs[i].src;
        }
   }
}

function loadImage(img,maxw,maxh)
{
    var imgwh = getImgeWH(img.src);
     var imgw=imgwh.width;//item.getStyle('width');
    var imgh=imgwh.height;//item.getStyle('height');
    
    imgw=parseInt(imgw);
    imgh=parseInt(imgh);

    if(imgw>maxw){           
        imgh=maxw*imgh/imgw;
        imgw=maxw;
    }
    
    if(imgh>maxh){               
        imgw=maxh*imgw/imgh;
        imgh=maxh;
    }
    
    imgw=parseInt(imgw);
    imgh=parseInt(imgh);
    
    img.height=imgh;
    img.width=imgw;
}

function getImgeWH(url)
{
    var imgw=0;
    var imgh=0;
    var maxtime=100;
    
    while(imgw*imgh==0&&maxtime>0)
    {
         var img = new Image();
         img.src = url;
         imgw=img.width;
         imgh=img.height;
         imgw=parseInt(imgw);
         imgh=parseInt(imgh);
         maxtime--;
    }
    
    return {width:imgw,height:imgh};      
}


function fitImageEl(srcimg,maxW,maxH){

    var img = new Image(); 
    img.src = srcimg.src;
    
    if (img.complete) {
        var imgw=img.width;
        var imgh=img.height;
        imgw=parseInt(imgw);
        imgh=parseInt(imgh); 
                                              
        if(imgw>maxW)
        {           
            imgh=imgh*maxW/imgw;
            imgw=maxW;
        }
       
        if(imgh>maxH)
        {           
            imgw=imgw*maxH/imgh;
            imgh=maxH;
        }
        
        srcimg.width=imgw;
        srcimg.height=imgh;
       
        return; 
    }

    img.onload = function () {

        var imgw=img.width;
        var imgh=img.height;
        imgw=parseInt(imgw);
        imgh=parseInt(imgh);
        
        if(imgw>maxW)
        {           
            imgh=imgh*maxW/imgw;
            imgw=maxW;
        }
       
        if(imgh>maxH)
        {           
            imgw=imgw*maxH/imgh;
            imgh=maxH;
        }
                                            
        srcimg.width=imgw;
        srcimg.height=imgh;
    };

}
preloadAllImages();