var gdir;//defined here because it must be available out side the ready

$(document).ready(function() {
//	alert('ready');

	
	var splitUrl = document.URL.split("/");
	var lastUrlItem = splitUrl[splitUrl.length-1];
	
	if (lastUrlItem == "offerteaanvraag") {
		$(this+'/img:last').hide();
																		 
		$('#containerOfferteAanvraagFrm').show();
		$("h1#contactnroute").text('Offerte aanvragen bij Nitesco');
		document.title = 'Nitesco - Offerteaanvraag | Vraag direct een offerte aan bij Nitesco';
		$('#containerContactFormulierFrm').hide();
 
		hideGmaps();		
	} else if (lastUrlItem == "contactaanvraag") {
		$('#containerContactFormulierFrm').show();
		$("h1#contactnroute").text('Wat kunnen wij voor u betekenen?');
		document.title = 'Nitesco - Contactaanvraag | Wat kan Nitesco voor u betekenen?';
		$('#containerOfferteAanvraagFrm').hide();
		hideGmaps();		
	}
	
	$("#frm_offerte_submitbtn").click(function(){ submitOfferte();});
	$("#frm_contact_submitbtn").click(function(){ validateContactform();});			
		
	$('#frm_contact_firstname,#frm_contact_lastname,#frm_contact_companyname,#frm_contact_email,#frm_contact_subject,#frm_contact_msg').focus(function () {
		if($(this).attr('class') == 'frmrequired') $(this).removeClass('frmrequired'); 		
	});

		var geocoder;
		var map;
		var name = "..";
		var address = "Claude Debussylaan 223, 1082 MC AMSTERDAM, The Netherlands";

	   function init(){
		  map = new GMap2(document.getElementById("gmaps"));
		  map.addControl( new GSmallMapControl() );
		  geocoder = new GClientGeocoder();
		  geocoder.getLocations(address, addToMap);

//			gdir = new GDirections(map, document.getElementById("directions"));
//      GEvent.addListener(gdir, "load", onGDirectionsLoad);
//      GEvent.addListener(gdir, "error", handleErrors);
	   }

	   function addToMap(response){
		  place = response.Placemark[0];
		  point = new GLatLng(place.Point.coordinates[1],
							  place.Point.coordinates[0]);
		  map.setCenter(point, 13);
		  marker = new GMarker(point);
		  map.addOverlay(marker);
	   }


	$('#offertefrmBtn').click(function(){
   		$("h1#contactnroute").text('Offerte aanvragen bij Nitesco');
		document.title = 'Nitesco - Offerteaanvraag | Vraag direct een offerte aan bij Nitesco';
		$(this+'/img:last').hide();
																		 
		$('#containerOfferteAanvraagFrm').show();
		$('#containerContactFormulierFrm').hide();

		hideGmaps();
	});
	$('#contactfrmBtn').click(function() {
		$("h1#contactnroute").text('Wat kunnen wij voor u betekenen?')
		document.title = 'Nitesco - Contactaanvraag | Wat kan Nitesco voor u betekenen?';
		$('#containerContactFormulierFrm').show();
		$('#containerOfferteAanvraagFrm').hide();
		hideGmaps();
	});	
	
	$('#calcRouteBtn').click(function(){


		var street = $('#frm_route_street').attr('value');
		var city = $('#frm_route_city').attr('value');

		if(street == 'Uw straat' || city == 'Uw stad'){
			alert('Veld straat & stad in.');
		}else{
			setDirections(street, city);		
		}

	});		
	
	
	$('#frm_route_street').focus(function () {
		if($(this).attr('value') == 'Uw straat') $(this).attr('value', ''); 
	});
	$('#frm_route_city').focus(function () {
		if($(this).attr('value') == 'Uw stad') $(this).attr('value', ''); 
	});	
	
	init();//start gmaps
});


function hideGmaps(){
		$('#gmaps_container').hide();
		$('#gmaps_form_container').hide();
}

	function validateContactform(){
		
		var error = false;	

		if($("#frm_contact_firstname").attr("value") == ''){
			 $("#frm_contact_firstname").addClass("frmrequired");
			 error = true;
		}
		if($("#frm_contact_lastname").attr("value") == ''){
			 $("#frm_contact_lastname").addClass("frmrequired");
			 error = true;
		}
		if($("#frm_contact_companyname").attr("value") == ''){
			 $("#frm_contact_companyname").addClass("frmrequired");
			 error = true;
		}
		if($("#frm_contact_email").attr("value") == ''){
			 $("#frm_contact_email").addClass("frmrequired");
			 error = true;
		}
		if($("#frm_contact_subject").attr("value") == ''){
			 $("#frm_contact_subject").addClass("frmrequired");
			 error = true;
		}
		if($("#frm_contact_msg").attr("value") == ''){
			 $("#frm_contact_msg").addClass("frmrequired");
			 error = true;
		}	

		if(error == true) return false;

		submitContactForm();
		
	}

	function submitContactForm(){
		var queryString = $('#frm_contact').formSerialize();
		
		$.ajax({type: "POST",
					 url: "/contact/sendcontactmail",
					 data: queryString,
			beforeSend: function(){ $.blockUI({ message: '<h1>Proccessing...</h1>'}); }, //show loading just when link is clicked  
			success: function(html){
				if(html == true){
					$.blockUI({ message: "<h1>Send successfully...</h1>"}); 
					$('#containerContactFormulierFrm').hide();	
				}else{
					$.blockUI({ message: "<h1>Send successfully...</h1>"}); 
					//$.blockUI({ message: '<h1>'+html+'</h1>' });
					$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
				}
				setTimeout($.unblockUI, 2500);
			}//END success
		}); //end $.ajax
	}

