/**
 * Star Rating - jQuery plugin
 *
 * Copyright (c) 2007 Wil Stuckey
 * Modified by John Resig
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a degradeable star rating interface out of a simple form structure.
 * Returns a modified jQuery object containing the new interface.
 *   
 * @example jQuery('form.rating').rating();
 * @cat plugin
 * @type jQuery 
 *
 */
jQuery.fn.rating = function(){
    return this.each(function(){
        var div = jQuery("<span/>").attr({
            title: this.title,
            className: this.className
        }).insertAfter( this );

        var averageRating = this.attributes.value.nodeValue,
            url = this.attributes.action.nodeValue,
            averageIndex = Math.floor(averageRating),
            averagePercent = Math.floor(averageRating*100%100),
            enabled = this.attributes.enabled.nodeValue.toLowerCase() == 'true';
        
        div.append(this.title);
        if (enabled) div.append("<span class='cancel'><a href='#0' title='Cancel Rating'>Cancel Rating</a></span>");
        for (var i=1; i<=5; i++)
            div.append(enabled ? 
                "<span class='star'><a href='#" + i + "' title='Give it a " + i + " Star Rating'>" + i + "</a></span>"
                :"<span class='star'><a></a></span>"
            );

        var stars = div.find("span.star");
        if (enabled) {
            // hover events and focus events added
            stars
                .mouseover(drainFill).focus(drainFill)
                .mouseout(drainReset).blur(drainReset)
                .click(click);
        
            // cancel button events
            div.find("span.cancel")
                .mouseover(drainAdd).focus(drainAdd)
                .mouseout(resetRemove).blur(resetRemove)
                .addClass('hover')
                .click(click);
        }
        reset();

        function drainFill(){ drain(); fill(this); }
        function drainReset(){ drain(); reset(); }
        function resetRemove(){ reset(); jQuery(this).removeClass('on'); }
        function drainAdd(){ drain(); jQuery(this).addClass('on'); }
        function drain(){ stars.removeClass("on hover"); } // drain all the stars.

        function click(){
            averageIndex = stars.index(this) + 1;
            averagePercent = 0;
            if (averageIndex == 0) drain();
            var result=jQuery(this).find('a')[0].href.split('#')[1];
            $('#votes').load(url+result);
            //fetchdiv("#votes",url+result);
            return false;
        }
        // fill to the current mouse position.
        function fill( elem ){
            stars.find("a").css("width", "100%");
            if (enabled) stars.slice( 0, stars.index(elem) + 1 ).addClass("hover"); // lt(idx)   -> slice( 0, idx 
        }
        // Reset the stars to the default index.
        function reset(){
            stars.slice(0, averageIndex).addClass("on");
            var percent = averagePercent ? averagePercent : 0;
            if (percent > 0) stars.eq(averageIndex).addClass("on").children("a").css("width", percent + "%");
        }
    }).remove();
};

// fix ie6 background flicker problem.
if ( jQuery.browser.msie == true ) document.execCommand('BackgroundImageCache', false, true);

