/* Admin jquery function */
/*-------------------------------------------------------------------------*/

// Pretty photo + serach input focus
$(document).ready(function(){
    $("a[rel^='prettyPhoto']").prettyPhoto();

    if (document.location == 'http://www.karaoketexty.cz/') {
        $("#input").focus();
    }
});


// Remove row from table
$(document).ready(function(){
    //$(".del_row").css("border","1px solid red");
    $(".del_row").click(function(event){
        $.get($(this).attr('href'));
        $(this).parent().parent().fadeOut("slow", function() {
            $(this).remove();
        });
        event.preventDefault();
    });
});

// Remove row from table with prompt
$(document).ready(function(){
    $(".del_row_prompt").click(function(event){
        var x = confirm('!!! POZOR opravdu chceš smazat CELÉHO INTERPRETA včetně všechn písní?');
        if (x == true) {
            $.get($(this).attr('href'));
            $(this).parent().parent().fadeOut("slow", function() {
                $(this).remove();
            });
        }
        event.preventDefault();
    });
});

// Toggle admin menu
$(document).ready(function(){
    $(".toggle_admin_menu").click(function(event) {
        $("#admin_menu").slideToggle(600);        

        if (readCookie('hideAdminMenu')) {
            eraseCookie('hideAdminMenu');
            $(this).text('-');
        } else {
            createCookie('hideAdminMenu', 1, 100);
            $(this).text('+');
        }
        event.preventDefault();
    });
  
});

// Fanclubs selector
$(document).ready(function(){
    $("select#fanclubs").change(function(event) {
        window.location = $(this).attr('value');
    });
});


// autocomplete search
$(document).ready(function() {
    
    $('input#input').autocomplete({
        delay: 50,
        source: function(request, response) {

            var q = $('input#input').attr("value");
            
            $.ajax({
                url: "http://search.karaoketexty.cz/index.php",
                dataType: "jsonp",
                data: {
                    q: q
                },
                success: function(data) {                    
                    response(data);                    
                }
            })
        },
        select: function(event, ui) {
            var hostname = top.location.host;
            var url = "http://" + hostname + "/search?q=";
            window.location.replace(url + ui.item.value + "&t=at&from=sug&type=" + ui.item.type + "&id=" + ui.item.id);
        }

    });


    $("#search_type").change(function() {
        var type = $(this).attr("value");
        if (type == 'f') {
            $('input#input').autocomplete("disable");            
        } else {
            $('input#input').autocomplete("enable");            
        }
    });

});

// coockie functions
function createCookie(name,value,days) {
    var expires;
    if (days) {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            expires = "; expires="+date.toGMTString();
    } else {
        expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
