/* common AJAX helper class */
var Helper = {
	lastResult: null,
	// shows or hides an element by clicking on a link
	// the link becomes enriched with a further class to show the current state
	switchDetail : function( destId, linkId, listenerFunc )
	{
		var dest_el = $( destId );
		var link_el = $( linkId );

		if (!dest_el || !link_el)
			return;

		var visible = dest_el.style.display != 'none';
		dest_el.style.display = visible ? 'none' : '';
		link_el.className = !visible ? 'switchDetail' : 'switchDetail expand';

		if (typeof( listenerFunc ) == 'function')
			listenerFunc( !visible );	// admit current state, it's already negotiated
	},
	
	switchResultDetail : function( id, listenerFunc )
	{
		if (Helper.lastResult && id != Helper.lastResult)
			Helper.switchDetail( 'details' + Helper.lastResult, 'detailsLink' + Helper.lastResult, function(){$( 'entry' + Helper.lastResult ).className = 'entry';	} );
		
		Helper.switchDetail( 'details' + id, 'detailsLink' + id, function( state )
		{
			listenerFunc();
			$( 'entry' + id ).className = state ? 'entry curEntry' : 'entry';
			
			Helper.lastResult = (!state && id==Helper.lastResult) ? null : id;
		} );
	},
	
	changeDetailState: function( state )
	{
		$( 'details' ).value = state ? '1' : '0';
	}
};

/* Manages Maps and global points, created to act like a global map helper */
var MapManager = {
	mapPoints: new Array(),
	mapStack: new Array(),

	addMapPoint: function( point )
	{
		if (!point[0] || !point[1])
			return;

		MapManager.mapPoints.push( point );
	},
	
	createMap: function( elId, options )
	{
		if ( typeof( MapManager.mapStack['elId'] ) != 'undefined' )
			return;

		MapManager.mapStack[ elId ] = new Map( elId, options );
		var map_ctrl = $( elId ).up().getElementsByClassName( 'mapCtrl' );
		if (map_ctrl.length)
			new MapControl( map_ctrl[0], MapManager.mapStack[ elId ] );

	}
}


/* map class to provide map functions */
var Map = Class.create();
Map.prototype = {
	initialize: function( elId, options )
	{
		if (!GBrowserIsCompatible()) 
			return;

		this.lat = (typeof( options ) != 'undefined' && 'lat' in options) ? options.lat : 47.66978023497618;
		this.lng = (typeof( options ) != 'undefined' && 'lng' in options) ? options.lng : 9.438285827636719;
		
		if (this.lat == 0 || this.lng == 0)
			$( elId ).up().hide();

		this.map = new GMap2( $( elId ) );
		this.map.setCenter( new GLatLng( this.lat, this.lng ), typeof( options )=='undefined' ? 12 : ( $(elId).className == 'bigDetailMap' ? 12 : 13 ) );

		if ( typeof( options ) != 'undefined' )
		{
			this.options;
			this.addMarker( new GLatLng( this.lat, this.lng ) );
			if ( "large" in options && options.large )
				this.map.addControl( new GLargeMapControl() );
		}
		else
		{
			this.map.addControl( new GLargeMapControl() );
			this.displayMapPoints();
		}
	},
	
	addMarker: function( point )
	{
		this.map.addOverlay( new GMarker( point ) );
	},

	// using MapManagers mapPoints to display the search results
	displayMapPoints: function()
	{
		var baseIcon = new GIcon( G_DEFAULT_ICON );
		var icon = new GIcon( baseIcon );
		var bounds = new GLatLngBounds();

		if (MapManager.mapPoints.length == 0)
			return;

		MapManager.mapPoints.each( function( source, index )
		{
			var latlng = new GLatLng( source[0], source[1] );
			var letter = String.fromCharCode( "A".charCodeAt(0) + index);
			icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
			var marker = new GMarker( latlng, {icon:icon} );
			
			this.map.addOverlay( marker );
			
			GEvent.addListener( marker, "mouseover", function() {marker.openInfoWindowHtml( source[2] );} );
			bounds.extend( latlng );
		}.bind( this ) );
		
		this.map.setCenter( bounds.getCenter() );
		this.map.setZoom( this.map.getBoundsZoomLevel( bounds ) );
	}
}

/* map control for Maps */
var MapControl = Class.create();
MapControl.prototype = {
	initialize: function( el, mapObj )
	{
		this.el = el;
		this.mapObj = mapObj;
		
		this.btnSmaller = this.el.getElementsByClassName( 'mapSmaller' )[0];
		this.btnNormal = this.el.getElementsByClassName( 'mapNormal' )[0];
		this.btnBigger = this.el.getElementsByClassName( 'mapBigger' )[0];
		this.btnLast = this.btnNormal;
		
		this.btnSmaller.onclick = function(){ this.executeAction( this.btnSmaller, function(){ this.map.setZoom( 12 ) }.bind( this.mapObj ) ) }.bind( this );
		this.btnNormal.onclick = function(){ this.executeAction( this.btnNormal, function(){ this.map.setZoom( 13 ) }.bind( this.mapObj ) ) }.bind( this );
		this.btnBigger.onclick = function(){ this.executeAction( this.btnBigger, function(){ this.map.setZoom( 14 ) }.bind( this.mapObj ) ) }.bind( this );
	},
	
	executeAction: function( btn, func )
	{
		if (btn == this.btnLast)
			return;

		if (this.btnLast)
			this.btnLast.className = this.btnLast.className.replace( 'curBtn', '' );
			
		btn.className += ' curBtn';
		this.btnLast = btn;
		
		func();
	}
}
