/*
传入的参数
    1，皮肤文件的路径（默认为随机）
	2，窗口的  top left （默认为随机）
	3，窗口的  width height（默认为随机）
	4，设置窗口的ID（默认为随机）


	5, 显示信息的  top left （默认有）
	6, 显示信息的  右下角的rightbottom,righttop （默认有）
	7，设置是否需要关闭按纽（默认有）
	8，设置标题（默认为空）

*/

function WebFrame(){

		//获取随机的 strID
		var strTemp = "WebFrameID_"
		var i=0;
		while( $(strTemp+i) )  { i++; }  
		this.strID = strTemp + i ;

		this.strImagePath   =   "../js/js_packet/WebFrameImages/";
		this.strTitle   =   "";
		this.strImage       =   "skin1";
		this.iHeadLeftWidth =   12;
		this.iHeadRightWidth=   12;
		this.iHeadHeight    =   28;
		this.iTailHeight    =   14;

		this.iBodyLeft		=   5;
		this.iBodyTop		=   28;
		this.iBodyLeftTail	=   5;
		this.iBodyTopTail	=   5;

		this.bIsShutBtn = true;
		this.iShutBtnTop	= 8  ;
		this.iShutBtnRight	= 12  ;
		this.iShutBtnWidth	= 21 ;// 21 
		this.iShutBtnHeight	= 21 ;// 21
		
		
		this.iDefaultWidth=400;
		this.iDefaultHeight=58;
		

		this.iDefaultLeft=200;
		this.iDefaultTop=200;
		
		
		
		
		//标题
		this.strTitle		=	"" ;
		this.iTitleLeft		=	0  ;
		this.iTitleTop		=	6  ; 
		
		this.strBackGroundImage = "BackGround.gif";
		
		//
		this.arrDNA = null ;
		


		
		//慢慢显示出来, 
		this._iMaxOpacity=100;
		this._iMinOpacity=0;
		this._iCurOpacity=0;
		this._browserdetect=false;
		this._bSlowDispay = false;
		this._iSlowDispayRate = 2;
		
		this.iRun = 0;
}

WebFrame.prototype.Run=function(){
	this.iRun = this.iRun + 1;
	var obj = $(this.strID);
	
	if ( this._bSlowDispay && this.iRun % this._iSlowDispayRate == 0)
	{ 
		
		if (this._iCurOpacity >= this._iMaxOpacity)
		{
			this._bSlowDispay = false;
		}
		else
		{
			this._iCurOpacity+=10;
			if (this._browserdetect=="mozilla")
				obj.style.MozOpacity=this._iCurOpacity/100;
			else if (this._browserdetect=="ie")
				obj.filters.alpha.opacity=this._iCurOpacity;
		}

	}
}
WebFrame.prototype.runPosition=function(){
			this.iDefaultLeft = document.documentElement.scrollLeft + 100; 
			this.iDefaultTop = document.documentElement.scrollTop + 100; 
			this.getObj("").style.left = this.iDefaultLeft+"px";
			this.getObj("").style.top = this.iDefaultTop+"px"; 
}

WebFrame.prototype.SlowDisplay=function()
	{
	
	var obj = $(this.strID);
	obj.style.visibility="visible";
	
	this._browserdetect=obj.filters? "ie" : typeof obj.style.MozOpacity=="string"? "mozilla" : ""
	
	this._iCurOpacity = this._iMinOpacity;
	if (this._browserdetect=="mozilla"){
			obj.style.MozOpacity=this._iCurOpacity/100;
	}
	else if (this._browserdetect=="ie"){
			obj.filters.alpha.opacity=this._iCurOpacity;
	}

	this._bSlowDispay = true;
}



