(function(){
    var $ie6 = (jQuery.browser.msie && jQuery.browser.version == "6.0");
    var popupFactory = false;

    var popup = window.popup = function(html,options) {
        return this instanceof popup ?
        this.init(html,options) :
        new popup(html,options);
    };

    popup.prototype = {
        init: function(html,options){
            if(!options && typeof html == "object") {
                options = html;
            } else if(!options && html) {
                options = {
                    html: html
                };
            } else if(options && html) {
                options.html = html;
            } else {
                options = {
                    html: ''
                };
            }
            this.options = options;
            this.userInput = null;
            
            this.create();
        },
        create: function(){
            var o = this.options;
            this.clean();
            
            var scrollY = (window.scrollY) ? window.scrollY : document.documentElement.scrollTop;
            
            var div = document.createElement("div");
            div.className = (!o.error) ? "popup" : "popup error";
            div.style.height = o.height || "80px";
            div.style.width = o.width || "400px";
            div.style.left = ($(window).width()/2 - parseInt(div.style.width)/2) + "px";
            div.style.top = ($(window).height()/2 + scrollY - parseInt(div.style.height)/2) + "px";
            
            var hdr = document.createElement("div");
            hdr.className = "popup_hdr";
            hdr.style.width = div.style.width;
            hdr.innerHTML = o.title || "";
            
            if(!o.force){
                var close = document.createElement("div");
                var cl_img = $ie6 ? "/img/close.gif" : "/img/close.png";
                close.innerHTML = '<img src="'+ base.context + cl_img + '"/>';
                close.onclick = this.close.bindAsEventListener(this);
                hdr.appendChild(close);
            }
            
            var content = document.createElement("div");
            content.className = "popup_content";
            content.style.height = parseInt(div.style.height)-10 + "px";

            if(o.iframe){
                $("<iframe width='100%' height='95%' src='"+o.iframe+"' frameborder='no'></iframe>").appendTo(content);
            }
            else if(typeof o.html == "object" ){
                o.html.style.display = "block";
                content.appendChild(o.html);
            }
            else {
                content.innerHTML = o.html || "";
            }
            
            if(o.inputType || o.callback) {
                this.handleActions(content);
            }

            var that = this;
            var data = "h="+(parseInt(div.style.height)+10)+"&w="+(parseInt(div.style.width)+10);
            if(o.error) {
                data += "&error=true";
            }
            $.ajax({
                url: base.context +"/layouts/popup_vml.jsp",
                cache: false,
                data: data,
                success: function(html){
                    $(div).append(html);
                    div.appendChild(hdr);
                    div.appendChild(content);

                    $("#wrap").after(div);
                    if($ie6) {
                        $("select").css("visibility","hidden");
                    }

                    that.popup = div;
                    that.open();
                }
            });
        },
        handleActions: function(obj){
            var o = this.options;
            
            var div = document.createElement("div");
            div.className = "popup_input";
            
            if(o.inputType){
                var input;
                if(o.inputType === 'text'){
                    input = document.createElement("input");
                    input.value = o.inputValue;
                    this.submitVal = input.value;
                }
                else if(o.inputType === 'select'){
                    input = document.createElement("select");
                    if(o.inputValueNames){
                        for(var i=0; i < o.inputValueNames.length; i++){
                            if(o.inputValues != null){
                                $(input).append("<option value='"+o.inputValues[i]+"' >"+o.inputValueNames[i]+"</option>");
                            }else{
                                $(input).append("<option>"+o.inputValueNames[i]+"</option>");
                            }
                        }
                    }
                }
                else if(o.inputType === 'file'){
                    input = document.createElement("input");
                    input.type = "file";
                    this.submitVal = input.value;
                }

                this.userInput = input;
                div.appendChild(input);
                div.style.marginTop = "10px";
            }
            
            if(o.callback) {
                var btn_div = document.createElement("div");
                btn_div.className = "buttons";
                var btn = document.createElement("button");
                btn.style.margin = "0 3px";
                btn.onclick = this.callback.bindAsEventListener(this);
                btn.innerHTML = o.button1 || "Continue";
                btn_div.appendChild(btn);

                if(!o.force){
                    var btn2 = document.createElement("button");
                    btn2.onclick = this.close.bindAsEventListener(this);
                    btn2.innerHTML = o.button2 || "Cancel";
                    btn_div.appendChild(btn2);
                }
                div.appendChild(btn_div);
            }
            
            obj.appendChild(div);
        },
        clean: function(){
            if(popupFactory){
                popupFactory.remove();
                popupFactory = this;
            }
            else {
                popupFactory = this;
            }
        },
        callback: function() {
            var o = this.options;
            if(!o.inputType){
                this.options.callback();
            }
            else if(o.inputType == "text"){
                this.options.callback(this.userInput.value);
            }
            else if(o.inputType == "select"){
                this.options.callback(this.userInput[this.userInput.selectedIndex].value);
            }
            this.close();
        },
        remove: function(){
            $(this.popup).remove();
        },
        open: function(){
            var height = ie6 ? "style='height:"+document.body.offsetHeight+"px;'" : "";
            $("#wrap").after("<div class='popup_opacityscreen' "+height+"></div>");
            $(this.popup).show();
        },
        close: function(){
            var that = this;
            $(this.popup).hide();
            if(that.options.close) {
                that.options.close();
            }
            if($ie6) {
                $("select").css("visibility","visible");
            }
            $("div.popup_opacityscreen").remove();
        }
    };
    Function.prototype.bindAsEventListener = function(object, args) {
        var __method = this;
        return function(event) {
            return __method.call(object, event || window.event, args);
        };
    };
})();
