// JavaScript Document
// author: wind.li
// date: 2008-08-01

var IS_CONNER_INITED = false;


function DIV() {
	this.nWidth = 680;
	this.nHeight = 650;
	this.nLeft = 300;
	this.nTop = 110;
	this.sURL = "";
	this.sInfo = "淘宝试衣间欢迎您！";
	this.bHasInitShow = false;
	this.bDisableBackground = true;
	this.bHasBorder = true;

	
	var WD_IS_IE            = (document.all?true:false);
//	var WD_WIN_MAX_WIDTH  = WD_IS_IE?(screen.width  - 20):window.innerWidth;
//	var WD_WIN_MAX_HEIGHT = WD_IS_IE?(screen.height - 200):window.innerHeight;	
	
//	var objDIV = this;
	//覆盖背景的DIV
	var objCoverBackground = document.createElement("DIV");
	//包含浮出窗口的DIV
	var objContainer = document.createElement("DIV");
	
	var objBody = null;

	var bShowStat = false;
	
	this.setSize = function(_nWidth, _nHeight) {
		this.nWidth  = _nWidth;
		this.nHeight = _nHeight;
		this.nLeft = (screen.availWidth-this.nWidth)/2;
		this.nTop = (screen.availHeight-this.nHeight-20)/2;
	}
	
	this.setPosition = function(_nLeft, _nTop) {
		this.nLeft = _nLeft;
		this.nTop = _nTop;
	}
	
	this.setInfo = function(_sInfo) {
		this.sInfo = _sInfo;
	}
	
	this.setURL = function(_sURL) {
		this.sURL = _sURL;
	}

	this.setDisableBackground = function(_bDisableBackground) {
		this.bDisableBackground = _bDisableBackground;
	}
	
	this.setHasBorder = function(_bHasBorder) {
		this.bHasBorder = _bHasBorder;
	}
	

	var setClassName = function(_obj,_sClassName){
		_obj.setAttribute("class",_sClassName);
		_obj.setAttribute("className",_sClassName);		
	}
	
	var setStyleString = function(_obj,_sStyleString){
		_obj.setAttribute("style",_sStyleString);
		_obj.style.cssText = _sStyleString;
	}

	this.floatAllWays = function() {
		if(!bShowStat) return;
		var windowHeight=0;
		var windowWidth=0;
		if(window.innerHeight){
			windowHeight = window.innerHeight;
			windowWidth = window.innerWidth;
		}else{
			windowHeight = window.document.documentElement.offsetHeight ;
			windowWidth = window.document.documentElement.offsetWidth ;
		}
		
		var h = (windowHeight-objContainer.scrollHeight)/2;
		var w = (windowWidth-objContainer.scrollWidth)/2;
		if(h<0){
			h=0;
		}
		if(w<0){
			w=0;
		}
		var nTop = document.documentElement.scrollTop + h;	
		var nLeft = document.documentElement.scrollLeft + 1.5*w;
		
		if(nTop+this.nHeight>windowHeight){
			nTop = windowHeight+document.documentElement.scrollTop-this.nHeight-50;
			if(nTop<0){
				nTop=0;
			}
		}
		if(nLeft+this.nWidth>windowWidth){
			nLeft = windowWidth+document.documentElement.scrollLeft-this.nWidth-50;
			if(nLeft<0){
				nLeft=0;
			}
		}
		var documentWidth = 0;
		var documentHeight = 0;
		if(document.documentElement.scrollWidth){
			documentHeight = document.documentElement.scrollHeight;
			documentWidth = document.documentElement.scrollWidth;
		}else{
			documentHeight = window.document.body.scrollHeight;
			documentWidth = window.document.body.scrollWidth;
		}
		if(nTop+this.nHeight>documentHeight){
			nTop = documentHeight-this.Height-50;
		}
		if(nLeft+this.nWidth>documentWidth){
			nLeft = documentWidth-this.nWidth-50;
			if(nLeft<0){
				nLeft=0;
			}
		}
		if(this.nTop == nTop&& this.nLeft == nLeft){
			return;
		}
		var sTempStyleString = "top:"+nTop+"px; left:"+nLeft+"px;";
		this.nTop = nTop;
		this.nLeft = nLeft;
		setStyleString(objContainer, sTempStyleString);
		/*
		var sBgWidth=document.documentElement.scrollWidth;
		var sBgHeight=Math.max(document.body.scrollHeight,document.body.clientHeight);
		if (this.bDisableBackground){
			var sCoverBGStyleString = "top:"+document.documentElement.scrollTop+"px; left:0px;";
			sCoverBGStyleString    += "width:"  + sBgWidth  + "px;";
			sCoverBGStyleString    += "height:" + sBgHeight  + "px;";	
			setClassName(objCoverBackground,"screendiv");
			setStyleString(objCoverBackground,sCoverBGStyleString);	
		}
		*/
	}

	this.show = function() {
		IS_CONNER_INITED = false;//弹出框圆角是否初始化
		bShowStat = true;
		objBody = document.getElementsByTagName("BODY")[0];
		var sBgWidth=document.documentElement.scrollWidth;
		var sBgHeight=Math.max(document.body.scrollHeight,document.body.clientHeight);
		if (this.bDisableBackground){
			var sCoverBGStyleString =  "top:0px; left:0px;";
			sCoverBGStyleString    += "width:"  + sBgWidth  + "px;";
			sCoverBGStyleString    += "height:" + sBgHeight  + "px;";
			setClassName(objCoverBackground,"screendiv");
			setStyleString(objCoverBackground,sCoverBGStyleString);
		}
		
		var nTop = (screen.availHeight-this.nHeight-20)/2;	
		var nLeft = (screen.availWidth-this.nWidth)/2;
		if(this.nLeft+this.nWidth>document.body.scrollWidth){
			this.nLeft = document.body.scrollWidth-this.nWidth;
		}
		var sTempStyleString = "top:"+this.nTop+"px; left:"+this.nLeft+"px;";
		setStyleString(objContainer, sTempStyleString);
		setClassName(objContainer,"containlogin");
		if(this.bHasBorder){		
			var table;
			var td_pop_content;
			if(document.getElementById("borderTable")){
				table = document.getElementById("borderTable");
				td_pop_content = document.getElementById("pop_content");
			}else{
				table = document.createElement("table");
				table.border = 0;
				table.id = "borderTable";
				table.cellPadding=0;
				table.cellSpacing=0;
		
				var tr1 = table.insertRow(0);
				var td_pop_topleft = tr1.insertCell(0);
				td_pop_topleft.setAttribute("class","pop_topleft");
				td_pop_topleft.setAttribute("className","pop_topleft");
				
				var td_pop_topborder = tr1.insertCell(1);
				td_pop_topborder.setAttribute("class","pop_border");
				td_pop_topborder.setAttribute("className","pop_border");
				
				var td_pop_topright = tr1.insertCell(2);
				td_pop_topright.setAttribute("class","pop_topright");
				td_pop_topright.setAttribute("className","pop_topright");
				
				var tr2 = table.insertRow(1);
				var td_pop_leftborder = tr2.insertCell(0);
				td_pop_leftborder.setAttribute("class","pop_border");
				td_pop_leftborder.setAttribute("className","pop_border");
				
				td_pop_content = tr2.insertCell(1);
				td_pop_content.id = "pop_content";
				td_pop_content.setAttribute("class","pop_content");
				td_pop_content.setAttribute("className","pop_content");
				
				var td_pop_rightborder = tr2.insertCell(2);
				td_pop_rightborder.setAttribute("class","pop_border");
				td_pop_rightborder.setAttribute("className","pop_border");
				
				var tr3 = table.insertRow(2);
				var td_pop_bottomleft = tr3.insertCell(0);
				td_pop_bottomleft.setAttribute("class","pop_bottomleft");
				td_pop_bottomleft.setAttribute("className","pop_bottomleft");
				
				var td_pop_bottonborder = tr3.insertCell(1);
				td_pop_bottonborder.setAttribute("class","pop_border");
				td_pop_bottonborder.setAttribute("className","pop_border");
				
				var td_pop_bottomright = tr3.insertCell(2);
				td_pop_bottomright.setAttribute("class","pop_bottomright");
				td_pop_bottomright.setAttribute("className","pop_bottomright");
			}
			
			td_pop_content.innerHTML = '<div id="divIframe"><iframe id="popFrame" name="popFrame" src="'+this.sURL+'" frameborder="0" scrolling="no" style="overflow:hidden;height:'+this.nHeight+'px; width:'+this.nWidth+'px;" onload="javascript:resizeIframe(this);"></iframe></div>';
			//objContainer.innerHTML = '<div id="divIframe"><iframe id="popFrame" name="popFrame" src="'+this.sURL+'" frameborder="0" scrolling="no" style="overflow:hidden;height:'+this.nHeight+'px; width:'+this.nWidth+'px;" onload="javascript:resizeIframe(this);"></iframe></div>'
			objContainer.id="popContainer";
			objContainer.appendChild(table);
		}else{
			objContainer.innerHTML = '<div id="divIframe"><iframe id="popFrame" name="popFrame" src="'+this.sURL+'" frameborder="0" scrolling="no" style="overflow:hidden;height:'+this.nHeight+'px; width:'+this.nWidth+'px;" onload="javascript:resizeIframe(this);"></iframe></div>';
		}
		
		if (this.bDisableBackground){
			objBody.appendChild(objCoverBackground);
		}
		objBody.appendChild(objContainer);
		
		
		this.bHasInitShow = true;
	}
	
	this.showAgain = function() {
		if (this.bDisableBackground){
			objCoverBackground.style.dispaly = "";
		}
		objContainer.style.dispaly = "";
		bShowStat = true;
	}

	this.close = function() {
		if (this.bDisableBackground){
			objCoverBackground.style.display = "none";
		}
		objContainer.style.display = "none";
		bShowStat = false;
	}
	
	this.destory = function (){
		objBody.removeChild(objContainer);
	}
	
	this.isInitShow = function() {
		return this.bHasInitShow;
	}
}