WebFrame.prototype.GetDNA=function(){
		
		var strPath;
		strPath = this.strImagePath+this.strImage+"/";
		var iMidWidth,iMidHeight;
		iMidWidth = this.iDefaultWidth-this.iHeadLeftWidth-this.iHeadRightWidth;
		iMidHeight = this.iDefaultHeight-this.iHeadHeight-this.iTailHeight;
		
		var iBodyWidth = this.iDefaultWidth-this.iBodyLeft-this.iBodyLeftTail;
		var iBodyHeight = this.iDefaultHeight-this.iBodyTop-this.iBodyTopTail;
		
		this.arrDNA =
		[
			[
			 	["create","tempobj","div"],
			 	["append","objContainer"],
				["attribute","id",this.strID+"_MoveID"],
				["style","position","absolute"],
				["style","visibility","hidden"],
				["style","border","medium solid gray"],
				["style","backgroundImage","url("+strPath+"space_1616.gif)"],
				["style","width",this.iDefaultWidth+"px"],
				["style","height",this.iDefaultHeight+"px"],
				["style","left",this.iDefaultLeft+"px"],
				["style","top",this.iDefaultTop+"px"]
			],
			[
			 	["create","objFrm","div"],
			 	["append","objContainer"],
				["attribute","id",this.strID],
				["style","position","absolute"],
				["style","visibility","visible"],
				["style","width",this.iDefaultWidth+"px"],
				["style","height",this.iDefaultHeight+"px"],
				["style","left",this.iDefaultLeft+"px"],
				["style","top",this.iDefaultTop+"px"]
			],
			[
			 	["create","tempobj","div"],
			 	["append","objFrm"],
				["attribute","id",this.strID+"_BackgroundID"],
				["style","position","absolute"],
				["style","backgroundImage","url("+strPath+this.strBackGroundImage+")"],
				["style","width",this.iDefaultWidth-5+"px"],
				["style","height",this.iDefaultHeight-5+"px"],
				["style","left","2px"],
				["style","top","2px"]
			],
			[
			 	["create","tempobj","div"],
			 	["append","objFrm"],
				["attribute","id",this.strID+"_BodyID"],
				["style","position","absolute"],
				["style","backgroundImage","url("+strPath+this.strBackGroundImage+")"],
				["style","width",iBodyWidth+"px"],
				["style","height",iBodyHeight+"px"],
				["style","left", this.iBodyLeft+"px"],
				["style","top",  this.iBodyTop+"px"],
				["style","overflow",  "hidden"]
			],
			[
			 	["create","tempobj","div"],
			 	["append","objFrm"],
				["attribute","id",this.strID+"_TopLeftID"],
				["style","position","absolute"],
				["style","backgroundImage","url("+strPath+"TopLeft.gif)"],
				["style","width",this.iHeadLeftWidth+"px"],
				["style","height",this.iHeadHeight+"px"],
				["style","left", "0px"],
				["style","top",  "0px"],
				["style","backgroundRepeat",  "no-repeat"]
			],
			[
			 	["create","tempobjTopBox","div"],
			 	["append","objFrm"],
				["attribute","id",this.strID+"_TopID"],
				["style","position","absolute"],
				["style","backgroundImage","url("+strPath+"Top.gif)"],
				["style","width",iMidWidth+"px"],
				["style","height",this.iHeadHeight+"px"],
				["style","left", this.iHeadLeftWidth+"px"],
				["style","top",  "0px"],
				["style","backgroundRepeat",  "repeat-x"],
				["style","overflow",  "hidden"],
				["attribute","align","left"],
				["style","cursor","default"],
				["style","paddingTop","5px"]

			],
			[
			 	["createTextNode","tempobj",this.strTitle],
			 	["append","tempobjTopBox"]
			],
			[
			 	["create","tempobj","div"],
			 	["append","objFrm"],
				["attribute","id",this.strID+"_TopRightID"],
				["style","position","absolute"],
				["style","backgroundImage","url("+strPath+"TopRight.gif)"],
				["style","width",this.iHeadRightWidth+"px"],
				["style","height",this.iHeadHeight+"px"],
				["style","right", "0px"],
				["style","top",  "0px"],
				["style","backgroundRepeat",  "no-repeat"]
			],
			[
			 	["create","tempobj","div"],
			 	["append","objFrm"],
				["attribute","id",this.strID+"_LeftID"],
				["style","position","absolute"],
				["style","backgroundImage","url("+strPath+"Left.gif)"],
				["style","width",this.iHeadLeftWidth+"px"],
				["style","height",iMidHeight+"px"],
				["style","left", "0px"],
				["style","top",  this.iHeadHeight+"px"]
			],
			[
			 	["create","tempobj","div"],
			 	["append","objFrm"],
				["attribute","id",this.strID+"_RightID"],
				["style","position","absolute"],
				["style","backgroundImage","url("+strPath+"Right.gif)"],
				["style","width",this.iHeadLeftWidth+"px"],
				["style","height",iMidHeight+"px"],
				["style","right", "0px"],
				["style","top",  this.iHeadHeight+"px"]
			],
			[
			 	["create","tempobj","div"],
			 	["append","objFrm"],
				["attribute","id",this.strID+"_BottomLeftID"],
				["style","position","absolute"],
				["style","backgroundImage","url("+strPath+"BottomLeft.gif)"],
				["style","width",this.iHeadLeftWidth+"px"],
				["style","height",this.iTailHeight+"px"],
				["style","left", "0px"],
				["style","bottom",  "0px"],
				["style","backgroundRepeat",  "no-repeat"]
			],
			[
			 	["create","tempobj","div"],
			 	["append","objFrm"],
				["attribute","id",this.strID+"_BottomID"],
				["style","position","absolute"],
				["style","backgroundImage","url("+strPath+"Bottom.gif)"],
				["style","width",iMidWidth+"px"],
				["style","height",this.iTailHeight+"px"],
				["style","right", this.iHeadLeftWidth+"px"],
				["style","bottom",  "0px"]
			],
			[
			 	["create","tempobj","div"],
			 	["append","objFrm"],
				["attribute","id",this.strID+"_BottomRightID"],
				["style","position","absolute"],
				["style","backgroundImage","url("+strPath+"RightBottom.gif)"],
				["style","width",this.iHeadRightWidth+"px"],
				["style","height",this.iTailHeight+"px"],
				["style","right", "0px"],
				["style","bottom",  "0px"],
				["style","backgroundRepeat",  "no-repeat"]
			],
			[
			 	["create","tempobjShutBtn","div"],
			 	["append","objFrm"],
				["attribute","id",this.strID+"_ShutBtnImgID"],
				["attribute","title","关闭"],
				["style","position","absolute"],
				["style","backgroundImage","url("+strPath+"ShutDefault.gif)"],
				["style","width",this.iShutBtnWidth+"px"],
				["style","height",this.iShutBtnHeight+"px"],
				["style","right", this.iShutBtnRight+"px"],
				["style","top",  this.iShutBtnTop+"px"],
				["style","visibility",  "visible"],
				["style","backgroundRepeat",  "no-repeat"],
				["on","onclick","$(\""+this.strID+"_BodyID\").innerHTML=\"\";document.body.removeChild( $(\""+this.strID+"\") );document.body.removeChild( $(\""+this.strID+"_MoveID\") );"],
				["on","onmouseover","this.style.backgroundImage='url("+strPath+"ShutActive.gif)'"],
				["on","onmouseout","this.style.backgroundImage='url("+strPath+"ShutDefault.gif)'"]
			]
		];
		return this.arrDNA;
	}


