/**
 * @package Woodland_WordPress
 * @subpackage Theme
 * @author Alex Southan
 * @link http://tiptapdesign.co.uk/
 */
jQuery(document).ready(function($) {
	var mapElem = $('#google-map').addClass('enabled');
	var windowText = mapElem.attr('title');

	var address = $('#daddr').val().split('@');
	var latLngArray = address[1].split(',');
	var latLng = new google.maps.LatLng(latLngArray[0], latLngArray[1]);

	var map = new google.maps.Map(mapElem.get(0), {
		zoom: 15,
		center: latLng,
		mapTypeId: google.maps.MapTypeId.HYBRID,
		navigationControlOptions: {style: google.maps.NavigationControlStyle.ZOOM_PAN},
		scrollwheel	: false
	});

	var infoWindow = new google.maps.InfoWindow();
	infoWindow.setContent(windowText);
	var marker = new google.maps.Marker({clickable: true, position: latLng, map: map});
	google.maps.event.addListener(marker, 'click', function() {
		infoWindow.open(map, marker);
	});

	var results = $('#directions');
	var directions = new google.maps.DirectionsService();
	var renderer = new google.maps.DirectionsRenderer();
		renderer.setMap(map);
		renderer.setPanel(results.get(0));

	$('#direction-form').submit(function() {
		var destination = $('#google_map_destination_override').text();
		if (destination.length < 2)
			destination = latLng;
		var address = $('#saddr').val();
		address = $.trim(address);
		if (address.length < 2)
			return false;
		var config = {
			destination: destination,
			origin: address,
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		};
		directions.route(config, function(result, status) {
			if (status != google.maps.DirectionsStatus.OK)
				return results.html('<p>Sorry, but that address could not be translated into directions.</p>');
			renderer.setDirections(result);
		});
		return false;
	});
});