
(function($) {

	$.fn.avote = function(c) {
		if (!c) c = {};
		
		c = $.extend({
			type: '',
			type_icon_inactive: '',
			type_icon_active: '',
			type_icon_size: [],
			highscore: 10
		}, c);
		
		$(this).each(function(i, elm) {
			elm = $(elm);
			
			var i = 1;
			var v = parseInt(elm.attr('vote-score'));
			// var a = (elm.attr('vote-active')=='true') ? true : false ;
			var a = true;
			
			while (i<c.highscore+1) {
				var img = (i<=v) ? c.type_icon_active : c.type_icon_inactive ;
				
				var rate = $('<img src="' + img + '" alt="' + i + '"/>')
							.css({width:0, height:0, cursor:'pointer'})
							.appendTo(elm)
							.delay(i*50)
							.animate({width:c.type_icon_size[0], height:c.type_icon_size[1]}, 400);
				
				if (a) {
					rate.bind('click', function(e){
						var cnt = parseInt($(this).attr('alt'));
						var voteid = parseInt($(this).parent().attr('vote-id'));
						
						$.getJSON('http://rvsv.webavance.nl/module/ajax/nl/vote_process?id=' + voteid + '&value=' + cnt + '&callback=?', function(data) {
							if (data.error==0) {
								elm.find('img').attr({ src: c.type_icon_inactive });
								elm.find('img:lt(' + Math.ceil(data.data.score) + ')').attr({ src: c.type_icon_active });
							} else {
								$('<span>')
									.text(data.mssg)
									.css({margin:'0 5px',fontWeight:'bold'})
									.appendTo(elm);
							}
						});
					});
				}
				
				rate.bind('mouseenter', function(e){
					var cnt = parseInt($(this).attr('alt'));
					
					$(this).parent().find('img').attr({ src: c.type_icon_inactive });
					$(this).parent().find('img:lt(' + cnt + ')').attr({ src: c.type_icon_active });
				}).bind('mouseleave', function(e){
					var orig = parseInt($(this).parent().attr('vote-score'));
					
					$(this).parent().find('img').attr({ src: c.type_icon_inactive });
					$(this).parent().find('img:lt(' + orig + ')').attr({ src: c.type_icon_active });
				});
				
				i++;
			}
		});
	}
	
})($);
