if (typeof Phej == 'undefined' || !Phej) var Phej = { };

Phej.Notify = {
    
    CORNER_TOPLEFT: 1,
    CORNER_TOPRIGHT: 2,
    CORNER_BOTTOMLEFT: 3,
    CORNER_BOTTOMRIGHT: 4,

    popupStyle:                 "internal",
    popupClass:                 false,
    popupStylePadding:          "8px",
    popupStyleBorder:           "1px solid #8A8",
    popupStyleFontSize:         "9pt",
    popupStyleBackground:       "#EFE",
    popupStyleOpacity:          "0.95",
    
    popupMaxWidth:              -300,                   // popup width (negative number for auto-fit maximum)
    popupCorner:                2,
    popupOffsetHorizontal:      8,
    popupOffsetVertical:        8,
    popupRepositionDelay:       200,
    popupDuration:              5000,
    
    windowEventsSet:            false,
    repositionTimeout:          null,
    
    popupStack: new Array(),
    
    Popup: function(text, args) {
        if (!text) text = "";
        if (!args) args = { };
        if (!args.corner) args.corner = this.popupCorner;
        if (!args.maxWidth) args.maxWidth = this.popupMaxWidth;
        if (!args.offsetHorizontal) args.offsetHorizontal = this.popupOffsetHorizontal;
        if (!args.offsetVertical) args.offsetVertical = this.popupOffsetVertical;
        if (!args.duration) args.duration = this.popupDuration;

        var p = { };
        p.id = "pheJS_popup_" + (new Date().getDate());
        jQuery("body").append('<div id="' + p.id + '" />');
        p.obj = jQuery("#" + p.id);
        
        if (this.popupStyle == "internal") {
            p.obj
                .html(text)
                .css("position", "absolute")
                .css("padding", this.popupStylePadding)
                .css("border", this.popupStyleBorder)
                .css("background", this.popupStyleBackground)
                .css("opacity", 0)
                .css("font-size", this.popupStyleFontSize);
        } else {
            p.obj.html(text).addClass(this.popupClass).css("position", "absolute");
        }
        
        p.obj.prepend('<a href="javascript:void(0);" onClick="Phej.Notify.Clear(this, true);" style="float: right;">[X]</a>');
        
        if (args.maxWidth < 0 && p.obj.width() > args.maxWidth) p.obj.width((-args.maxWidth) + "px");
        else if (args.maxWidth > 0) p.obj.width(args.maxWidth + "px");

        if (args.corner == this.CORNER_TOPLEFT || args.corner == this.CORNER_BOTTOMLEFT) p.obj.css("left", args.offsetHorizontal + "px");
        else if (args.corner == this.CORNER_TOPRIGHT || args.corner == this.CORNER_BOTTOMRIGHT) p.obj.css("right", args.offsetHorizontal + "px");
        
        if (!this.windowEventsSet) {
            jQuery(window)
                .resize(function() { Phej.Notify.PopupReposition(); })
                .scroll(function() { Phej.Notify.PopupReposition(); })
        }
        
        p.obj.fadeTo("slow", this.popupStyleOpacity);
        p.args = args;
        this.popupStack.push(p);
        this.PopupRepositionReal();
        if (args.duration > 0) setTimeout('Phej.Notify.Clear("' + p.id + '");', args.duration);
    },
    
    PopupReposition: function() {
        if (this.popupStack.length > 0) {
            if (this.popupRepositionDelay > 0) {
                // timeout
                if (this.repositionTimeout) clearTimeout(this.repositionTimeout);
                this.repositionTimeout = setTimeout("Phej.Notify.PopupRepositionReal();", this.popupRepositionDelay);
            } else {
                // move immediately
                this.PopupRepositionReal();
            }
        }
    },
    
    PopupRepositionReal: function() {
        var winHeight = jQuery(window).height();
        var winScroll = jQuery(window).scrollTop();
        if (this.popupStack.length > 0) {
            for (var i = 0; i < this.popupStack.length; i++) {
                var p = this.popupStack[i];
                if (p.args.corner == this.CORNER_TOPLEFT || p.args.corner == this.CORNER_TOPRIGHT)
                    p.obj.css("top", (winScroll + p.args.offsetVertical) + "px");
                else if (p.args.corner == this.CORNER_BOTTOMLEFT || p.args.corner == this.CORNER_BOTTOMRIGHT)
                    p.obj.css("bottom", (winScroll + winHeight - p.obj.height() - p.args.offsetVertical) + "px");
            }
        }
    },
    
    Clear: function(obj, findObj) {
        if (this.popupStack.length == 0) return false;
        if (findObj) obj = jQuery(obj).parents("div[id^=pheJS_popup_]").eq(0);
        if (typeof obj == 'string') obj = jQuery("#" + obj);
        for (var i = 0; i < this.popupStack.length; i++) {
            if (this.popupStack[i].obj.attr("id") == obj.attr("id")) {
                var p = this.popupStack[i]
                this.popupStack.splice(i, 1);
                p.obj.fadeOut("slow", function() { jQuery(this).remove(); });
            }
        }
    }
    
};
