// JavaScript Document
if(!CommonFunctions._isIE){
Event.prototype.__defineGetter__("srcElement",function(){
        var node=this.target;
        while(node.nodeType!=1)node=node.parentNode;
        return node;
        });
}
var oWins = [];
var BUTTONSPACE = 5;
var BUTTONSTARTRIGHT = 10;
//var SYSWINDEFAULTWIDTH = 300;
//var SYSWINDEFAULTHEIGHT = 150;
var xWin_styles = {
						win_mask:"syswinBar_mask",
						win:"syswinBar",
						win_fix:"syswinBar_fix",
						win_head:"syswinBar_head",
						win_body:"syswinBar_body",
						win_foot:"syswinBar_foot",
						win_minimizeButton:"minimize",
						win_maximizeButton:"maximize",
						win_closeButton:"close",
						win_buttongroup:"syswinBar_buttongroup",
						win_button:"button",
						win_top:"syswinBar_top",
						win_content:"syswinBar_content",
						win_bottom:"syswinBar_bottom",
						win_xt1:"xt1",
						win_xt2:"xt2",
						win_xt3:"xt3",
						win_xt4:"xt4",
						win_xb1:"xb1",
						win_xb2:"xb2",
						win_xb3:"xb3",
						win_xb4:"xb4"
					};
					
