var NOBG_FLAG=true; //设置是否有背景
var iframeColor="FFF";
/**
 * title：窗口标题
 * content_str：窗口下的内容，需要以div开头，同时要使用和html代码
 * w：窗口的宽度
 * flag：窗口可不可以拖动
 * nobgflag：是否有背景
 */
setFloatWindow = function(title,content_str,w,flag,nobgflag){
    var windowDiv = document.getElementById("float_window");
    if(windowDiv!=null){
        return false;
    }else{
		var iHeight = Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
		if(nobgflag==null || nobgflag=='undefined' || nobgflag==false) {
			//生成ifram底层
			var ifObj = document.createElement("iframe");
			ifObj.setAttribute('id','ifObj');
			document.body.appendChild(ifObj);
			
			//设置iframe的样式
			ifObj.style.borderWidth="0";
			ifObj.style.position = "absolute";
			ifObj.style.zIndex = "1000";
			ifObj.style.top = "0";
			ifObj.style.left = "0";
			ifObj.style.marginLeft = '-'+(Math.abs(window.screen.availWidth-document.body.clientWidth))/2+'px';    
			ifObj.style.width = (window.screen.availWidth)+ "px";
			ifObj.style.height = iHeight + "px";
			ifObj.style.background=iframeColor;
			ifObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=80)";
			ifObj.style.opacity = "0.8";
			ifObj.allowTransparency=false;
			
			var coveriframe = ifObj.contentWindow;
			if(coveriframe!=null)
			{
				coveriframe.document.open();//打开文档流
				coveriframe.document.writeln('<html><head>');
				coveriframe.document.writeln("</head><body></body></html>");
				coveriframe.document.body.style.backgroundColor=iframeColor;
				coveriframe.document.close();
			}
			
			NOBG_FLAG=false;
		}
		//生成浮动窗口
		var msgObj=document.createElement("div");		//弹出窗口
		msgObj.setAttribute('id','float_window');
		document.body.appendChild(msgObj);
		
		//设置浮动窗口样式
		msgObj.style.backgroundColor="#fff";
		msgObj.style.marginLeft = "3px";
		msgObj.style.marginTop = "3px";
		msgObj.style.left = (document.body.clientWidth-w)/2+"px";
		msgObj.style.position = "absolute";
		msgObj.style.zIndex = "1003";
		msgObj.style.width = w+"px";
		msgObj.style.border="3px #ddd solid";
		var inner_str='<h3 id="titleBar" style="padding:0 8px;height:30px;line-height:30px;background:#ddd;font-size:14px;"><span class="l bold">'+title+'</span>' +
				'<span id="_CLOSEX" onmousedown="javascript:closeFloatWindow();"' +
				'style="font-size:14px;font-weight:bold;color:#000;float:right;cursor:pointer;font-family:taho,verdana,sans-serif;">×</span></h3>';
		inner_str+=content_str;
		msgObj.innerHTML=inner_str;
		
		//设置浮动窗的Top和高度
		if(navigator.userAgent.indexOf('Chrome') > -1){
			msgObj.style.top = document.body.scrollTop + (window.screen.availHeight-msgObj.offsetHeight)/4+"px";
		}else{
			msgObj.style.top = document.documentElement.scrollTop + (window.screen.availHeight-msgObj.offsetHeight)/4+"px";
		}

		msgObj.focus();
		
		if(flag==true){
			var titleBar=document.getElementById("titleBar");
			titleBar.style.cursor='move';
			var curItemTop=0;
			var curItemLeft=0;
			var MouseStartX=0;
			var MouseStartY=0;
			var moveable = false;
			var docMouseMoveEvent = document.onmousemove;
			var docMouseUpEvent = document.onmouseup;
			
			titleBar.onmousedown = function()
			{
				var evt = getEvent();
				moveable = true;
				curItemTop=parseInt(msgObj.style.top);
				curItemLeft=parseInt(msgObj.style.left);
				MouseStartX=evt.clientX;
				MouseStartY=evt.clientY;
						
				document.onmousemove = function() 
				{
					if (moveable) 
					{
						var evt = getEvent();
						
						var FinalItemLeft=curItemLeft+evt.clientX-MouseStartX;
						var FinalItemTop=curItemTop+evt.clientY-MouseStartY;
						if (FinalItemLeft>0 && FinalItemTop>0 && (FinalItemLeft+w<document.body.clientWidth) && (FinalItemTop+msgObj.offsetHeight)<iHeight) 
						{
							msgObj.style.left = FinalItemLeft-2 + "px";
							msgObj.style.top = FinalItemTop-2 + "px";
						}
					}
				};
				document.onmouseup = function () 
				{
					if (moveable) 
					{
						document.onmousemove = docMouseMoveEvent;
						document.onmouseup = docMouseUpEvent;
						moveable = false;
						
						curItemTop=0;
						curItemLeft=0;
						MouseStartX=0;
						MouseStartY=0;
					}
				};
			};
		}
		document.documentElement.style.overflowX='hidden';
		return true;
	}
}
/**
 * 关闭弹出框
 */
closeFloatWindow = function(){
	if(NOBG_FLAG==false){
		document.body.removeChild(document.getElementById('ifObj'));
		NOBG_FLAG=true;
	}
	if(document.getElementById('float_window')!=null) {
		document.body.removeChild(document.getElementById('float_window'));
	}
	document.documentElement.style.overflowX='';
}
getEvent = function() {
	return window.event || arguments.callee.caller.arguments[0];
}
