/** 
 * Star Rating - jQuery plugin 
 * Modified by Aaron (http://hypercas.com) 
 **/ 
/** 
 * 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(){ 
	
    var $entity; 
    $entity = jQuery(this).find('select');
	
    return this.each(function(){ 
        var div = jQuery("<div/>").attr({ 
            title: this.title, 
            className: this.className 
        }).insertAfter( this ); 

        //find the form 'select option'
        jQuery(this).find("select option").each(function(){
            div.append( this.value == "" ? 
                //"<div class='cancel'><a href='#0' title='Cancel Rating'>Cancel Rating</a></div>" :
                "" :
                "<div class='star'><a href='#" + this.value + "' title='Give it a " + 
                    this.value + " Star Rating'>" + this.value + "</a></div>" ); 
        }); 
         //left overs from old plugin 
         var averageRating = $entity.val(), 
            url = this.action, 
            averageIndex = $entity.val(), 
            averagePercent = 0; 


        // hover events and focus events added 
        var stars = div.find("div.star") 
            .mouseover(drainFill).focus(drainFill) 
            .mouseout(drainReset).blur(drainReset) 
            .click(click); 

        // cancel button events 
        div.find("div.cancel") 
            .mouseover(drainAdd).focus(drainAdd) 
            .mouseout(resetRemove).blur(resetRemove) 
            .click(cancelclick); 

        reset(); 

        function drainFill(){ drain(); fill(this); } 
        function drainReset(){ drain(); reset(); } 
        function resetRemove(){ reset(); jQuery(this).removeClass('star_on'); } 
        function drainAdd(){ drain(); jQuery(this).addClass('star_on'); } 

        function click(){
            averageIndex = stars.index(this) + 1; 
            averagePercent = 0; 
            $entity.val(averageIndex); 
            if ( averageIndex == 0 )drain();
            /* Removed as the form submiting will cause post anyways. 
            jQuery.post(url,{
            	rating: jQuery(this).find('a')[0].href.slice(1)
            });
            */ 
			//on click on a star for rating remove the .mouseover event for all class="star" div(s).
            $('.star').unbind('mouseover');
            	    
            //get the submitting forms data
			$("#"+jQuery(this).closest("form").attr("id")+"").submit();
            
            return false; 
        } 
        
        function cancelclick(){
            averageIndex = stars.index(this) + 1; 
            averagePercent = 0; 
            $entity.val(averageIndex); 
            if ( averageIndex == 0 )drain(); 
            jQuery.post(url,{
            	rating: jQuery(this).find('a')[0].href.slice(1)
            }); 
//on click on a star for rating remove the .mouseover event for all class="star" div(s).
            $('.star').mouseover(drainFill).focus(drainFill);
            return true; 
        } 

        // fill to the current mouse position. 
        function fill( elem ){ 
            stars.find("a").css("width", "100%"); 
            stars.slice(0, stars.index(elem) + 1 ).addClass("star_hover"); 
        } 

        // drain all the stars. 
        function drain(){ 
            stars.removeClass("star_on star_hover"); 
        } 

        // Reset the stars to the default index. 
        function reset(){ 
            stars.slice(0,averageIndex).addClass("star_on"); 
            var percent = averagePercent ? averagePercent * 10 : 0; 
            if (percent > 0) 
                stars.eq(averageIndex).addClass("star_on").children("a").css("width", percent + "%");
        } 
    }).hide(); 
}; 

// fix ie6 background flicker problem. 
if ( jQuery.browser.msie == true ) {
    document.execCommand('BackgroundImageCache', false, true);
}
