/*
=========================================================
 Version: Jax 0.5
 Date: 2006-5-1
 Script Written by Utoper Jax Group
 Copyright (C) 2006 Utoper Corp. All rights reserved.
 Web: http://Jax.Utoper.com
 Email: UtoJax@163.com
=========================================================
****************************************************************** 
** 名: windows  属于JBM － Jax Base Module
** 创建：welsham 2006-12-11  修改: welsham 2006-12-11
** 描述: parser之窗口
** 修改描述: 
******************************************************************
*/

//**************初始化parser对象和show对象************************
if(typeof parser != "object") { var parser={}; }
if(typeof parser.windows != "object") { parser.windows={}; }

parser.windows.oSelName=null;
parser.windows.oSelValue={};

//**************跳转****************************************
//参数：
parser.windows.openWin=function(_url,_width,_height,_oSelName)
{
	if(!_width) _width=720;
	if(!_height) _height=400;
	if(_oSelName) this.oSelName=_oSelName; //选择的控件名称，格式为{"UtorID":"pID","UtorName":"pName"}
	
	var winName;
	if(window.name=="selWin") winName="selWin1";
	else winName="selWin";
	window.open(_url,winName,"height="+_height+",width="+_width+",scrollbars=yes");
};

//**************弹出窗口设置父窗口的值*************************
//参数：oValue－传回的值是个JSON数据对象
parser.windows.setValue=function(oValue)
{
	var key;
	this.oSelValue={};
	for(key in oValue)
	{
		if(this.oSelName)
		{
			if(this.oSelName[key]&&$(this.oSelName[key])) $(this.oSelName[key]).value=oValue[key];
		}
		else
		{
			if($(key)) $(key).value=oValue[key];
		}
		this.oSelValue[key]=oValue[key];
	}	
	
	parser.windows.onSetValue();
};
parser.windows.onSetValue=function()
{
}
parser.windows.resize = function(_winID, _frameID, _scrollToHutFrame)
{
	var win   = null;
	if (_winID) win   = $(_winID);
	var winFrame   = self.frames[_frameID];
	var rHeight, rWidth;
	if (winFrame)
	{
		if ($(_frameID).contentDocument)
		{
			rHeight	= $(_frameID).contentDocument.documentElement.scrollHeight;
		}
		else rHeight	= winFrame.document.documentElement.scrollHeight;
		//rWidth	= winFrame.document.documentElement.scrollWidth;
		
		$(_frameID).style.height = rHeight + "px";
		if(win) win.style.height = (rHeight + 40) + "px";
		
		//$(_frameID).style.width = (rWidth + 3) + "px";
		//win.style.width = (rWidth + 5) + "px";
		
		//winFrame向上调整hut窗口和win窗口
		if (parent && parent!=self)
		{
			if (parent.frames['hutFrame']) parent.parser.windows.resize(null,'hutFrame');
			if (parent.frames['winFrame']) parent.parser.windows.resize('win','winFrame');
		}		
		
		//hutFrame移动
		if (_scrollToHutFrame) parser.windows.scrollToHutFrame();
	}	
};
parser.windows.scrollToHutFrame = function()
{
	var hutUrl, nHeight1, nHeight1, oHeight, oHeight1, oHeight2;
	
	hutUrl = self.frames['hutFrame'].document.location.toString();
	if (hutUrl.indexOf('http') == -1) return;
	
	$("hutBar").style.display = 'block';
	
	nHeight1 = $('hutFrame').offsetTopt;
	nHeight2 = $('hutFrame').offsetHeight;
	oHeight = document.documentElement.offsetHeight*1.05;
	if(nHeight1 < oHeight || nHeight2 < oHeight)
	{
		oHeight1 = nHeight1 < oHeight ? oHeight - nHeight1 : 0
		oHeight2 = nHeight2 < oHeight ? oHeight - nHeight2 : 0
		$('hutFrame').style.margin = '0 0 ' + (oHeight1 + oHeight2) + 'px 0'
	}
	document.documentElement.scrollTop = $('hutFrame').offsetTop - 27;
};
parser.windows.closeHut = function()
{
	$("hutBar").style.display = 'none';
	$('hutFrame').style.height = 0;
	
	parser.windows.scrollToTop();
};
parser.windows.scrollToTop = function()
{
	document.documentElement.scrollTop = 0;
};
	
parser.windows.window=function(_name,_z,_title,_left,_top,_width,_height,_path,_isOpen,_isOpenFrame,_isOpenMsg)
{
	this.name = 'win';	if(_name) this.name=_name;
	this.z=8000;			if(_z) this.z=_z;
	this.title = 'Bto Win';	if(_title) this.title=_title;
	this.left = 200;		if(_left) this.left=_left;
	this.top = 100;		if(_top) this.top=_top;
	this.width = 300;	if(_width) this.width=_width;
	this.height = 180;	if(_height) this.height=_height;
	this.path='';	if(_path) this.path=_path;	
	this.isOpen=false;	if(_isOpen) this.isOpen=_isOpen;
	this.isOpenFrame=false;	if(_isOpenFrame) this.isOpenFrame=_isOpenFrame;
	this.isOpenMsg=false;	if(_isOpenMsg) this.isOpenMsg=_isOpenMsg;
	this.isOpenScreen=false;
	this.isInit=false;
};

parser.windows.window.prototype.setParameters=function(_title,_left,_top,_width,_height)
{
	if(_title) $( this.name+'Title' ).innerHTML=_title;
	
	if(_left) $( this.name ).style["left"]=_left+"px";
	if(_top) $( this.name ).style["top"]=_top+"px";
	if(_width) $( this.name ).style["width"]=_width+"px";
	if(_height) $( this.name ).style["height"]=_height+"px";
};