var g_DIV = new DIV();

function closeDiv() {
	g_DIV.close();
}

/**
 * 若调用页面会多次生成浮出窗口，每次浮出窗口的URL地址不同，则必须调用此函数
 * 
 * arg0: sURL	浮出窗口中页面URL
 * arg1: bDisableBackground	是否禁掉背景
 * arg2: nWidth 窗口宽度，px
 * arg3: nHeight 窗口高度，px
 * arg4: nLeft 窗口左上角距屏幕左侧距离，px
 * arg5: nTop 窗口左上角距屏幕上侧距离，px
 */
function showDiv() {
	var argv = showDiv.arguments;	
	var	argc = showDiv.arguments.length;
	if(argc>0) {
		var sURL = argv[0];
		g_DIV.setURL(sURL);		
	}
	if (argc>1) {
		var bDisableBackground = argv[1];
		g_DIV.setDisableBackground(bDisableBackground);
	}else{
		g_DIV.setDisableBackground(true);
	}
	if (argc>2) {
		var nWidth = g_DIV.nWidth, nHeight = g_DIV.nHeight;
		if (argc>=3) {
			nWidth = argv[2];
//			if(screen.availWidth>1024){
//				nWidth += (screen.availWidth-1024)/2;
//			}
		}else{
			nWidth = 680;
		}
		if (argc>=4) {
			nHeight = argv[3];
		}else{
			nHeight = 600;
		}
		g_DIV.setSize(nWidth, nHeight);
	}else{
		g_DIV.setSize(680, 600);
	}
	
	if (argc > 4) {
		var nLeft = g_DIV.nLeft, nTop = g_DIV.nTop;
		if (argc>=5) {
			nLeft = argv[4];
		}else{
			nLeft = 300;
		}
		if (argc>=6) {
			nTop = argv[5];
		}else{
			nTop = 110;
		}
		g_DIV.setPosition(nLeft, nTop);
	}else{
		g_DIV.setPosition(300, 110);
	}
	if (argc >6) {
		var bHasBorder = argv[6];
		g_DIV.setHasBorder(bHasBorder);
	}else{
		g_DIV.setHasBorder(true);
	}
	
//	if (g_DIV.isInitShow()) {
//		g_DIV.showAgain();
//	} else {
		//setInterval("g_DIV.floatAllWays()",600);
		g_DIV.show();
//	}
}

 var newDiv = null;
 function closeNewDiv(){
 	if(newDiv){
 		newDiv.destory();
 	}
 }
