// ha a posturl nem success akkor nem jegyzi meg a valuet
(function($){
	$.fn.rate = function(options) {
	
		var defaults = {
			posturl : "",
			rateval: 0
		};
		
		var options = $.extend(defaults, options);
		return this.each(function() {
			obj = $(this);
			obj.mousemove(function(e){
				var percent = (e.pageX-$(this).offset().left) / $(this).width() * 100;
				$(this).find(".value").width(percent+"%");
			});
			obj.click(function(e){
				var percent = (e.pageX-$(this).offset().left) / $(this).width() * 100;
				$.post(
					options.posturl,
					{
						value: percent
					},
					function (data, textStatus) {
						if (textStatus == 'success') {
							options.rateval = Math.round(percent);
							obj.refresh();
						}
				  },
				  "json"
				);
			});
			obj.mouseleave(function(e){
				obj.refresh();
			});
			obj.refresh = function() {
				v = options.rateval>0 ? options.rateval : 0;
				$(this).find(".value").width(v+"%");
			}
			obj.refresh();
		});
	};
})(jQuery);

