/**
 * 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 
 *
 */
 /*
 	params
		showMessages: bool
		messages : array
 
 */
 
jQuery.fn.rating = function(params){

    return this.each(function(){
        var div = jQuery("<div/>").attr({
            title: this.title,
            className: this.className
        }).insertAfter( this );
	
		if(params.showMessages){
		
			var messages = params.messages;
			div.append("<div id='ratingMessage' class='label'>" + messages[0] + "</div>");
        }
		
		jQuery(this).find("select option").each(function(){
          
			div.append( this.value == "0" ?
               "<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>" );
		
        });
		
        var averageRating = this.title.split(/:\s*/)[1].split("."),
            url = this.action,
            averageIndex = averageRating[0],
            averagePercent = averageRating[1];

        // 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(click);
		
		var entry_id = jQuery(this).find("input[@name=entry_id]")[0].value;
		var titles = jQuery(this).find("input[@name=titles]")[0].value;
		
        reset();

        function drainFill(){ drain(); fill(this); if(params.showMessages){showMessage(this);} }
        function drainReset(){ drain(); reset(); if(params.showMessages){removeMessage();} }
        function resetRemove(){ reset(); jQuery(this).removeClass('on'); }
        function drainAdd(){ drain(); jQuery(this).addClass('on'); }

        function click(){
  
		    averageIndex = stars.index(this) + 1;
            averagePercent = 0;

            if ( averageIndex == 0 )
                drain();
				

            jQuery.post(url,{
 				rating: averageIndex,
				entry_id: entry_id,
				titles: titles
            }, function(data){
            	if(params.showMessages){
            	var msg = data.split("/");
				div.find("div#ratingMessage").html(msg[1]);
				}
            
            
            });
			
			/*if(params.showMessages){
				div.find("div#ratingMessage").html("Thanks for rating!");
			}*/
			
			
			
			div.find("div.star").unbind();
			/*stars.each(function(){
				jQuery(this).empty();
				jQuery(this).removeClass("hover");
				jQuery(this).addClass("on");
				jQuery(this).append("<span class='star'>&nbsp;</span>");
			});*/
			
			stars.slice(0,averageIndex).empty().removeClass("hover").addClass("on").append("<span class='star'>&nbsp;</span>");
            stars.slice(averageIndex - 1).empty().append("<span class='star'>&nbsp;</span>");
			
			return false;
        }

        // fill to the current mouse position.
        function fill( elem ){
            stars.find("a").css("width", "100%");
            stars.slice(0, stars.index(elem) + 1 ).addClass("hover");
        }
    
        // drain all the stars.
        function drain(){
            stars.removeClass("on hover");
        }
		
		//add to show a message above star
		function removeMessage(){
			
			div.find("div#ratingMessage").html(messages[0]);
		}
		
		//add to remove the message above star
		function showMessage(elm){
			div.find("div#ratingMessage").html(messages[stars.index(elm) + 1]);
		}
        // Reset the stars to the default index.
        function reset(){
            stars.slice(0, averageIndex).addClass("on");

            var percent = averagePercent ? averagePercent * 10 : 0;
            if (percent > 0)
                stars.slice(0, averageIndex + 1).addClass("on").children("a").css("width", percent + "%");
        }
    }).remove();
};

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