﻿// JScript 文件
//<!--
    String.prototype.trim=function(t){
	    return (!t)?(this.replace(/^\s+|\s+$/,"")):((t=="L")?(this.replace(/^\s+/,"")):((t=="R")?(this.replace(/\s+$/,"")):(this)));
    };

//图片按比例缩放

function imgErr(obj){
   obj.src="/images/nopic.jpg"; 
}

var flag=false;
function DrawImage(ImgD,pwidth,pheight){
    var image=new Image();
    var iwidth = ImgD.clientWidth; //定义允许图片宽度，当宽度大于这个值时等比例缩小
    var iheight = ImgD.clientHeight; //定义允许图片高度，当宽度大于这个值时等比例缩小
    if(pwidth!=null) iwidth=pwidth;
    if(pheight!=null) iheight=pheight;
    image.src=ImgD.src;
    if(image.width>0 && image.height>0){
        flag=true;
        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.style.marginTop=(iheight-ImgD.height)/2+"px";
            }

            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.style.marginTop=(iheight-ImgD.height)/2+"px";
            }
            ImgD.alt=image.width+"×"+image.height;
        }
    }
} 
//调用：<img src="图片" onload="javascript:DrawImage(this)">

function DrawImage2(ImgD,pwidth,pheight){
    var image=new Image();
    var iwidth = 160; //定义允许图片宽度，当宽度大于这个值时等比例缩小
    var iheight = 120; //定义允许图片高度，当宽度大于这个值时等比例缩小
    if(pwidth!=null) iwidth=pwidth;
    if(pheight!=null) iheight=pheight;
    image.src=ImgD.src;
    if(image.width>0 && image.height>0){
        flag=true;
        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;
        }
    }
} 

function imgAutoSize(theType,myWidth,myHeight,myCss){
	var img = getEvent();
	var image = new Image();
	image.src = img.src
	img.onload='';
	var maxWidth = myWidth
	var maxHeight = myHeight
	if(!maxWidth && !maxHeight){
		maxWidth = img.parentNode.currentStyle.width.replace('px','')
		maxHeight = img.parentNode.currentStyle.height.replace('px','')
	}else if(!maxWidth){
		maxWidth = image.width
	}else if(!maxHeight){
		maxHeight = image.height
	}
	var theWidth = image.width;
	var theHeight = image.height;
	var newW = image.width;
	var newH = image.height;
	if (theType == 2) {
		if (theWidth/maxWidth<=theHeight/maxHeight) {
			newW = maxWidth;
			newH = theHeight*maxWidth/theWidth;
		} else {
			newH = maxHeight;
			newW = theWidth*maxHeight/theHeight;
		}
	}else{
		if (theWidth/maxWidth>=theHeight/maxHeight) {
			if (theWidth>maxWidth || theType == 1) {
				newW = maxWidth;
				newH = theHeight*maxWidth/theWidth;
			}
		} else {
			if (theHeight>maxHeight || theType == 1) {
				newH = maxHeight;
				newW = theWidth*maxHeight/theHeight;
			}
		}
	}
	img.width = newW
	img.height = newH
	if(!myWidth && myHeight){
		maxWidth = img.width
	}else if(!myHeight && myWidth){
		maxHeight = img.height
	}
	if(!myCss){
		myCss = ''
	}
	img.style.cssText = 'margin:0px;border:0px;padding:0px;'
	var div = document.createElement("div");
	div.style.cssText = myCss+'background-color:red;margin:0px;border:0px;padding:0px;overflow:hidden;width:'+newW+'px;height:'+newH+'px;margin-top:'+String((maxHeight-newH)/2)+'px;margin-right:'+String((maxWidth-newW)/2)+'px;margin-bottom:'+String((maxHeight-newH)/2)+'px;margin-left:'+String((maxWidth-newW)/2)+'px;';
	div.innerHTML = img.outerHTML;
	img.parentNode.replaceChild(div,img);
	image = null
}


function DrawImg(ImgD,pwidth,pheight){
    var image=new Image();
    var iwidth = 85; //定义允许图片宽度，当宽度大于这个值时等比例缩小
    var iheight = 65; //定义允许图片高度，当宽度大于这个值时等比例缩小
    if(pwidth!=null) iwidth=pwidth;
    if(pheight!=null) iheight=pheight;
    image.src=ImgD.src;
    if(image.width>0 && image.height>0){
        if(image.width/iwidth>image.height/iheight){
            ImgD.width=image.width/(image.height/iheight);
            ImgD.height=iheight;
        }
        else{
            ImgD.height=image.height/(image.width/iwidth);
            ImgD.width=iwidth;
        }
    }
} 


function img_zoom(e, o)  //图片鼠标滚轮缩放
{
var zoom = parseInt(o.style.zoom, 10) || 100;
zoom += event.wheelDelta / 12;
if (zoom > 0) o.style.zoom = zoom + '%';
  return false;
}


function exampleimg(img){
    alert(img.src);
}





//让FF支持IE的方法///////////////////////////////////////////////
if(window.HTMLElement) {
	
	/*outerHTML*//////////////////////////////////////////////////////////////////////////
	HTMLElement.prototype.__defineSetter__("outerHTML",function(sHTML){
		var r=this.ownerDocument.createRange();
		r.setStartBefore(this);
		var df=r.createContextualFragment(sHTML);
		this.parentNode.replaceChild(df,this);
		return sHTML;
	});
	HTMLElement.prototype.__defineGetter__("outerHTML",function(){
		var attr;
		var attrs=this.attributes;
		var str="<"+this.tagName.toLowerCase();
		for(var i=0;i<attrs.length;i++){
			attr=attrs[i];
			if(attr.specified)
				str+=" "+attr.name+'="'+this[attr.name]+'"';
				//str+=" "+attr.name+'="'+attr.value+'"';
		}
		if(!this.canHaveChildren)
			return str+">";
		return str+">"+this.innerHTML+"</"+this.tagName.toLowerCase()+">";
	});
	
	/*innerText*/////////////////////////////////////////////////////////////////////////
	HTMLElement.prototype.__defineSetter__("innerText",function(theText){
		this.textContent = theText
	});
	HTMLElement.prototype.__defineGetter__("innerText",function(){
		return this.textContent
	});
	
	/*window.attachEvent*/////////////////////////////////////////////////////////////////
	window.attachEvent = function(theName,theFun){
		window.addEventListener(theName.replace('on',''),theFun, true);	
	}
	/*currentStyle*/////////////////////////////////////////////////////////////////
	HTMLElement.prototype.__defineGetter__("currentStyle", function () {
	//getComputedStyle方法参数一是目标对象，参数二是具体属性。。。把注释对换下就知道了。。
	//return this.ownerDocument.defaultView.getComputedStyle(this, ":first-line");
	return this.ownerDocument.defaultView.getComputedStyle(this, null);
	});
}
//ff与ie下都支持获取Event的方法
function getEvent(){
	if(navigator.appName=='Microsoft Internet Explorer'){
		return window.event.srcElement; 
	}       
	var myFun=getEvent.caller;
	while(myFun!=null){
		var myArg=myFun.arguments[0];
		if(myArg){
			if((myArg.constructor==Event || myArg.constructor ==MouseEvent) || (typeof(myArg)=="object" && myArg.preventDefault && myArg.stopPropagation)){    
				return myArg.target;
			}
		}
		myFun=myFun.caller;
	}
	return null;
}





//-->