//************************************************************
//
//************************************************************
//CPopupBox box
function CPopupBox(id, content, showClose) {
    this.divId = id;
    this.div = null;
    this.divContent = null;
    this.loaderId = 'loader';
    this.loader = null;
    this.center = {X:0, Y:0};
    this.size = {width:0, height:0};
    this.interval = 100;
    this.timerID = null;
    this.clearOnClose = false; //

    this.showClose = typeof(showClose) == 'undefined' ? true : showClose;
    this.onshow = null;
    this.onhide = null;
    this.onclose = null;
    this.isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;


    this.setShowClose = function(show) {
        this.showClose = show;
    }
    this.setClearOnClose = function(closed) {
        this.clearOnClose = closed;
    }
    this.close = function() {
        this.hide();

        if(this.clearOnClose && this.divContent)
            this.divContent.innerHTML = '';

        if(null != this.onclose) this.onclose();
    }

    this.show = function() {
        var box = null;
	    if(!(this.getBox()))return;

        var h=document.body.offsetHeight;

        var ov = document.getElementById('overlay')
        if(ov) ov.style.display='block';
        if(ov) ov.style.height=h+'px';
        if(this.loader) this.loader.style.display='none';
        this.div.style.position = 'absolute';
        this.div.style.display = 'block';

        if ((parent_popup_obj && this.divId != parent_popup_obj.divId && this.divId != error_popup_obj.divId ) || (!parent_popup_obj && this.divId != error_popup_obj.divId))
        {
            eval("parent_popup_obj = popup_obj_"+this.divId);
        }


        this.calculateCenter();
        this.checkResize(1);

        var oThis = this;
        if (this.showClose) {
		    document.onkeydown = function(e){oThis.checkKeyPress(e);};
        }
        if(null != this.onshow) this.onshow();
    }

    this.hide = function() {
        if(!this.getBox())return;
        this.div.style.display = 'none';
        var ov = document.getElementById('overlay')
        if(ov) ov.style.display='none';
        if(this.loader) this.loader.style.display='none';
        clearTimeout(this.timerID);
        this.timerID = null;
		document.onkeydown = "";
        if(null != this.onhide) this.onhide();
    }

    this.hideLoader = function()
    {
        if(this.loader) this.loader.style.display='none';
    }

    this.showLoader = function() {
        var box = null;
	    if(!(this.getLoader()))return;

        var h=document.body.offsetHeight;
        var ov = document.getElementById('overlay')
        if(ov) ov.style.display='block';
        if(ov) ov.style.height=h+'px';
        this.loader.style.display='block';
        this.calculateCenter();
        this.loader.style.top=(this.center.Y-16)+'px';
        this.loader.style.left=(this.center.X-16)+'px';
        var oThis = this;
		document.onkeydown = function(e){oThis.checkKeyPress(e);};
    }

    this.checkKeyPress = function(e) {
        var Esc = (e == null) ? 27 : e.DOM_VK_ESCAPE; // MSIE : Firefox
        //keycode from ie or mozilla
        var keycode = (e == null && event) ? event.keyCode : e.which;
        // check Esc key and close window
        if(keycode == Esc) this.hide();
    }

    this.checkResize = function(forsed) {
		var oThis = this;
		if(!this.getBox())return;
		if(forsed || this.size.width != this.div.offsetWidth || this.size.height != this.div.offsetHeight){
		    this.resized();
		}
        this.timerID = window.setTimeout(function(){oThis.checkResize();},this.interval);
    }

    this.resized = function() {
		var margin_y = (document.all? parseInt(document.body.topMargin) : 0);
		var scrollTop = document.body.parentNode.scrollTop;
		if (!scrollTop) scrollTop = document.body.scrollTop;
		this.size = {width: this.div.offsetWidth, height: this.div.offsetHeight};
		//var y = this.center.Y-this.size.height/2;
		var y = scrollTop+margin_y+120;

		if(y<0) y = 0;
        this.jumpTo(this.center.X-this.size.width/2, y);
        if(this.onresize) this.onresize();
    }

	this.jumpTo = function(x, y){
	    if(!this.getBox())return;
	    if ( this.isIE ) {
	        x = -10;
	    }
		this.div.style.left = x+'px';
		this.div.style.top = y+'px';
	}

    this.getBox = function() {
	    if(!this.div) this.div = document.getElementById(this.divId);
	    if(!this.div || !this.div.tagName) this.div = null;
	    return this.div;
	}

    this.getLoader = function() {
	    if(!this.loader) this.loader = document.getElementById(this.loaderId);
	    if(!this.loader || !this.loader.tagName) this.loader = null;
	    return this.loader;
	}

	this.calculateCenter  = function(){
		var margin_x = (document.all? parseInt(document.body.leftMargin) : 0);
		var margin_y = (document.all? parseInt(document.body.topMargin) : 0);
        var winWidth = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement.clientWidth: document.body.clientWidth;
        var winHeight = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement.clientHeight : document.body.clientHeight;
		var scrollLeft = document.body.parentNode.scrollLeft + document.body.scrollLeft;
		var scrollTop = document.body.parentNode.scrollTop + document.body.scrollTop;
		this.center = {X: scrollLeft+margin_x+winWidth/2, Y: scrollTop+margin_y+winHeight/2};
		//this.center = {X: scrollLeft+margin_x+winWidth/2, Y: scrollTop+margin_y};
	}


	this.init = function(content){
        if(!this.getBox())
        {
            document.write(
                '<table border="0" cellspacing="0" cellpadding="0" summary="" class="pop_up_window" id="'+this.divId+'">'+
                '<tr><td class="lt"><img src="/images/t.gif" alt="" /></td><td class="ct"><img src="/images/t.gif" alt="" /></td><td class="rt"><div style="position:relative">'+
                ( this.showClose ? '<img src="/images/ico_popup_win_close.gif" width="14" height="14" alt="Close window" button="close" class="pop_up_win_close" />' : '')+'<img src="/images/t.gif" width="10" height="10" alt="" /></div></td></tr>'+
                '<tr><td class="lc"><img src="/images/t.gif" alt="" /></td><td class="cc" cellname="content" id="pop_up_window_content_'+this.divId+'">'+content+'</td><td class="rc"><img src="/images/t.gif" alt="" /></td></tr>'+
                '<tr><td class="lb"><img src="/images/t.gif" alt="" /></td><td class="cb"><img src="/images/t.gif" alt="" /></td><td class="rb"><img src="/images/t.gif" alt="" /></td></tr>'+
                '</table>');
            if(!this.getBox()) return;
        }
		this.div.setAttribute("object", this);
		var oThis = this;

        var cells = this.div.getElementsByTagName('td');
        for(var i=0; i<cells.length; i++)
        {
            var cellName = cells[i].getAttribute("cellname");
            if( cellName == "content") this.divContent = cells[i];
        }

        var imgs = this.div.getElementsByTagName('img');
        for(var i=0; i<imgs.length; i++)
        {
            var button = imgs[i].getAttribute("button");
            if( button == "close") imgs[i].onclick = function(){oThis.close();}
        }
	}
	//init code
	this.init(content);
	return this;
}
