var TheatreLocation_Maps = new Array();

jQuery(document).ready(
	function()
	{
		TheatreLocation_LoadAllMaps();
	}
);

function TheatreLocation_SetupMap(map_id, address, height, html) {

	TheatreLocation_Maps[map_id] = new Array();
	TheatreLocation_Maps[map_id]['point'] = false;
	TheatreLocation_Maps[map_id]['element'] = document.getElementById(map_id);
	TheatreLocation_Maps[map_id]['height'] = height;
	TheatreLocation_Maps[map_id]['html'] = html;
	TheatreLocation_Maps[map_id]['address'] = address;
	TheatreLocation_Maps[map_id]['loaded'] = false;
	TheatreLocation_Maps[map_id]['gmap'] = false;
	
}


function TheatreLocation_LoadAllMaps()
{
	
		for(map_id in TheatreLocation_Maps)
		{
			TheatreLocation_LoadMap(map_id);
		}
	
}


function TheatreLocation_LoadMap(map_id) {
	
	var TheatreLocation_GeoCoder = new google.maps.Geocoder();
	
	if (TheatreLocation_GeoCoder) {

    	TheatreLocation_GeoCoder.geocode( { 'address': TheatreLocation_Maps[map_id]['address']}, 
    	function(results, status) {
       		if (status == google.maps.GeocoderStatus.OK) {
          		
          		TheatreLocation_Maps[map_id]['latlng'] = results[0].geometry.location;
          		TheatreLocation_Maps[map_id]['viewport'] = results[0].geometry.viewport;
          		
          		var mapOptions = {
				  zoom: 8,
				  center: TheatreLocation_Maps[map_id]['latlng'],
				  mapTypeId: google.maps.MapTypeId.ROADMAP,
				  disableDefaultUI: true,
				  scrollwheel:false,
				  navigationControl: true,
				  navigationControlOptions: {
    				style: google.maps.NavigationControlStyle.DEFAULT
 				 }
				};
			
				TheatreLocation_Maps[map_id]['gmap'] = new google.maps.Map(TheatreLocation_Maps[map_id]['element'], mapOptions);
         		
         		var markerOptions = {
             		map: TheatreLocation_Maps[map_id]['gmap'], 
              		position: TheatreLocation_Maps[map_id]['latlng']
         		 };
         		 
         		 var marker = new google.maps.Marker(markerOptions);
         		 
         		 var infoOptions = {
				 	content: TheatreLocation_Maps[map_id]['html']
				 };
				
         		 var info = new google.maps.InfoWindow(infoOptions);
				
				TheatreLocation_Maps[map_id]['gmap'].fitBounds(TheatreLocation_Maps[map_id]['viewport']);
				
				info.open(TheatreLocation_Maps[map_id]['gmap'],marker);				
      		
      		}
    	});
    }
}

function TheatreLocation_GetDirections(map_id)
{
	var directionsRenderer = new google.maps.DirectionsRenderer();	
	var directionsPanel = document.getElementById('TheatreLocation_Directions');
	
	directionsPanel.innerHTML = '';
	
	var errorPanel = document.getElementById('TheatreLocation_Error');
	
	var from = document.getElementById('TheatreLocation_DirectionsFrom');
	var to = document.getElementById('TheatreLocation_DirectionsTo');
	var method = document.getElementById('TheatreLocation_DirectionTypes')[document.getElementById('TheatreLocation_DirectionTypes').selectedIndex].value;
	
	var travelMethod;
	
	switch(method)
	{
		case '0':
			travelMethod = google.maps.DirectionsTravelMode.DRIVING;
			break;
		case '1':
			travelMethod = google.maps.DirectionsTravelMode.WALKING;
			break;
		case '2':
			travelMethod = google.maps.DirectionsTravelMode.BICYCLING;
			break;
		default:
			travelMethod = google.maps.DirectionsTravelMode.DRIVING;
			break;
	}
	
	var directionsService = new google.maps.DirectionsService;
	
	var directionsOptions = {
		origin:from.value,
		destination:to.value,
		travelMode:travelMethod,
		provideRouteAlternatives:true
	};
	
	directionsService.route(directionsOptions, function(result, status) {
    	if (status == google.maps.DirectionsStatus.OK) {
    		directionsRenderer.setMap(TheatreLocation_Maps[map_id]['gmap']);
			directionsRenderer.setPanel(directionsPanel);
	      	directionsRenderer.setDirections(result);
	      	errorPanel.style.display = 'none';
    	}
    	else if(status == google.maps.DirectionsStatus.NOT_FOUND)
    	{	
    		directionsRenderer.setMap(null);
    		directionsRenderer.setPanel(null);
    		errorPanel.innerHTML = TheatreLocation.text_notfound;
    		errorPanel.style.display = 'block';
    	}
    	else if(status == google.maps.DirectionsStatus.ZERO_RESULTS)
    	{
    		directionsRenderer.setMap(null);
    		directionsRenderer.setPanel(null);
    		errorPanel.innerHTML = TheatreLocation.text_0results;
    		errorPanel.style.display = 'block';
    	}
    	else
    	{
    		directionsRenderer.setMap(null);
    		directionsRenderer.setPanel(null);
    		errorPanel.innerHTML = TheatreLocation.text_other;
    		errorPanel.style.display = 'block';
    	}
	  });
  
}
