/*****************************************************************************
 * JS funkce pro www.legie.info
 * za pouziti js frameworku jQuery 
 *****************************************************************************/
 
/*****************************************************************************
 * 
 */
function nechci_priznak(id, klicove_slovo){
    if (!confirm('opravdu chcete smazat tento záznam / příznak?')) return true;
    $.ajax({
        type: "GET",
        url: "exec/flag-akce.php",
        data: klicove_slovo + "=0&kasp=kniha&kasp_id=" + id,
        success: function(msg){
            $("#row-"+id).fadeTo("normal", 0, function() {
                $(this).hide();
            } );
        }
    });             
}

/**
 * 
 */
function smazat_hodnoceni(id, karta)
{
    if (!confirm('opravdu chcete smazat vaše hodnocení tohoto díla?')) return true;
    $.ajax({
        type: "GET",
        url: "exec/flag-akce.php",
        data: "ohodnoceno=0&kasp=" + karta + "&kasp_id=" + id,
        success: function(msg){
            $("#row-"+id).fadeTo("normal", 0, function() {
                $(this).hide();
            } );
        }
    });             
}

/**
 * 
 */
function odstran_z_knihovny(id)
{
    if (!confirm('opravdu chcete smazat vaše hodnocení tohoto díla?')) return true;
    $.ajax({
        type: "GET",
        url: "exec/smazat-id.php",
        data: "tab=knihovna&id=" + id,
        success: function(msg){
            $("#row-"+id).fadeTo("normal", 0, function() {
                $(this).hide();
            });
        }
    });             
}

/**
 * 
 */
function show_input_cat(tag)
{
    $(tag).parent().html('<form onsubmit="add_cat(this); return false;"><input id="nazev-kat" type="text" /><input type="submit" value="uložit" /></form>');
    $('#nazev-kat').focus();
}

/**
 * 
 */
function show_input_subcat(tag)
{
    //schovam [+]
    $(tag).hide();
    $(tag).parent().append('<ul><li><form onsubmit="add_subcat(this); return false;"><input id="nazev-kat" type="text" /><input type="submit" value="uložit" /></form></li></ul>');
    $('#nazev-kat').focus();
}

/**
 * 
 */
function add_cat(tag)
{
    // form     li       ul
         
    id = $(tag).parent().parent().attr('id');
    id = id.substring(3);
    $.ajax({
        type: "GET",
        url: "exec/add-knihovna-strom.php",
        data: "otec=" + id + "&nazev=" + $('#nazev-kat').val(),
        success: function(html){
            $('#vizualni_knihovna').html(html);
        }
    });
    
    return false;
}

/**
 * 
 */
function add_subcat(tag)
{
    // form   li     ul        li
         
    id = $(tag).parent().parent().parent().attr('id');
    id = id.substring(3);
    $.ajax({
        type: "GET",
        url: "exec/add-knihovna-strom.php",
        data: "otec=" + id + "&nazev=" + $('#nazev-kat').val(),
        success: function(html){
            $('#vizualni_knihovna').html(html);
        }
    });
    
    return false;
}

/**
 * 
 */
function del_subcat(tag)
{
    // a    li
    id = $(tag).parent().attr('id');
    id = id.substring(3);
    $.ajax({
        type: "GET",
        url: "exec/del-knihovna-strom.php",
        data: "idknihovna_strom=" + id,
        success: function(html){
            $('#vizualni_knihovna').html(html);
        }
    });
}

/**
 * 
 */
function otevri_patro(tag, idpatra)
{
    //  a   li/div   ul/div   td
    id = $(tag).parent().parent().parent().attr('id'); 
       
    $.ajax({
        type: "GET",
        url: "4ajax/otevri_patro.php",
        data: "patro=" + idpatra,
        success: function(html){
            $('#' + id).html(html);
            $.ajax({
                type: "GET",
                url: "4ajax/knihy_patra.php",
                data: "patro=" + idpatra,
                success: function(html){
                    $('#' + id + "-vypis").html(html);
                }
            });            
        }
    });
}

/**
 * presun ve stromu sve vizualni knihovny
 */