////////////////////////////////
	function submitOfferte(){
		var error = false;
		//checkform
		
	if($("#frm_offerte_firstname").attr("value") == ''){
		 $("#frm_offerte_firstname").addClass("frmrequired");
		 error = true;
	}
	if($("#frm_offerte_lastname").attr("value") == ''){ 
		$("#frm_offerte_lastname").addClass("frmrequired");
		 error = true;
	}
	if($("#frm_offerte_companyname").attr("value") == ''){
		$("#frm_offerte_companyname").addClass("frmrequired");
		 error = true;
	}
	if($("#frm_offerte_address").attr("value") == ''){
		$("#frm_offerte_address").addClass("frmrequired");
		error = true;
	}
	if($("#frm_offerte_zip_city").attr("value") == ''){
		$("#frm_offerte_zip_city").addClass("frmrequired");
		error = true;
	}
	if($("#frm_offerte_email").attr("value") == ''){
		$("#frm_offerte_email").addClass("frmrequired");
		error = true;
	}
	if($("#frm_offerte_phone").attr("value") == ''){
		$("#frm_offerte_phone").addClass("frmrequired");
		error = true;
	}
	if($("#frm_offerte_stonetype").attr("value") == ''){
		$("#frm_offerte_stonetype").addClass("frmrequired");
		error = true;
	}
	if($("#frm_offerte_size").attr("value") == ''){
		$("#frm_offerte_size").addClass("frmrequired");
		error = true;
	}
	
	if(error == true) return false;

///		$.blockUI({ message: '<h3>Een moment aub...</h3>' });

		var queryString = $('#frm_offerte').formSerialize();
		
		$.ajax({type: "POST",
					 url: "/contact/sendoffertemail",
					 data: queryString,
			beforeSend: function(){ $.blockUI({ message: '<h1>Proccessing...</h1>'}); }, //show loading just when link is clicked  
			success: function(html){
				if(html == true){
					$.blockUI({ message: "<h1>Send successfully...</h1>"}); 
					$('#containerOfferteAanvraagFrm').hide();	
				}else{
					//$.blockUI({ message: '<h1>'+html+'</h1>' });
					$.blockUI({ message: '<h1>Send successfully...</h1>' });
					$('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
				}

				$('#containerOfferteAanvraagFrm').hide();				
				setTimeout($.unblockUI, 2500);
			
			}//END success
			
		}); //end $.ajax
		
		
	}

	function setDirections(fromStreet, fromCity){
			var toAddress = 'Claude Debussylaan 223, 1082 MC AMSTERDAM, The Netherlands';
			var fromAddress = fromStreet + ' ' + fromCity;
			gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": "en_UK" });
    }



    function handleErrors(){
			if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){

				alert('Addresses not found.');
				var errortext = "Addresses not found.";

//	     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);

			}else if (gdir.getStatus().code == G_GEO_SERVER_ERROR){

//	     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);

			 var errortext = "A geocoding or directions request could not be successfully processed";   

			}else if (gdir.getStatus().code == G_GEO_MISSING_QUERY){

//	     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

			 var errortext = "The HTTP q parameter was either missing or had no value.";   	     

			}else if (gdir.getStatus().code == G_GEO_BAD_KEY){

//	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

			 var errortext = "The given key is either invalid or does not match the domain for which it was given.";

			}else if (gdir.getStatus().code == G_GEO_BAD_REQUEST){

//	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);

			 var errortext = "A directions request could not be successfully parsed.";

		 }else{

//			  alert("An unknown error occurred.");

	 			 var errortext = "A directions request could not be successfully parsed.";

		 }

		 

			 $("#directions").hide();

			 $("#directions").html(errortext);

			 $("#directions").addClass("gmapsError").show("slow");

	}



	function onGDirectionsLoad(){ 

      // Use this function to access information about the latest load()

      // results.



      // e.g.

      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;

	  // and yada yada yada...

	}





