jQuery(document).ready(function($){
    $('.ratingBox').css({width: "105px", margin: "0 auto"});
    $('.auto-submit-star').rating({
        required: true,
        callback: function(value, link)
        {      
            if(!value)
            {
                return;
            }
    
            var values      = $(this).attr('name').split('_');
            var category_id = parseInt(values[1]);
            var titel_id    = parseInt(values[2]);
            var amount      = parseInt(value);
            
            var votingBox = $(this).parent('.ratingBox');
            
            $(votingBox).html('<img src="/images/loading.gif" alt="Lade.. Bitte Warten" title="Bewertung läuft.. Bitte Warten" />');
            
            //var data = 'category_id='+category_id+'&amp;titel_id='+titel_id+'&amp;amount='+amount;
            
            $.get("vote.php", { category_id: category_id, titel_id: titel_id, amount: amount },
                function(data){
                    var result = $.trim(data);
                    
                    if(result == 'vote complete')
                    {
                        $(votingBox).html('<img class="icon" src="/images/icons/ok.png" title="Erfolgreich bewertet mit '+amount+' Sternen" />');
                    }
                    else if(result == 'already voted')
                    {
                        $(votingBox).html('<img class="icon" src="/images/icons/warning.png" title="Fehler: Du hast diesen Titel schon bewertet!" />');
                    }
                    else if(result == 'max votes exceeded')
                    {
                        $(votingBox).html('<img class="icon" src="/images/icons/warning.png" title="Fehler: Du hast bereits die maximal mögliche Anzahl an Titeln bewertet!" />');
                        $(".auto-submit-star").each(function(){
                            $(this).attr('disabled','disabled');
                        });
                        window.location.reload();
                    }
                    else
                    {
                        $(votingBox).html('<img class="icon" src="/images/icons/warning.png" title="Fehler: '+data+'" />');
                    }
                }
            );
        }     
    });
});
