// JavaScript Document

$.fn.voting = function(id,score_full,voted,readonly) {

	var imgpath = 'images/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)]+')';

	//sterren erneer zetten
	init = '<div id="voting_'+current_id+'_msg" class=\"voting_msg_box\">Stem</div>';
	for (i=1;i<6;i++) { init += '<img class="voting_s_'+current_id+'" src="'+imgpath+'/grijs.gif" 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.gif'); }	}
	
	if (!readonly) 
	{ 		
		$(".voting_s_"+current_id).css('cursor','pointer').mouseover(function()
		{ 
			$('#voting_'+current_id+'_msg').show();
			 
			
			if (!voted) { $("#"+current_id+" img").attr('src',imgpath+'/grijs.gif'); $(this).attr('src',imgpath+'/rood.gif');	}
			for (i=1;i<6;i++) 
			{ 
				if (voted!=1)
				{
					$("#voting_s_"+current_id+"_"+i).attr('src',imgpath+'/rood.gif');
					$('#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)
			{						
				$.ajax(
				{
					url:'beoordeel.php',
					data:{ id:id,score:i },
					dataType:"json",
					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;
							$("#"+current_id+" img").attr('src',imgpath+'/grijs.gif'); 
							for (i=1;i<6;i++) {	if (data.stand>=i) {	$("#voting_s_"+current_id+"_"+i).attr('src',imgpath+'/geel.gif'); }	}
							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.gif');   
			for (i=1;i<6;i++) {	if (Math.round(score_full)>=i) {	$("#voting_s_"+current_id+"_"+i).attr('src',imgpath+'/geel.gif'); }	}   
			$('#voting_'+current_id+'_msg').hide();
		});	}
}; 