/**
 * 生成一个新的div
 * 
 * arg0: sURL	浮出窗口中页面URL
 * arg1: bDisableBackground	是否禁掉背景
 * arg2: nWidth 窗口宽度，px
 * arg3: nHeight 窗口高度，px
 * arg4: nLeft 窗口左上角距屏幕左侧距离，px
 * arg5: nTop 窗口左上角距屏幕上侧距离，px
 * arg6: bHasBorder 是否要边框
 */
function showNewDiv() {
	if(!newDiv){
		newDiv = new DIV();
	}
	var argv = showNewDiv.arguments;	
	var	argc = showNewDiv.arguments.length;
	if(argc>0) {
		var sURL = argv[0];
		newDiv.setURL(sURL);		
	}
	if (argc>1) {
		var bDisableBackground = argv[1];
		newDiv.setDisableBackground(bDisableBackground);
	}else{
		newDiv.setDisableBackground(true);
	}
	if (argc>2) {
		var nWidth = newDiv.nWidth, nHeight = newDiv.nHeight;
		if (argc>=3) {
			nWidth = argv[2];
//			if(screen.availWidth>1024){
//				nWidth += (screen.availWidth-1024)/2;
//			}
		}else{
			nWidth = 680;
		}
		if (argc>=4) {
			nHeight = argv[3];
		}else{
			nHeight = 502;
		}
		newDiv.setSize(nWidth, nHeight);
	}else{
		newDiv.setSize(680, 502);
	}
	
	if (argc > 4) {
		var nLeft = newDiv.nLeft, nTop = newDiv.nTop;
		if (argc>=5) {
			nLeft = argv[4];
		}else{
			nLeft = 300;
		}
		if (argc>=6) {
			nTop = argv[5];
		}else{
			nTop = 110;
		}
		newDiv.setPosition(nLeft, nTop);
	}else{
		newDiv.setPosition(300, 110);
	}
	if (argc >6) {
		var bHasBorder = argv[6];
		newDiv.setHasBorder(bHasBorder);
	}else{
		newDiv.setHasBorder(true);
	}
		
//	if (g_DIV.isInitShow()) {
//		g_DIV.showAgain();
//	} else {
		//setInterval("g_DIV.floatAllWays()",600);
		newDiv.show();
//	}
}