//初始化
parser.windows.window.prototype.init=function()
{
	var sHtml,winDisplay,frameDisplay,msgDisplay;
	//添加样式
	if( this.isOpen ) winDisplay="block";
	else winDisplay="none";
	
	if( this.isOpenFrame ) frameDisplay="";
	else frameDisplay="none";
	
	if( this.isOpenMsg ) msgDisplay="block";
	else msgDisplay="none";
	//添加Box
	sHtml = '<iframe id="'+ this.name +'BGFrame" style="position:absolute;z-index:'+(this.z-1)+';width:expression(document.getElementById(\''+ this.name+'\').offsetWidth);height:expression(document.getElementById(\''+ this.name+'\').offsetHeight);top:expression(document.getElementById(\''+ this.name+'\').offsetTop);left:expression(document.getElementById(\''+ this.name+'\').offsetLeft);" frameborder="0" ></iframe>';
	sHtml += '<div class="win" style="display:'+winDisplay+';width:'+this.width+'px;height:'+this.height+'px;left:'+this.left+'px;top:'+this.top+'px;z-index:'+this.z+'" id="'+ this.name +'"> \
	<div class="winTitle"> \
		<div class="winTitle1" id="'+ this.name +'Title">'+ this.title +'</div> \
		<div class="winTitle2"><a href="javascript:'+ this.name+'.close();">×</a></div> \
	</div> \
	<div class="winMain" id="'+ this.name +'Main"> \
		<div class="winMsg" id="'+ this.name+'Msg" style="display:'+msgDisplay+';"></div> \
		<iframe id="'+ this.name +'Frame" name="'+ this.name +'Frame" style="display:'+frameDisplay+';" width="100%" height="100%" scrolling="auto" frameborder="0" src="" onload="parser.windows.resize(\''+ this.name +'\',\''+ this.name +'Frame\')"></iframe> \
	</div> \
	</div>';

	var el = document.createElement('span');
	el.innerHTML = sHtml;
	document.getElementsByTagName("body")[0].appendChild(el);
	
	this.isInit=true;
};


parser.windows.window.prototype.open = function(_url,_oSelName,_msg,_msgType,_title,_o,_left,_top,_width,_height,_isOpenScreen)
{
	if(_isOpenScreen)
	{
		this.z=9999;
	}
	//初始化
	if( !this.isInit ) this.init();
	
	//打开
	$( this.name ).style.display="block";
	this.isOpen=true;
	//参数设置
	if(_left && _left==-1)
	{
		_left = document.documentElement.scrollLeft+document.documentElement.offsetWidth/2;
		if(_width) _left -= _width/2;
		else _left -= $( this.name ).offsetWidth/2;
	}
	else if( _o && !_left ) _left=getXY(_o).left;
	
	if(_top && _top==-1)
	{
		_top = document.documentElement.scrollTop+document.documentElement.offsetHeight/2;
		if(_height) _top -= _height/2;
		else _top -= $( this.name ).offsetHeight/2;
	}
	else if( _o && !_top ) _top=getXY(_o).top+_o.offsetHeight;
	
	this.setParameters(_title,_left,_top,_width,_height);

	if(_url) //地址转向
	{
		this.openFrame(_url,_oSelName);
		this.closeMsg();
	}
	else if(_msg) //提示信息
	{
		this.closeFrame();
		this.openMsg(_msg,_msgType);
	}
	
	if(_oSelName) parser.windows.oSelName=_oSelName; //选择的控件名称，格式为{"UtorID":"pID","UtorName":"pName"}
	else parser.windows.oSelName = null;
	
	if(_isOpenScreen) this.isOpenScreen=true;
	if(this.isOpenScreen && parser.show)
	{
		parser.show.showScreen();
		this.isOpenScreen=true;
	}
};
parser.windows.window.prototype.close = function()
{
	$( this.name ).style.display="none";
	this.isOpen=false;
	
	if( this.isOpenScreen && parser.show )
	{
		parser.show.cleanScreen();
		this.isOpenScreen=false;
	}
};

parser.windows.window.prototype.openFrame = function(_url,_oSelName,_msg)
{
	$( this.name+'Frame' ).style.display="";
	this.isOpenFrame=true;
	
	if(_url)
	{
		self.frames[this.name+'Frame'].location.href = _url;
	}
	if(_msg)
	{
		var eBody=self.frames[this.name+'Frame'].document.getElementsByTagName("body");
		if(eBody)
		{
			eBody.innerHTML=_msg;
		}
		else
		{
			var eBody = document.createElement('body');
			eBody.innerHTML = _msg;
			self.frames[this.name+'Frame'].document.appendChild(eBody);
		}
	}
};
parser.windows.window.prototype.closeFrame = function()
{
	$( this.name+'Frame' ).style.display="none";
	this.isOpenFrame=false;
};

parser.windows.window.prototype.openMsg = function(_msg,_msgType)
{
	$( this.name+'Msg' ).style.display="";
	this.isOpenMsg=true;
	
	if(_msg)
	{
		switch(_msgType)
		{
			case 1:
			{
				_msg += '<br /><br /><input type="button" onclick="win.close();"  value="确定" />';				
				break;	
			}
		}
		$( this.name+'Msg' ).innerHTML = _msg;
	}
};
parser.windows.window.prototype.closeMsg = function()
{
	$( this.name+'Msg' ).style.display="none";
	this.isOpenMsg=false;
};
