$(function(){
    $('.rating-stars.interactive-mode').each(function(){
        var $div=$(this);
        
        $('img',$div[0]).each(function(idx){
            
            var inactive_star=$(this).attr('src').replace('star.gif','star_inactive.gif');
            var star=$(this).attr('src').replace('star_inactive','star');

            $(this).mouseover(function(){
                $('img:lt(' + (idx+1) + ')',$div[0]).attr('src',star);
                $('img:gt(' + (idx) + ')',$div[0]).attr('src',inactive_star);
                $('span',$div[0]).html($(this).attr('text'));
            });
            $(this).mouseout(function(){
                var value=parseFloat($div.attr('value'));
                if(!value)
                    value=0;

                $('img',$div[0]).attr('src',inactive_star);
                $('img:lt(' + (value) + ')',$div[0]).attr('src',star);
                var $img=$('img[value=' + value + ']',$div[0]);
                $('span',$div[0]).html($img.length?$img.attr('text'):'Not rated!');
            });
            $(this).click(function(){
                $div.attr('value',idx+1);
            });
            if(idx==0)
                $(this).mouseout();
        });
    });
});