function xWin(title,width,height,panel,mask,dragable,minimizeButton,maximizeButton,closeButton,styles,btn1_value,btn1_fuc,btn2_value,btn2_fuc,btn3_value,btn3_fuc,cls_fuc){
	this.title = title;
	this.width = width == null ? "" : width;
	this.height = height == null ? "" : height;
	this.panel = panel == null ? "" : $(panel).innerHTML;
	if(panel != null) $(panel).innerHTML = "";
	this.mask = mask;
	this.dragable = dragable;
	this.minimizeButton = minimizeButton;
	this.maximizeButton = maximizeButton;
	this.closeButton = closeButton;
	this.styles = styles;
	this.btn1_value = btn1_value;
	this.btn2_value = btn2_value;
	this.btn3_value = btn3_value;
	this.btn1_fuc = btn1_fuc == null ? function(){} : btn1_fuc;
	this.btn2_fuc = btn2_fuc == null ? function(){} : btn2_fuc;
	this.btn3_fuc = btn3_fuc == null ? function(){} : btn3_fuc;
	this.cls_fuc = cls_fuc == null ? function(){} : cls_fuc;
	
	var oWin;
	var oWin_fix;
	var oWin_head;
	var oWin_head_mask;
	var oWin_body;
	var oWin_foot;
	var oTitle;
	var oCloseButton;
	var oMaximizeButton;
	var oMinimizeButton;
	//if(this.mask) var oMask;
	var oButtonGroup;
	var oBtn1;
	var oBtn2;
	var oBtn3;
	
	var originalWidth;
	var originalHeight;

	this.isMaximize = false;
	this.isMinimize = false;

	this.createToObj = function(obj){
		oWin = document.createElement('div');
		oWin_fix = document.createElement('div');
		oWin_head = document.createElement('div');
		oWin_head_mask = document.createElement('div');
		oWin_head_mask.style.background = "#fff";
		oWin_head_mask.style.position = "absolute";
		if(CommonFunctions._isIE){
			oWin_head_mask.style.filter = "alpha(opacity=0)";
		}else{
			oWin_head_mask.style.opacity = ".0";	
		}
		oWin_head.appendChild(oWin_head_mask);
		oWin_body = document.createElement('div');
		oWin_foot = document.createElement('div');
		oTitle = document.createElement('span');
		/*if(this.mask){
			oMask = new Mask(this.styles.win_mask,[0,CommonFunctions.getInnerSize()[0],0,CommonFunctions.getInnerSize()[1]);
			oMask.createToObj(obj);
		}*/
		oButtonGroup = document.createElement('div');
		if(btn1_fuc){
			oBtn1 = document.createElement('input');
			oBtn1.className = this.styles.win_button;
			oBtn1.type = "button";
			oBtn1.value = this.btn1_value;
			//oBtn1.onclick = function(){this.btn1_fuc();this.hide();}.bind(this)
			oBtn1.onclick = function(){this.btn1_fuc();}.bind(this)
			oButtonGroup.appendChild(oBtn1);
		}
		if(btn2_fuc){
			oBtn2 = document.createElement('input');
			oBtn2.className = this.styles.win_button;
			oBtn2.type = "button";
			oBtn2.value = this.btn2_value;
			oBtn2.onclick = function(){this.btn2_fuc();this.hide();}.bind(this)
			if(btn1_fuc) oButtonGroup.appendChild(document.createTextNode(" "));
			oButtonGroup.appendChild(oBtn2);
		}
		if(btn3_fuc){
			oBtn3 = document.createElement('input');
			oBtn3.className = this.styles.win_button;
			oBtn3.type = "button";
			oBtn3.value = this.btn3_value;
			oBtn3.onclick = function(){this.btn3_fuc();this.hide();}.bind(this)
			if(btn1_fuc || btn2_fuc) oButtonGroup.appendChild(document.createTextNode(" "));
			oButtonGroup.appendChild(oBtn3);
		}
		oTitle.innerHTML = this.title;
		
		oWin.className = this.styles.win;
		oWin_fix.className = this.styles.win_fix;
		oWin_head.className = this.styles.win_head;
		oWin_body.className = this.styles.win_body;
		oWin_body.innerHTML = this.panel;
		oWin_foot.className = this.styles.win_foot;	
		oButtonGroup.className = this.styles.win_buttongroup;	
		
		oWin.style.width = this.width != "" ? this.width + "px" : "auto";
		oWin.style.height = this.height != "" ? this.height + "px" : "auto";
		oWin_head.appendChild(oTitle);
		
		oWin_body.appendChild(oButtonGroup);
		
		var buttons = [];
		
		if(this.closeButton){
			oCloseButton = document.createElement('img');
			oCloseButton.border = 0;
			if(typeof(basePath) == "undefined") { 
				basePath = "..";
			}
			oCloseButton.src = basePath + "/static/default/image/close.gif";
			//oCloseButton.href = "javascript:void(0)";
			oCloseButton.className = this.styles.win_closeButton;
			oWin_head.appendChild(oCloseButton);
			buttons.push(oCloseButton);
			oCloseButton.onclick = function(){
				this.cls_fuc();
				oWin.style.display = "none";
		 		if(this.mask) oMask.hide();
				if(typeof(calendarDiv) != 'undefined' && calendarDiv != false){
					calendarDiv.style.display = 'none' ;
					if(iframeObj != false) iframeObj.style.display = 'none';
					if(iframeObj2 != false) iframeObj2.style.display = 'none';
				}
			}.bind(this)
		}
		if(this.maximizeButton){
			oMaximizeButton = document.createElement('a'); 
			oMaximizeButton.className = this.styles.win_maximizeButton;
			oWin_head.appendChild(oMaximizeButton);
			buttons.push(oMaximizeButton);
			oMaximizeButton.onclick = function(){
				this.maximizeSize();
			}.bind(this)
		}
		if(this.minimizeButton){
			oMinimizeButton = document.createElement('a');
			oMinimizeButton.className = this.styles.win_minimizeButton;
			oWin_head.appendChild(oMinimizeButton);
			buttons.push(oMinimizeButton);
			oMinimizeButton.onclick = function(){
				this.minimizeSize();
			}.bind(this)
		}
		

		oWin_fix.appendChild(oWin_head);
		oWin_fix.appendChild(oWin_body);
		oWin_fix.appendChild(oWin_foot);
		//oWin.appendChild(oWin_fix);
		
		var oWin_top = document.createElement('div');
		oWin_top.className = this.styles.win_top;
		var oWin_xt1 = document.createElement('div');
		oWin_xt1.className = this.styles.win_xt1;
		var oWin_xt2 = document.createElement('div');
		oWin_xt2.className = this.styles.win_xt2;
		var oWin_xt3 = document.createElement('div');
		oWin_xt3.className = this.styles.win_xt3;
		var oWin_xt4 = document.createElement('div');
		oWin_xt4.className = this.styles.win_xt4;
		
		var oWin_content = document.createElement('div');
		oWin_content.className = this.styles.win_content;
		
		var oWin_bottom = document.createElement('div');
		oWin_bottom.className = this.styles.win_bottom;
		var oWin_xb1 = document.createElement('div');
		oWin_xb1.className = this.styles.win_xb1;
		var oWin_xb2 = document.createElement('div');
		oWin_xb2.className = this.styles.win_xb2;
		var oWin_xb3 = document.createElement('div');
		oWin_xb3.className = this.styles.win_xb3;
		var oWin_xb4 = document.createElement('div');
		oWin_xb4.className = this.styles.win_xb4;
		
		oWin_top.appendChild(oWin_xt1);
		oWin_top.appendChild(oWin_xt2);
		oWin_top.appendChild(oWin_xt3);
		oWin_top.appendChild(oWin_xt4);
		
		oWin_bottom.appendChild(oWin_xb4);
		oWin_bottom.appendChild(oWin_xb3);
		oWin_bottom.appendChild(oWin_xb2);
		oWin_bottom.appendChild(oWin_xb1);
		
		oWin_content.appendChild(oWin_fix);
		oWin.appendChild(oWin_top);
		oWin.appendChild(oWin_content);
		oWin.appendChild(oWin_bottom);
		
		//var pstr = '<div> <div class="syswinBar_top"> <div class="xt1"></div> <div class="xt2"></div> <div class="xt3"></div> <div class="xt4"></div> </div> <div class="syswinBar_content"> ' + oWin_fix.outerHTML + '</div> <div class="syswinBar_bottom"> <div class="xb4"></div> <div class="xb3"></div> <div class="xb2"></div> <div class="xb1"></div> </div> '
		//oWin.innerHTML += pstr;
		obj.appendChild(oWin);
		oWin_head_mask.style.width = oWin_head.offsetWidth - 40 + "px";
		oWin_head_mask.style.height = oWin_head.offsetHeight + "px";
		
		if(this.mask) oMask.setRange([0,CommonFunctions.getInnerSize()[0] + CommonFunctions.getScrollSize()[0],0,CommonFunctions.getInnerSize()[1] + CommonFunctions.getScrollSize()[1]]);
		
		for(var i = 0; i < buttons.length; i++){
			buttons[i].style.right = (BUTTONSPACE + buttons[i].offsetWidth) * i + BUTTONSTARTRIGHT + "px";
		}
		
		oWin_body.style.width = oWin.offsetWidth - 2 + "px";
		//oWin_body.style.height = oWin.offsetHeight - oWin_head.offsetHeight - oWin_foot.offsetHeight - 2 + "px";
		
		//originalX = this.getPosition().x;
		//originalY = this.getPosition().y;
		
		originalWidth = this.getSize()[0];
		originalHeight = this.getSize()[1];
		
		if(this.dragable){
			oWin_body.style.cursor = oWin_head.style.cursor = "move";
			oWin_body.onmousedown = oWin_head.onmousedown = function(e){
				//var ev = event || window.event;					
				//var x = ev.layerX || ev.offsetX,y = ev.layerY || ev.offsetY;
				if(!e) e = window.event; 
				var posX = e.clientX - parseInt(oWin.offsetLeft);
				var posY = e.clientY - parseInt(oWin.offsetTop);
				//var innerSize = CommonFunctions.getInnerSize();
				//var range = [0,CommonFunctions.getInnerSize()[0] + CommonFunctions.getScrollSize()[0] - oWin.offsetWidth,y,CommonFunctions.getInnerSize()[1] + CommonFunctions.getScrollSize()[1] - oWin.offsetHeight + y];
				var range = [-1 * oWin.offsetWidth,CommonFunctions.getInnerSize()[0] + CommonFunctions.getScrollSize()[0],-1 * oWin.offsetHeight,CommonFunctions.getInnerSize()[1] + CommonFunctions.getScrollSize()[1]];
				if (['INPUT', 'TEXTAREA', 'SELECT'].include(e.srcElement.tagName)) {
					//
					e.srcElement.style.cursor = "default";
				} else {
					document.onmousemove = function(ev){
						if(ev == null) ev = window.event;
						/*var px = CommonFunctions.getPointer(ev).x;
						var py = CommonFunctions.getPointer(ev).y;
						var hp = vp = 0;
						var hx = px - x;
						var vy = py - y;
						hp = (hx <= range[0] ? range[0] : hx >= range[1] ? range[1] : hx);
						vp = (vy <= range[2] ? range[2] : vy >= range[3] ? range[3] : vy);*/
					 
						//oWin.style.left = hp + "px";
						//oWin.style.top = vp + "px";
						oWin.style.left = (ev.clientX - posX) + "px"; 
	   					oWin.style.top = (ev.clientY - posY) + "px";
						
						/*if(CommonFunctions._isIE){
							document.onselectstart = function(){return false};
						}*/
						
					}
	
					document.onmouseup = function(ev){
						document.onmousemove = null;
						document.onmouseup = null;
						/*if(CommonFunctions._isIE){
							document.onselectstart = function(){return true};
						}*/
					}
				}
			}
		}else{
			oWin_body.style.cursor = oWin_head.style.cursor = "default";
		}
		this.hide();
		oWins.push(this);
	}.bind(this)
	
	this.hide = function(){
                   oWin.style.display = "none";
         }.bind(this)
         
         this.returnObject = function(){
                   return oWin;
         }

	
	this.show = function(left, top){
                   if(typeof(oSummaryBlocks) != 'undefined'){
                            for(var i = 0; i < oSummaryBlocks.length; i++){
                                     oSummaryBlocks[i].hide();
                            }
                   }
                   oWin.style.display = "";
                   if(this.mask) oMask.show();
                   if(typeof(left) != "undefined" && typeof(top) != "undefined"){
                            this.setPosition(left, top);
                   }else{
                            this.setPosition((CommonFunctions.getInnerSize()[0] - oWin.offsetWidth) / 2 + CommonFunctions.getScrollSize()[0],(CommonFunctions.getInnerSize()[1] - oWin.offsetHeight) / 2 + CommonFunctions.getScrollSize()[1]);
                   }
                   
                   function resetWins(){
                            oWin.style.left = (CommonFunctions.getInnerSize()[0] - oWin.offsetWidth) / 2 + CommonFunctions.getScrollSize()[0] + "px";
                            oWin.style.top = (CommonFunctions.getInnerSize()[1] - oWin.offsetHeight) / 2 + CommonFunctions.getScrollSize()[1] + "px";
                            if(oMask != "undefined") oMask.setRange([0,CommonFunctions.getInnerSize()[0] + CommonFunctions.getScrollSize()[0],0,CommonFunctions.getInnerSize()[1] + CommonFunctions.getScrollSize()[1]]);
                   }
                   
                   if(window.addEventListener){
                            window.addEventListener('resize', resetWins, false);
                   }else if(window.attachEvent){
                            window.attachEvent('onresize', resetWins);
                   }
                   if(window.addEventListener){
                            window.addEventListener('scroll', resetWins, false);
                   }else if(window.attachEvent){
                            window.attachEvent('onscroll', resetWins);
                   }
         }.bind(this)

	
	this.setPosition = function(x,y){
		oWin.style.left = x + "px";
		oWin.style.top = y + "px";
		if(this.mask) oMask.setRange([0,CommonFunctions.getInnerSize()[0] + CommonFunctions.getScrollSize()[0],0,CommonFunctions.getInnerSize()[1] + CommonFunctions.getScrollSize()[1]]);
	}.bind(this)
	
	this.getPosition = function(){
		var x = CommonFunctions.getAbsolutePoint(oWin).l;
		var y = CommonFunctions.getAbsolutePoint(oWin).t
		return {"x":x , "y":y};
	}
	
	this.maximizeSize = function(){
		oWin_body.style.display = "";
		if(this.isMaximize){
			this.setPosition((CommonFunctions.getInnerSize()[0] - originalWidth) / 2,(CommonFunctions.getInnerSize()[1] - originalHeight) / 2);
			this.setSize(originalWidth,originalHeight);
			this.isMaximize = false;
		}else{
			this.setPosition(0,0);
			this.setSize(CommonFunctions.getInnerSize()[0],CommonFunctions.getInnerSize()[1]);
			this.isMaximize = true;
		}
		this.isMinimize = false;
	}.bind(this)
	
	this.minimizeSize = function(){
		this.setSize(originalWidth,oWin_head.offsetHeight + oWin_foot.offsetHeight + 2);
		oWin_body.style.display = "none";
		this.setPosition(0,CommonFunctions.getInnerSize()[1] - oWin.offsetHeight);
		this.isMinimize = true;
		this.isMaximize = false;
	}.bind(this)
	
	this.setSize = function(w,h){
		this.width = w;
		this.height = h;
		oWin.style.width = oWin.style.height = "";
		oWin_body.style.width = oWin_body.style.height = "";
		oWin_fix.style.width = oWin_fix.style.height = "";
		
		//oWin.style.width = this.width != "" ? this.width + "px" : "auto";
		//oWin.style.height = this.height != "" ? this.height + "px" : "auto";
		
		oWin_body.style.width = oWin.offsetWidth - 2 + "px";
		//oWin_body.style.height = oWin.offsetHeight - oWin_head.offsetHeight - oWin_foot.offsetHeight - 2 + "px";
	
		if(this.mask) oMask.setRange([0,CommonFunctions.getInnerSize()[0] + CommonFunctions.getScrollSize()[0],0,CommonFunctions.getInnerSize()[1] + CommonFunctions.getScrollSize()[1]]);
	}.bind(this)
	
	this.getSize = function(){
		return [this.width,this.height];
	}.bind(this)
	
	this.returnObject = function(){
		return oWin;
	}
}