WebFrame.prototype.UpdateObject=function()
	{
		var arrInit;
		arrInit= this.GetDNA();
		for(var i=0;i<arrInit.length;i++){
			/*首先检测是否是创建的元素,*/
			if(arrInit[i][0][0] != "create"){
				continue;
			}
			for(var j=0; j<arrInit[i].length; j++){
				var tempstr=''
				switch ( arrInit[i][j][0] )
				{
					case "style":
						tempstr = "this.obj_"+arrInit[i][2][2]+".style."+arrInit[i][j][1]+" = '"+arrInit[i][j][2]+"'";
					  	break
					default:
					  break
				}
				eval(tempstr);
			}
		}
}

WebFrame.prototype.CreateObject=function()
	{
		//目的窗口
		var objContainer;
		objContainer= document.createDocumentFragment();
		var arrInit;
		arrInit= this.GetDNA();
		var exec_arr = new Array();
		for(var i=0;i<arrInit.length;i++){
			for(var j=0; j<arrInit[i].length; j++){
				switch ( arrInit[i][j][0] )
				{
					case "createTextNode":
						exec_arr.push("var "+arrInit[i][j][1]+"=document.createTextNode(\""+arrInit[i][j][2]+"\")");
					 	 break
					case "create":
						exec_arr.push("var "+arrInit[i][j][1]+"=document.createElement(\""+arrInit[i][j][2]+"\");this.obj_"+arrInit[i][2][2]+"="+arrInit[i][j][1]);
					 	 break
					case "append":
						exec_arr.push(arrInit[i][j][1]+".appendChild("+arrInit[i][0][1]+")");
					  	break
					case "attribute":
						exec_arr.push(arrInit[i][0][1]+".setAttribute(\""+arrInit[i][j][1]+"\",\""+arrInit[i][j][2]+"\")");
					  	break
					case "style":
						exec_arr.push(arrInit[i][0][1]+".style."+arrInit[i][j][1]+" = \""+arrInit[i][j][2]+"\"");
					  	break
					case "on":
						exec_arr.push(arrInit[i][0][1]+"."+arrInit[i][j][1]+"=function(a){ "+arrInit[i][j][2]+" }");
					  	break
					default:
					  break
				}
			}
		}
		eval( exec_arr.join(";") );
		document.body.appendChild(objContainer);
}
WebFrame.prototype.destroyObject=function(){
		document.body.removeChild( eval("this.obj_"+this.strID ) );
		document.body.removeChild( eval("this.obj_"+this.strID+"_MoveID" ) );
}
WebFrame.prototype.setSize=function( iWidth , iHeight ){
		this.iDefaultWidth=iWidth;
		this.iDefaultHeight=iHeight;
}
WebFrame.prototype.setImagePath=function( strPath ){
	this.strImagePath   =   strPath + "WebFrameImages/";
}
WebFrame.prototype.setStyle=function( str ){
	this.strImage   =   str;
}
WebFrame.prototype.setBackgroundTransparence=function(  ){
	this.strBackGroundImage   =   "space_1616.gif";
}
WebFrame.prototype.setSize=function( iWidth , iHeight ){
	
		this.iDefaultWidth=iWidth;
		this.iDefaultHeight=iHeight;
}
WebFrame.prototype.setSizeRelative=function( tx , ty ){
	
		this.iDefaultWidth+=tx;
		this.iDefaultHeight+=ty;
}
WebFrame.prototype.setBodyPosition=function( iBodyLeft , iBodyTop ,iBodyLeftTail,iBodyTopTail ){
	
		this.iBodyLeft		=   iBodyLeft;
		this.iBodyTop		=   iBodyTop;
		this.iBodyLeftTail	=   iBodyLeftTail;
		this.iBodyTopTail	=   iBodyTopTail;
}
WebFrame.prototype.setStrID=function( strID ){
	this.strID   =   strID;
}
WebFrame.prototype.setTitle=function( strTitle ){
	this.strTitle = strTitle;
}
WebFrame.prototype.clear=function(){
	$(this.strID+"_BodyID").innerHTML = "";
}
WebFrame.prototype.appendChild=function(o){
	$(this.strID+"_BodyID").appendChild( o );
}
WebFrame.prototype.setHidden=function(){
	$(this.strID).style.visibility="hidden";
}
WebFrame.prototype.setVisible=function(){
	$(this.strID).style.visibility="visible";
}
WebFrame.prototype.setWinZIndexPRI=function(){
	eval("this.obj_"+this.strID+".style.zIndex="+(++window.vWinZIndex));
}
WebFrame.prototype.setZIndex=function(str){
	eval("this.obj_"+str+".style.zIndex="+(++window.vWinZIndex));
}
WebFrame.prototype.setBackgroundColor=function( strColor){

	$(this.strID+"_BodyID").style.background =			strColor ;
	$(this.strID+"_BackgroundID").style.background =	strColor ;
}

