// JavaScript Document

var LocationsManager = {
	
	init: function(){
		// Location 1: 
		var locations_elements_1 = $$('a.link_locations_1');
		if(locations_elements_1){
			for(var i=0;i<locations_elements_1.length;i++){
				locations_elements_1[i].addEvent('click',LocationsManager.setCenterListener_1);
			}
		}
		// Location 2: 
		var locations_elements_2 = $$('a.link_locations_2');
		if(locations_elements_2){
			for(var i=0;i<locations_elements_2.length;i++){
				locations_elements_2[i].addEvent('click',LocationsManager.setCenterListener_2);
			}
		}
		// Location 3: 
		var locations_elements_3 = $$('a.link_locations_3');
		if(locations_elements_3){
			for(var i=0;i<locations_elements_3.length;i++){
				locations_elements_3[i].addEvent('click',LocationsManager.setCenterListener_3);
			}
		}
		// Location 4: 
		var locations_elements_4 = $$('a.link_locations_4');
		if(locations_elements_4){
			for(var i=0;i<locations_elements_4.length;i++){
				locations_elements_4[i].addEvent('click',LocationsManager.setCenterListener_4);
			}
		}
	},
	
	setCenterListener_1: function(event){
		LocationsManager.setCenter(this, 1);
		//event.preventDefault();
	},
	
	setCenterListener_2: function(event){
		LocationsManager.setCenter(this, 2);
		//event.preventDefault();
	},
	
	setCenterListener_3: function(event){
		LocationsManager.setCenter(this, 3);
		//event.preventDefault();
	},
	
	setCenterListener_4: function(event){
		LocationsManager.setCenter(this, 4);
		//event.preventDefault();
	},
	
	setCenter: function(element, num){
		// Get Map And Build Object GMap2
		var map = new GMap2(document.getElementById("map"));
		if(map){
			// Map Settings
			map.addControl(new GLargeMapControl());
			map.addControl(new GMapTypeControl());
			map.enableContinuousZoom();
			map.enableScrollWheelZoom();
			map.enableDoubleClickZoom();
			// Switch -> Map Set Center
			switch(num){
				case 1:
					map.setCenter(new GLatLng(46.79800297953153,9.823075532913208),16,G_HYBRID_MAP);
					break;
				case 2:
					map.setCenter(new GLatLng(46.79400733273959,9.821176528930664),16,G_HYBRID_MAP);
					break;
				case 3:
					map.setCenter(new GLatLng(46.867423995418605,9.883382320404053),16,G_HYBRID_MAP);
					break;
				case 4:
					map.setCenter(new GLatLng(46.85669,9.88230),16,G_HYBRID_MAP);
					break;	
				default:
					map.setCenter(new GLatLng(46.79800297953153,9.823075532913208),16,G_HYBRID_MAP);
					break;
			}
			// Array Of Locations Points (lat,lon)
			var points = new Array();
			points[0] = new GLatLng(46.79800297953153,9.823075532913208);
			points[1] = new GLatLng(46.79400733273959,9.821176528930664);
			points[2] = new GLatLng(46.867423995418605,9.883382320404053);
			points[3] = new GLatLng(46.85669,9.88230);
			// Array of HTML Info Windows
			var htmlInfoWindows = new Array();
			htmlInfoWindows[0] = LocationsManager.buildInfoWindow("fileadmin/templates/php_contents/google_map/images/logo_map.gif","Schneider\'s Restaurant-Caf&eacute", "Promenade 68", "CH", "7270", "Davos Platz", "Switzerland", "Route berechnen von:", 46.79800297953153,9.823075532913208);
			htmlInfoWindows[1] = LocationsManager.buildInfoWindow("fileadmin/templates/php_contents/google_map/images/logo_map.gif", "Filiale im Morosani Posthotel<br />Confiserie Schneider & Shop", "Promenade 42", "CH", "7270", "Davos Platz", "Switzerland", "Route berechnen von:", 46.79400733273959, 9.821176528930664);
			htmlInfoWindows[2] = LocationsManager.buildInfoWindow("fileadmin/templates/php_contents/google_map/images/logo_map.gif", "Filiale im Restaurant Fellini<br>Confiserie Schneider", "Bahnhofstrasse 22", "CH", "7250", "Klosters", "Switzerland", "Route berechnen von:", 46.867423995418605,9.883382320404053);
			htmlInfoWindows[3] = LocationsManager.buildInfoWindow("fileadmin/templates/php_contents/google_map/images/logo_map.gif", "Filiale in Selfranga<br>Schneider&lsquo;s Railway Bistro", "Selfranga", "CH", "7250", "Klosters", "Switzerland", "Route berechnen von:", 46.85669,9.88230);
			// Marker 1
			var marker_1 = new GMarker(points[0]);
			GEvent.addListener(marker_1, "click", function(){
					marker_1.openInfoWindowHtml(htmlInfoWindows[0]);
				}
			);
			map.addOverlay(marker_1);
			// Marker 2
			var marker_2 = new GMarker(points[1]);
			GEvent.addListener(marker_2, "click", function(){
					marker_2.openInfoWindowHtml(htmlInfoWindows[1]);
				}
			);
			map.addOverlay(marker_2);
			// Marker 3
			var marker_3 = new GMarker(points[2]);
			GEvent.addListener(marker_3, "click", function(){
					marker_3.openInfoWindowHtml(htmlInfoWindows[2]);
				}
			);
			map.addOverlay(marker_3);
			// Marker 4
			var marker_4 = new GMarker(points[3]);
			GEvent.addListener(marker_4, "click", function(){
					marker_4.openInfoWindowHtml(htmlInfoWindows[3]);
				}
			);
			map.addOverlay(marker_4);
			
			switch(num){
				case 1:
					marker_1.openInfoWindowHtml(htmlInfoWindows[0]);
					break;
				case 2:
					marker_2.openInfoWindowHtml(htmlInfoWindows[1]);
					break;
				case 3:
					marker_3.openInfoWindowHtml(htmlInfoWindows[2]);
					break;
				case 4:
					marker_4.openInfoWindowHtml(htmlInfoWindows[3]);
					break;	
				default:
					marker_1.openInfoWindowHtml(htmlInfoWindows[0]);
					break;
			}
			var location = window.location + "";
			var splitted_locations = location.split("#");
			element.href = splitted_locations[0]+"#anchor_map";
		}
	},
	
	buildInfoWindow: function(logoUrl,name,address,countryLetters,zipCode,city,country,directions,lat,lon){
		var html_info_window = '<div id="map_info_window">' +
									'<div id="map_info_window_image">' +
										'<img src="' + logoUrl + '" />' +
									'</div>' +
									'<div id="map_info_window_name">'+
										name +
									'</div>' +
									'<div id="map_info_window_address">' +
										address + '<br />' + countryLetters + ' - ' + zipCode + ' ' + city +
									'</div>' +
									'<div id="map_info_window_direction">' +
										'<div id="map_info_window_direction_text">' +
											directions +
										'</div>' +
										'<div id="map_info_window_direction_search">' +
											'<form action="http://maps.google.com/maps" method="get" target="_blank">' +
												'<input type="hidden" name="daddr" value="' + address + ', ' + zipCode + ' ' + city + ', ' + country + '" />' +
												'<input type="hidden" id="lng" value="' + lon + '" />' +
												'<input type="hidden" id="lat" value="' + lat + '" />' +
												'<input type="text" name="saddr" class="map_search_field" value="" />' +
												'<input type="submit" name="go" class="map_search_button" value="" style="" />' +
											'</form>' +
										'</div>' +
									'</div>' +
								'</div>';
		return html_info_window;
	}
};

window.addEvent('domready', function(){
		LocationsManager.init();
	}
);
