
(function($) {
$.extend($.ui.dialog.prototype, {
    panelHeight : null,
    ie6Height : null,
    addon:false,
    
    seek4AddOn : function(_opt){
        this._cont = this.uiDialogContainer.children('.ui-dialog-content');
        var css = this.uiDialogContainer.attr("class");
        var ht = this._cont.html();
        this._cont.html("");
        this._cont.append("<div class='inside-panel' />");
        this._panel = this._cont.children(".inside-panel");
        this._panel.rcorner();
        this._panel.children(".inside-panel-content").html(ht);
        
        this.addon = true;
        if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7){
            this.resizeHandles = this.uiDialog.resizable().children(".ui-resizable-handle");
            for (var i=0; i < this.resizeHandles.length;i++){
                var origBg = this.resizeHandles[i].currentStyle.backgroundImage.toString().replace('url("','').replace('")','');
                var filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + origBg + "', sizingMethod='scale')";
                this.resizeHandles[i].runtimeStyle.filter = filter;
                $(this.resizeHandles[i]).css("background-image","url(/Design/gfx/blank.gif)");
            }
        }
    },
    
    _resizeContent : function(){
        //jQuery.Console.writeLn("On Resize Content: "+this.element.height());
        var k = (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7 ) ? 55 - parseInt(this._cont.css("margin-bottom")) : 45 - parseInt(this._cont.css("margin-bottom"));
        this.contentHeight = this._cont.height();
        this.panelHeight = this.contentHeight-k;
        this._panel.height(this.panelHeight);
        this._cont.height(this.contentHeight);
        this._panel.children(".inside-panel-content").height("100%");
        this._cont.width("auto");
        this._panel.width("auto");
        

        if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7){
            for (var i=0; i < this.resizeHandles.length;i++){
                if ($(this.resizeHandles[i]).hasClass("ui-resizable-n") || $(this.resizeHandles[i]).hasClass("ui-resizable-s")){
                    $(this.resizeHandles[i]).css("width", this._cont.width());
                }else if ($(this.resizeHandles[i]).hasClass("ui-resizable-e") || $(this.resizeHandles[i]).hasClass("ui-resizable-w")){
                    $(this.resizeHandles[i]).css("height", this._cont.height() + this.uiDialogTitlebar.height() - 7);
                }
            }
        }
        
    },
    
    setTitle : function(title){
        this.uiDialogTitlebar.children(".ui-dialog-title").html(title);
    },
    
    copyToForm : function(){
        if ($("form").length < 1) return;
        var inpts = $("#"+this.element[0].id+" input");
        var l = inpts.length;
        var ht = "<div style='display:none'>";
        for (var i=0;i<l;i++){
            switch (inpts[i].type.toLowerCase())
            {
                case "text":
                case "hidden":
                    ht += "<input type='hidden' name='"+inpts[i].name+"' value='"+inpts[i].value+"' />"; 
                    break;
                case "radio":
                case "checkbox":
                    if (inpts[i].checked) ht += "<input type='hidden' name='"+inpts[i].name+"' value='"+inpts[i].value+"' />"; 
                    break;
            }
            
        }
        ht += "</div>";
        $("form").append(ht);
    }
    
});
})(jQuery);

