//Vykresli dialog pro nacitani
function sowa_loading(title, text, width, height){
    var $loading = $('<div><img src="/m/icons/loading.gif" alt="loading" /> '+text+'</div>');
    $loading.dialog({title: title, modal: true, width:width, height:height,});
    return $loading
}

//Vykresli dialog pro alert
function sowa_alert(title, text, width, height){
    var $alert = $('<div><img src="/m/icons/delete.png" alt="loading" /> '+text+'</div>');
    $alert.dialog({title: title, modal: true, width:width, height:height,});
    return $alert
}

function sowa_dialog(selector, title, width, height, button){

    $(selector).click(function(event) {

        var buttons = {}
        buttons[button] = function() {
            $(this).find("form").submit();
            if ($(this).find(".error").size() == 0){
                $(this).dialog("close");
                sowa_loading("Čekejte prosím", "Čekejte prosím", 400, 100);
            }
        }
        buttons["Storno"] = function() {
            $dialog.html("");
            $(this).dialog("close");
        }

         var $dialog = $('<div></div>').dialog({
            autoOpen: false,
            modal: true,
            title: title,
            width: width,
            height: height,
            buttons: buttons,
        });

        $loading = sowa_loading("Čekejte prosím", "Čekejte prosím, probíhá načítání formuláře", 400, 100);
        $dialog.load($(this).attr("href"), function(response, status, xhr){
            $loading.dialog('close');
            if(status == "error"){
                sowa_alert("Chyba", "Chyba při načítání formuláře", 400, 200);
            }
            else{
                $dialog.dialog('open');
            }
        });
        return false;
    });


}


function sowa_popup(url, title, width, height){

        var buttons = {}
        buttons["Zavřít"] = function() {
            $dialog.html("");
            $(this).dialog("close");
        }

         var $dialog = $('<div></div>').dialog({
            autoOpen: false,
            modal: true,
            title: title,
            width: width,
            height: height,
            buttons: buttons
        });

        $loading = sowa_loading("Čekejte prosím", "Čekejte prosím, probíhá načítání", 400, 100);
        $dialog.load(url, function(response, status, xhr){
            $loading.dialog('close');
            if(status == "error"){
                sowa_alert("Chyba", "Chyba při načítání", 400, 200);
            }
            else{
                $dialog.dialog('open');
            }
        });
        return false;


}



