/**
 * @author Ivor
 */

 $(document).ready( function () {
	 // Init some vars
	 bHaveDirections = false;
	 sPrevDirections = $('#directions').html();
	 
	// Inputs to clear on first focus
	$('input.clearable').focus( function () {
		if(!$(this).attr('changed')) {
			$(this).attr({value:'',changed:true});
		}
	});	
	

	// Bind to routebutton
	$('#routesubmit').click(planRoute);
	

	// Bind route events
	$('#routesubmit').click(planRoute);
	$('#routeaddress').focus(function() {
		if(!$(this).attr('changed')) {
			$(this).attr({value:'',changed:true});
		}
	});
	
	// Enter key for route
	$('#routeaddress').keyup(function(e) {
		if(e.keyCode == 13) {
			planRoute();
		}
	});	

	
	
 	
 });
 
 
 // -- START FUNCTIONS -- //
 
 
 // Plan route
 function planRoute() {
	 $('#directions').slideUp();

	// Get location
	var sLocation = $('#routeaddress').attr('value');
	
	
	// Set waiting..
	//$('#routeaddress').attr({value: LANG_PLEASE_WAIT,disabled: true});
	
	var aCoords = $.getJSON(LINKROOT+'/do/frontend/geocode/',{location: sLocation},function(oFrom) {
	
		if(bHaveDirections) {
			gdir.clear();
		}
		
						
		if(!oFrom.lat) {
			alert(LANG_LOCATION_NOT_FOUND);
		} else {
			bHaveDirections = true;
			// Set
			
			var sFrom = oFrom.lat+','+oFrom.lon;
			var sTo = '52.644656,4.725220';
			
			gdir = new GDirections(map, document.getElementById('directions'));
			gdir.load('from: ' + sFrom + ' to: ' + sTo ,{ 'locale': 'nl' });	
			
			$('#directions').slideDown({duration: 1000, easing: 'easeOutBounce'});
			$('#directions').focus();	
			document.location.href='#directions';
			
	
				
		}
		
		// Set ready
		$('#routeaddress').attr({value: sLocation, disabled: false});

	
	});
	
	}
	
 
 // Check e-mail
 function checkEmail(sEmail) {
 	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(sEmail)){
	return (true)
	} else {
	return false;
	}
 
 }