$(document).ready(

	function()
	{
		initActionForm();
   	}	

);

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function initActionForm()
{
	if ( !document.getElementById("actions-form") ) return;
	/*
	var rate_text = new Array();
	rate_text['1star'] = "slecht...";
	rate_text['2star'] = "matig";
	rate_text['3star'] = "oke";
	rate_text['4star'] = "goed";
	rate_text['5star'] = "super!";
	*/
	var expr = "#actions-form .gegevens";
	var form = $(expr);
	
	form.hide();
	
	//send to friend	
	$("#actions-form .send a").click(
	
		function()
		{
			if ( $(expr + ":hidden") )
			{
				form.slideDown();
			}
		}
		
	);
	
	$("#actions-form .data a").click(
		
		function()
		{
			form.slideUp();			
		}
	);	
	
	//rating
	
		$("#actions-form .rating .star").each(
			
			function(i)
			{
				var m = i + 1;
				
				$(this).mouseover( 
					function() 
					{ 
						$("#actions-form .rating .star:lt(" + m + ")").addClass("over");
						//set vote text
						/*if($('#voted').val() = 1) {
							$("#actions-form .rating #votes").html(rate_text[$(this).attr('id')]);
						}
						else {
							$("#actions-form .rating #votes").html('U hebt al gestemd!');
						}*/
					}
				);
				
				$(this).mouseout(
					function() 
					{ 
					
						if($('#vote_status').val() != 1) {
							/*							
							if($('#votesval').val() == 1) {
								$("#actions-form .rating #votes").html('(' + $('#votesval').val() + " stem)");
							}
							else {
								$("#actions-form .rating #votes").html('(' + $('#votesval').val() + " stemmen)");
							}
							*/
							$("#actions-form .rating .star:lt(" + m + ")").removeClass("over");
						}
					} 
				);
				
				$(this).click(

						function()
						{	
						
							if($('#voted').val() != 1) 
							{ 
								//ajax post
								var clicked_star = $(this).attr('id');	
								var item_id = $('#item_id').val();								
								//ajax post 
								$.ajax({
								type: "POST",
								url: "/page/ajax/waardering",
								data: {clicked_star:clicked_star, item_id:item_id },
								dataType: "json",
									success: function(json){ 
										
										//set values
										$('#voted').val('1');
										$("#actions-form .rating #votes").html("Bedankt");
										$("#vote_status").val('1');
										
										//set cookie
										createCookie('rating_' + item_id, 1 ,90);
										
										//sleep
										setTimeout(function() {
										
											if(json.votes == 1) {
												$("#actions-form .rating #votes").html('(' + json.votes + " stem)");
											}
											else {
												$("#actions-form .rating #votes").html('(' + json.votes + " stemmen)");
											}
																												
											$("#actions-form .rating .star").mouseover(function(){
											
												$("#actions-form .rating #votes").html('u hebt al gestemd!');
											});
											$("#actions-form .rating .star").mouseout(function(){
											
												if(json.votes == 1) {
													$("#actions-form .rating #votes").html('(' + json.votes + " stem)");
												}
												else {
													$("#actions-form .rating #votes").html('(' + json.votes + " stemmen)");
												}
											});
											$("#vote_status").val('0');
																																
											//set actual rating
											$("#actions-form .rating .star").removeClass("selected");
											$("#actions-form .rating .star").removeClass("over");
											
											$("#actions-form .rating .star").each(
			
												function(j)
												{
													var k = j + 1;
																											
													if(json.rating >= k) {
																										
														$("#actions-form .rating .star:lt(" + k + ")").addClass("selected");
													}

												}
											);
											
										
										}, 3000);
										$('#votesval').val(json.votes);
										
									}
						
								});		
							$("#actions-form .rating .star").removeClass("selected");
						
							$("#actions-form .rating .star:lt(" + m + ")").addClass("selected");
						
							$("#numstars").val( m );
							}
						}		
					
								
				);
			}	
		);

}

