var tools={

    dialogWindow: function(message, options) {
        if (options && options.url) {
            jQuery.ajax(options.url, {
                success:function(data) {
                    delete options.url;
                    if (data.message) {
                        if (data.options) {
                            if (data.options.buttons) {
                                jQuery.each(data.options.buttons, function(index,item) {
                                    if (data.options.buttons[index]["click"]) {
                                        eval("var tmp_funct_"+index+"= "+item["click"]);
                                        data.options.buttons[index]["click"]=eval("tmp_funct_"+index);
                                    }
                                })
                            }

                        }jQuery.extend(options, data.options);
                        tools.dialogWindow(data.message, options);
                    } else {
                        tools.dialogWindow(data, options);
                    }
                }
            });
            return null;
        }
        var option={
            toggleBanners:false,
            hideTitle:false,
            autoOpen:true,
            modal:true,
            buttons:[
              {
                text:'OK',
                click: function(event,ui) {
                   dialog.dialog("close");
                }
              }
            ],
            close:function(event,ui) {
                dialog.dialog("destroy");
                dialog.remove();
            }
        };
        jQuery.extend(option, options);
        var dialog=jQuery("<div />").html(message);
        if (option.toggleBanners) {
            dialog.bind({
                'dialogopen' : function(event, ui){jQuery('.hide-for-popup').css('visibility', 'hidden');},
                'dialogclose' : function(event, ui){jQuery('.hide-for-popup').css('visibility', 'visible');}
            })
        }          
        dialog.dialog(option);
        if (option.hideTitle) jQuery(".ui-dialog-titlebar", dialog.dialog("widget")).addClass("hide");
        dialog.keyup(function(e){
            if (e.keyCode == 13) {
                jQuery(e.currentTarget).dialog("widget").find('button:first').trigger('click');
            }
        });
        return dialog;
    },

    alertWindow: function(message, options) {
        var option={
            title:"Figyelem!"
        };
        jQuery.extend(option, options);
        tools.dialogWindow(message, option);
    },

    confirmWindow: function(message, options) {
        var option={
            buttons:[
                {
                    text:'Igen',
                    click: function(event,ui) {
                        jQuery(this).dialog("close");
                        if (options.confirm) options.confirm();
                    }
                },
                {
                    text:'Nem',
                    click: function(event,ui) {
                        jQuery(this).dialog("close");
                        if (options.cancel) options.cancel();
                    }
                }
            ]
        };
        jQuery.extend(option, options);
        tools.dialogWindow(message, option);
    },

    promptWindow: function(message, options) {
        var option={
            defaultText:'',
            maxLength:0,
            selectText:true,
            hideTitle:true,
            buttons:[
                {
                    text:'OK',
                    click: function(event,ui) {
                        var txt=jQuery("input:first",this).val()
                        jQuery(this).dialog("close");
                        if (options.prompt) {
                            options.prompt(txt);
                        }
                    }
                },
                {
                    text:'Mégsem',
                    click: function(event,ui) {
                        jQuery(this).dialog("close");
                        if (options.cancel) options.cancel();
                    }
                }
            ]
        };
        jQuery.extend(option, options);

        var text=jQuery("<div />").html(message);
        var inputField=jQuery('<input type="text" />').val(option.defaultText);
        if (option.maxLength>0) inputField.attr("maxlength",option.maxLength);
        text.append(inputField.addClass("ui-dialog-confirm-input").wrap('<div>'));

        tools.dialogWindow(text, option);
        if (option.selectText) inputField.focus(function(){this.select();});
        inputField.focus();
    },

    reloadWindow: function() {
       window.location.reload();
    },

    changeLanguage : function() {
        this.dialogWindow("", {
            toggleBanners:true,
            hideTitle   :   false,
            resizable   :   false,
            url         :   "/Language/changeDialog"
        });
    },

    setLanguage : function(lang) {
        jQuery.ajax({
            url         :   "/Language/setLanguage",
            data        :   {language_id:lang},
            success     :   function() {
                window.location.reload();
            }
        });
    },


    addLike: function(response,widget) {
        var ref=widget.getAttribute("ref");
        if (ref.substr(0, 5)=="vote_") {
            jQuery.ajax({
                url : "/Vote/addLike",
                data : {
                    ref : ref
                }
            });
        }
    },
    
    removeLike: function(response,widget) {
        var ref=widget.getAttribute("ref");
        if (ref.substr(0, 5)=="vote_") {
            jQuery.ajax({
                url : "/Vote/removeLike",
                data : {
                    ref : ref
                }
            });
        }
    }


}
