var map;
var directionsService;
var directionDisplay;
var directionsService;
var home_loc;
var home_name;
var map_location;
var zoom;
	
function initialise() {
	if	(map_location == 1)	{
		home_loc = new google.maps.LatLng(51.574242,-3.216387);
		home_name = "South Wales Monuments";
		zoom = 12;
	}	else	{
		if	(map_location == 2)	{
			home_loc = new google.maps.LatLng(51.607477,-3.658023);
			home_name = "South Wales Monuments";
			zoom = 12;
		}	else	{
			if	(map_location == 3)	{
				home_loc = new google.maps.LatLng(51.502104,-3.576844);
				home_name = "Ewenny Memorials";
				zoom = 12;
			}
		}
	}
	
	var mapOptions = {
		zoom: zoom,
		center: home_loc,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	// setup map
	map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
	var marker = new google.maps.Marker({
		position: home_loc, 
		map: map, 
		title:home_name
	});
	directionsService = new google.maps.DirectionsService();
	
	directionsDisplay = new google.maps.DirectionsRenderer();
	directionsDisplay.setMap(map);
	google.maps.event.trigger(map, 'resize');
	directionsDisplay.setPanel(document.getElementById("direction_list"));
	google.maps.event.trigger(map, 'resize');
	$("#map").css('display', 'block');
}

function calcRoute() {
	var user_loc = $("#directions #user_loc").val();
	var request = {
		origin:user_loc, 
		destination:home_loc,
		travelMode: google.maps.DirectionsTravelMode.DRIVING,
		unitSystem: google.maps.DirectionsUnitSystem.METRIC
	};
	directionsService.route(request, function(result, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(result);
			
			if	(!($('a.view.show').length > 0))	{
				var show_link = $('<a class="view show">Toggle instructions</a>');
				show_link.click(function()	{
					$('#direction_list').slideToggle();
					$(this).toggleClass('show').toggleClass('hide');
				});
				$('#map_head').append(show_link);
			}
			if	(!($('a.print').length > 0))	{
				var print_link = $('<a class="print">Print Instructions</a>');
				print_link.click(function()	{
					print_directions();
				});
				$('#map_head').append(print_link);
			}
		}
	});
	google.maps.event.trigger(map, 'resize');
}

function print_directions() {
	var print_element = document.getElementById('direction_list').innerHTML;
	win = window.open();
	self.focus();
	win.document.open();
	win.document.write('<'+'html'+'><'+'head'+'>');
	win.document.write('<' + 'title' + '>' + 'Your Travel Itinerary to ' + home_name + '<' + '/' + 'title' + '>');
	win.document.write('<'+'/'+'head'+'><'+'body'+'>');
	win.document.write(print_element);
	win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
	win.document.close();
	win.print();
	win.close();
}

jQuery(document).ready(function($) {
	$('a[rel*=facebox]').facebox();
	$(document).bind('reveal.facebox', function() {
		initialise();
	});
	$(document).bind('afterReveal.facebox', function()	{
		$('.content #directions').submit(function()	{
			calcRoute();
			return false;
		});
	});
});