(function($) {

jQuery.Seek4Alert = function(_message,_title, _width, _height){
    if (this.dlg == null){
        this.dlg = new jQuery.Seek4BoxInstance(_message, _title, _width, _height);
        var ht  = "<div class='ui-dialog-buttonpane'>";
        ht += "<span class='btn-wrapper'><span class='btn-left'>&nbsp;</span><input type='button' class='btn-middle' onclick=\"jQuery('"+this.dlg.box+"').dialog('close');\" value='Ok' /><span class='btn-right'>&nbsp;</span></span>";
        ht += "</div>";
        $(this.dlg.textbox).parent().append(ht);
        
    }
    else{
        if (_title != this.dlg.title) this.dlg.setTitle(_title);
        if (_message != this.dlg.message) this.dlg.setText(_message);
        if (_width != this.dlg.width) this.dlg.setWidth(_width);
        if (_height != this.dlg.height) this.dlg.setHeight(_height);
    }
    this.dlg.show();
}

jQuery.Seek4Confirm = function(_message,_callback, _o){
    var self = this;
    _o = _o?_o:{};
    if (this.confirmdlg == null){
        this.confirmdlg = new jQuery.Seek4BoxInstance(_message, _o.title, _o.width, _o.height);
        this.confirmdlg.setCallbackData(_o.data);
        this.confirmdlg.setCallback(_callback);
        this.confirmdlg.setButtons(_o.buttons);
    }
    else{
        if (_o.title != this.confirmdlg.title) this.confirmdlg.setTitle(_o.title);
        if (_message != this.confirmdlg.message) this.confirmdlg.setText(_message);
        if (_o.data != this.confirmdlg.callbackData) this.confirmdlg.setCallbackData(_o.data);
        if (_callback != this.confirmdlg.callback) this.confirmdlg.setCallback(_callback);
        if (_o.width != this.confirmdlg.width) this.confirmdlg.setWidth(_o.width);
        if (_o.height != this.confirmdlg.height) this.confirmdlg.setHeight(_o.height);
        if (_o.buttons != this.confirmdlg.buttons) this.confirmdlg.setButtons(_o.buttons);
        this.confirmdlg.setCallback(_callback);
    }
    this.confirmdlg.show();
}

jQuery.Seek4Dialog = function(_contentObjectId, _callback, _o){

    var self = this;
    _o = _o?_o:{};
    if (typeof(_o.buttons) == "undefine" || _o.buttons == null || _o.buttons.length == 0){
        _o.buttons = ["Save", "Cancel"];
    }
    if (typeof (this.roster)=="undefined"){
        this.roster = [];
        this.register = function(_dlg){
            this.roster[_dlg.id] = _dlg;
        }
    }
    if (typeof(this.roster[_contentObjectId])=="undefined" || this.roster[_contentObjectId] == null){
        if($J.browser.msie){
            _o.height += 25;
        }
        this.register(new jQuery.Seek4BoxInstance(null, _o.title, _o.width, _o.height, _contentObjectId));
        this.roster[_contentObjectId].setCallbackData(_o.data);
        this.roster[_contentObjectId].setCallback(_callback);
        this.roster[_contentObjectId].setContent(_contentObjectId);
        this.roster[_contentObjectId].setButtons(_o.buttons);
        if (typeof(_o.onclose)!="undefined" && _o.onclose!=null)this.roster[_contentObjectId].setOnClose(_o.onclose);
    }
    else{
        if (_o.title != this.roster[_contentObjectId].title) this.roster[_contentObjectId].setTitle(_o.title);
        if (_o.data != this.roster[_contentObjectId].callbackData) this.roster[_contentObjectId].setCallbackData(_o.data);
        if (_callback != this.roster[_contentObjectId].callback) this.roster[_contentObjectId].setCallback(_callback);
        if (_o.width != this.roster[_contentObjectId].width) this.roster[_contentObjectId].setWidth(_o.width);
        if (_o.height != this.roster[_contentObjectId].height) this.roster[_contentObjectId].setHeight(_o.height);
        if (_o.buttons != this.roster[_contentObjectId].buttons) this.roster[_contentObjectId].setButtons(_o.buttons);
        if (_o.onclose != this.roster[_contentObjectId].onclose) this.roster[_contentObjectId].setOnClose(_o.onclose);
        this.roster[_contentObjectId].setCallback(_callback);
    }
    this.roster[_contentObjectId].show();
    return this.roster[_contentObjectId];
}

jQuery.Seek4Dialog.prototype = {
    close : function(_contentObjectId){
        this.roster[_contentObjectId].close();
    }
}


jQuery.Seek4BoxInstance = function(_message,_title, _width, _height, _contentObjectId){
    this.message = _message;
    this.contentObjectId = _contentObjectId;
    this.title = _title?_title : this.defTitle;
    this.width = _width?_width:this.defWidth;
    this.height = _height?_height:this.defHeight;
    this._createBox();
    this.id = _contentObjectId;
    return this;
}

jQuery.Seek4BoxInstance.prototype = {
    box : null,
    ID : null,
    textbox : null,
    defWidth:270,
    defHeight:160,
    defTitle : "Secret4Play",
    defButtons : ["Ok", "Cancel"],
    buttons:0,
    callback : null,
    callbackData : null,
    content : null,
    contentObjectId : null,
    onclose : null,
    
    show : function(){
        $(this.box).dialog("open");
    },
    _createBox : function(){
        this.box = "seek4_alertbox_"+Math.round(Math.random()*100);
        this.textbox = this.box+"_text";
        $("body").append("<div id='"+this.box+"' class='dlgRed'><div id='"+this.textbox+"'></div></div>")
        this.box = "#"+this.box;
        this.textbox = "#"+this.textbox;
        var self = this;
        $(this.box).dialog({
            modal:true,
            overlay: {backgroundColor:'black', opacity:0.7},
            autoOpen:false,
            resizable:false,
            height:self.height,
            width:self.width,
            title:self.title
        });

        $(this.box).dialog("seek4AddOn",null);
        if (this.content != null){
            this.setContent(this.contentObjectId);
        }else{
            this.setText(this.message);
        }
    },
    
    setOnClose : function(_onClose){
        this.onclose = _onClose;
        var self = this;
        jQuery(this.box).bind("dialogclose", function(evnt, ui){if (typeof(self.onclose) != "undefined" && self.onclose!=null) self.onclose() });
    },
    
    setContent : function(_contentId){
        $(this.textbox).html("");
        this.contentObjectId = _contentId;
        this.content = $("#"+_contentId);
        this.content.show();
        $(this.textbox).append(this.content[0]);
        //this.content.parent().remove("#"+_contentId);
    },
    
    setText : function(_text){
        this.message = _text;
        $(this.textbox).html(_text);
    },
    
    setTitle : function(_title){
        this.title = _title?_title:this.defTitle;
        $(this.box).dialog("setData", "title", this.title);
    },
    
    setWidth : function(_width){
        this.width = _width?_width:this.defWidth;
        $(this.box).dialog("setData", "width", this.width );
    },
    setHeight : function(_height){
        this.height = _height?_height:this.defHeight;
        $(this.box).dialog("setData", "height", this.height);
    },
    
    setButtons : function(_btns){
        this.buttons = _btns?_btns:this.defButtons;
        if (this.buttons == "none") return;
        $(this.textbox).parent().children(".ui-dialog-buttonpane").remove();
        ht = "<div class='ui-dialog-buttonpane'>";
        for (var i=0;i<this.buttons.length;i++){
            window["__callbackData"] = this.callbackData;
            ht+="<span class='btn-wrapper'><span class='btn-left'>&nbsp;</span><input type='button' class='btn-middle' onclick=\"jQuery('"+this.box+"').dialog('copyToForm'); if ("+this.callback+"({value:'"+this.buttons[i]+"',index:"+i+", data:__callbackData}) !== false) jQuery('"+this.box+"').dialog('close');\" value='"+this.buttons[i]+"' /><span class='btn-right'>&nbsp;</span></span>";
        }
        ht += "</div>";
        $(this.textbox).parent().append(ht);
    },
    
    setCallback : function(_call){
        this.callback = _call;
    },
    
    setCallbackData : function (_data){
        this.callbackData = _data;
    },
    
    close : function(){
        jQuery(this.box).dialog('close');
    }
 }
})(jQuery);