var txTdsPi1 = {
	loading: false,
	icons: {},
	searchFormFx: false,
		
	setupMap:	function() {
		var point = new GLatLng(46.9127509564, 8.19580078125);
		
		this.searchFormFx = new Fx.Slide($('events-search')).hide();
		
		$$('#events-search-header a#link-show-search').addEvent('click', function(event){
			new Event(event).stop();
			
			txTdsPi1.searchFormFx.toggle();
 		});
		
		
		this.map = new GMap2($('map'));

		this.map.setCenter(point, 8);
		
		this.map.setUIToDefault();
		//this.map.setMapType(G_PHYSICAL_MAP );
		
		new GKeyboardHandler(this.map);

		this.setupIcons();
		this.initForm();
		this.geocodeClient = new GClientGeocoder();
		this.geocodeClient.setBaseCountryCode('ch');
		
		this.setupClustering();
		
		this.initMarkers();
	},
	
	setupIcons: function() {
		this.icons['cluster'] = new GIcon(G_DEFAULT_ICON);
		this.icons['cluster'].image = "/fileadmin/tagedersonne.ch/images/ggmapMarkerCluster.gif";
		this.icons['cluster'].iconSize = new GSize(21,21);
		this.icons['cluster'].iconAnchor = new GPoint(10, 10);
		this.icons['cluster'].shadow = "/fileadmin/tagedersonne.ch/images/markerShadowEmpty.gif";
		this.icons['cluster'].shadowSize = new GSize(1, 1);
		this.icons['cluster'].printImage = this.icons['cluster'].image;
		this.icons['cluster'].mozPrintImage = this.icons['cluster'].image;
		this.icons['cluster'].printShadow = this.icons['cluster'].shadow;
		this.icons['cluster'].infoWindowAnchor = new GPoint(10, 10);
		
		/** Markers for standard Events**/
		this.icons[1] = new GIcon(G_DEFAULT_ICON);
		this.icons[1].image = "/fileadmin/tagedersonne.ch/images/ggmapMarker.gif";
		this.icons[1].iconSize = new GSize(21, 21);
		this.icons[1].iconAnchor = new GPoint(10, 10);
		this.icons[1].shadow = "/fileadmin/tagedersonne.ch/images/markerShadowEmpty.gif";
		this.icons[1].shadowSize = new GSize(1,1);
		this.icons[1].printImage = this.icons[1].image;
		this.icons[1].mozPrintImage = this.icons[1].image;
		this.icons[1].printShadow = this.icons[1].shadow;
		this.icons[1].infoWindowAnchor = new GPoint(10, 10);
		
		/** Markers for Events of Energy-Towns **/
		this.icons[2] = new GIcon(G_DEFAULT_ICON);
		this.icons[2].image = "/fileadmin/tagedersonne.ch/images/ggmapMarkerEnergytown.gif";
		this.icons[2].iconSize = new GSize(21, 21);
		this.icons[2].iconAnchor = new GPoint(10, 10);
		this.icons[2].shadow = "/fileadmin/tagedersonne.ch/images/markerShadowEmpty.gif";
		this.icons[2].shadowSize = new GSize(1, 1);
		this.icons[2].printImage = this.icons[2].image;
		this.icons[2].mozPrintImage = this.icons[2].image;
		this.icons[2].printShadow = this.icons[2].shadow;
		this.icons[2].infoWindowAnchor = new GPoint(10, 10);
 	},
	
 	setupClustering: function() {
 		this.cluster = new ClusterMarker(this.map, {
			clusterMarkerTitle : txTdsPi1LLL.clickZoom,
			clusterMarkerIcon: this.icons['cluster'],
			intersectPadding: 2
		});
	},
 	
 	initForm: function() {
 		$('eventSearch').addEvent('submit', function(event){
			new Event(event).stop();
			
			txTdsPi1.search();
 		});
 		
 		$('input-location').addEvent('focus', function(event){
 			this.value = '';
 		});
 		
 		$('input-location').addEvent('blur', function(event){
 			if(this.value == '') {
 				this.value = txTdsPi1LLL.defaultLocation;
 			}
 		});
	},
	
	search: function() {
		if(this.loading == true) {
			return false;
		}
	
		$('tx_tds_pi1_search').set({
			'value' : txTdsPi1LLL.searchLoading,
			'disabled': 'disabled'
		});
		
		this.loading = true;
	
		var location = $('input-location').value;
		if(location && location!=txTdsPi1LLL.defaultLocation) {
			this.geocodeLocation(location);
		} else {
			this.refreshMarkers();
		}
	},
	
	geocodeLocation: function(location) {
		this.geocodeClient.getLocations(location+', Schweiz', function(response) {
			if (response && response.Status.code == 200) {
				var latLonBox = response.Placemark[0].ExtendedData.LatLonBox;
				var sw = new GLatLng(latLonBox.west,latLonBox.south);
				var ne = new GLatLng(latLonBox.east,latLonBox.north);
	
				var zoom = txTdsPi1.map.getBoundsZoomLevel(new GLatLngBounds(sw, ne));
	
				txTdsPi1.map.setCenter(new GLatLng(response.Placemark[0].Point.coordinates[1], response.Placemark[0].Point.coordinates[0]), zoom);
			}
			
			$('tx_tds_pi1_search').set({
				'value' : txTdsPi1LLL.searchSubmit,
				'disabled': ''
			});
			
			txTdsPi1.loading = false;
			
			txTdsPi1.refreshClustering();
		});
	},
	
	initMarkers: function() {
 		this.refreshMarkers();
 	},
 	
 	refreshMarkers: function() {
 		this.map.clearOverlays();
 			
 		var request = new Request.JSON({
			'url' : 'index.php?type=2&id='+txTdsId+'&lang='+txTdsLang+'&tx_tds_pi1[activityYear]='+txTdsActivityYear,
			onComplete: function(jsonObj) {
				this.setMarkers(jsonObj);
				
				$('tx_tds_pi1_search').set({
					'value' : txTdsPi1LLL.searchSubmit,
					'disabled': ''
				});
				
				this.loading = false;
			}.bind(this)
		}).post($('eventSearch'));
 	},
 	
	setMarkers: function(jsonObj) {
		this.markersOnMap = [];

		var length = jsonObj.length;
		var i=0;
		for(i=0; i < length; i++) {
			if(!jsonObj[i].uid) {
				break;
			}

			var marker = this.createMarker(jsonObj[i], false);
			this.markersOnMap.push(marker);
		}

		this.refreshClustering();
	},
	
	refreshClustering: function() {
		this.cluster.removeMarkers();
		this.cluster.addMarkers(this.markersOnMap);
		this.cluster.refresh(true);
	},
	
	createMarker: function(markerObj, draggable) {
		var options = {
			icon: this.icons[markerObj.energytown]
		};
		
		var marker = new GMarker(new GLatLng(markerObj.latitude, markerObj.longitude), options);
		marker.bindInfoWindowTabsHtml(this.createMarkerTabs(markerObj), {maxWidth: 250});
		
		marker.markerObj = markerObj;
		
		return marker;
	},
	
	createMarkerTabs: function(markerObj) {
		tabs = [];
		
		tabs.push(this.createMarkerTabDetail(markerObj));
		
		if(markerObj.event_description) {
			tabs.push(this.createMarkerTabDescription(markerObj));
		}
		
		return tabs;
	},
	
	createMarkerTabDetail: function(markerObj) {
		var tabHtml = '<div class="events-tooltip-tab-content">'
			+ '<h3>'+markerObj.event_type+'</h3>'
			+ '<p><strong>'+markerObj.event_organizer+'</strong></p>'
			+ '<p>';
		
		if(markerObj.event_street) {
			tabHtml = tabHtml + markerObj.event_street+' <br />'
		}	
			
		if(markerObj.event_zip || markerObj.event_city) {
			tabHtml = tabHtml + markerObj.event_zip+' '+markerObj.event_city+' '+markerObj.event_country_or_canton;
		}

		tabHtml = tabHtml + '</p><p>';
		
		if(markerObj.event_phone) {
			tabHtml = tabHtml + markerObj.event_phone+' <br />';
		}	
	
		if(markerObj.event_email) {
			tabHtml = tabHtml + markerObj.event_email+' <br />';
		}	
	
		if(markerObj.event_www) {
			tabHtml = tabHtml + markerObj.event_www+' <br />';
		}	
	
		tabHtml = tabHtml + '</p>';
		
		tabHtml = tabHtml + '<p>'+markerObj.event_days+'</p>';
		
		tabHtml = tabHtml+ '</div>';
		
		return new GInfoWindowTab(txTdsPi1LLL.tooltipLabel1, tabHtml);
	},
	
	createMarkerTabDescription: function(markerObj) {
		var tabHtml = '<div class="events-tooltip-tab-content">'
			+ '<p>'+markerObj.event_description.replace(/([^>]?)\n/g, '$1<br />\n')+'</p>';
		
		tabHtml = tabHtml + markerObj.registration_link
			+ '</div>';
		
		return new GInfoWindowTab(txTdsPi1LLL.tooltipLabel2, tabHtml);
	}
}