function checkEnter(e){
	var characterCode //literal character code will be stored in this variable
	
	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		searchRecords(); //submit the form
		return false
	}else{
		return true
	}

}

$(function() {
	var map = new GMap2(document.getElementById('map'));
	var directions = new GDirections(map);
	var geocoder = new GClientGeocoder();
	var us = new GLatLng(-36.848841,174.755912);
	var openWidth = 700;
	var closedWidth = 980;
	var fadeDuration = 100;
	var animateDuration = 300;
	//var markerIcon = new GIcon(G_DEFAULT_ICON);
	//markerIcon.image = "http://www.nami.co.nz/images/marker_icon.gif";
	var marker = new GMarker(us);
	
	map.setCenter(us, 16);
	map.disableDragging();
	map.addOverlay(marker);
	
	GEvent.addListener(directions, "load", function() {
		var route = directions.getRoute(0);
    	var i = 0;
    	var step;
    	
    	$('#directions').append('<ol id="directions-list"></ol>');
    	
    	while(i < route.getNumSteps())
    	{
	    	step = route.getStep(i);
	    	
	    	$('#directions-list').append('<li>' + step.getDescriptionHtml() + '</li>');
	    	
	    	i++;
	    }
	});

	$('#directions-button').live('click', function() {
		directions.clear();
		$('#directions-list').remove();
		
		geocoder.getLatLng($('#directions-input').val(), function(you) {
			directions.load("from: " + you + " to: " + us, { "locale":"en_NZ", "getSteps":true });			
		});
	});
	
	$('#map.closed').live('click', function() {
		$(this).parent()
			   .find('#map-label')
			   .fadeOut(fadeDuration, function() {
			       $(this).parent()
			       		  .find('#map')
			       		  .removeClass('closed')
			   	          .addClass('open')
			   	          .animate({width: openWidth}, animateDuration, 'swing', function() {
			   	              $(this).parent()
			   	              		 .find('#directions')
			   	              		 .fadeIn(fadeDuration);
			   			  });
			   });		
	});
	
	$('#directions-clear').live('click', function() {
		$('#map').parent()
       		   .find('#directions')
       		   .fadeOut(fadeDuration, function() {
       		       $(this).parent()
       		       		  .find('#map')
       		       		  .removeClass('open')
			   	   	      .addClass('closed')
			   			  .animate({width: closedWidth}, animateDuration, 'swing', function() {
						      directions.clear();
							  map.setCenter(us, 16);						      
						      $('#directions-input').val('Start address')
						      						.css({color:'#666'});
						      
						      $('#directions-list').remove();
			   	   
							  $(this).parent()
							         .find('#map-label')
							         .fadeIn(fadeDuration);
			   			  });
			   	   });
	});
	
	$('#directions-input').focus(function() {
		if($(this).val() == 'Start address')
		{
			$(this).val('');
		}
		
		$(this).css({'color':'#000'});
	}).blur(function() {
		if($(this).val() == '')
		{
			$(this).val('Start address').css({'color':'#666'});
		}
	}).keydown(function(event) {
		switch(event.keyCode) {
			case 13:
				$('#directions-button').click();
				break;
		}
	});
});