var imgshow;
var showimgid = "";
function imgonload(id) {
    showimgid = id;
	imgshow=new iShow(id);
	imgshow.iMouse();
}
function div(id){return document.getElementById(id);}
var ie=!-[1,];
var Obj={
	Create:function(id,id2,ele){
		var obj=document.createElement(ele);

		if(id2 == "wrap"){
			obj.setAttribute("title","按住拖动");
		}
		obj.setAttribute("id",id2);
		div(id).appendChild(obj);
		},
	del:function(id){
		var obj=div(id);
		obj.parentNode.removeChild(obj);
		},
	inner:function(id,text){
		div(id).innerHTML=text;
		},
	css:function(){
		this.cssinfo();
		var obj=div(this.css.id);
			with(obj.style){
			ie?(this.css.float!=undefined?styleFloat=this.css.float:""):(this.css.float!=undefined?cssFloat=this.css.float:"");
			this.css.background!=undefined?background=this.css.background:"";
			this.css.width!=undefined?width=this.css.width:"";
			this.css.height!=undefined?height=this.css.height:"";
			this.css.position!=undefined?position=this.css.position:"";
			this.css.left!=undefined?left=this.css.left:"";
			this.css.top!=undefined?top=this.css.top:"";
			this.css.bottom!=undefined?bottom=this.css.bottom:"";
			this.css.padding!=undefined?padding=this.css.padding:"";
			this.css.margin!=undefined?margin=this.css.margin:"";
		//	this.css.filter!=undefined?filter=this.css.filter:"";
		//	this.css.opacity!=undefined?opacity=this.css.opacity:"";
			this.css.zIndex!=undefined?zIndex=this.css.zIndex:"";
			this.css.border!=undefined?border=this.css.border:"";
			this.css.overflow!=undefined?overflow=this.css.overflow:"";
			this.css.display!=undefined?display=this.css.display:"";
			this.css.textAlign!=undefined?textAlign=this.css.textAlign:"";
			this.css.fontSize!=undefined?fontSize=this.css.fontSize:"";
			this.css.color!=undefined?color=this.css.color:"";
			this.css.verticalAlign!=undefined?verticalAlign=this.css.verticalAlign:"";
			this.css.cursor!=undefined?cursor=this.css.cursor:"";
			this.css.lineHeight!=undefined?lineHeight=this.css.lineHeight:"";
			}
		},
	oEventX:function(e){
		var e=window.event?window.event:e;
		return e.clientX;
	},
	oEventY:function(e){
		var e=window.event?window.event:e;
		return e.clientY;
	}
}
var Create=Obj.Create,inner=Obj.inner,del=Obj.del,oX=Obj.oEventX,oY=Obj.oEventY;
Obj.css.prototype.cssinfo=function(){
	this.css=css;
	return this;
}
function iShow(objid){
	this.iControl="1"        /*1为图片大小自适应*/
	this.iControlSize=0.8;    /*最大图片大小占可视区域比例(此项需要this.iControl=1)*/
	this.ibody = showimgid;      /*bodyid*/
	this.bgid="container"    /*背景id*/
	this.borderid="wrap";    /*边框id*/
	this.boxid="imgwrap";    /*图像容器id*/
	this.listid=objid;       /*图像列表id*/
	this.borderSize=10;      /*边框大小*/
	this.titleSize=10;       /*标题区域大小*/
//	this.alpha=3;            /*透明度---1~10*/
	this.bgc="fff";           /*背景色*/
	/*默认弹出框样式*/
	this.w=420;
	this.h=226;
	this.c="#BFBFBF";
	this.w1=this.w+this.borderSize*2;
	this.h1=this.h+this.borderSize*2+this.titleSize;
	this.c1="#BFBFBF";
}
iShow.prototype.iMouse=function(){
	/*鼠标事件*/
	var iDel=this.iDel;
	var iLoad=this.iLoad,iStyle=this.iStyle;
	var ele={box:this.boxid,bd:this.borderid,bdSize:this.borderSize*2,tSize:this.titleSize,iCount:this.iCount,iControl:this.iControl,iControlSize:this.iControlSize}
	var eles=div(this.listid).getElementsByTagName("img");
	for(var i=0;i<eles.length;i++){	
		eles[i].onclick=function(){	
			if(typeof(this.ctype)!="undefined"){
				if(this.ctype=="max"){
					iStyle(iDel);
					iLoad(this,ele);
					return false;
				}
			}
		}
	}
}
iShow.prototype.iStyle=function(iDel){
	/*对象初始化*/
	this.iShow.call(this)
	Create(this.ibody,this.bgid,"div");
	Create(this.ibody,this.borderid,"div");
	Create(this.ibody,this.boxid,"div");
	css={id:this.bgid,background:"#"+this.bgc,position:"absolute",top:0,left:0}
	new Obj.css;
	css={id:this.borderid,background:this.c1,width:this.w1+"px",height:this.h1+"px",position:"absolute",margin:-this.h1/2+"px "+"0 0 "+-this.w1/2+"px",left:"50%",top:"50%",overflow:"hidden"}
	new Obj.css;
	css={id:this.boxid,textAlign:"center",background:this.c,width:this.w+"px",height:this.h+"px",position:"absolute",margin:-(this.h+this.titleSize)/2+"px "+"0 0 "+-this.w/2+"px",left:"50%",top:"50%"}
	new Obj.css;
	inner(this.boxid,"<span style='display:block;margin-top:50px;color:#fff;'>图片正在加载中，请稍后……</span><span style='color:#f00 ;cursor:pointer;' onclick='imgshow.iDel();'>点击此处关闭</span>")
	document.onkeydown=function(e){
		var e=e?e:window.event;
		if(e.keyCode=="27"){
			iDel();
		}
	}

	/*移动*/
	var obj1=div(this.borderid);
	var obj2=div(this.boxid);
	var Site=imgshow.Site;
	var Move=imgshow.Move;
	obj1.onmousedown=function(){Site(arguments[0],obj1,obj2);this.style.cursor="move";document.onmousemove=function(){Move(arguments[0],obj1,obj2)};}
	document.onmouseup=function(){this.onmousemove="";obj1.style.cursor=""}
}
iShow.prototype.iLoad=function(e,ele){
	var image=new Image();
	image.src=e.href;
	if(image.complete){Reset();}else{
		image.onload=function(){Reset();}
		}
	function Reset(){
		var iw0=image.width,ih0=image.height;
		var W0=iw0+ele.bdSize,H0=ih0+ele.bdSize+ele.tSize;
		function curSize(){
			var tmph = div("pop_mail_inner").scrollTop;
			var tmph1=h+94;
			var tmph2=h+100;
			css={id:borderid,width:W0+"px",height:H0+"px",left:"50%",top:tmph1+"px",margin:"0 0 0 "+-W0/2+"px"};
			new Obj.css;
			css={id:boxid,width:iw0+"px",height:ih0+"px",left:"50%",top:tmph2+"px",margin:ele.tSize/2+"px 0 0 "+-(W0-ele.bdSize)/2+"px"};
			new Obj.css;
		}
		var Size=(image.width>document.body.clientWidth*ele.iControlSize || image.height>document.body.clientHeight*ele.iControlSize)?true:false
		if(ele.iControl=="1" && Size==true){
			var iInfo=ele.iCount(image,ele.iControlSize);
			image.width=iInfo.iW,image.height=iInfo.iH;
		}
	//	var bt1="<a href='#' style='color:#fff; text-decoration:none;' onclick='imgshow.iDel()'>关闭</a>";
		var bt2="<a href='#' style='color:#ff0000;text-decoration:none;' id='iZoom_cur'>原图</a>"
		var bt="<span style='float:right;width:100%;text-align:right;background:#bfbfbf'>"+bt2+"</span>";
		var text1="<img class='imgmin' src='"+image.src+"' style='width:100%; height:100%'onclick='imgshow.iDel()' alt='点击关闭' />";
		var text2="<span id='imgshow_title' style='padding-top:4px; display:block;background:#bfbfbf'>"+bt+"</span>"
		var text=text1+text2;
		var W=image.width+ele.bdSize,H=image.height+ele.bdSize+ele.tSize;
		css={id:borderid,width:W+"px",height:H+"px",left:"50%",top:"50%",margin:-H/2+"px"+" 0 0 "+-W/2+"px"};
		new Obj.css;
		css={id:boxid,width:image.width+"px",height:image.height+"px",left:"50%",top:"50%",margin:-(image.height+ele.tSize)/2+"px"+" 0 0 "+-image.width/2+"px"};
		new Obj.css;
		inner(ele.box,text);
		//设置位置
		var obj1=div(this.borderid);
		var obj2=div(this.boxid);
 		var w = div("pop_mail_inner").scrollLeft;
	  	var h = div("pop_mail_inner").scrollTop;
		obj1.style.top= h+obj1.offsetTop;
		obj1.style.left= w+obj1.offsetLeft;
		obj1.style.margin= 0;
	  	obj2.style.top= h+obj2.offsetTop;
	  	obj2.style.left= w+obj2.offsetLeft;
	  	obj2.style.margin= 0;
	  	div("iZoom_cur").onclick=curSize;
	}
}
iShow.prototype.iCount=function(e,f){
	var doc=document.body,W=doc.clientWidth*f,H=doc.clientHeight*f;
	var obj=e.width/W>e.height/H?{iW:W,iH:e.height*W/e.width,iType:1}:{iW:e.width*H/e.height,iH:H,iTyle:2};
	return obj;
}
iShow.prototype.Site=function(e,obj1,obj2){
	this.X=oX(arguments[0]);
	this.Y=oY(arguments[0]);
	this.oLeft1=obj1.offsetLeft;
	this.oTop1=obj1.offsetTop;
	this.oLeft2=obj2.offsetLeft;
	this.oTop2=obj2.offsetTop;
}
iShow.prototype.Move=function(e,obj1,obj2){
	var X=oX(arguments[0])-this.X,Y=oY(arguments[0])-this.Y;
	var Left1=this.oLeft1+X,Top1=this.oTop1+Y,Left2=this.oLeft2+X,Top2=this.oTop2+Y;
	css={id:obj1.id,left:Left1+"px",top:Top1+"px",margin:0}
	new Obj.css;
	css={id:obj2.id,left:Left2+"px",top:Top2+"px",margin:0}
	new Obj.css;
}
iShow.prototype.iDel = function () {
    del(boxid); del(borderid); del(bgid);
    document.onkeydown = '';
}