﻿
jQuery.extend({
    PopImpromptuDefaults: {
        url: "",
        tumdnlist: "",
        tuidlist: "",
        usernamelist: "",
        moduleid: "",
        message: "",
        width: "300",
        height: "100",
        buttons: { Ok: true },
        loaded: function() { },
        submit: function() { return true; },
        callback: function() { },
        opacity: 0.6,
        zIndex: 999,
        overlayspeed: 'slow',
        promptspeed: 'fast',
        show: 'show',
        focus: 0,
        useiframe: false
    },
    PopSetImpromptuDefaults: function(o) {
        jQuery.ImpromptuDefaults = jQuery.extend({}, jQuery.ImpromptuDefaults, o);
    },
    Popprompt: function(m, o) {
        o = jQuery.extend({}, jQuery.PopImpromptuDefaults, o);
        var ie6 = (jQuery.browser.msie && jQuery.browser.version < 7);
        var b = jQuery(document.body);
        var w = jQuery(window);
        var msgbox = '<div class="jqibox" id="jqibox">';
        if (ie6) $('select').css('visibility', 'hidden');
        msgbox += '<div class="jqifade" id="jqifade"><iframe class="iframewin" id="iframewin" src="window2.html" scrolling="no" frameborder="0"  style="width:100%;height:100%; background-color:#485b70;"></iframe></div>';
        msgbox += '<div class="jqi" id="jqi"><div class="jqicontainer"><div class="jqimessage"></div>';
        msgbox += '</div></div></div>';
        var jqib = b.append(msgbox).children('#jqibox');
        var jqi = jqib.children('#jqi');
        var jqif = jqib.children('#jqifade');
        var jqic = jqi.children('.jqicontainer');
        var jqiclose = jqic.children('#jqititle');
        var getWindowScrollOffset = function() {
            return (document.documentElement.scrollTop || document.body.scrollTop) + 'px';
        };

        var getWindowSize = function() {
            var size = {
                width: window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth),
                height: window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight)
            };
            return size;
        };

        var ie6scroll = function() {
            jqib.css({ top: getWindowScrollOffset() });
        };

        var flashPrompt = function() {
            var i = 0;
            jqib.addClass('jqiwarning');
            var intervalid = setInterval(function() {
                jqib.toggleClass('jqiwarning');
                if (i++ > 1) {
                    clearInterval(intervalid);
                    jqib.removeClass('jqiwarning');
                }
            }, 100);
        };


        var escapeKeyClosePrompt = function(e) {
            var kC = (window.event) ? event.keyCode : e.keyCode; // MSIE or Firefox?
            var Esc = (window.event) ? 27 : e.DOM_VK_ESCAPE; // MSIE : Firefox
            if (kC == Esc) {
                removePrompt();
            }
        };

        var positionPrompt = function() {
            var wsize = getWindowSize();
            jqib.css({ position: (ie6) ? "absolute" : "fixed", width: "100%",height:"100%", top: (ie6) ? getWindowScrollOffset() : 0, left: 0, right: 0, bottom: 0 });
            jqif.css({ position: "absolute", width: "100%", height: "100%", top: 0, left: 0, right: 0, bottom: 0 });
            jqi.css({ position: "absolute",
                width: o.width + "px",
                height: o.height + "px",
                left: (wsize.width - o.width) / 2 + "px",
                top: (wsize.height - o.height) / 2 + "px"
            });
            jqic.css({ width: o.width - 10 + "px", height: o.height - 10 + "px" });
            jqiclose.css({ width: "100%", height: "20px", background: "red" });
        };

        var stylePrompt = function() {
            jqif.css({ zIndex: o.zIndex, display: "none", opacity: o.opacity });
            jqi.css({ zIndex: o.zIndex + 1, display: "none" });
        }

        var removePrompt = function(callCallback, clicked, msg) {
            jqi.remove();
            if (ie6) b.unbind('scroll', ie6scroll); //ie6, remove the scroll event
            w.unbind('resize', positionPrompt);
            jqif.fadeOut(o.overlayspeed, function() {
                jqif.unbind('click', flashPrompt);
                jqif.remove();
                if (callCallback) o.callback(clicked, msg);
                jqib.unbind('keypress', escapeKeyClosePrompt);
                jqib.remove();
                if (ie6 && !o.useiframe) $('select').css('visibility', 'visible');
            });
        }

        positionPrompt();
        stylePrompt();

        if (o.url != '') {
            var msg = jqi.children('.jqicontainer').children('.jqimessage');
            msg.load(o.url);
            $(".iframewin").attr({ "src": "window2.html" });
        }
        else {
            var msg = jqi.children('.jqicontainer').children('.jqimessage');
            msg.html(o.message);
            $(".iframewin").attr({ "src": "window2.html" });
        }


        //close window button
        jqi.find('.jqiclose')
		    .mouseover(function() { jQuery(this).addClass('jqiclose2'); })
		    .mouseout(function() { jQuery(this).removeClass('jqiclose2'); })
		    .click(removePrompt);


        //Show it
        jqif.fadeIn(o.overlayspeed);
        jqi[o.show](o.promptspeed, o.loaded);
        jqi.find('#jqibuttons button:eq(' + o.focus + ')').focus(); //focus the default button
        return jqib;
    }
});

