/*
 * jQuery Star Rating plugin
 * Version 1.0.0 (10/02/2009)
 * @requires jQuery v1.2.1 or later
 *
 * Copyright (c) 2009 Aleksandar Pavic
 */
(function($) {
    $.fn.starRating = function(options) {
        //grab the default options and merge with passed options
        var opts = $.extend({}, $.fn.starRating.defaults, options);

        return $(this).each(function() {
            //merge local options with global options
            var o = $.meta ? $.extend({}, opts, $(this).data()) : opts;

            //grab id of element star rating is used on
            var ratingID = $(this).attr('id');

            //read settings (enabled/disabled) from configuration or starRating object if it's already used
            if(o.clickable == false) {
                $.fn.starRating.clickable[ratingID]=false;
            } else if($.fn.starRating.clickable[ratingID] != false) $.fn.starRating.clickable[ratingID] = true;
            
            //read settings (enabled/disabled) from configuration or starRating object if it's already used
            if(o.hoverable == false) {
                $.fn.starRating.hoverable[ratingID]=false;
            } else if ($.fn.starRating.hoverable[ratingID] != false) {
                $.fn.starRating.hoverable[ratingID] = true;

                $(this).bind('mouseout', function() {
                    var o = $.fn.starRating.opts[this.id];
                    $.fn.starRating.updateStars(o.rating, this.id);
                });
            }

            //remember the settings for rate function
            $.fn.starRating.opts[ratingID]=o;			
            $.fn.starRating.drawStars(o.rating,ratingID);
        });
    };
 
    // rating function, performs ajax rating
    $.fn.starRating.rate = function(val,ratingID) {
        var o = $.fn.starRating.opts[ratingID];
        var paramId = o.paramId;
        var paramValue = o.paramValue;

        //ajax request with callback
        var params = {};
        params[paramId] = ratingID;
        params[paramValue] = val;
        for(param in o.customParams) {
            params[param] = o.customParams[param];
        }

        //perform ajax voting and update stars with new result
        $.get(o.ratingUrl, params , function(data) {
            if(data!='0') {
                if (!o.mayChange) {
                    $.fn.starRating.clickable[ratingID] = false;
                    $.fn.starRating.hoverable[ratingID] = false;
                }

                $("#"+ratingID+"no").html(o.rating);
                $.fn.starRating.updateStars(data,ratingID);
                o.rating = data;

                //fire success event
                if(typeof(o.success)=='function') {
                    o.success();
                }
            } else { 
                if(typeof(o.failure)=='function') o.failure();
            }
        });
    };

    $.fn.starRating.drawStars = function(rate,ratingID) {
        var o = $.fn.starRating.opts[ratingID];
        
        //create stars for voting
        for(var i = 0; i < o.ratingStars; i++) {
            var j = i+1;
            var star = $("<img />").attr({
                title: j,
                alt: j,
                id: (ratingID + "StarRating" + j),
                src: ((j <= rate) ? o.ratedImage : o.basicImage)
            });

            $("#" + ratingID).append(star);

            //add onclick voting functionality if click functionality is enabled
            if($.fn.starRating.clickable[ratingID] == true) {
                $('#' + star[0].id).click(function() {
                    if (!o.mayChange) {
                        $.fn.starRating.hoverable[ratingID] = false;
                    }

                    // $("#" + ratingID).unbind('mouseout');                    
                    if ($.fn.starRating.clickable[ratingID] == true) {
                        $.fn.starRating.rate($(this).attr('title'),ratingID);
                    }

                    return false;
                });
            }
		
            //add hover functionality if click functionality is enabled
            if($.fn.starRating.hoverable[ratingID] == true) {
                $('#' + star[0].id).hover(function() {
                    if($.fn.starRating.hoverable[ratingID] == true) {
                        $("#"+ratingID+"no").html($(this).attr('alt'));
                        if($(this).attr("src") == o.basicImage || $(this).attr("src") == o.ratedImage) {
                            $(this).attr("src",o.hoverImage);
                            $(this).prevAll().attr("src",o.hoverImage);
                        } else {
                            if($(this).attr('alt')<=rate) { //if you are hovering a rated star
                                $(this).attr("src",o.ratedImage);
                                $(this).nextAll(function(){
                                    if($(this).attr('alt')<=rate)$(this).attr("src",o.ratedImage);
                                    else $(this).attr("src",o.basicImage);
                                });
                            } else {
                                if($(this).attr('alt')>=rate) { //if you are hovering unrated star
                                    $(this).nextAll().attr("src",o.basicImage);
                                }
                            }
                        }
                    }
                },function() {
                    if($.fn.starRating.hoverable[ratingID] == true) {
                        if($(this).attr('alt')<=rate) $(this).attr("src",o.ratedImage);
                        else $(this).attr("src",o.basicImage);
                    }
                });
            }		
        }
        //show value as number after the stars ***** 4
        if(o.showNumber) { 
            $("#" + ratingID).append(
                $('<div />').css({"font-size":"12px","font-weight":"bold"}).attr({id: (ratingID + "no")}).html(o.rating)
            );
        }
    }

    $.fn.starRating.updateStars = function(rate,ratingID) {
        var o = $.fn.starRating.opts[ratingID];
        $("#"+ratingID+"no").html(rate);
        for(var i=0;i<o.ratingStars;i++) {
            var j = i+1;
            if(j<=rate) $("#"+ratingID+"StarRating" + j).attr('src',o.ratedImage);
            else $("#"+ratingID+"StarRating" + j).attr('src',o.basicImage);
        }
    }
  
    $.fn.starRating.clickable = [];
    $.fn.starRating.hoverable = [];
    $.fn.starRating.opts = {};
  
    $.fn.starRating.defaults = {
        basicImage    : 'star.gif',
        ratedImage    : 'star_hover.gif',
        hoverImage    : 'star_blue.gif',
        ratingStars   : 5,             //how much stars to draw
        ratingUrl	  : 'rate.php',    //url for ajax request which will manage  ratings
        paramValue    : 'rate',        //parameter holding the value of rating ?rate=4
        paramId       : 'id',          //parameter holding the id of rate ?rate=4&id=photo442
        customParams  : {},            //lets you use custom params for request for example ?rate=4&id=photo442&type=photos ...
        clickable	  : true,          //click enabled or disabled
        hoverable	  : true,          //hover effect enabled or disabled
        success		  : null,          //callback on successful rating
        failure		  : null,          //callback on failure rating
        rating		  : 0,             //current rating
        showNumber	  : false,         //show number after stars
        mayChange     : false          //change the original vote possible?
    };

})(jQuery);