function Mask(styleName,range){
	this.styleName = styleName;
	this.range = range;
	var oMask;
	if(CommonFunctions._isIE) var oIframe;
	var object;
	
	this.createToObj = function(obj){
		object = document.createElement('div');
		if(CommonFunctions._isIE){
			oIframe = document.createElement('iframe');
			oIframe.className = this.styleName + "_iframe";
			oIframe.style.left = this.range[0] + "px";
			oIframe.style.top = this.range[2] + "px";
			oIframe.style.width = this.range[1] + "px";
			oIframe.style.height = this.range[3] + "px";
			object.appendChild(oIframe);
		}
		oMask = document.createElement('div');
		oMask.className = this.styleName;
		this.setRange(this.range);
		object.appendChild(oMask);
		obj.appendChild(object);
		this.hide();
	}.bind(this)
	
	this.setRange = function(r){
		oMask.style.left = r[0] + "px";
		oMask.style.top = r[2] + "px";
		oMask.style.width = r[1] + "px";
		oMask.style.height = r[3] + "px";
		if(CommonFunctions._isIE){
			oIframe.style.width = r[1] + "px";
			oIframe.style.height = r[3] + "px";
		}
	}
	
	this.getRange = function(){
		return [this.range[0],this.range[1],this.range[2],this.range[3]];
	}.bind(this)
	
	this.returnObject = function(){
		return object;
	}
	
	this.hide = function(){
		object.style.display = "none";
		document.getElementsByTagName("html")[0].style.overflow = "";
	}
	
	this.show = function(){
		object.style.display = "";
		document.getElementsByTagName("html")[0].style.overflow = "hidden";
	}
}