﻿StretchPopupExtender = {};
StretchPopupExtender.extend = function (popup, doReparent) {
    popup = $(popup);
    popup.doReparent = doReparent;
    popup.doReparentBack = doReparent;

    // bind utility methods to the popup
    popup.Show = StretchPopupUtil.Show.bind(popup);
    popup.Hide = StretchPopupUtil.Hide.bind(popup);
    popup.ClearContents = StretchPopupUtil.ClearContents.bind(popup);
    popup.SetTitle = StretchPopupUtil.SetTitle.bind(popup);
    popup.SetContents = StretchPopupUtil.SetContents.bind(popup);
    popup.SetHelpLink = StretchPopupUtil.SetHelpLink.bind(popup);
    popup.SetHelpTarget = StretchPopupUtil.SetHelpTarget.bind(popup);
    popup.ClickHelpLink = StretchPopupUtil.ClickHelpLink.bind(popup);
    popup.SetHelpVisibility = StretchPopupUtil.SetHelpVisibility.bind(popup);

    // assign close button onclick
    $$('#' + popup.id + ' .top .topright .closebutton img').each(function (btn) {
        btn.onclick = StretchPopupUtil.CloseButtonClick.bind(popup);
    });
    // assign help button onclick
    popup.HelpLinkDiv = $$('#' + popup.id + ' .top .topright .helpbutton');
    if (popup.HelpLinkDiv && popup.HelpLinkDiv.length > 0)
        popup.HelpLinkDiv = popup.HelpLinkDiv[0];
    else
        popup.HelpLinkDiv = null;

    if (popup.HelpLinkDiv) {
        popup.HelpLink = $$('#' + popup.HelpLinkDiv.id + ' a');
        if (popup.HelpLink && popup.HelpLink.length > 0)
            popup.HelpLink = popup.HelpLink[0];
        else
            popup.HelpLink = null;

        $$('#' + popup.HelpLinkDiv.id + ' img').each(function (btn) {
            btn.onclick = StretchPopupUtil.ClickHelpLink.bind(popup);
        });
    }
};

// use a utility class so that the function definitions don't get copied for each popup
// these methods will be re-bound, so this will refer to the popup
StretchPopupUtil = {};
StretchPopupUtil.Show = function() {
    // skip out if already visible
    if (this.style.display != "none")
        return;

    var overlay = $('translucent_overlay');

    // initialize the popup stack if reqd
    if (overlay.popupStack == undefined) overlay.popupStack = new Array();

    overlay.popupStack.push(this);

    // reparent myself to be a sibling of translucent_overlay and remember where I "belong"
    if (this.doReparent) {
        this.origParent = this.ancestors()[0];
        this.remove();
        overlay.ancestors()[0].insert(this);
    }

    // set the overlay to above the last popup (old position +2 since any other popups will be +1)
    overlay.style.zIndex = parseInt(overlay.getStyle('z-index')) + 2;
    overlay.show();

    // set my zIndex to be 1 above the overlay
    this.style.zIndex = parseInt(overlay.getStyle('z-index')) + 1;

    this.show();

    // call extension method
    if (this.onShow) {
        try {
            this.onShow();
        }
        catch (ex) { }
    }
};
StretchPopupUtil.Hide = function() {
    // skip out if already hidden
    if (this.style.display == "none")
        return;

    var overlay = $('translucent_overlay');

    // remove me from the stack
    overlay.popupStack = overlay.popupStack.without(this);
    this.hide();

    if (this.doReparent && this.doReparentBack) {
        // reparent me to where I was originally located
        this.remove();
        this.origParent.insert(this);
    }

    if (overlay.popupStack.length == 0) {
        // set default
        overlay.style.zIndex = 1000;
        overlay.hide();
    }
    else {
        // set overlay to be underneath the popup at the top of the stack
        overlay.style.zIndex = parseInt(overlay.popupStack.last().getStyle('z-index')) - 1;
    }

    if (this.onClose) {
        try {
            this.onClose();
        }
        catch (ex) { }
    }
};
StretchPopupUtil.ClearContents = function() {
    $$('#' + this.id + ' .middle .middlecentre')[0].update('');
};
StretchPopupUtil.SetContents = function(value) {
    $$('#' + this.id + ' .middle .middlecentre')[0].update(value);
};
StretchPopupUtil.SetTitle = function(value) {
    $$('#' + this.id + ' .top .topcentre .popup_title')[0].update(value);
};
StretchPopupUtil.CloseButtonClick = function() {
    var result = true;
    // check for an OnClose method, run it, and allow it to cancel hiding the popup
    if (this.OnClose)
        result = this.OnClose();

    if (result != false)
        this.Hide();
    return false;
};
StretchPopupUtil.SetHelpLink = function (value) {
    this.HelpLink.href = value;
};
StretchPopupUtil.SetHelpTarget = function (value) {
    this.HelpLink.target = value;
};
StretchPopupUtil.ClickHelpLink = function () {
    this.HelpLink.click();
};
StretchPopupUtil.SetHelpVisibility = function (value) {
    this.HelpLinkDiv.style.visibility = (value ? "" : "hidden");
};

