derive(PanelWindow, ClassBase);
function PanelWindow() {
   
    this.OnShow = null;
    this.OnClose = null;
    
    this.CloseOnFocusOut = true;
    this.HorizontalAlign = 'NoSet';
    this.VerticalAlign = 'NoSet';
    
    this.IsClose = true;

	this._interval = null;
	this._ant_element = null;
	
	this._dragging = false;
	this._init_coord = null;
	this._init_pos = null;
    
    this.set_Caption = function(aValue) {
	    var lref = document.getElementById(this.ClientId).firstChild;
	    while(lref.tagName != 'TABLE') lref = lref.nextSibling;
	    lref.rows[0].cells[1].firstChild.rows[0].cells[0].innerHTML = aValue;
    }
    
	this.ProcInterval = function() {
		var lelem = this.get_FocusElement();
		if(this._ant_element != lelem)
		{
			this.FocusChange(lelem);
			this._ant_element = lelem;
		}
	}
	
	this.get_FocusElement = function() {
		if(Browser_obj.is_Msie() && !Browser_obj.is_Opera()) {
			return document.activeElement;
		}else{
			var sel = this.get_Selection();
			var rng = this.get_Range();
	
			if (!sel || !rng)
				return null;
	
			var elm = rng.commonAncestorContainer;
	
			// Handle selection a image or other control like element such as anchors
			if (!rng.collapsed) {
				// Is selection small
				if (rng.startContainer == rng.endContainer) {
					if (rng.startOffset - rng.endOffset < 2) {
						if (rng.startContainer.hasChildNodes())
							elm = rng.startContainer.childNodes[rng.startOffset];
					}
				}
			}
			// Get the element parent of the node
			elm = this.get_ParentElement(elm);
			return elm;
		}	
	}
	
	this.set_FocusElement = function(aRef) {
		if(Browser_obj.is_Msie())
			aRef.focus();
		else
		{
			var sel = this.get_Selection();
			sel.removeAllRanges();
			var rng = document.createRange();
			rng.setEnd(aRef,0);
			rng.setStart(aRef,0);
			sel.addRange(rng);
		}
	}
	
	this.get_ParentElement = function(node, names, attrib_name, attrib_value) {
	    if (typeof(names) == "undefined") {
			if (node.nodeType == 1)
				return node;
			// Find parent node that is a element
			while ((node = node.parentNode) != null && node.nodeType != 1) ;
			return node;
		}
	
		if (node == null)
			return null;
	
		var namesAr = names.toUpperCase().split(',');
		do {
			for (var i=0; i<namesAr.length; i++) {
				if (node.nodeName == namesAr[i] || names == "*") {
					if (typeof(attrib_name) == "undefined")
						return node;
					else if (node.getAttribute(attrib_name)) {
						if (typeof(attrib_value) == "undefined") {
							if (node.getAttribute(attrib_name) != "")
								return node;
						} else if (node.getAttribute(attrib_name) == attrib_value)
							return node;
					}
				}
			}
		} while ((node = node.parentNode) != null);
	
		return null;
	}
	
	this.get_Selection = function() {
	    if (Browser_obj.is_Msie() && !Browser_obj.is_Opera())
		    return document.selection;
		return window.getSelection();    
	}
	
	this.get_Range = function() {
		var sel = this.get_Selection();
		if (sel == null)
			return null;
		if (Browser_obj.is_Msie() && !Browser_obj.is_Opera())
			return sel.createRange();
		if (Browser_obj.is_Safari() && !sel.getRangeAt)
			return '' + window.getSelection();
		var rng = null;
		try
		{
			rng = sel.getRangeAt(0);
		}
		catch(e)
		{
			rng = null;
		}  
		return rng;
	}
	
	this.Close = function() {
	    if(this.CloseOnFocusOut)
		    clearInterval(this._interval);
		var ltop = Page_obj._arr_div_window.pop();
		while(ltop != null && ltop != this.ClientId) {
			eval(ltop+'_obj.Close()');
			ltop = Page_obj._arr_div_window.pop();
		}
		if(Page_obj._arr_div_window.length > 0) {
		    ltop = Page_obj._arr_div_window[Page_obj._arr_div_window.length-1];
		    var lref_top = document.getElementById(ltop);
		    if(lref_top)
		        this.set_FocusElement(lref_top);
		}
		var lref = document.getElementById(this.ClientId);
		lref.style.display = 'none';
		lref.style.zIndex = '';
		this._interval = null;
		this._ant_element = null;
		this._dragging = false;
		this.IsClose = true;
		if(Browser_obj.is_Msie())
			Util_obj.RestoreSelect(lref);
		if(this.OnClose)
		    this.OnClose();
	}
	
	this.AutomaticClose = function(aElem) {
		var ldiv = Util_obj.get_DivParent(aElem);
		if(ldiv == null) {
			if(Page_obj._arr_div_window.length > 0)
				eval(Page_obj._arr_div_window[0]+'_obj.Close()');
		} else {
			var lsel_index = -1;
			for(var lindex = 0; lindex < Page_obj._arr_div_window.length; lindex++) {
				if(Page_obj._arr_div_window[lindex] == ldiv.id) {
					lsel_index = lindex;
					break;
				}
			}
			lsel_index++;
			if(lsel_index < Page_obj._arr_div_window.length)
				eval(Page_obj._arr_div_window[lsel_index]+'_obj.Close()');
		}
	}
	
	this.FocusChange = function(aNewElement) {
		var ltop = Page_obj._arr_div_window.length > 0 ? Page_obj._arr_div_window[Page_obj._arr_div_window.length-1] : null; 
	    var lDiv = Util_obj.get_DivParent(this._ant_element,ltop);
	    if(ltop && aNewElement && lDiv != null && (Browser_obj.is_Msie() || aNewElement.tagName != 'BODY') && Util_obj.get_DivParent(aNewElement,ltop) == null) {
	    	this.AutomaticClose(aNewElement);
		}	
	}
		
	this.Show = function(aElem, aDifLeft, aDifTop, aCaption) {

	    var lref = document.getElementById(this.ClientId);
	    if(lref.parentNode != document.forms[0])
		    document.forms[0].appendChild(lref);
			
		this.AutomaticClose(aElem);

		var lPos = null;
		if(this.HorizontalAlign == 'NotSet' || this.VerticalAlign == 'NotSet')
		{
		    lPos = Util_obj.get_ElementPosition(aElem);
		    lPos.top = lPos.top + aElem.offsetHeight + aDifTop;
		    lPos.left = lPos.left + aDifLeft;
		    if(this.VerticalAlign == 'NotSet')
		        lref.style.top = lPos.top + 'px';
		    if(this.HorizontalAlign == 'NotSet')
    		    lref.style.left = lPos.left + 'px';
        }
        //if(Page_obj._arr_div_window.length > 0)
            //lref.style.zIndex = Page_obj._arr_div_window.length;
        lref.style.zIndex = 40000 + Page_obj._arr_div_window.length;
	    lref.style.display = '';
	    if(lref.offsetParent && (this.HorizontalAlign == 'NotSet' || this.VerticalAlign == 'NotSet'))
	    {
			var lHeight = Util_obj.get_ClientHeight();//window.innerHeight?window.innerHeight:lref.offsetParent.clientHeight;
		    lHeight += window.scrollY?window.scrollY:(document.body.parentElement && document.body.parentElement.scrollTop ? document.body.parentElement.scrollTop : lref.offsetParent.scrollTop); //window.scrollY?window.scrollY:lref.offsetParent.scrollTop;
		    var lWidth = Util_obj.get_ClientWidth();//window.scrollY?window.scrollY:(document.body.parentElement && document.body.parentElement.scrollTop ? document.body.parentElement.scrollTop : lref.offsetParent.scrollTop);//window.innerWidth?window.innerWidth:lref.offsetParent.clientWidth;	
		    lWidth += window.scrollX?window.scrollX:(document.body.parentElement && document.body.parentElement.scrollLeft ? document.body.parentElement.scrollLeft : lref.offsetParent.scrollLeft);//window.scrollX?window.scrollX:lref.offsetParent.scrollLeft;		    lWidth += window.scrollX?window.scrollX:lref.offsetParent.scrollLeft;
		    if(lref.offsetWidth + lref.offsetLeft > lWidth && this.HorizontalAlign == 'NotSet')
			    lref.style.left = (lPos.left - (lref.offsetWidth + lref.offsetLeft - lWidth) - (!Browser_obj.is_Msie() ? 20 : 1)) + 'px';
		    if(lref.offsetHeight + lref.offsetTop > lHeight && this.VerticalAlign == 'NotSet')
			    lref.style.top = (lPos.top - (lref.offsetHeight + lref.offsetTop - lHeight) - (!Browser_obj.is_Msie() ? 20 : 1)) + 'px';
	    }
		if(this.HorizontalAlign != 'NotSet') {
		    var lWidth = Util_obj.get_ClientWidth(); //window.innerWidth?window.innerWidth:lref.offsetParent.clientWidth - (!(Browser_obj.is_Msie()) ? 20 : 1);
		    var lScroll = window.scrollX?window.scrollX:(document.body.parentElement && document.body.parentElement.scrollLeft ? document.body.parentElement.scrollLeft : lref.offsetParent.scrollLeft);
		    if(this.HorizontalAlign == 'Left')
		        lref.style.left = lScroll + 'px';
		    else if(this.HorizontalAlign == 'Center')
		        lref.style.left = (lScroll + Math.round(lWidth / 2) - Math.round(lref.offsetWidth / 2)) + 'px';
		    else if(this.HorizontalAlign == 'Right')
		        lref.style.left = (lScroll + lWidth - lref.offsetWidth) + 'px';
		}
		if(this.VerticalAlign != 'NotSet') {
    	    var lHeight = Util_obj.get_ClientHeight(); //window.innerHeight?window.innerHeight:lref.offsetParent.offsetHeight - (!Browser_obj.is_Msie() ? 20 : 1);
    	    var lScroll = window.scrollY?window.scrollY:(document.body.parentElement && document.body.parentElement.scrollTop ? document.body.parentElement.scrollTop : lref.offsetParent.scrollTop);
		    if(this.VerticalAlign == 'Top')
		        lref.style.top = lScroll + 'px';
		    else if(this.VerticalAlign == 'Middle') {
		        if(lScroll + Math.round(lHeight / 2) - Math.round(lref.offsetHeight / 2) - (Browser_obj.is_Msie() ? 2 : 0) < 0)
		            lref.style.top = '0px';
		        else
		            lref.style.top = (lScroll + Math.round(lHeight / 2) - Math.round(lref.offsetHeight / 2) - (Browser_obj.is_Msie() ? 2 : 0)) + 'px';
		    } else if(this.VerticalAlign == 'Right')
		        lref.style.top = (lScroll + lHeight - lref.offsetHeight) + 'px';
		}
		
		lref.setAttribute('mydiv_id', this.ClientId);
		if(aCaption)
		    this.set_Caption(aCaption);
		
		Page_obj._arr_div_window.push(this.ClientId);
		this.set_FocusElement(lref);
		if(this.CloseOnFocusOut)
		    this._interval = setInterval(this.ClientId + '_obj.ProcInterval();',1000);
		if(this.OnShow != null)
		    this.OnShow();
		this.IsClose = false;
	
		if(Browser_obj.is_Msie())
			Util_obj.HideSelect(lref);
	}
	
	this.StartDrag = function(aEvent) {
	    this._init_coord = Util_obj.get_MousePosition(aEvent);
	    var lref = document.getElementById(this.ClientId);
	    this._init_pos = {left: lref.offsetLeft, top: lref.offsetTop};
	    this._dragging = true;
	    Page_obj._active_dragging = this;
	    Util_obj.AddEvent(document.body,'mousemove',function(aEvent){if(typeof(Page_obj)!='undefined')Page_obj._active_dragging.DoDrag(aEvent);});
	}
	
	this.DoDrag = function(aEvent) {
	    if(this._dragging) {
	        var lcoord = Util_obj.get_MousePosition(aEvent);
	        var lref = document.getElementById(this.ClientId);
	        var ldifx = lcoord.left - this._init_coord.left;
	        var ldify = lcoord.top - this._init_coord.top;
		    if(Browser_obj.is_Msie())
			    Util_obj.RestoreSelect(lref);
	        lref.style.top = (this._init_pos.top + ldify) + 'px';
	        lref.style.left = (this._init_pos.left + ldifx) + 'px';
		    if(Browser_obj.is_Msie())
			    Util_obj.HideSelect(lref);
	    }
	}
	
	this.StopDrag = function(aEvent) {
	    this._dragging = false;
	    Util_obj.RemoveEvent(document.body,'mousemove',function(aEvent){if(typeof(Page_obj)!='undefined')Page_obj._active_dragging.DoDrag(aEvent);});
	}
	
	this.set_Width = function(aValue) {
	    if(aValue) {
            var lref = document.getElementById(this.ClientId);
            lref.style.width = aValue;
        }
        return 0;
	}

	this.set_Height = function(aValue) {
	    if(aValue) {
            var lref = document.getElementById(this.ClientId);
            lref.style.height = aValue;
        }
        return 0;
	}
}