WebFrame.prototype.setPosition=function(  tx , ty ){
			this.iDefaultLeft=tx;
			this.iDefaultTop=ty;
			var strExe = "this.obj_"+this.strID+".style.left=\""+this.iDefaultLeft+"px\";";
			eval(strExe);
			var strExe = "this.obj_"+this.strID+".style.top=\""+this.iDefaultTop+"px\";";
			eval(strExe);
}
WebFrame.prototype.setPositionRelative=function(  tx , ty ){
			this.iDefaultLeft+=tx;
			this.iDefaultTop+=ty;
			var strExe = "this.obj_"+this.strID+"_MoveID.style.left=\""+this.iDefaultLeft+"px\";";
			eval(strExe);
			var strExe = "this.obj_"+this.strID+"_MoveID.style.top=\""+this.iDefaultTop+"px\";";
			eval(strExe);
}


WebFrame.prototype.getObj=function( str ){
	eval("var o = this.obj_"+this.strID+str+";");
	return o;
}
WebFrame.prototype.getObjSizeBody=function(  ){
	var iWidth =  parseInt(eval("this.obj_"+this.strID+"_BodyID.style.width;"));
	var iHeight =  parseInt(eval("this.obj_"+this.strID+"_BodyID.style.height;"));
	var obj = { width:iWidth , height:iHeight }
	return obj;
}
WebFrame.prototype.getObjSize=function(  ){
	return { width:this.iDefaultWidth , height:this.iDefaultHeight };
}
WebFrame.prototype.addResizeEvent=function( callbackf ){
	WebFrame_fResizeEvent( $(this.strID+"_BottomRightID") , this , callbackf );
}
function WebFrame_fResizeEvent( objHand , obj , f ) {
	var ho=objHand;
	ho.onmousedown=function(a){
		obj.getObj("").style.zIndex=++window.vWinZIndex;
		obj.getObj("_MoveID").style.zIndex=++window.vWinZIndex;
		obj.getObj("_MoveID").style.visibility="visible";
		var d=document;if(!a)a=window.event;
		var Down_mus= getMouseCoords(a);
		
		if(ho.setCapture)
			ho.setCapture();
		else if(window.captureEvents)
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		d.onmousemove=function(a){
			if(!a)a=window.event;
			var mus=getMouseCoords(a);
			var tx=  mus.x - Down_mus.x;
			var ty=  mus.y - Down_mus.y;
			Down_mus =mus;
			f( tx , ty );
			obj.setSizeRelative( tx , ty);
			obj.getObj("_MoveID").style.width = obj.iDefaultWidth+"px";
			obj.getObj("_MoveID").style.height= obj.iDefaultHeight+"px";
		};
		d.onmouseup=function(){
			obj.UpdateObject(); 
			obj.getObj("").style.zIndex=++window.vWinZIndex;
			obj.getObj("_MoveID").style.visibility="hidden";
			if(ho.releaseCapture)
				ho.releaseCapture();
			else if(window.captureEvents)
				window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			d.onmousemove=null;
			d.onmouseup=null;
		};
	};
}

