
/* Scripts for Argos Yachtcharter GmbH
 * Copyright © 2002-2008 Inceoeren IT Consulting. All rights reserved. 
 */
 
var map;
 
function mapZoom( a )
{
	map.setZoom( map.getZoom() + a );
}

function mapLoad( x, y, z, maptitle)
{
	if( GBrowserIsCompatible() )
	{
		map = new GMap2( document.getElementById( "googleMap" ) );
		
		document.getElementById( "googleMap" ).style.backgroundColor="white";
		document.getElementById( "googleMapLabelDetail" ).innerHTML = maptitle;
		
		map.setCenter( new GLatLng( x, y ), z );
		map.setMapType( G_HYBRID_MAP );
		//map.addControl( new GSmallMapControl() );
		map.addControl( new GMapTypeControl() );
		map.addControl( new GScaleControl(50) );
		map.enableScrollWheelZoom();
		
		var tooltip = document.createElement( "div" );
		document.getElementById( "googleMap" ).appendChild( tooltip );
		tooltip.style.visibility="hidden";
		
        function markerIcon( type_id, dim_x, dim_y, anchor_x, anchor_y )
        {
        	var icon = new GIcon();
			icon.image = "http://www.argos-yachting.de/images/marker_"+type_id+".png";
			icon.iconSize = new GSize( dim_x, dim_y );
			icon.iconAnchor = new GPoint( anchor_x, anchor_y );
			icon.infoWindowAnchor = new GPoint( 5, 1 );
			return icon;
        }
                
        function createMarker( coords, zoom, type_id, dim_x, dim_y, anchor_x, anchor_y, html )
        {
        	var marker = new GMarker( coords, markerIcon( type_id, dim_x, dim_y, anchor_x, anchor_y ) );
        	
        	marker.tooltip = '<div class="googleMapTooltip"><p>'+html+'</p></div>';
        	
			GEvent.addListener( marker, "click", function()
			{
				tooltip.style.visibility="hidden"
				map.setCenter( coords );
				map.setZoom( zoom );
			} );
			
			GEvent.addListener( marker, "mouseover", function()
			{
				showTooltip( marker );
			} );
			
			GEvent.addListener( marker, "mouseout", function()
			{
				tooltip.style.visibility="hidden"
			} );

			return marker;
		}
				
		// initialize variables
		var gmarkers = new Array();
		var oceans = new Array();
		var harbours = new Array();
		var locations = new Array();
		
		harbours[0] = new Object();
harbours[0]['name'] = 'Langkawi (Langkawi Yacht Club)<br><ul><li>Sunsail</li></ul>';
harbours[0]['google_x'] = Number(6.302480577016523);
harbours[0]['google_y'] = Number(99.8501443862915);
harbours[0]['google_z'] = Number(16);
harbours[0]['type_id'] = '0';
harbours[0]['dim_x'] = 10;
harbours[0]['dim_y'] = 10;
harbours[0]['anchor_x'] = 1;
harbours[0]['anchor_y'] = 1;
harbours[0]['shown'] = true;
harbours[1] = new Object();
harbours[1]['name'] = 'Langkawi (Telaga Marina)<br><ul><li>Dream Yacht Charter</li></ul>';
harbours[1]['google_x'] = Number(6.366780457078888);
harbours[1]['google_y'] = Number(99.68457698822021);
harbours[1]['google_z'] = Number(15);
harbours[1]['type_id'] = '0';
harbours[1]['dim_x'] = 10;
harbours[1]['dim_y'] = 10;
harbours[1]['anchor_x'] = 1;
harbours[1]['anchor_y'] = 1;
harbours[1]['shown'] = true;
harbours[2] = new Object();
harbours[2]['name'] = 'Tioman Island (Telek Marina)<br><ul><li>Dream Yacht Charter</li></ul>';
harbours[2]['google_x'] = Number(2.8224299734677167);
harbours[2]['google_y'] = Number(104.16111946105957);
harbours[2]['google_z'] = Number(14);
harbours[2]['type_id'] = '0';
harbours[2]['dim_x'] = 10;
harbours[2]['dim_y'] = 10;
harbours[2]['anchor_x'] = 1;
harbours[2]['anchor_y'] = 1;
harbours[2]['shown'] = true;
locations[0] = new Object();
locations[0]['name'] = 'Langkawi (LGK)';
locations[0]['google_x'] = Number(6.328265470782941);
locations[0]['google_y'] = Number(99.72684860229492);
locations[0]['google_z'] = Number(13);
locations[0]['type_id'] = '2';
locations[0]['dim_x'] = 10;
locations[0]['dim_y'] = 10;
locations[0]['anchor_x'] = 1;
locations[0]['anchor_y'] = 1;
locations[0]['shown'] = true;
locations[1] = new Object();
locations[1]['name'] = 'Kuala Lumpur (KUL)';
locations[1]['google_x'] = Number(2.753760978329042);
locations[1]['google_y'] = Number(101.71005249023438);
locations[1]['google_z'] = Number(12);
locations[1]['type_id'] = '2';
locations[1]['dim_x'] = 10;
locations[1]['dim_y'] = 10;
locations[1]['anchor_x'] = 1;
locations[1]['anchor_y'] = 1;
locations[1]['shown'] = true;
locations[2] = new Object();
locations[2]['name'] = 'Singapore (SIN)';
locations[2]['google_x'] = Number(1.3434704720384343);
locations[2]['google_y'] = Number(104.01065826416016);
locations[2]['google_z'] = Number(13);
locations[2]['type_id'] = '2';
locations[2]['dim_x'] = 10;
locations[2]['dim_y'] = 10;
locations[2]['anchor_x'] = 1;
locations[2]['anchor_y'] = 1;
locations[2]['shown'] = true;
locations[3] = new Object();
locations[3]['name'] = 'Tioman Island (TOD)';
locations[3]['google_x'] = Number(2.8148002836743853);
locations[3]['google_y'] = Number(104.15854454040527);
locations[3]['google_z'] = Number(14);
locations[3]['type_id'] = '2';
locations[3]['dim_x'] = 10;
locations[3]['dim_y'] = 10;
locations[3]['anchor_x'] = 1;
locations[3]['anchor_y'] = 1;
locations[3]['shown'] = true;
			
		function getMarkers( array_name )
		{
			var batch = [];
			
			// loop through all locations and set markers
			for( var i=0; i < array_name.length; i++ )
			{
				if( array_name[i].google_x && array_name[i].google_y )
				{
					var coords = new GLatLng( array_name[i].google_x, array_name[i].google_y );
					batch.push( createMarker( coords, array_name[i].google_z, array_name[i].type_id, array_name[i].dim_x, array_name[i].dim_y, array_name[i].anchor_x, array_name[i].anchor_y, array_name[i].name ) );
				}
			}
			
			return batch;
		}
		
		function setupMarkers() 
		{
			mgr = new GMarkerManager( map );
			
			mgr.addMarkers( getMarkers( oceans ), 0 );
			mgr.addMarkers( getMarkers( harbours ), 6 );
			mgr.addMarkers( getMarkers( locations ), 8, 14 );
						
			mgr.refresh();
		}
		
		window.setTimeout( setupMarkers, 0 );
		
		function showTooltip( marker )
		{
			tooltip.innerHTML = marker.tooltip;
			
			var point=map.getCurrentMapType().getProjection().fromLatLngToPixel( map.getBounds().getSouthWest(),map.getZoom() );
			var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel( marker.getPoint(),map.getZoom() );
			var anchor=marker.getIcon().iconAnchor;
			var width=marker.getIcon().iconSize.width;
			var pos = new GControlPosition( G_ANCHOR_BOTTOM_LEFT, new GSize( offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y ) ); 
			
			pos.apply(tooltip);
			
			tooltip.style.visibility="visible";
		}
		
		GEvent.addListener( map, "wheelup", function(p)
		{
		  if ( map.getZoomLevel() > 0 ) {
		    map.centerAndZoom(
		      p.scaleRelative( map.getCenterLatLng() ),
		      map.getZoomLevel() - 1
		    );
		  }
		} );
		
		GEvent.addListener( map, "wheeldown", function(p)
		{
		  if ( map.getZoomLevel() <= 16 )
		    map.centerAndZoom(
		      p.scaleRelative( map.getCenterLatLng(), -1 ),
		      map.getZoomLevel() + 1
		    );
		} );
		
		GEvent.addDomListener(map.getContainer(), "DOMMouseScroll",
		function(oEvent)
		{
			if( oEvent.preventDefault )
			oEvent.preventDefault();
		} ); 
		
		// things that happen when the map is moved - basically just for debugging and detecting the coordinates for locations
		GEvent.addListener( map, "moveend", function()
		{
			var center = map.getCenter();
			var zoom = map.getZoom();
			document.getElementById("googleMapLabelDetail").innerHTML = center.toString() + ' ' + zoom.toString();
        } );
	}
}