
function $(elementId){
	return document.getElementById(elementId)
}
//判断浏览器的类型
function getBrowser()
{
	var ua = navigator.userAgent.toLowerCase ();
	var os = new Object();
	os.isFirefox = ua.indexOf ("gecko") != -1;
	os.isOpera = ua.indexOf ("opera") != -1;
	os.isIE = !os.isOpera && ua.indexOf ("msie") != -1;
	return os;
}	
//判断文档类型
function getBrowserDocument()
{
   var _dcw = document.documentElement.clientHeight;
   var _dow = document.documentElement.offsetHeight;
   var _bcw = document.body.clientHeight;
   var _bow = document.body.offsetHeight;
   
   if(_dcw == 0) return document.body;
   if(_dcw == _dow) return document.documentElement;		   
   if(_bcw == _bow && _dcw != 0) 
	 return document.documentElement;
   else
	 return document.body;
}	
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
function getPageSize(){ 
	 var xScroll, yScroll; 
	 if (window.innerHeight && window.scrollMaxY) { 
	  xScroll = document.body.scrollWidth;
	  yScroll = window.innerHeight + window.scrollMaxY;
	 } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
	  xScroll = document.body.scrollWidth;
	  yScroll = document.body.scrollHeight;
	 } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
	  xScroll = document.body.offsetWidth;
	  yScroll = document.body.offsetHeight;
	 }
	 
	 var windowWidth, windowHeight;
	 if (self.innerHeight) { // all except Explorer
	  windowWidth = self.innerWidth;
	  windowHeight = self.innerHeight;
	 } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
	  windowWidth = document.documentElement.clientWidth;
	  windowHeight = document.documentElement.clientHeight;
	 } else if (document.body) { // other Explorers
	  windowWidth = document.body.clientWidth;
	  windowHeight = 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;
	 }
	 arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	 return arrayPageSize;
}
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
function getPageScroll(){
	 var XScroll,yScroll;
	 if (self.pageXOffset) {
		XScroll = self.pageXOffset;
	 } else if (document.documentElement && document.documentElement.scrollLeft){  // Explorer 6 Strict
		XScroll = document.documentElement.scrollLeft;
	 } else if (document.body) {// all other Explorers
		XScroll = document.body.scrollLeft;
	 }
	 if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	 } else if (document.documentElement && document.documentElement.scrollTop){  // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	 } else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	 }
//	 alert(yScroll);
	 arrayPageScroll = new Array(XScroll,yScroll) 
	 return arrayPageScroll;
}
function miniWindow(initValues){
	//初始化常量
	this.config=initValues;	
	if(typeof(initValues)!="object"){alert("初始化窗口参数出错！");return;}
	if(typeof(this.config.windowType)=="undefined"){alert("窗口类型必须指定！");return;}
	if(typeof(this.config.obj)=="undefined"){alert("未指定与之绑定的对象名！");return;}
	this.config.width=this.config.width==undefined?350:this.config.width;
	this.config.height=this.config.height==undefined?200:this.config.height;
	this.config.hint=this.config.hint==undefined?"":"<font color='blue'>"+this.config.hint+"</font>";
	this.config.title=this.config.title==undefined?"百易百乐网":this.config.title;	
	this.config.speed=this.config.speed==undefined?25:this.config.speed;
	this.btnsLabs=this.config.btnsLabs;
	if(typeof(this.btnsLabs)=="undefined") this.btnsLabs={};
	this.lab_btnOk=this.btnsLabs.lab_btnOk==undefined?'确定':this.btnsLabs.lab_btnOk;
	this.lab_btnYes=this.btnsLabs.lab_btnYes==undefined?' 是 ':this.btnsLabs.lab_btnYes;
	this.lab_btnNo=this.btnsLabs.lab_btnNo==undefined?' 否 ':this.btnsLabs.lab_btnNo;
	this.lab_btnCancel=this.btnsLabs.lab_btnCancel==undefined?'取消':this.btnsLabs.lab_btnCancel;
	//初始化界面
	this.init=function(){
		if($("miniWindow")==null){
//			alert(this.config.width);
			var MW_strSource  = "<div id=\"miniWindow\">"+
								"<table border=\"0\" width=\""+this.config.width+"\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">"+
								"  <tr>"+
								"   <td width=\"5%\"><img src=\"images\/miniWindow\/window_r1_c1.gif\" width=\"100%\" height=\"30\" border=\"0\" alt=\"\" \/><\/td>"+
								"   <td width=\"100%\" class=\"win_title\"><div id=\"MW_title\"><\/div><\/td>"+
								"   <td width=\"10%\" class=\"win_title\" id=\"MW_btnClose\"><\/td>"+
								"   <td width=\"5%\"><img src=\"images\/miniWindow\/window_r1_c4.gif\" width=\"100%\" height=\"30\" border=\"2\" alt=\"\" \/><\/td>"+
								"  <\/tr>"+
								"  <tr>"+
								"   <td colspan=\"4\" valign=\"top\" align=\"center\" class=\"win_content\">"+
								"   <div id=\"MW_content\"><\/div>"+
								"   <div id=\"MW_control\"><\/div>"+
								"   <\/td>"+
								"  <\/tr>"+
//								"  <tr>"+
//								"   <td colspan=\"4\" ><img width=\"100%\" src=\"images\/miniWindow\/window_r3_c1.gif\" height=\"2\" border=\"0\" alt=\"\" \/><\/td>"+
//								"  <\/tr>"+
								"<\/table>"+
								"<\/div>"+
								"<img id=\"MW_mask\" src=\"images\/miniWindow\/Mask.gif\" width=\"100%\" alt=\"\"  width=\"100%\" height=\"100%\" \/>"
			var elem
			elem = document.createElement("div"); 
			elem.innerHTML = MW_strSource;
			document.body.insertBefore(elem, document.body.firstChild); 
		}
		$("MW_title").innerHTML=this.config.title;
		$("MW_btnClose").innerHTML="<a href=\"javascript:"+this.config.obj+".MW_Method(\'close\')\" ><img src=\"images\/miniWindow\/window_r1_c3.gif\" alt=\"\" width=\"22\" height=\"30\" border=\"0\" class=\"win_title\" title=\"关闭窗口\" \/><\/a>";
	}
	this.init();
	//判断窗口类型
	switch(this.config.windowType){
		//类型为只读窗口
		case "readonly":
			$("MW_btnClose").innerHTML="";
			$("MW_control").style.display="none";
			$("MW_content").innerHTML=this.config.hint;
			break;
		//类型为警告窗口
		case "alert" :
			$("MW_control").style.display="";
			$("MW_content").innerHTML=this.config.hint;
			$("MW_control").innerHTML='<input name="MW_btnYes" type="button" value="'+this.lab_btnOk+'" onclick='+this.config.obj+'.MW_Method("close") style="cursor:hand"/>'
			break;
		//类型为确认窗口
		case "confirm":
			$("MW_control").style.display="";
			$("MW_content").innerHTML=this.config.hint;
			$("MW_control").innerHTML='<input name="MW_btnYes" type="button" value="'+this.lab_btnYes+'" onclick='+this.config.obj+'.MW_Method("yes") style="cursor:hand"/>'+
									  '<input name="MW_btnNo" type="button" value="'+this.lab_btnNo+'" onclick='+this.config.obj+'.MW_Method("no") style="cursor:hand"/>'
			break;
		//类型为询问窗口
		case "ask":
			$("MW_control").style.display="";
			$("MW_content").innerHTML=this.config.hint;
			$("MW_control").innerHTML='<input name="MW_btnYes" type="button" value="'+this.lab_btnYes+'" onclick='+this.config.obj+'.MW_Method("yes") style="cursor:hand"/>'+
									  '<input name="MW_btnNo" type="button" value="'+this.lab_btnNo+'" onclick='+this.config.obj+'.MW_Method("no") style="cursor:hand"/>'+
									  '<input name="MW_btnCancel" type="button" value="'+this.lab_btnCancel+'" onclick='+this.config.obj+'.MW_Method("cancel") style="cursor:hand"/>'
			break;
		//类型为可允许输入窗口
		case "text" :
			var elem
			elem = document.createTextNode(this.config.hint)
			$("MW_content").appendChild(elem)
			elem = document.createElement("br"); 
			$("MW_content").appendChild(elem)
			elem = document.createElement("textarea"); 
			elem.id = "MW_textarea";
			$("MW_content").appendChild(elem)
			$("MW_control").style.display="";			
			$("MW_control").innerHTML='<input name="MW_btnYes" type="button" value="'+this.lab_btnYes+'" onclick='+this.config.obj+'.MW_Method("yes") style="cursor:hand"/>'+
									  '<input name="MW_btnNo" type="button" value="'+this.lab_btnNo+'" onclick='+this.config.obj+'.MW_Method("no") style="cursor:hand"/>'
			break;
		case "checkCode":
//			updateCheckCode();
			$("MW_content").innerHTML=this.config.hint+"<br>请输入验证码：<input size='5' maxlength='4' id='MW_input'><img src='image.jsp' id='checkCodeImg' /><a href='javascript:updateCheckCode()'>看不清换一张</a>" ;
			$("MW_control").style.display="";			
			$("MW_control").innerHTML='<input name="MW_btnYes" type="button" value="'+this.lab_btnOk+'" onclick='+this.config.obj+'.MW_Method("yes") style="cursor:hand"/>'+
									  '<input name="MW_btnNo" type="button" value="'+this.lab_btnCancel+'" onclick='+this.config.obj+'.MW_Method("no") style="cursor:hand" />'
			
			break;
		//类型为模式窗口
		case "window" :
			$("MW_content").innerHTML='<iframe name="MW_iframe" id="MW_iframe" align="center" frameborder="0" height="'+
									  this.config.height+'" width="488" scrolling="yes" src="'+this.config.url+'" ></iframe>'
			$("MW_control").style.display="none";
			break;
		default: 
			break;
	}			
	this.o=0;
	this.opacity=30;
	this.doAlpha;
	//打开miniWindow
	this.open=function(){
//		getBrowserDocument().style.overflow = "hidden";
//		alert("打开");
		var posLeft,posTop
		var winWidth = $('miniWindow').clientWidth;
		var winHeight = $('miniWindow').clientHeight;
		var pageScroll=getPageScroll();
		var pageSize=getPageSize();
//		alert(pageScroll);
//		alert(pageSize);
		//解决firefox下滚动条会复位
		var scrollTop = getBrowserDocument().scrollTop; 
		var scrollLeft = getBrowserDocument().scrollLeft;
		getBrowserDocument().scrollTop = scrollTop;   
		getBrowserDocument().scrollLeft = scrollLeft; 
		//初始化窗口位置
		
//		alert(document.body.clientHeight);
		
		posLeft=pageScroll[0]+(pageSize[2]-winWidth)/2;
		posTop=pageSize[1]/2;
		$('miniWindow').style.left =posLeft +"px";
		$('miniWindow').style.top = posTop + "px"; 
		$("miniWindow").style.display="block";
		//背景图
		$("MW_mask").style.display="block";
		$("MW_mask").width=pageSize[0];
		$("MW_mask").height=pageSize[1];
		this.hideSelect(true);
		//获得焦点
		$("miniWindow").focus();
		try{if($("MW_mask").filters) $("MW_mask").filters("alpha").opacity = 30;}catch(e){}
//		clearInterval(this.doAlpha)
//		this.doAlpha=setInterval(this.config.obj+".fadeIn()",this.config.speed) 
		
		if($("MW_input")!=null){
			$("MW_input").focus();
		}
	}
	//关闭miniWindow
	this.close=function(){
		this.reset();
		getBrowserDocument().style.overflow = "";
		this.hideSelect(false);
//		try{if($("MW_mask").filters) $("MW_mask").filters("alpha").opacity = 50;}catch(e){}
		$("MW_mask").style.display="none";
	}
	this.reset=function(){
		$("MW_title").innerHTML="";
		$("MW_content").innerHTML="";
		$("MW_control").innerHTML="";
		$("miniWindow").style.display="none";
	}
	//获取输入值
	this.getValue=function(){
		if($("MW_textarea")==null){
			if($("MW_input")==null){ 
				return false;
			}else{
				return ($("MW_input").value);
			}
		}else{
			return ($("MW_textarea").value);
		}
	}
	//设置输入框默认值
	this.setValue=function(value){
		if($("MW_textarea")!=null) $("MW_textarea").value=value;
		if($("MW_input")!=null) $("MW_input").value=value;
	}
	//设置窗口URL
	this.getURL=function(url){
		if($("MW_iframe")!=null) $("MW_iframe").src=url;
	}
	//隐藏所有select标签
	this.hideSelect=function(hide){
		var elemSelect = document.getElementsByTagName('select');
		var intN=elemSelect.length;	
		for(var i=0;i<intN;i++){
			if(hide){
				elemSelect[i].style.visibility="hidden";
			}else{
				elemSelect[i].style.visibility="visible";
			}
		}
	}
	//按钮事件
	this.MW_Method=function(method){
		this.callback(method)
	}
	//回调函数
	this.callback=function(args){this.close()}
}