
/* global variables for window */

window._popModal = null; /* for modal child */
window._popModalFunc = null; /* for modal child */
window._Popups = new Object(); /* for children windows (hash) */

function capwin(w) {
//    _AddEventListener(w, "click", _parentEvent);
//    _AddEventListener(w, "mousedown", _parentEvent);
//    _AddEventListener(w, "focus", _parentEvent);

    _AddEventListener(w, "mouseup", _parentEvent);
};
function relwin(w) {
//    _RemoveEventListener(w, "click", _parentEvent);
//    _RemoveEventListener(w, "mousedown", _parentEvent);
//    _RemoveEventListener(w, "focus", _parentEvent);
};

function _parentEvent(evt){
    if(window._popModal){
	window._popModal.focus();
    }else{
	//autohide windows
	for(var k in window._Popups){
	    if(window._Popups[k].autoHide){
		if(window._Popups[k].type == 'win' || window._Popups[k].type == 'iframe'){
		    window._Popups[k].hide();
		} else if(window._Popups[k].type == 'div'){


		    if (document.all) { // Need to hard-code this to trap IE for error-handling
			var t = window.event.srcElement;
			var isClicked = false;
			while (t.parentElement != null) {
			    if (t.id==window._Popups[k].name) {
//					return true;
					isClicked = true;
					break;
					//clicked
			    }
			    t = t.parentElement;
			}
			//return false;
			if(!isClicked)window._Popups[k].hide();
		    }else {
			var t = evt.originalTarget;
			var isClicked = false;
			while (t.parentNode != null) {
			    if (t.id==window._Popups[k].name) {
//					return true;
					//clicked - skip
					isClicked = true;
					break;
			    }
			    t = t.parentNode;
			}
//			return false;

			if(!isClicked)window._Popups[k].hide();
		    }



		
		}
	    }
	}
    }
}

capwin(document);
// capture other frames
for (var i = 0; i < window.frames.length; capwin(window.frames[i++]));

