(function($){
    var $ie6 = (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7);
    var vexPopupFactory = false;
    
    var vexPopup = window.vexPopup = function(html,options) {
        return this instanceof vexPopup ?
            this.init(html,options) :
            new vexPopup(html,options);
    };
    
    vexPopup.prototype = {
        init: function(html,options){
            if(!options && typeof html == "object") var options = html;
            else if(!options && html) var options = {html: html};
            else if(options && html) options.html = html;
            else var 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 = "vexPopup";
            div.style.height = o.height || "50px";
            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 = "vexPopup_hdr";
            hdr.style.width = div.style.width;
            hdr.innerHTML = o.title || "";
            
            if(!o.force){
                var close = document.createElement("div");
                close.innerHTML = "Close This Window";
                close.onclick = this.close.bindAsEventListener(this);
                hdr.appendChild(close);
            }
            
            var content = document.createElement("div");
            content.className = "vexPopup_content";
            content.style.height = parseInt(div.style.height)-10 + "px";
            
            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);
            
            div.appendChild(hdr);
            div.appendChild(content);
            
            $("div.wrap").after(div);
            if($ie6) $("#content select").css("visibility","hidden");
            
            this.popup = div;
            this.open();
        },
        handleActions: function(obj){
            var o = this.options;
            
            var div = document.createElement("div");
            div.className = "vexPopup_input"
            
            if(o.inputType){
                var input;
                if(o.inputType === 'text'){
                    input = document.createElement("input");
                    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>");
                            }
                        }
                    }
                    this.userInput = input;
                }
                div.appendChild(input);
                div.style.marginTop = "10px"
            }
            
            if(o.callback) {
                var btn = document.createElement("button");
                btn.style.margin = "0 3px";
                btn.onclick = this.callback.bindAsEventListener(this);
                btn.innerHTML = o.button1 || "Continue";
                div.appendChild(btn);
                
                if(!o.force){
                    var btn2 = document.createElement("button");
                    btn2.onclick = this.close.bindAsEventListener(this);
                    btn2.innerHTML = o.button2 || "Cancel";
                    div.appendChild(btn2);
                }
            }
            
            obj.appendChild(div);
        },
        clean: function(){
            if(vexPopupFactory){
                vexPopupFactory.remove();
                vexPopupFactory = this;
            }
            else {
                vexPopupFactory = 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);
            }
        },
        remove: function(){
            $(this.popup).remove();
        },
        open: function(){
            $("div.wrap").fadeTo(500, .3);
            $(this.popup).fadeIn(1000);
        },
        close: function(){
            var that = this;
            
            $(this.popup).fadeOut(function(){
                $("div.fadePopup").remove();
                if(that.options.close) that.options.close();
                if($ie6) $("#content select").css("visibility","visible");
            });
            $(".wrap").fadeTo(500, 1);
        }
    }
    Function.prototype.bindAsEventListener = function(object, args) {
        var __method = this;
        return function(event) {
            return __method.call(object, event || window.event, args);
        };
    };
})(jQuery);
