﻿/***********************************************************
//
//  Name  : DivWindow
//  Author: KaiSin
//  link  : http://Res.Aooshi.com/Script/DivWindow.Js
//
//	ex:
//	DivWindow.Show(title,src,width,height);
//  DivWindow.Show('调用实例','http://www.baidu.com',500,300);
//
//  DivWindow.BlockLayer >> 遮罩;
//  DivWindow.Div >> 浮动动;
************************************************************/

var DivWindow = new Object();
DivWindow.Document = document;
DivWindow.Empty = "http://lib.homev.cn/jscript/divwindow/empty.html";
DivWindow.Gid	=	function(id){ return DivWindow.Document.getElementById(id);}
//关闭窗体
DivWindow.Close = function(){
  DivWindow.Document.body.removeChild(DivWindow.Gid('dw-DivWindow'));
  DivWindow.Document.body.removeChild(DivWindow.Gid('dw-BlockLayer'));
}
//是否由脚本自动调整宽度
DivWindow.WindowSize = function(){
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = DivWindow.Document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (DivWindow.Document.body.scrollHeight > DivWindow.Document.body.offsetHeight){ // all but Explorer Mac
			xScroll = DivWindow.Document.body.scrollWidth;
			yScroll = DivWindow.Document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = DivWindow.Document.body.offsetWidth;
			yScroll = DivWindow.Document.body.offsetHeight;
		}
		if(DivWindow.Document.documentElement && DivWindow.Document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = DivWindow.Document.documentElement.clientWidth;
			windowHeight = DivWindow.Document.documentElement.clientHeight;
		}
		else if  (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		}	
		else if (DivWindow.Document.body) { // other Explorers
			windowWidth = DivWindow.Document.body.clientWidth;
			windowHeight = DivWindow.Document.body.clientHeight;
		}
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			//pageWidth = windowWidth;
		} else {
			//pageWidth = xScroll;
		}
		
		pageWidth = xScroll;

    return new Array(xScroll,yScroll,windowWidth,windowHeight,pageWidth,pageHeight);
}
DivWindow.DefaultStyle = {
  //浮层
  Div:"width:0px;padding:5px;border:1px solid #666666;background-color:#CCCCCC; font-size:13px; color:#666666; font-weight:bold;",
  //遮罩
  BlockLayer:"position:absolute;top:0px;left:0px;filter: Alpha(Opacity=60);-moz-opacity:0.7;width:100%;height:100%;",
  //关闭按键
  Close:"width:15px !important; text-align:center; float:right;font-size:12px; z-Index:10089;cursor:pointer;background-color: #CCCCCC;border: 1px solid #666666;",
  //关闭按键
  CloseColor:"#ffffff"
}
//初始窗体并打开
DivWindow.Init = function(Style,ifr,isbtnclose){
	
	if (!Style) Style = DivWindow.DefaultStyle;
	
	var cls = '';
	if (isbtnclose === true)
	    cls = '<div title="关闭窗体" style="'+ Style.Close +'" onmouseover="this.style.color=\''+ Style.CloseColor +'\';" onmouseout="this.style.color=\'\';" onclick="javascript:DivWindow.Close();">×</div>';
	
	//if (!DivWindow.HTML)
    DivWindow.HTML = '<div style="height:20px;"><span style="float:left;" id="dw-title"></span>' + cls +'</div>' +
				     '<div style="border:solid 1px #666666; background-color:#FFFFFF; height:50px;" id="dw-content">'+ ((ifr===true)?'<iframe id="dw-ifm" src="" scrolling="no" frameborder="0" width="100%" height="100%"></iframe>':'<div id="dw-ifm" style="width:100%;"></div>') +'</div>';

	var _Div	=	DivWindow.Document.createElement('div');
	DivWindow.Document.body.appendChild(_Div);
	_Div.style.cssText = Style.Div;
	_Div.setAttribute('id','dw-DivWindow');
	_Div.innerHTML = DivWindow.HTML;
    _Div.style.zIndex = 10081;
	
	var _Layer = DivWindow.Document.createElement('iframe');
	DivWindow.Document.body.appendChild(_Layer);
	_Layer.style.zIndex = 10080;
	_Layer.style.cssText = Style.BlockLayer;
	_Layer.setAttribute('id','dw-BlockLayer');
	_Layer.setAttribute('frameborder','0');
	//_Layer.setAttribute('src','http://lib.homev.cn/jscript/divwindow/empty.html');
	_Layer.setAttribute('src',DivWindow.Empty);
  
	if (parseInt(DivWindow.Document.body.style.margin) != 0)
    	_Layer.style.width = DivWindow.WindowSize()[4] + "px";
    
    _Layer.style.height = DivWindow.WindowSize()[5] + "px";
	
  DivWindow.BlockLayer = _Layer;
  DivWindow.Div = _Div;
}
//设置内容
//	src		>>	连接地址
DivWindow.SetSrc = function(src){
	DivWindow.Gid('dw-ifm').src = src;
}
//设置内容
DivWindow.SetBody = function(src){
	DivWindow.Gid('dw-ifm').innerHTML = src;
}
//设置内容
DivWindow.GetBody = function(){
    return DivWindow.Gid('dw-ifm');
}
//得到内容体
DivWindow.GetContent = function(){
    return DivWindow.Gid('dw-content');
}
//设置标题
DivWindow.setTitle	=	function(title){
	DivWindow.Gid('dw-title').innerHTML = title;
}
//	设置大小
//	width	>>	宽度
//	height	>>	高度
DivWindow.SetSize	=	function(width,height){
	if (width)
	{
		if (isNaN(width))
		{
			DivWindow.Gid('dw-DivWindow').style.width = width;
			DivWindow.Gid('dw-ifm').height = width;
		}
		else
		{
			DivWindow.Gid('dw-DivWindow').style.width = width + 'px';
			DivWindow.Gid('dw-ifm').height = width + 'px';
		}
	}
	if (height)
	{
		if (isNaN(height))
		{
			DivWindow.Gid('dw-content').style.height = height;
			DivWindow.Gid('dw-ifm').height = height;
		}
		else
		{
			DivWindow.Gid('dw-content').style.height = height + 'px';
			DivWindow.Gid('dw-ifm').height = height + 'px';
		}
	}
	DivWindow.Gid('dw-ifm').frameborder = 0;
	DivWindow.Middle('dw-DivWindow');
}
//	显示窗体
//	title 	>>  窗体标题
//	src		>>	连接地址
//	width	>>	宽度
//	height	>>	高度
//  style >> 样式
DivWindow.Show = function(title,src,width,height,Style,ifr,isbtnclose){
	DivWindow.Init(Style,ifr,isbtnclose);
	if (parseInt(DivWindow.Document.body.style.margin) != 0)
    DivWindow.Gid('dw-BlockLayer').style.width = DivWindow.WindowSize()[4] + "px";
	DivWindow.Gid('dw-BlockLayer').style.height = DivWindow.WindowSize()[5] + "px";
	DivWindow.setTitle(title);
	if (ifr === true)
	    DivWindow.SetSrc(src);
	else
	    DivWindow.SetBody(src);
	DivWindow.SetSize(width,height);
}
// 系统调用
DivWindow.Middle = function(_sId){
	var theWidth;
	var theHeight;
	if (_sId == "") return;
	var obj = _sId;
	if (_sId && typeof(_sId) == 'string') obj=DivWindow.Gid(_sId);
	if (obj == null) return;
    if (DivWindow.Document.documentElement && DivWindow.Document.documentElement.clientWidth) { 
			theWidth = DivWindow.Document.documentElement.clientWidth+DivWindow.Document.documentElement.scrollLeft*2;
			theHeight = DivWindow.Document.documentElement.clientHeight+DivWindow.Document.documentElement.scrollTop*2;
	}
	else if(window.innerWidth)
	{
		theWidth = window.innerWidth;
		theHeight = window.innerHeight;
	}
	else
	{
	    theWidth  = 800;
	    theHeight = 150;
	}
	if (theHeight > 150) theHeight = theHeight - 100;
	obj.style.display = '';
	obj.style.position = "absolute";
	obj.style.left = (theWidth / 2) - (obj.offsetWidth / 2)+"px";
	if(DivWindow.Document.all)
	{
		if (DivWindow.Toped)
			obj.style.top = DivWindow.Document.body.scrollTop + (obj.offsetHeight/2) + "px";
		else
			obj.style.top = ( (theHeight / 2 + DivWindow.Document.body.scrollTop) - (obj.offsetHeight / 2) ) + "px";
		//( (theHeight / 2 + DivWindow.Document.body.scrollTop) - (obj.offsetHeight / 2) ) + "px";
	}
	else
	{
		//var sClientHeight = parent ? parent.DivWindow.Document.body.clientHeight : DivWindow.Document.body.clientHeight;
		var sClientHeight = DivWindow.Document.body.clientHeight;
		//var sScrollTop = parent ? parent.DivWindow.Document.body.scrollTop : DivWindow.Document.body.scrollTop;
		var sScrollTop = DivWindow.Document.body.scrollTop;
		var sTop = -80 + (sClientHeight / 2 + sScrollTop) - (obj.offsetHeight / 2);
		obj.style.top = (theHeight / 2 + DivWindow.Document.body.scrollTop) - (obj.offsetHeight / 2)+"px";
	}
	if (parseInt(obj.style.top) < 10) obj.style.top = "10px";
}