// JavaScript Document
var votings = Array();
$.fn.voting = function() {

	$(this).each(function()
	{
		var ie6 = ($.browser.msie && ($.browser.version == "6.0")) ? this : false; 
		if (ie6) { ext = 'gif'; } else { ext = 'png'; }
		params = $(this).html();
		if (!params) { return false; }
		params = params.split(' ');
		id = params[0]; 
		score_full = params[1];
		voted = params[2];
		readonly = params[3];

		
		if (typeof votings[id] != 'undefined') { score_full = votings[id]; voted=1; }
		
		$(this).show();
		
		var imgpath = '/img/ster',
			init ='',
			current_id=$(this).attr('id'),
			voted,
			beoordeling_tekst=Array('niet gestemd','slecht','matig','redelijk','goed','zeer goed'),
			scores=Array();
		var score2 = Math.round(score_full*100)/100;
		var huidige_score_tekst = 'Huidige score: '+score2+' ('+beoordeling_tekst[Math.round(score_full)]+')';
		if (score2==0) { huidige_score_tekst = 'Huidige score: (<i>nog niet gestemd</i>)'; }
		//sterren erneer zetten
		init = '<div id="voting_'+current_id+'_msg" class=\"voting_msg_box\"></div>';
		for (i=1;i<6;i++) { init += '<img class="voting_s_'+current_id+'" src="'+imgpath+'/grijs.'+ext+'" id="voting_s_'+current_id+'_'+i+'" alt=""/>'; }
		
		$("#"+current_id).html(init);
		for (i=1;i<6;i++) {	if (Math.round(score_full)>=i) { $("#voting_s_"+current_id+"_"+i).attr('src',imgpath+'/geel.'+ext); } }

		if (readonly!='1') 
		{ 			
			$(".voting_s_"+current_id).css('cursor','pointer').mouseover(function()
			{ 
				$('#voting_'+current_id+'_msg').show();

				if (voted==-1) { $('#voting_'+current_id+'_msg').html('Log in om te stemmen'); return false;   }
				
				if (!voted) { $("#"+current_id+" img").attr('src',imgpath+'/grijs.'+ext); $(this).attr('src',imgpath+'/rood.'+ext);	}
				for (i=1;i<6;i++) 
				{ 
					if (voted!=1)
					{
						$("#voting_s_"+current_id+"_"+i).attr('src',imgpath+'/rood.'+ext);
						$('#voting_'+current_id+'_msg').html(huidige_score_tekst+'<br/>Jouw beoordeling: <b>'+beoordeling_tekst[i]+'</b>');
					}
					else
					{
						$('#voting_'+current_id+'_msg').html(huidige_score_tekst+'<br/><span style=\"color:red\">Je hebt al gestemd op deze foto.</span>');
					}	
					if ($(this).attr('id')=='voting_s_'+current_id+'_'+i) {	break; } 
				}			
			});	
			
			
			$("#"+current_id).click(function() 
			{ 	
				if (voted==-1) { return false; }
				if (voted==0)
				{	
					
					$.ajax(
					{
						url:'/ajax/beoordeel.php',
						data:{ id:id,score:i },
						dataType:"json",
						type:"POST",
						beforeSend :function() { $('#voting_'+current_id+'_msg').html('<b>Even geduld a.u.b.</b>');	 },
						success:function(data)
						{ 
							$('#voting_'+current_id+'_msg').html('<b>Je stem is opgeslagen.</b>');
							//if (data.stand!=score_full) 
							//{ 
								score_full = data.stand;
								votings[id]=score_full; 
								$("#"+current_id+" img").attr('src',imgpath+'/grijs.'+ext); 
								for (i=1;i<6;i++) {	if (data.stand>=i) { $("#voting_s_"+current_id+"_"+i).attr('src',imgpath+'/geel.'+ext); } }
								score2 = Math.round(score_full*100)/100;
								huidige_score_tekst = 'Huidige score: '+score2+' ('+beoordeling_tekst[Math.round(score_full)]+')';
							//}
							voted=1;
							$(".voting_s_"+current_id).css('cursor','pointer').mouseover(function(){});
							
						},
						error:function(xhr,err,e){ alert("Er is een fout opgetreden"); }
						}); 			
					return false;
					}
					else
					{
						$('#voting_'+current_id+'_msg').html(huidige_score_tekst+'<br/><span style=\"color:red\">Je hebt al gestemd op deze foto.</span>');
					}
				});
		
	
			
			$("#"+current_id).mouseout(function() 
			{ 
				$("#"+current_id+" img").attr('src',imgpath+'/grijs.'+ext);   
				for (i=1;i<6;i++) {	if (Math.round(score_full)>=i) {	$("#voting_s_"+current_id+"_"+i).attr('src',imgpath+'/geel.'+ext); }	}   
				$('#voting_'+current_id+'_msg').hide();
			});	}

	});
}; 


