
function getRating(storyID,toolID) {
    var $on = $('div#testRater_'+storyID+'_'+toolID).find('.ui-rater-starsOn');
    var $off = $('div#testRater_'+storyID+'_'+toolID).find('.ui-rater-starsOff');
    
    $.jsonp(
    {
        url: $('#ratingServiceURI').attr('value') +'GetToolRating?intStoryID='+storyID+'&intToolID='+toolID+'&callback=?',
        success: function(data, status) {
            if (parseInt(data.intVoteCount) > 0 && data.fltVoteRating > 0) {

                $off.fadeTo(600, 0.1, function() {
                    $on.removeClass('ui-rater-starsHover').width(data.fltVoteRating * 18);
                    var $count = $('div#testRater_'+storyID+'_'+toolID).find('.ui-rater-rateCount');
                    if (parseInt(data.intVoteCount) == 1)
						$count.text(parseInt(data.intVoteCount) + ' rating ');
					else
						$count.text(parseInt(data.intVoteCount) + ' ratings ');
                    $('div#testRater_'+storyID+'_'+toolID).find('.ui-rater-rating').text(data.fltVoteRating.toFixed(1));
                    $('div#testRater_'+storyID+'_'+toolID).find('.ui-rater-rateCountBased').text(' Based on ');
                    
                    $off.fadeTo(600, 1);
                    var r = ($on.width() / $off.width() * 5);
                    $('testRater_'+storyID+'_'+toolID).attr('title', 'Your rating: ' + r.toFixed(1));
                });
            }
        },
        error: function(xOptions, status) {
            $this.hide();
        }
    });
}

$.fn.rater = function(options) {
    var opts = $.extend({}, $.fn.rater.defaults, options);
        
    return this.each(function() {
        var $this = $(this);
        var $on = $this.find('.ui-rater-starsOn');
        var $off = $this.find('.ui-rater-starsOff');
        opts.size = $on.height();
        if (opts.rating == undefined) opts.rating = $on.width() / opts.size;
        if (opts.id == undefined) opts.id = $this.attr('id');

        $off.mousemove(function(e) {
            var left = e.clientX - $off.offset().left;
            var width = $off.width() - ($off.width() - left);
            //width = Math.ceil(width / (opts.size / opts.step)) * opts.size / opts.step;
            $on.width(width);
        }).hover(function(e) {  $on.addClass('ui-rater-starsHover'); }, function(e) {
            $on.removeClass('ui-rater-starsHover'); 
            $on.width($this.find('.ui-rater-rating').text() * opts.size);
        }).click(function(e) {
            var r = ($on.width() / $off.width() * (opts.units * opts.step)) / opts.step;
            //$off.unbind('click').unbind('mousemove').unbind('mouseenter').unbind('mouseleave');
            //$off.css('cursor', 'default'); $on.css('cursor', 'default');
            $.fn.rater.rate($this, opts, r, opts.storyID,opts.toolID);
        }).css('cursor', 'pointer'); $on.css('cursor', 'pointer');
    });
};

$.fn.rater.defaults = {
    postHref: location.href,
    units: 5,
    step: 1
};

$.fn.rater.rate = function($this, opts, rating, storyID, toolID) {
    var $on = $this.find('.ui-rater-starsOn');
    var $off = $this.find('.ui-rater-starsOff');
        
    if($.cookie("toolCookie_"+storyID+"_"+toolID)!=storyID+'_'+toolID){
		$off.fadeTo(600, 0.4, function() {
		$.jsonp(
		{
			url: $('#ratingServiceURI').attr('value') + 'GetUpdatedToolRating?intStoryID='+storyID+'&intToolID='+toolID+'&fltVoteRating=' + rating + '&callback=?',
			success: function(data, status) {
				
				if (parseInt(data.intVoteCount.toString()) > 0) {
					$off.fadeTo(600, 0.1, function() {
						$on.removeClass('ui-rater-starsHover').width(data.fltVoteRating * opts.size);
						var $count = $this.find('.ui-rater-rateCount');
						if (parseInt(data.intVoteCount) == 1)
							$count.text(parseInt(data.intVoteCount) + ' rating ');
						else
							$count.text(parseInt(data.intVoteCount) + ' ratings ');
						$this.find('.ui-rater-rating').text(data.fltVoteRating.toFixed(1));
						$this.find('.ui-rater-rateCountBased').text(' Based on ');
						$off.fadeTo(600, 1);
						$this.attr('title', 'Your rating: ' + rating.toFixed(1));
						alert("Thank you for your rating.");
						$.cookie("toolCookie_"+storyID+"_"+toolID, storyID+'_'+toolID, { expires: 30 });
					});
				}
			},
			error: function(xOptions, status) {
				$this.hide();
			}
		});
    });       
    }
   else{
    alert("Thank you for rating this tool. Your rating is already counted.");
    getRating(storyID,toolID);
   }
};


$.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
