var cWindow_Popup = {
    // public methods
    open: function(modal, opener)
    {
        this.opener = opener;
        this.setVisibility(true);
        this.hideProcessingAnimation();
        this.popupCentre();
        jQuery(this.dom_popup).css('z-index', 501);

        if (modal || this.modal) {
            jGlassWindow.showGlassWindow();
        }
    },

    popupCentre: function()
    {
        /*
            var vd = viewportDimensions();
            var vo = viewportOffset();

            var left = Math.ceil((vd.width - this.dom_popup.offsetWidth) / 2);
            var top = Math.ceil((vd.height - this.dom_popup.offsetHeight) / 2);

            $(this.dom_popup).css({'top': top + 'px',
                                   'left': left + 'px'});

        */
         var viewport_offset = viewportOffset()
         $(this.dom_popup).css({
            'left' : Math.ceil(($(window).width() -  $(this.dom_popup).outerWidth()) / 2 + viewport_offset.offsetX) + 'px',
            'top'  : Math.ceil(($(window).height() -  $(this.dom_popup).outerHeight()) / 2 + viewport_offset.offsetY) + 'px'
        });

    },

    close: function()
    {
        jGlassWindow.hideGlassWindow();
        this.setVisibility(false);
        this.hideProcessingAnimation();
        return true;
    },

    processButtonClick: function(button_id)
    {
        if (button_id == 'popup_close') {
            this.close();
            return false;
        }
        alert('Button clicked, id is \''  + button_id + '\'');
    },

    getButtonId: function(button_id)
    {
        return button_id.substr(this.name.length + 8); // 8 = '_button_'.length
    },

    //drag & drop
    startDrag: function(x, y)
    {
        this.xStart = parseInt(x);
        this.yStart = parseInt(y);
        this.status_select = false;
        documentEventDispatcher.addListener(this, ['mouseup', 'mousemove', 'selectstart']);
        return false;
    },

    doDrag: function(x, y)
    {
        var xDelta = 0, yDelta = 0;
        var tCur = 0, lCur = 0;

        el = this.dom_popup;
        xDelta = this.xStart - parseInt(x);
        yDelta = this.yStart - parseInt(y);

        tCur = parseInt(el.style.top);
        lCur = parseInt(el.style.left);

        var dD = documentDimensions();
        var vD = viewportDimensions();

        var h = dD.height;
        var w = dD.width;
        if (h < vD.height) { h = vD.height; }
        if (w < vD.width) { w = vD.width}

        if (tCur-yDelta <= 0 || tCur-yDelta >= (h - el.offsetHeight)) {
            el.style.top = tCur + 'px';
        } else {
            el.style.top = (parseInt(el.style.top) - yDelta) + 'px';
        }

        if (lCur-xDelta <= 0 || lCur-xDelta >= (w - el.offsetWidth)) {
            el.style.left = lCur + 'px';
        } else {
            el.style.left = (parseInt(el.style.left) - xDelta) + 'px';
        }

        this.xStart = parseInt(x);
        this.yStart = parseInt(y);
    },

    stopDrag: function(e)
    {
        this.status_select = true;
        window.documentEventDispatcher.removeListener(this, ['mouseup', 'mousemove', 'selectstart']);
    },

    //handles
    sysAreaClick: function(e)
    {
        if (this.drag_drop) {
            this.startDrag(e.clientX, e.clientY);
        }
        return true;
    },

    clientAreaClick: function(e)
    {
        return true;
    },

    documentSelection: function(e)
    {
        if (!this.status_select) {
            e.preventDefault();
        }
    },

    listenerEvent: function(e)
    {
        if (e.type == 'mouseup') {
           this.stopDrag(e);
        } else if (e.type == 'mousemove') {
           this.doDrag(e.clientX, e.clientY);
        } else if (e.type == 'selectstart') {
            this.documentSelection(e);
        }
    },

    showProcessingAnimation: function()
    {
        this.dom_popup_close.style.display = 'none';
        this.dom_popup_progress_indicator.show();
    },

    hideProcessingAnimation: function()
    {
        this.dom_popup_close.style.display = '';
        this.dom_popup_progress_indicator.hide();
    }
}

function Window_Popup(data)
{
    this.pc = Window;
    this.pc(data);

    for (var f in cWindow_Popup) {
        this[f] = cWindow_Popup[f];
    }

    this.topOffset = 70;
    this.leftOffset = 35;
    this.parent_constructor = Window;
    this.parent_constructor(data);
    this.opener = null;


    this.xStart = 0;
    this.yStart = 0;

    this.dom_popup = _(this.name + '_popup');
    this.dom_popup_progress_indicator = $(_(this.name + '_popup_progress_indicator'));
    this.dom_popup_client_area = _(this.name + '_popup_cleint_area');
    this.dom_popup_close = _(this.name + '_system_popup_close');
    this.dom_popup_title = $(_(this.name + '_system_popup_title'));

    var _this = this;

    var handleSysAreaClick = function(e)
    {
        var rs = _this.sysAreaClick(e);
        if (rs) jEvent.fixEvent(e).stopPropagation();
    }

    var handleClientAreaClick = function(e)
    {
        var rs = _this.clientAreaClick(e);
        if (rs) jEvent.fixEvent(e).stopPropagation();
    }

    var handleSysAreaClose = function(e)
    {
       _this.close();
       jEvent.fixEvent(e).preventDefault();
    }

    jEvent.addEvent(this.dom_popup, 'mousedown', handleSysAreaClick);
    jEvent.addEvent(this.dom_popup_client_area, 'mousedown', handleClientAreaClick);
    jEvent.addEvent(this.dom_popup_close, 'click', handleSysAreaClose);

    this.drag_drop = data.drag_drop;

    if (this.drag_drop) {
        jQuery(this.dom_popup).css('cursor', 'move');
    } else {
        jQuery(this.dom_popup).css('cursor', 'default');
    }

    this.click_handler = function(e)
    {
        _this.processButtonClick(_this.getButtonId(this.id));
        jEvent.fixEvent(e).preventDefault();
    }

    if (data.client_area_class) {
        $(this.dom_popup_client_area).addClass(data.client_area_class);
    }

    if (data.buttons) {
	    for (var button_id in data.buttons) {
	       if (!this.buttons) {
	           this.buttons = {};
                this.dom_popup_buttons = $(_(this.name + '_popup_buttons'));
	       }

	        var b = _(data.name + '_button_' + button_id);
	        b.onclick = this.click_handler;
	        this.buttons[button_id] = b;
	    }
    }

    this.modal = data.modal;
}