function zmenit_umisteni(tag){
    
    id = $(tag).parent().attr('id').substring(3);
    
    // ve kterem jsme bloku - vlevo ci vpravo
    tdid = $(tag).parent().parent().parent().attr('id');
    
    // ul-id v opacnem bloku
    if (tdid == 'leve-patro-vypis') 
    {
        cil = $("#prave-patro-vypis ul").attr('id').substring(3);
    }
    else if (tdid == 'prave-patro-vypis') 
    {
        cil = $("#leve-patro-vypis ul").attr('id').substring(3);
    }
    
    if ((id > 0) && (cil > 0)) 
    {
        $.ajax({
            type: "GET",
            url: "4ajax/zmenit_umisteni.php",
            data: "co=" + id + "&cil=" + cil,
            success: function(msg){
                $("#li-" + id).fadeTo("fast", 0, function() {
                    $("#li-" + id).appendTo("#ul-" + cil).fadeTo("fast", 1);
                });
            }
        });             
    }

}

/**
 * filtrace pro knihy ve virtualni knihovne
 */
function filtrKnihyPatra(tag) 
{
    hledany_retezec = $(tag).val();
    hledany_retezec = jQuery.trim(hledany_retezec);
    delka_hledaneho_retezce = $(tag).val().length;
   
    // filtruji, jen pokud je delka min 3 znaky
    if (delka_hledaneho_retezce > 2 || delka_hledaneho_retezce == 0) {
        kde_hledam = $(tag).attr('name');
        // cast_knihy nebo cast_autora
        
        // zjistim kde jsem
        //      input td       tr       tbody    table    td
        tdBlokId = $(tag).parent().parent().parent().parent().parent().attr('id');        
        
        // zjistim obsah druheho input boxu
        if (kde_hledam == 'cast_knihy') 
        {
            druhy_input = 'cast_autora';
        }
        else if (kde_hledam == 'cast_autora') 
        {
            druhy_input = 'cast_knihy';
        }
        
        druhy_input_retezec = $("#" + tdBlokId + " input[name='" + druhy_input + "']").val();
        
        druh_ret = '';
        if (jQuery.trim(druhy_input_retezec).length > 0) {
            druh_ret = '&' + druhy_input + '=' + druhy_input_retezec;
        }
        
        // zjistim id UL se seznamem knih 
        tdid = $(tag).parent().parent().parent().parent().parent().attr('id');
        ulid = $("#"+ tdid + " ul").attr('id');
        
        idpatra = ulid.substring(3);

        $.ajax({
            type: "GET",
            url: "4ajax/knihy_patra.php",
            data: "patro=" + idpatra + "&" + kde_hledam + "=" + hledany_retezec + druh_ret,
            success: function(html){
                $('#ul-' + idpatra).html(html);
            //$("#" + tdid).html(html);
            }
        });            
    }
}

$(function() {
    $(".carousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        btnMoreNext: ".moreNext",
        btnMorePrev: ".morePrev",
        btnLast: ".last",
        btnFirst: ".first",
        mouseWheel: true,
        visible: 5,
        scroll: 1,
        moreScroll: 5,
        circular: false

    });
});

/**
Vertigo Tip by www.vertigo-project.com
Requires jQuery
*/
this.vtip=function(_selector){
    this.xOffset=-10;
    this.yOffset=10;
    $(_selector).unbind().hover(function(a){
        this.t=this.title;
        if (this.t != "") {
            this.title="";
            this.top=(a.pageY+yOffset);
            this.left=(a.pageX+xOffset);
            $("body").append('<p id="vtip"><img id="vtipArrow" />'+this.t+"</p>");
            $("p#vtip #vtipArrow").attr("src","engine/vtip_arrow.png");
            $("p#vtip").css("top",this.top+"px").css("left",this.left+"px").fadeIn("slow")
        }
    },function(){
        if (this.title == "" && this.t != "") {
            this.title=this.t;
        }
        $("p#vtip").fadeOut("slow").remove()
    }).mousemove(function(a){
        this.top=(a.pageY+yOffset);
        this.left=(a.pageX+xOffset);
        $("p#vtip").css("top",this.top+"px").css("left",this.left+"px")
    })
//    .click(function(){
//        if (this.title == "" && this.t != "") {
//            this.title=this.t;
//        }
//        $("p#vtip").fadeOut("slow").remove();
//        return true;
//    })
};