WebFrame.prototype.addMoveEvent=function(){
	WebFrame_fMoveEvent( $(this.strID+"_TopLeftID") , this);
	WebFrame_fMoveEvent( $(this.strID+"_TopID") , this);
	WebFrame_fMoveEvent( $(this.strID+"_TopRightID") ,this);
}
function WebFrame_fMoveEvent( objHand , obj , f ) {
	var ho=objHand;
	ho.onmousedown=function(a){
		obj.getObj("").style.zIndex=++window.vWinZIndex;
		obj.getObj("_MoveID").style.zIndex=++window.vWinZIndex;
		obj.getObj("_MoveID").style.visibility="visible";
		var d=document;if(!a)a=window.event;
		var Down_mus= getMouseCoords(a);
		
		if(ho.setCapture)
			ho.setCapture();
		else if(window.captureEvents)
			window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
		d.onmousemove=function(a){
			if(!a)a=window.event;
			var mus=getMouseCoords(a);
			var tx=  mus.x - Down_mus.x;
			var ty=  mus.y - Down_mus.y;
			Down_mus =mus;
			
			obj.setPositionRelative( tx , ty ); 
		};
		d.onmouseup=function(){
			if(ho.releaseCapture)
				ho.releaseCapture();
			else if(window.captureEvents)
				window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
			d.onmousemove=null;
			d.onmouseup=null;
			obj.getObj("").style.left = obj.iDefaultLeft+"px";
			obj.getObj("").style.top= obj.iDefaultTop+"px";
			obj.getObj("").style.zIndex=++window.vWinZIndex; 
			obj.getObj("_MoveID").style.visibility="hidden";
		};
	};
}




