jQuery(document).ready(function()
{
    jQuery('div.rating').initialize_voting();
});

jQuery.fn.initialize_voting = function()
{
    var value = 0;
    
    jQuery(this).mouseout(function()
    {
        jQuery(this).find('span.star').removeClass('hover');
    });
    
    jQuery(this).find('input[@type="submit"]').each(function(i)
    {
        // jQuery(this).attr('value', '');
        jQuery(this).hover(
            function()
            {
                var name = jQuery(this).attr('name');
                
                if (   !name
                    || !(regs = name.match(/\[(.+)\]/)))
                {
                    // console.log('name not found');
                    return;
                }
                
                value = Number(regs[1]);
                
                // console.log('Value is ' + value);
                // console.log(jQuery(this).parents('div.rating').find('input[@type="submit"]').size());
                
                jQuery(this).parents('div.rating').find('input[@type="submit"]').each(function(i)
                {
                    var name = jQuery(this).attr('name');
                    
                    if (   !name
                        || !(regs = name.match(/\[(.+)\]/)))
                    {
                        // console.log('name not found');
                        return;
                    }
                    
                    if (Number(regs[1]) <= value)
                    {
                        jQuery(this).parent(0).addClass('hover');
                        // console.log(Number(regs[1]) + ' set to hover');
                    }
                    else
                    {
                        jQuery(this).parent(0).removeClass('hover');
                        // console.log(Number(regs[1]) + ' should not hover');
                    }
                });
            },
            function()
            {
            }
        );
        
        jQuery(this).click(function()
        {
            jQuery('<input type="hidden" />')
                .attr('id', 'voting_member')
                .attr('name', jQuery(this).attr('name'))
                .attr('value', jQuery(this).attr('value'))
                .appendTo(jQuery(this));
            
        });
    });
}

jQuery.fn.submit_vote = function()
{
    var action = jQuery(this).attr('action');
    
    if (!action)
    {
        action = String(window.location);
    }
    
    if (!action.match(/\?/))
    {
        action = action + '?ajax&vote';
    }
    
    if (!action.match(/ajax&vote/))
    {
        action = action + '&ajax&vote';
    }
    
    var date = new Date();
    
    action = action.replace(/(&random=.+|$)/, '&random=' + date.getTime());
    
    if (!jQuery(this).attr('id'))
    {
        var date = new Date();
        jQuery(this).attr('id', 'jquery_temporary_id_' + date.getTime());
    }
    
    var id = '#' + jQuery(this).attr('id');
    
    // If the loading div is not there, create it
    if (jQuery(this).parent().find('div.loading').size() == 0)
    {
        jQuery('<div></div>')
            .addClass('loading')
            .css({
                display: 'none',
                position: 'absolute'
            })
            .insertBefore(jQuery(this));
    }
    
    // Hide vote divs
    jQuery(this).find('div.voted').fadeOut('slow');
    
    // Show the loading animation
    jQuery(this).parent().find('div.loading')
        .css({
            width: (jQuery(this).width() + 40) + 'px',
            height: (jQuery(this).height() + 50) + 'px',
            marginTop: '10px'
//            marginLeft: '-10px'
        })
        .fadeIn('slow');
        
    // Change the action location of the form
    jQuery(this).attr('action', action);
    
    jQuery(this).ajaxSubmit(
    {
        target: jQuery(id),
        success: function()
        {
            if (jQuery(this).hasClass('not-voted'))
            {
                jQuery(this).addClass('voted');
                jQuery(this).removeClass('not-voted');
            }
            else
            {
                jQuery(this).removeClass('voted');
                jQuery(this).addClass('not-voted');
            }
            jQuery('div.rating').initialize_voting();
            jQuery(this).parent().find('div.loading').fadeOut('slow');
        }
    });
    
    return false;
}