var Util = {
	$ : function(params) {
		if( document.getElementById(params) )
			return document.getElementById(params);
		
		return null;
	},

	getValue : function(selector, type) {
		if( null == type )
			type = 'select';
			
		switch( type ) {
			case 'select' :
				var index = this.$(selector).selectedIndex;
				return this.$(selector).options[index].value;
				break;
		}
	},
	
	redirect : function(url) {
		document.location = url;
	}

};

var GeoControls = {
		
	currentId : null,
	currentMarkerId : null,
	map : null,
	markers : new Array(),
	zooms : new Array(),
	bounds : null,
	propositionMarker : null,
		
	addMarker : function(id, marker, zoom) {
		this.markers[id] = marker;
		this.zooms[id] = zoom;
	},
	
	updateCategory : function() {
		var newId = Util.getValue('idgeocat');
		if( newId != this.currentId && newId != 'null' ) {
			Util.redirect('?idgeocat=' + newId);
		}
	},
	
	signalError : function(cat, id) {
		if( confirm("Veuillez confirmer que vous souhaitez signaler ce lieu commme erroné.") ) {
			Util.redirect('?idgeocat=' + cat + "&idplace=" + id + "&alert=1");
		}
	},
	
	updatePoint : function() {
		var data = Util.getValue('idpoint').split('|');
		if( data[0] == 'all' ) {
			return this.showAll();
		}
		
		var latitude = data[0];
		var longitude = data[1];
		var zoom = data[2];
		var id = data[3];
		
		if( id == this.currentMarkerId )
			return;

		this.map.setZoom( Number(zoom) );
		this.map.panTo( new GLatLng(latitude, longitude) );
		this.currentMarkerId = id;
		setTimeout('GeoControls.showMarkerInfos()', 500);
	},
	
	zoomToPoint : function(id)
	{
		var marker = this.markers[id];
		if( null != marker ) {
			this.map.setCenter( marker.getLatLng(), this.zooms[id] );
			this.currentMarkerId = id;
			setTimeout('GeoControls.showMarkerInfos()', 500);
		}
	},
	
	showMarkerInfos : function(id) {
		if( null == id )
			id = this.currentMarkerId;
		
		GEvent.trigger(this.markers[id], 'click');
	},
	
	showAll : function() {
		if( null != this.currentMarkerId ) {
			this.markers[this.currentMarkerId].closeInfoWindow();
			this.currentMarkerId = null;
		}
		this.map.setCenter(this.bounds.getCenter(), this.map.getBoundsZoomLevel(this.bounds));
	},
	
	setUpPropositionMode : function() {
		GEvent.addListener(this.map, 'dblclick', function(overlay, latlng) { GeoControls.addPropositionMarker(latlng); } );
		GEvent.addListener(this.map, 'zoomend', function() { GeoControls.updatePropositionCoords() } );
	},
	
	openRoute : function(id) {
		var from = Util.$('from_' + id).value;
		if( from == '' || from == ' ') {
			return alert('Veuillez indiquer votre ville de départ');
		}
			
		var marker = this.markers[id];
		var to = marker.getLatLng().lat().toString() + ' ' + marker.getLatLng().lng().toString();
		var link = 'http://maps.google.fr/?q=from:' + from + ' to:' + to;
		openWithSelfMain(link, 'route', 1010, 770);
	},
	
	addPropositionMarker : function(latlng) {
		this.map.clearOverlays();
		
		GeoControls.propositionMarker = new GMarker(
			latlng, {title:"Vous pouvez déplacez ce point", draggable:true}
		);
		GEvent.addListener(this.propositionMarker, 'drag', function() { GeoControls.updatePropositionCoords() } );
		this.map.addOverlay( this.propositionMarker );
		GeoControls.updatePropositionCoords();
	},
	
	registerPropositionMarker : function(marker) {
		this.propositionMarker = marker;
		GEvent.addListener(this.propositionMarker, 'drag', function() { GeoControls.updatePropositionCoords() } );
		GEvent.addListener(this.map, 'zoomend', function() { GeoControls.updatePropositionCoords() } );
	},
	
	updatePropositionCoords : function() {
		if( null != GeoControls.propositionMarker ) {
			var markerPos = GeoControls.propositionMarker.getLatLng();
			Util.$('pi_latitude').value = markerPos.lat().toString();
			Util.$('pi_longitude').value = markerPos.lng().toString();
			Util.$('pi_zoom').value = GeoControls.map.getZoom().toString();
		}
	},
	
	checkEnterKey : function(e, link) {
		var characterCode;
		if(e && e.which){
			e = e;
			characterCode = e.which;
		} else {
			e = event;
			characterCode = e.keyCode;
		}
		if(characterCode == 13){
			GeoControls.openRoute(link);
		}
	},
	
	searchResultSet : function(params) {
		if( params.length > 0 ) {
			GeoControls.addPropositionMarker( params[0].marker.getLatLng() );
		}
	},
	
	createIcon : function(name) {
		var icon = new GIcon(G_DEFAULT_ICON);
		icon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/" + name + "-dot.png";
		icon.iconSize = new GSize(36, 36);
		icon.iconAnchor = new GPoint(18, 36);
		icon.infoWindowAnchor = new GPoint(18, 0);
		icon.shadowSize = new GSize(0, 0);
		return icon;
	}
		
};