// @TODO: so many many many many hardcoded things around here...

$(document).ready(function() {
    
    $(".ic_starRate").live("mouseover", function() { // Hover function
        
        myRate = $(this).attr('rateValue');
        highlightUntill($(this), myRate, false);
        
    }) // -end hover)
    
    
    $(".ic_starRate").live("mouseout",function() { // out function
        
        myRate = $(this).parent().attr('initialrate');
        highlightUntill($(this), myRate, false);
        
    })
    
     $(".ic_starRate").live("click", function() {
        
        
        var myhash = $(this).parent().attr('ratehash');
        var myrating = $(this).attr('rateValue');
        var el = $(this);
        $(this).parent().hide().after('<img class="loading" src="/ressources/images/two/ajax-loader.gif" width="25px" height="25px" />');
        
        url = $(this).parent().attr('ratingurl');
        //alert("Sending " + myhash + " and "+ myrating +" to " + url)
        $.get(url, { hash: myhash, rating: myrating },
        
        function(data)
        {
            if(data != 'false')
            {
                //el.parent().attr('initialrate', myrating);
                stars_prevParent = el.parent().parent();
                stars_prevParent.children(".loading").remove();
                el.parent().replaceWith(data);
                highlightUntill(el, myrating, true);
                stars_prevParent.children(".tooltip").tooltip();
                
                $('.ic_starRating .tooltip').tooltip(); 
            }
            else
            {
                
                el.parent().parent().children(".loading").remove();
                el.parent().show();
                alert("Une erreur est survenue lors de l'enregistrement de votre vote. Veuillez réeassayeer ou contacter le responsable si le problème persiste.");
            }
        });
        
        
     })
  
})


function highlightUntill(el, rate, changeAlt)
{
    //alert(rate);
    el.parent().children(".ic_starRate").each(function() {
        
            if(changeAlt)
            {
                $(this).attr('title', 'Vous venez de donner une note de ' + rate)
                $(this).attr('alt', 'Vous venez de donner une note de ' + rate)
            }
            
           //alert($(this).attr('rateValue') +' <= '+ rate);
            if($(this).attr('rateValue') <= rate)
                $(this).attr('src', $(this).parent().attr('upUrl'))
            else
                $(this).attr('src', $(this).parent().attr('downUrl'))
        })
        
}
