// JavaScript Document

    // This is the first thing we add ------------------------------------------
	// NOTA: Cambiar ruta de ratings.php por la que corresponda
    jQuery(document).ready(function() {
        
        jQuery('.rate_widget').each(function(i) {
            var widget = this;
            var out_data = {
                widget_id : jQuery(widget).attr('id'),
                fetch: 1
            };
            jQuery.post(
                'http://www.cruzrojamadrid.org/contenidos/captcha/ratings.php',
                out_data,
                function(INFO) {
                    jQuery(widget).data( 'fsr', INFO );
                    set_votes(widget);
                },
                'json'
            );
        });
    

        jQuery('.ratings_stars').hover(
            // Handles the mouseover
            function() {
                jQuery(this).prevAll().andSelf().addClass('ratings_over');
                jQuery(this).nextAll().removeClass('ratings_vote'); 
            },
            // Handles the mouseout
            function() {
                jQuery(this).prevAll().andSelf().removeClass('ratings_over');
                // can't use 'this' because it wont contain the updated data
                set_votes(jQuery(this).parent());
            }
        );
        
        
        // This actually records the vote
		// NOTA: Cambiar ruta de ratings.php por la que corresponda
        jQuery('.ratings_stars').bind('click', function() {
            var star = this;
            var widget = jQuery(this).parent();
            
            var clicked_data = {
                clicked_on : jQuery(star).attr('class'),
                widget_id : jQuery(star).parent().attr('id')
            };
            jQuery.post(
                'http://www.cruzrojamadrid.org/contenidos/captcha/ratings.php',
                clicked_data,
                function(INFO) {
                    widget.data( 'fsr', INFO );
                    set_votes(widget);
                },
                'json'
            ); 
        });
        
        
        
    });

    function set_votes(widget) {

        var avg = jQuery(widget).data('fsr').whole_avg;
        var votes = jQuery(widget).data('fsr').number_votes;
        var exact = jQuery(widget).data('fsr').dec_avg;
    
        window.console && console.log('and now in set_votes, it thinks the fsr is ' + jQuery(widget).data('fsr').number_votes);
        
        jQuery(widget).find('.star_' + avg).prevAll().andSelf().addClass('ratings_vote');
        jQuery(widget).find('.star_' + avg).nextAll().removeClass('ratings_vote'); 
        jQuery(widget).find('.total_votes').text( votes + ' votos (' + exact + '%)' );
    }
    // END FIRST THING