//*******************************************************
//***** Javascript voor de Kinderjury - top 3 en stemmen
//*******************************************************

$(document).ready(function(){ 
						   
	$(function() {
		$("#sortable").sortable({ 
			cursor: 'move',
			placeholder: 'ui-state-highlight',
			update: function(){
			
				$('#sortable li').each(function(i){
					$(this).attr("id", "place_" + (i + 1));
				});
				
				/* Call session-updater */
				updateShelves()				
			}
		});
		
		$("#sortable").disableSelection();
		
	});
	
	//start the ajax  
	/*
	$.ajax({
		//the php file that processes the data 
		url: SESSION_URL,
		
		//GET method is used  
		type: "GET",
		
		//Do not cache the page  
		cache: false,
		
		//success  
		success: function(xml){			
			
			$(xml).find('book').each(function(){
		
				var id = $(this).find('id').text();
				var title = $(this).find('title').text();
				var place = $(this).attr("place");	
				if(id!="" && id!="null" && title!=""){
					$("#"+place).find(".id").html(id);
					$("#"+place).find(".title").html(title);
					$("#"+place).find(".delete a").css("display","block");
				}	
				
				// Call session-updater 
				updateShelves()
		     
			 });
		}	
	});
	*/
	
     $("#sortable .delete").click(function() {
	 	
			var shelve = $(this).parent();
			titleDIV = shelve.find(".title");
			deleteA = shelve.find(".delete a");
			idDIV = shelve.find(".id");
			
			titleDIV.html("&nbsp;");
			deleteA.css("display","none");
			idDIV.html("");
		  
			/* Call session-updater */
			updateShelves()
		  
     }); 

	$("#submitvote").click(function(){
		$(".vote_error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var ageReg = /^[0-9]+$/;


		var nameVal = $("#namevote").val();
		if(nameVal == '') {
			$("#namevote").after('<span class="vote_error">Vul je naam in</span>');
			hasError = true;
		}		
			
		var emailVal = $("#emailvote").val();
		if(emailVal == '') {
			$("#emailvote").after('<span class="vote_error">Je moet een e-mailadres invullen</span>');
			hasError = true;
		} else if(!emailReg.test(emailVal)) {
			$("#emailvote").after('<span class="vote_error">Dit is geen geldig e-mailadres.</span>');
			hasError = true;
		}

		var ageVal = $("#agevote").val();
		if (ageVal == '') {
			$("#agevote").after('<span class="vote_error">Vul een leeftijd in</span>');
			hasError = true;
		}
		else if (!ageReg.test(ageVal)) {
			$("#agevote").after('<span class="vote_error">Je leeftijd mag alleen uit cijfers bestaan.</span>');
			hasError = true;
		}
		else if (parseInt(ageVal) < 6 || parseInt(ageVal) > 12) {
			$("#agevote").after('<span class="vote_error">Je mag alleen stemmen als je tussen de 6 en 12 jaar oud bent.</span>');
			hasError = true;
		}				
				
				
		
		if(hasError == false) {
			 //$(this).hide();
			 //alert("send");
			 var data = '&first_id=' +  $("#place_1").find(".id").html()
			 data = data + '&second_id=' +  $("#place_2").find(".id").html()
			 data = data + '&third_id=' +  $("#place_3").find(".id").html()
			 data = data + '&name=' + nameVal;
			 data = data + '&email=' + emailVal;
			 data = data + '&age=' + ageVal;
			 data = data + '&vote=1'
			
			$.ajax({
				//the php file that processes the data 
				url: SESSION_URL,
			
			    //pass the data           
			    data: data,  
							
				//GET method is used  
				type: "POST",
				
				//Do not cache the page  
				cache: false,
				
				//success  
				success: function(xml){
					voteProcedure(3)
				}
			});
			
		}
		
		return false;
		
	});
	
});

function voteForBook(id, title){
	var full = true;
	var alreadyvoted = false;
	
	if($("#help_shelve").css("display")=="block"){
		$("#help_shelve").toggle("slow");
	}
	
	id1 = $("#place_1").find(".id").html();
	id2 = $("#place_2").find(".id").html();
	id3 = $("#place_3").find(".id").html();
	
	if(id != id1 && id !=  id2 && id !=  id3 ){
		$('#sortable li').each(function(i) {
			
			if($(this).find(".title").html()=="&nbsp;"){
				
				$(this).find(".id").html(id);
				$(this).find(".title").html(title);
				$(this).find(".delete a").css("display","block");
				
				/* Call session-updater */
				updateShelves()
				toggleHyves(2);
				
				full = false;
				return false;
				
			}
		});	
	}else{
		alreadyvoted = true;
	}
	if(alreadyvoted){
		alert("Dit boek staat al in je boekenkast.");
	}else if (full) {
		alert("Helaas, je boekenkast zit al vol. Verwijder eerst een boek uit je kast.");
	}
	
}

function updateShelves(){
	
	 var data = '&first_id=' +  $("#place_1").find(".id").html() + '&first_title=' +  $("#place_1").find(".title").html();
	 data = data + '&second_id=' +  $("#place_2").find(".id").html() + '&second_title=' +  $("#place_2").find(".title").html();
	 data = data + '&third_id=' +  $("#place_3").find(".id").html() + '&third_title=' +  $("#place_3").find(".title").html();
	 data = data + '&set=1'
	
	$.ajax({
		//the php file that processes the data 
		url: SESSION_URL,

        //pass the data           
        data: data,  
 				
		//GET method is used  
		type: "POST",
		
		//Do not cache the page  
		cache: false,
		
		//success  
		success: function(xml){
		
		}
	});	
}

/* Voting */
function voteProcedure(step){

	switch (step) {
		case 1:
			id1 = $("#place_1").find(".id").html();
			id2 = $("#place_2").find(".id").html();
			id3 = $("#place_3").find(".id").html();		
			
			if (id1 == '' && id1 == '' && id1 == '') {
					
					alert("Je moet minstens 1 boek in je kast hebben staan.");	
					return false;
					
			}else{
				$("#book_shelves_extend").animate({ 
					 height: "333"
				}, 1500);	
				$("#vote_page_1").css("display","none");
				$("#vote_page_2").css("display","block");
			}
		break;
		case 2:
			$("#vote_page_2").css("display","none");
			$("#vote_page_3").css("display","block");
		break;	
		case 3:
			$("#vote_page_3").css("display","none");
			$("#vote_page_4").css("display","block");
		break;
		case 4:
			$("#book_shelves_extend").animate({ 
				 height: "35"
			}, function() {
				$("#vote_page_4").css("display","none");
				$("#vote_page_2").css("display","none");
				$("#vote_page_1").css("display","block");
			});
		break;						
	}	
	

}

function toggleHyves(step){
	switch (step) {
		case 1:	
	
			id1 = $("#place_1").find(".id").html();
			id2 = $("#place_2").find(".id").html();
			id3 = $("#place_3").find(".id").html();			
			
			if (id1 == '' && id1 == '' && id1 == '') {
			
				alert("Je moet minstens 1 boek in je kast hebben staan.");
				return false;
				
			}
			else {
				
				var embedvalue = '<object width="300" height="398"><param name="movie" value="http://www.kinderjury.nl/hyves/HyvesBlok.swf?id='+id1+'/'+id2+'/'+id3+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.kinderjury.nl/hyves/HyvesBlok.swf?id='+id1+'/'+id2+'/'+id3+'" type="application/x-shockwave-flash" width="300" height="398" allowscriptaccess="always" allowfullscreen="false"></embed></object>';
				$("#embed_code").val(embedvalue);
				
				$("#book_shelves_extend").animate({
					height: "333"
				}, 1500);
				$("#vote_page_1").css("display", "none");
				$("#hyves_page").css("display", "block");
			}
		break;		
		case 2:
			
			$("#book_shelves_extend").animate({ 
				 height: "35"
			}, 500, function() {
				$("#hyves_page").css("display","none");
			    $("#vote_page_1").css("display","block");
			  });
		break;						
	}				
}

function toggleHelp(){

	$("#help_shelve").toggle("slow");


}