/**
 * 若调用页面会只生成一个浮出窗口，则建议调用此函数
 * 
 * arg0: sURL	浮出窗口中页面URL
 * arg1: bDisableBackground	是否禁掉背景
 * arg2: nWidth 窗口宽度，px
 * arg3: nHeight 窗口高度，px
 * arg4: nLeft 窗口左上角距屏幕左侧距离，px
 * arg5: nTop 窗口左上角距屏幕上侧距离，px
 */
function showDivJustOne() {
	var argv = showDivJustOne.arguments;
	var	argc = showDivJustOne.arguments.length;
	if(argc>0) {
		var sURL = argv[0];
		g_DIV.setURL(sURL);		
	}
	if (argc>1) {
		var bDisableBackground = argv[1];
		g_DIV.setDisableBackground(bDisableBackground);
	}
	if (argc>2) {
		var nWidth = g_DIV.nWidth, nHeight = g_DIV.nHeight;
		if (argc>=3) {
			nWidth = argv[2];
		}
		if (argc>=4) {
			nHeight = argv[3];
		}
		g_DIV.setSize(nWidth, nHeight);
	}
	if (argc > 4) {
		var nLeft = g_DIV.nLeft, nTop = g_DIV.nTop;
		if (argc>=5) {
			nLeft = argv[4];
		}
		if (argc>=6) {
			nTop = argv[5];
		}
		g_DIV.setPosition(nLeft, nTop);
	}
	if (g_DIV.isInitShow()) {
		g_DIV.showAgain();
	} else {
		//setInterval("g_DIV.floatAllWays()",600);
		g_DIV.show();
	}
}


////----------resize iframe
function resizeIframe(_ifrm) {
	if (_ifrm && !window.opera) {
		if (_ifrm.contentDocument
				&& _ifrm.contentDocument.documentElement.scrollHeight) {
			// FF中_ifrm.contentDocument.body.clientHeight 有时为0 改用documentElement
			var h = Math.min(
					_ifrm.contentDocument.documentElement.scrollHeight,
					_ifrm.contentDocument.body.scrollHeight)
			if (_ifrm.contentDocument.body.offsetWidth > 800)
				return;
			_ifrm.style.width = _ifrm.contentDocument.body.offsetWidth + "px";
			_ifrm.style.height = h + "px";
		} else if (_ifrm.Document) {
			if (_ifrm.Document.body.scrollHeight) {
				if (_ifrm.Document.body.scrollWidth > 800)
					return;
				_ifrm.style.width = _ifrm.Document.body.scrollWidth;
				_ifrm.style.height = _ifrm.Document.body.scrollHeight;
			} else {
				if (_ifrm.Document.documentElement.scrollWidth > 800)
					return;
				_ifrm.style.width = _ifrm.Document.documentElement.scrollWidth;
				_ifrm.style.height = _ifrm.Document.documentElement.scrollHeight;
			}
		}
	}
	if (parseInt(_ifrm.style.height) < 238) {
		_ifrm.style.height = 238 + "px";
	}
	/*
	 * try { if(window.parent == window) { return; }
	 * 
	 * var parentIframe = window.parent.document.getElementById("index_right");
	 * if(parent.resizeIframe){ parent.resizeIframe(parentIframe); } }catch(ex)
	 * {return;}
	 */
}

