$(document).ready(function() {
	fontSize("#fontmanager", "body", 8, 10, 14);
	var loggedin=false;
	var isUserValid=false;
	//ScrollTo extension
	jQuery.fn.extend({
  		scrollTo : function(speed, easing) {
    		return this.each(function() {
      		var targetOffset = $(this).offset().top;
      		$('html,body').animate({scrollTop: targetOffset}, speed, easing);
    	});
  		}
	});

    // right column instant search
	$("#searchbox").bind('keyup',function(event){
		if(event.which == "13"){
			getResults();
			$("#resultsContainer").scrollTo();
		}else{
		if($("#searchbox").val().length>=3){
		   $.get("instant_search.php",{query: $("#searchbox").val(), type: "count"}, function(data){
		   $("#resultsWrapper").slideDown('slow');
  		   switch(parseInt(data)){
				  case 0:
				  	$("#resultsContainer").html("Geen resultaten gevonden.");
 				    $("#sbutton").val("Geen resultaten");
					break;
				  case 9999:
				    $("#resultsContainer").html("<font color=red>Om misbruik te voorkomen is Live zoeken voor u tijdelijk niet beschikbaar. Druk op enter om te zien hoe lang u nog moet wachten.</font>");
 				    $("#sbutton").val("DRUK ENTER");
					break;
				  case 99999:
				    $("#resultsContainer").html("<font color=red>Er zijn ongeldige tekens in uw zoekopdracht gevonden. Gebruik alleen letters en cijfers.</font>");
 				    $("#sbutton").val("ONGELDIG");
					break;
				  default:
				    $("#resultsContainer").html("Druk op enter om de resultaten te bekijken.");
				    $("#sbutton").val(data + " gevonden");
			  }
		});
	}else{
			$("#resultsContainer").html("Voer minimaal 3 tekens in.");
			$("#sbutton").val("Live zoeken");
		}
		}
		});
	
	
	$("#sbutton").click(function(){
	
		getResults();
	
	});

	function getResults()
	{
	
		$.get("includes/ps_instant_search.php",{query: $("#searchbox").val(), type: "results"}, function(data){
		
			$("#resultsContainer").html(data);
			$("#resultsContainer").show("slidedown"); 
		});
	}
	
	
	// post for add spots form
	$("#addspotsubmitbutton").click(function(){
	
		addSpot();
	
	});

	function addSpot()
	{
		if ($('#form1').validationEngine({returnIsValid:true})==true){
			
			var categoryItems = new Array();
			$("input[name='category']:checked").each(function() {categoryItems.push($(this).val());});
			var spottypeItems = new Array();
			$("input[name='spottype']:checked").each(function() {spottypeItems.push($(this).val());});
            
			$.post("includes/ps_spot_add_validate.php",{submitter_name: $("#submitter_name").val(), submitter_email: $("#submitter_email").val(), title: $("#title").val(), short_description: $("#short_description").val(), long_description: $("#long_description").val(), spottype: spottypeItems.join(',') , category: categoryItems.join(',') , contact_name: $("#contact_name").val(), contact_address: $("#contact_address").val(), contact_phone: $("#contact_phone").val(), contact_email: $("#contact_email").val(), address: $("#addresss").val(), website: $("#website").val(),region: $("#region").val(), longitude: $("#longitude").val(), latitude: $("#latitude").val(), alongitude: $("#longitude2").val(), alatitude: $("#latitude2").val(), accessinfo: $("#accessinfo").val(), captcha_code: $("#captcha_code").val()}, function(data){
			
      		$("#fd_formarea").slideUp('slow', function() {
        		// Animation complete
				//$("#addspotresult").html(data);
				$("#addspotresult").html(data);
      		});
			$("#addspotresultContainer").fadeIn('slow');
			$('html, body').animate({scrollTop:0}, 'slow'); 
			
		});
		}
	}
	
	// post for rating form
	$("#ratingbutton").click(function(){
	
		addRating();
	
	});

	function addRating()
	{
		$.post("/includes/ps_rating_form_validate.php",{rating: $("#myrating").val(), spotid: $("#spotid").val()}, function(data){
      		$("#rating_formarea").slideUp('slow', function() {
        		// Animation complete
				$("#ratingresult").html(data);
      		});
			$("#ratingresultContainer").fadeIn('slow');
		});
	}
	
	// post for contact form
	$("#contactbutton").click(function(){
	
		addContact();
	
	});

	function addContact()
	{
		if ($('#contactform').validationEngine({returnIsValid:true})==true){
			$.post("/includes/ps_contact_form_validate.php",{submitter_name: $("#submitter_name").val(), submitter_email: $("#submitter_email").val(), submitter_phone: $("#submitter_phone").val(), reason: $("#reason").val(), message: $("#message").val(), captcha_code: $("#captcha_code").val() }, function(data){
																																																																												  				$("#contact_formarea").slideUp('slow', function() {
        			// Animation complete
					$("#contactresult").html(data);
      			});
			$("#contactresultContainer").fadeIn('slow');
			$('html, body').animate({scrollTop:0}, 'slow');
		});
		}
	}
	
	// post for reactions form
	$("#reactionsubmitbutton").click(function(){
	
		addReaction();
	
	});

	function addReaction()
	{
		if ($('#reactionsform').validationEngine({returnIsValid:true})==true){
			$.post("/includes/ps_reactions_form_validate.php",{submitter_name: $("#submitter_name").val(), submitter_email: $("#submitter_email").val(), comment: $("#comment").val(), spotcode: $("#spotcode").val(), captcha_code: $("#captcha_code").val() }, function(data){
																																																																												  				$("#reactions_formarea").slideUp('slow', function() {
        			// Animation complete
					$("#reactionsresult").html(data);
      			});
			$("#reactionsresultContainer").fadeIn('slow');
		});
		}
	}
	
	// post for send to friend form
	$("#tafsubmitbutton").click(function(){
	
		addTAF();
	
	});

	function addTAF()
	{
		if ($('#tafform').validationEngine({returnIsValid:true})==true){
			$.post("/includes/ps_tell_a_friend_validate.php",{submitter_name: $("#submitter_name").val(), submitter_email: $("#submitter_email").val(), receiver_name: $("#receiver_name").val(), receiver_email: $("#receiver_email").val(), comment: $("#comment").val(), spotcode: $("#spotcode").val(), captcha_code: $("#captcha_code").val() }, function(data){
																																																																												  				$("#taf_formarea").slideUp('slow', function() {
        			// Animation complete
					$("#tafresult").html(data);
      			});
			$("#tafresultContainer").fadeIn('slow');
		});
		}
	}
	
	// post for advanced search form
	$("#searchbutton").click(function(){
	
		performSearch('none','1');
	
	});
	
	$("input[name='resulttype']").change(function(){
    	if ($("input[name='resulttype']:checked").val() == 'spots')
        	$("#spot_options").fadeIn('slow')
    	else if ($("input[name='resulttype']:checked").val() == 'photos')
        	$("#spot_options").fadeOut('slow');
	});




	function performSearch($action,$page)
	{ 
		if ($('#searchform').validationEngine({returnIsValid:true})==true){
			var resulttypeItems = new Array();
			$("input[name='resulttype']:checked").each(function() {resulttypeItems.push($(this).val());});
			var category2Items = new Array();
			$("input[name='category']:checked").each(function() {category2Items.push($(this).val());});
			var spottype2Items = new Array();
			$("input[name='spottype']:checked").each(function() {spottype2Items.push($(this).val());});
			
			$.post("/includes/ps_advanced_search_results.php",{resulttype:resulttypeItems.join(','), category: category2Items.join(','), spottype: spottype2Items.join(','), search_all: $("#search_all").val(), search_exact: $("#search_exact").val(), search_one: $("#search_one").val(), search_without: $("#search_without").val(), rpp: $("#rpp").val(), captcha_code: $("#captcha_code").val(), action: $action, pg: $page }, function(data){
			
			$("#searchresultContainer").fadeOut('slow', function() {
        			// Animation complete
					$("#searchresult").html(data);
      			});																																																																									
			$("#searchresultContainer").fadeIn('slow');
		});
		}
	}
	
	$("#search_all").keyup(function(event)		{	if(event.keyCode == "13")	{	performSearch();	}	});
	$("#search_exact").keyup(function(event)	{	if(event.keyCode == "13")	{	performSearch();	}	});
	$("#search_one").keyup(function(event)		{	if(event.keyCode == "13")	{	performSearch();	}	});
	$("#search_without").keyup(function(event)	{	if(event.keyCode == "13")	{	performSearch();	}	});
	
	// post for login form
	$("#showlogin").click(function(){
								   if (!loggedin){
		$("#loginContainer").slideToggle('slow');
		
								   }
	});
	
	$("#loginbutton").click(function(){
									 
		doLogin();
	});
	
	$("#loginname").keyup(function(event){
		if(event.keyCode == "13")
		{
			doLogin();
		}
	});
	
	$("#loginpassword").keyup(function(event){
		if(event.keyCode == "13")
		{
			doLogin();
		}
	});
	
	$("#rememberme").keyup(function(event){
		if(event.keyCode == "13")
		{
			doLogin();
		}
	});
	
	$("#loginbutton").keyup(function(event){
		if(event.keyCode == "13")
		{
			doLogin();
		}
	});
	
	function doLogin(){
		$.post("/includes/ps_login_form_validate.php",{loginname: $("#loginname").val(), loginpassword: $("#loginpassword").val(), rememberme: $("#rememberme:checked").val() }, function(data){
  			
														  if (data.indexOf("1")!=-1){
															  $("#loginResult").html("<font color=red>U heeft geen gebruikersnaam of wachtwoord ingevuld.</font>");
															  $("#loginContainer").slideDown('slow');
														  }
														  if (data.indexOf("2")!=-1){
															  $("#loginResult").html("<font color=red>De door u opgegeven combinatie van gebruikersnaam en wachtwoord is bij ons niet bekend.</font><br /><br /><a href=?p=rmnd>Wachtwoord vergeten ?</a>");
															  $("#loginContainer").slideDown('slow');
														  }
														  if (data.indexOf("0")!=-1){
															  // Logged in
															   $("#loginResult").html("Welkom terug ! U bent nu ingelogd.");
															   $("#showlogin").fadeOut('slow', function() { $("#showlogout").fadeIn('slow'); $("#loginContainer").slideUp('slow'); });
														  }
														  
														  });
			$("#loginResultContainer").fadeIn('slow'); 
	}
	
	
	// post for logout form
	$("#logoutbutton").click(function(){
		doLogout();
	});
	
	function doLogout(){
		$.post("/includes/ps_logout_form_validate.php",{logoutbutton: $("#logoutbutton").val() }, function(data){
  			$("#loginContainer").slideUp('slow', function() {	$("#loginResult").html(data);	});
			$("#loginResultContainer").fadeIn('slow');
		});
	}
	
	// Logic for registrationform
	$("#registersubmitbutton").click(function(){
		registerUser();
	});
	
	function registerUser()
	{ 
		if ($('#registerform').validationEngine({returnIsValid:true})==true){
			$.post("/includes/ps_register_form_validate.php",{submitter_username: $("#submitter_username").val(), submitter_firstname: $("#submitter_firstname").val(), submitter_lastname: $("#submitter_lastname").val(), submitter_email: $("#submitter_email").val(), submitter_password: $("#submitter_password").val(), captcha_code: $("#captcha_code").val() }, function(data){
																																																																												  				$("#registerformContainer").slideUp('slow', function() {
        			// Animation complete
					$("#registerresult").html(data);
      			});
			$("#registerresultContainer").fadeIn('slow');
																																																																																												 });
			
		}
	}
	
	// Logic for radial search map form
	
	$("#radialmapformsubmit").click(function(){
		if ($('#radialmapform').validationEngine({returnIsValid:true})==true){
			showAddress2(document.radialmapform.searchlocation.value,document.radialmapform.radiusfield.value); 
		}
	});
	
    $("#searchlocation").keyup(function(event){
		if(event.keyCode == "13")
		{
			if ($('#radialmapform').validationEngine({returnIsValid:true})==true){
				showAddress2(document.radialmapform.searchlocation.value,document.radialmapform.radiusfield.value); 
			}
		}
	});
	
	$("#radiusfield").keyup(function(event){
		if(event.keyCode == "13")
		{
			if ($('#radialmapform').validationEngine({returnIsValid:true})==true){
				showAddress2(document.radialmapform.searchlocation.value,document.radialmapform.radiusfield.value); 
			}
		}
	});
	
	/*function doRadialSearch()
	{ 
	  $.post("/includes/ps_radial_search_spot_results.php",{lon: $("#userlon").val(), lat: $("#userlat").val(), radius: $("#userradius").val()}, function(data){
	     $("#radialmapsearchresults").html(data);
																																						  });
	}*/
	
});


