$(document).ready(function()
    {
        ilikeit_bind ("input[name^='ilikeit']");

        // zobrazi panel ilikeit
        if ($("#ilikeit .ilikeit-item").length > 0)
            $('#ilikeit-panel').show ();
		
        $('#ilikeit .ilikeit-button input').attr('checked', 'checked');
              
       /* $('.ilikeit-button').click(function(){
            alert('test')
            });
        */
    });

function ilikeit_bind (elm)
{
    //alert("test");
    // kliknuti na ilikeit zpusobi aktualizaci bocniho panelu --- unbind().
    $(elm).unbind().click(function()
    {
        
        // loader
        //$('#ilikeit').show().html('nahrávám...');
		
        // sestavi url pro ajax dotaz
        var element = $(this).val();
        var command = $(this).attr('checked') ? "add":"delete";
        var json_url = '/?ilikeit='+command+'&element='+element;
		
        // prida loader
        if (command == "add") {
            ilikeit_loader ();
            $("input[value='"+$(this).val()+"']").attr('checked','checked');
        }
        // zesedne odebirany prvek
        else {
            $(".ilikeit-item[rel="+element+"]").fadeOut("slow");
            $("input[value='"+$(this).val()+"']").attr('checked','');
        }
		
        var params = "";
        // komunikace se serverem
        $.getJSON(json_url,{params:params},function(data){
                    ilikeit_panel (data);
                    ilikeit_bind ("#ilikeit input[name^='ilikeit']");
                }
        );
    });
}

/**
 * Prida do panelu loader
 *
 * @return nill
 **/
function ilikeit_loader ()
{
    var loading_html = $('#ilikeit_loading').html();
		
    if (ilikeit_panel_order != "reverse")
        $('#ilikeit').append (loading_html);
    else
        $('#ilikeit').prepend (loading_html);
}

/**
 * Aktualizuje vypis produktu v panelu to se mi libi
 *
 * @param data string
 * @return nil
 **/
function ilikeit_panel (data)
{
    $('#ilikeit').html(data.html);
			
    if (!data.visible)
        $('#ilikeit-panel').hide ();
    else
        $('#ilikeit-panel').show ();
		
    $('#ilikeit .ilikeit-button input').attr('checked', 'checked');
}