// delete function
WebFrame.prototype.addMoveEvent2=function(){
	std_fAddMoveEvent( $(this.strID+"_TopLeftID") , this.strID , std_fChangePosition);
	std_fAddMoveEvent( $(this.strID+"_TopID") , this.strID , std_fChangePosition);
	std_fAddMoveEvent( $(this.strID+"_TopRightID") , this.strID , std_fChangePosition);
}


WebFrame.prototype.setWebFramePosition=function(  tx , ty ){
			var o = $(this.strID);
			
			o.style.left = parseInt(o.style.left)+tx+"px";
			o.style.top = parseInt(o.style.top)+ty+"px";
			this.iDefaultLeft=parseInt(o.style.left)+tx;
			this.iDefaultTop=parseInt(o.style.top)+ty;
}

WebFrame.prototype.setLeftTop=function( x , y ){

	$(this.strID ).style.left =	x+"px";
	$(this.strID ).style.top =	y+"px";
	this.iDefaultLeft=x;
	this.iDefaultTop=y;
}

WebFrame.prototype.getSize=function(  ){
	return { width:this.iDefaultWidth , height:this.iDefaultHeight };
}
WebFrame.prototype.getSize2=function(  ){
	var iWidth =  parseInt ( eval("this.obj_"+this.strID+".style.width;") );
	var iHeight = parseInt ( eval("this.obj_"+this.strID+".style.height;") );
	var obj = { width:iWidth , height:iHeight }
	return obj;
}
WebFrame.prototype.setInitSize=function( iWidth , iHeight ){
	
		this.iDefaultWidth=iWidth;
		this.iDefaultHeight=iHeight;
}

WebFrame.prototype.BodySize=function(  ){
	var iWidth =  parseInt( $(this.strID+"_BodyID").style.width );
	var iHeight = parseInt( $(this.strID+"_BodyID").style.height );
	var obj = { width:iWidth , height:iHeight }
	return obj;
}
WebFrame.prototype.getSizeBody=function(  ){
	var iWidth =  parseInt(eval("this.obj_"+this.strID+"_BodyID.style.width;"));
	var iHeight =  parseInt(eval("this.obj_"+this.strID+"_BodyID.style.height;"));
	var obj = { width:iWidth , height:iHeight }
	return obj;
}
WebFrame.prototype.clearShowArea=function(){
	$(this.strID+"_BodyID").innerHTML = "";
}

WebFrame.prototype.setWebFrameID=function( strID ){
	this.strID   =   strID;
}
WebFrame.prototype.SetShowArea=function(){
	$(this.strID+"_BodyID").innerHTML = arguments[0];
}
WebFrame.prototype.setInitBodyPosition=function( iBodyLeft , iBodyTop ,iBodyLeftTail,iBodyTopTail ){
	
		this.iBodyLeft		=   iBodyLeft;
		this.iBodyTop		=   iBodyTop;
		this.iBodyLeftTail	=   iBodyLeftTail;
		this.iBodyTopTail	=   iBodyTopTail;
}


WebFrame.prototype.setImagePathAndStyle=function( strPath ){
	this.strImagePath   =   strPath;
}


WebFrame.prototype.getBodyID=function(  ){
	var str;
	str = this.strID+"_BodyID" ;
	return str;
}
WebFrame.prototype.getObjBody=function(  ){
	eval("var obj = this.obj_"+this.strID+"_BodyID;");
	return obj;
}