/*
    var w_dlg = new _Popup('dlg1','win');
*/
function _Popup(name,type){
    this.type = type;
    this.name = name;
    this.handler = null;
    this.url = '';
    this.contents = '';
    this.visible = false;
    this.top = 0;
    this.left = 0;
    this.window_properties = ''; //for 'win' only - additional parameters
//    this.width = 40;
//    this.height = 40;

    this.autoHide = false;
    
    if(this.type == 'iframe' || this.type == 'div'){
	//autosize
	this.width=_Width(this.name);
	this.height=_Height(this.name);
    }

    if(this.type == 'iframe'){
/*    
	if(!_GetElementById(this.name)){
	    var i = document.createElement(this.type);
	    i.id=this.name;
	    i.frameBorder=0;
	    i.style.position='absolute';
	    i.style.display='none';
	    document.body.appendChild(i);
	    
	}
*/
    }


/*--------------------------------------------------------------------------------------*/
    /*protected*/

    /*public*/
    this.show = function (param){
	if(param) window._popModalParam = param;
	if(this.type == 'win'){
	    var winprop = //'statusbar=no,scrollbars=no'
	                +((this.width)?',width='+this.width:'')
			+((this.height)?',height='+this.height:'')
			+((this.left)?',left='+this.left+',screenX='+this.left:'')
			+((this.top)?',top='+this.top+',screenY='+this.top:'');
	    if(this.window_properties){
		winprop += ","+this.window_properties;
	    }else{
		winprop += ",statusbar=no,scrollbars=no,toolbar=no,resizable=yes";
	    }

	    if(!this.handler || (this.handler && this.handler.closed)) {
		this.handler = window.open('',this.name, winprop);
	    }
	}else if(this.type == 'div'){
	    this.handler = _GetElementById(this.name);
	    _Show(this.handler);
	}else if(this.type == 'iframe'){
	    this.handler = window.frames[this.name];
	    _Show(this.name);
	    var _win = (this.handler.contentWindow)? this.handler.contentWindow:this.handler.window;
	    _win.name = this.name;
    
	}

	this.resizeTo();
	this.moveTo();

	if(this.url) this.setUrl();
	if(this.contents) this.setContents();



	this.visible = true;
    }
/*--------------------------------------------------------------------------------------*/
    this.showModal = function (param,func){
	if(!this.autoHide){
	    window._popModal = this;   
	}
	window._popModalFunc = func;
	this.show(param);
    }

/*--------------------------------------------------------------------------------------*/
    this.hide = function (){
	if(this.handler){
//	    # проверяем наличие onhide
	    if(this.type == 'win'){
		this.handler.close();
		this.handler = null;
	    }else if(this.type == 'div'){
		_Hide(this.handler);
		this.handler = null;
	    }else if(this.type == 'iframe'){
		_Hide(this.name);
		this.handler = null;
	    }
	    if(window._popModal == this){
		window._popModal = null;
//	    	relwin(window);
		// capture other frames
//	    	for (var i = 0; i < window.frames.length; relwin(window.frames[i++]));
	    }
	    this.visible = false;
	}
    
    }

/*--------------------------------------------------------------------------------------*/
    this.setUrl = function(url){
	// for iframe & win
	if(_Str(url)) this.url = url;
	if(this.handler){
	    if(this.type == 'win'){
		this.handler.location.href = this.url;
	    }else if(this.type == 'iframe'){
		var w = (this.handler.contentWindow)? this.handler.contentWindow:this.handler.window;
//		alert(this.url);
		w.location.href = this.url;
	    }
	}
    }

/*--------------------------------------------------------------------------------------*/
    this.setContents = function(contents){
	if(_Str(contents)) this.contents = contents;
	if(this.handler){
	    if(this.type == 'win'){
		this.handler.document.open();
		this.handler.document.writeln(this.contents);
		this.handler.document.close();
	    }else if(this.type == 'iframe'){
		var w = (this.handler.contentWindow)? this.handler.contentWindow:this.handler.window;
		w.document.open();
		w.document.writeln(this.contents);
		w.document.close();
	    }else if(this.type == 'div'){
		_InnerHTML(this.handler, this.contents);
	    }
	}
    }

/*--------------------------------------------------------------------------------------*/
    this.resizeTo = function(w,h){
	if(_Num(w) && _Num(h)){
	    this.width=w;
	    this.height=h;
	}
	if(this.handler){
	    if(this.type == 'win'){
		if(this.width && this.height){
		    this.handler.resizeTo(this.width,this.height);
		}
	    }else if(this.type == 'div'){
		if(this.width && this.height){
		    _ResizeTo(this.handler,this.width,this.height);
		}
	    }else if(this.type == 'iframe'){
		if(this.width && this.height){
		    _ResizeTo(this.name,this.width,this.height);
		}
	    }
	}
    }
/*--------------------------------------------------------------------------------------*/
    this.moveTo = function(l,t){
	if(_Num(l) && _Num(t)){
	    this.left=l;
	    this.top=t;
	}
	if(this.handler){
	    if(this.type == 'win'){
		if(_Num(this.top, this.left)){
		    this.handler.moveTo(this.left,this.top);
		}
	    }else if(this.type == 'div'){
		if(_Num(this.top,this.left)){
		    _MoveTo(this.handler,this.left,this.top);
		}
	    }else if(this.type == 'iframe'){
		if(_Num(this.top,this.left)){
		    _MoveTo(this.name,this.left,this.top);
		}
	    }
	}
    }
/*--------------------------------------------------------------------------------------*/
    this.moveBy = function(dL,dT){
	this.left = this.left + dL;	
	this.top = this.top + dT;	
    }

/*--------------------------------------------------------------------------------------*/
    this.focus = function(){
	if(this.handler){
	    if(this.type == 'win'){
		if(!this.handler.closed){
		    this.handler.focus();
		}else{
		    if(window._popModal == this){
			//modal window closed
			window._popModal = null;
			window._popModalFunc = null;
		    }
		}
	    }else if(this.type=='iframe'){
		var w = (this.handler.contentWindow)? this.handler.contentWindow:this.handler.window;
	    }
	    
	}
	
    }

/*--------------------------------------------------------------------------------------*/
    this.moveToCenter = function(){
	if(this.type == 'win'){
	    if(screen){
		this.moveTo((screen.availWidth - this.width) / 2,(screen.availHeight - this.height) / 2);
	    }
	}else{
	    var y = _ScrollTop(window) + (_ClientHeight(window) - this.height) / 2;
	    this.moveTo(parseInt((_ClientWidth(window) - this.width) / 2), y);
	    
	}
    }

/*--------------------------------------------------------------------------------------*/
    this.moveToElement = function(e){
	/*taking page position*/
	var top = _OffsetTop(e);
	var left = _OffsetLeft(e);
	if(this.type == 'win'){
	    if(document.getElementById) {
		if(isNaN(window.screenX)) {
		    left = left-document.body.scrollLeft+window.screenLeft;
		    top = top-document.body.scrollTop+window.screenTop;
		}else{
		    left = left+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
		    top = top+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
		}
	    }else if(document.all){
		left = left-document.body.scrollLeft+window.screenLeft;
		top = top-document.body.scrollTop+window.screenTop;
	    }
	}
	this.moveTo(left,top);

    }

    this.isShowing = function(){
	return this.handler != null;
    }
/*--------------------------------------------------------------------------------------*/
    window._Popups[name] = this;
}
/*
window.onerror = function(msg,url,line){
    var err = new _Popup('err','win');
    err.resizeTo(600,250);
    err.moveToCenter();
    err.setContents('<html><head><title>JS error</title><body><h1>Javascript error</h1><b>Message:</b> '+msg+'<br><b>in file:</b> ' + url + '<br><b>at line:</b> ' + line + '<br><br><b>User-Agent:</b> ' + navigator.userAgent +'</body></html>');
    err.show();    
    return true;
}
*/
