(function($) {

$.extend({
    chimera: {
        custom_confirm: function(prompt, action, title, yes, no) {
            if (title === undefined) title = "Are you sure?";
            if (yes === undefined) yes = "Yes";
            if (no === undefined) no = "No";
            $("body").append('<div id="confirm" title="' + title + '">' + prompt + '</div>');
            $("#confirm").dialog({
                    position: 'center',
                    width: 350,
                    modal: true,
                    resizable: false,
                    show: "scale",
                    hide: "puff",
                    buttons: {
                        "Ano": function() { $(this).dialog('close');action(); },
                        "Ne": function() { $(this).dialog('close'); }
                    },
                    close: function(ev, ui) { $(this).remove(); }
            });
        },
        
        addWindow: function(name, link) {
            var title = name;
            name = $.chimera.webalize(name);
            var i = 0;
            $('div#window' + name).each(function(){i++});
            //console.log(i);
            if(i == 0) {
                $('<div id="window' + name + '"></div>').attr('title', title).appendTo('#wrapper');
                $.get(link + '?target-window=' + 'window' + name);
            }
            else {
                $.chimera.showWindow('window' + name);
            }
        },

        webalize: function(str) {
            var replace = new Array("ě", "š", "č", "ř", "ž", "ý", "á", "í", "é", "ú", "ů", "ť", "ď", "ó", "ň", "Ě", "Š", "Č", "Ř", "Ž", "Ý", "Á", "Í", "É", "Ú", "Ů", "Ť", "Ď", "Ó", "Ň", " ");
            var by      = new Array("e", "s", "c", "r", "z", "y", "a", "i", "e", "u", "u", "t", "d", "o", "n", "E", "S", "C", "R", "Z", "Y", "A", "I", "E", "U", "U", "T", "D", "O", "N", "-");
            for(var i=0; i<replace.length; i++) {
                str = str.replace(replace[i], by[i]);
            }
            str = str.replace(/[A-Z]/g, function(w) {return "-" + w.toLowerCase()} );

            return str;
        },

        showWindow: function(name) {
            //console.log('showed');
            //var str = $('#wrapper').css('height');
            $('#' + name).dialog({
                position: 'center',
                height: Math.round((window.screen.availHeight - 160) * 0.90),
                width: '98%',
                modal: false,
                resizable: true,
                show: 'scale',
                closeText: 'Zavřít',
                beforeclose: function(ev, ui) {ev.preventDefault();$(this).effect('transfer', {to: "#" + name + "-button"}, 'fast', function(){  $(this).remove(); }); return false; }
            });
        },

        windowExists: function(name) {
            var exists = false;
            $('div.ui-dialog #' + name).each(function(){
                exists = true;
            });

            return exists;
        },

        removeWindow: function(name) {
            $('#' + name).remove();
        }
    }
});

})(jQuery);

