/*
 *	Vote Stars plugin version 0.1
 *
 *	Konrad Dzwinel - socjum.pl
 */

(function($) {

	// plugin definition
	$.fn.vote_stars = function(options) {
		var opts = $.extend({}, $.fn.vote_stars.defaults, options);

		// iterate and reformat each matched element
		return this.each(function() {
			var options = $.extend({}, opts, {
				ratingMax: $(this).find('span.vote').length,
				elementId: $(this).attr('id').substring(opts.idSuffix.length),
				currentRating: Number($(this).find('.currentRating').html()),
				parentElement: $(this)
			});
			var sendingLock = false;

			setCurrentRating(options);

			$(this).mouseleave(function(){
				if(sendingLock == false)
				{
					setCurrentRating(options);
				}
			});

			if(options.canVote)
			{
				$(this).children('span.vote').css('cursor','pointer');

				$(this).children('span.vote').mouseover(function(){
					if(sendingLock == true)
					{
						return;
					}

					var position = Number($(this).attr('title'))-1;
					$(this).removeClass('ico_star_empty').removeClass('ico_star_half').addClass('ico_star_full');

					$(this).siblings('span.vote').removeClass('ico_star_full').removeClass('ico_star_half').addClass('ico_star_empty');
					$(this).siblings('span.vote:lt('+position+')').removeClass('ico_star_empty').removeClass('ico_star_half').addClass('ico_star_full');
				}).click(function(){
					if(sendingLock == true)
					{
						return;
					}

					var vote = $(this).attr('title');

					if(options.voteAjax==true)
					{
						$.ajax({
							type: "GET",
							url: options.sendTo+options.elementId+"/"+vote,
							beforeSend: function(){
								sendingLock = true;
								options.parentElement.fadeTo('fast',.7);
							},
							success: function(msg){
								options.parentElement.children('span.vote').css('cursor','default');
								options.votingSuccess(msg);
							},
							error: function(){
								sendingLock = false;
							},
							complete: function(){
								options.parentElement.fadeTo('fast',1);
							}
						});
					}
					else
					{
						location.href = options.sendTo+options.elementId+"/"+vote;
					}
				});
			}
			else
			{
				$(this).children('span.vote').css('cursor','default');
			}
		});
	};

	// plugin defaults
	$.fn.vote_stars.defaults = {voteAjax: false, canVote: true, votingSuccess: function(){}};

	function setCurrentRating(options)
	{
		var object = options.parentElement;

		if(options.currentRating >= 1 && options.currentRating <= options.ratingMax)
		{
			var rating = options.currentRating-1;
		}
		else
		{
			object.children('span.vote').removeClass('ico_star_full').removeClass('ico_star_half').addClass('ico_star_empty');
			return;
		}

		var ratingFloor = Math.floor(rating);
		object.children('span.vote:lt('+ratingFloor+')').removeClass('ico_star_empty').removeClass('ico_star_half').addClass('ico_star_full');
		object.children('span.vote:eq('+ratingFloor+')').removeClass('ico_star_empty').removeClass('ico_star_half').addClass('ico_star_full');
		object.children('span.vote:gt('+ratingFloor+')').removeClass('ico_star_full').removeClass('ico_star_half').addClass('ico_star_empty');

		if(Math.round(rating)>rating)
		{
			object.children('span.vote:eq('+Math.round(rating)+')').removeClass('ico_star_empty').removeClass('ico_star_full').addClass('ico_star_half');
		}
	};

})(jQuery);