// map object
var map;
var mapType;
var mapTypeControl;
var mapNavigation;
var mapOverview;
var geocoder;
var points;
var controllerVisibility;
var maxZoom = 15;
// geo coords array
var coords;
// infos array
var infos;
// map center
var center;

// Call this function when the page has been loaded
function init(){
	if ( GBrowserIsCompatible() ) {
		map = new google.maps.Map2( document.getElementById( "map" ) );
		map.setCenter( new google.maps.LatLng( 45, 0 ), 1 );
      	
      	geocoder = new GClientGeocoder();
      	points = [];
      	
      	
      	//create custom Marker     	
        var tinyIcon 		= new GIcon();
 		tinyIcon.image 		= "http://www.logwin-logistics.com/fileadmin/templates/corporatesite/img/marker.png";
 		tinyIcon.shadow 	= "http://www.logwin-logistics.com/fileadmin/templates/corporatesite/img/marker_shadow.png";
 		tinyIcon.iconSize 	= new GSize( 20, 19 );
 		tinyIcon.shadowSize = new GSize( 20, 19 );
 		tinyIcon.iconAnchor = new GPoint( 6, 19 );
 		markerOptions 		= { icon:tinyIcon };
 		
 		setPoints();
	}
}

// set map infos
function setMapInfos( c, i, b, z ){

	coords 	= c;
	infos	= i;
	if( z != undefined ) maxZoom = z;
	
	if( b ) controllerVisibility = b; 
	else controllerVisibility = false;
}

// set markers
function setPoints(){
	points = [];
    for( var i = 0; i < coords.length; i++ ){
    	if( coords[i].lat ){
			var point 	= new google.maps.LatLng( coords[i].lat, coords[i].lon );
    		map.addOverlay( createMarker( point, i ) );
  		  	points.push( point );
  		  	resizeMap( points );
    	} else {
    		getPointByAddress( unescape(coords[i]), i );
    	}
	}
	if( controllerVisibility ) showController();
	
	
}

function resizeMap( points ) {
	// create new bounds object
	var bounds = new GLatLngBounds();

	// Loop through the points, extending the bounds as necessary
	for ( var i = 0; i < points.length; i++ ) {
		bounds.extend( points[i] );
	}

	// Find the centre of the new bounds
	var lat = bounds.getSouthWest().lat() + ( ( bounds.getNorthEast().lat() - bounds.getSouthWest().lat() ) / 2 );
	var lon = bounds.getSouthWest().lng() + ( ( bounds.getNorthEast().lng() - bounds.getSouthWest().lng() ) / 2 );

	// Get the bounds zoom level
	var zoom = map.getBoundsZoomLevel( bounds );
	
	center = new GLatLng( lat, lon );
	
	if( zoom < 2 ){
		zoom = 1;
		center = new GLatLng( 30, 20 );
	}	
	
	if( zoom > maxZoom ) {
		zoom = maxZoom;
	}

	// Change the map to the new bounds values
	map.setCenter( center, zoom );
} 

// set center
function setCenter() {
	//alert(center);
	map.checkResize();
	map.panTo( center );
}

// create marker
function createMarker( point, number ){
    var marker		= new google.maps.Marker( point, markerOptions );
	var info 		= htmlspecialchars_decode( infos[number], 'ENT_QUOTES' );
	//alert(info);
	google.maps.Event.addListener( marker, "click", function() {
		map.openInfoWindowHtml( point, info );
	});
	
	return marker;
}

// returns long/lat Point from given address
function getPointByAddress( address, idx ){
	geocoder.getLatLng(
		address,
		function (point){
			if (!point) {
				//alert(address + " not found");
			} else {
				map.addOverlay( createMarker( point, idx ) );
  		  		points.push( point );
			}
			if( idx == coords.length-1) resizeMap( points );
		}
	);
}

// Show the controler on the map
function showController(){
	mapTypeControl = new GMapTypeControl();
	mapType = new GControlPosition( G_ANCHOR_TOP_RIGHT, new GSize( 10, 10 ) );
	mapNavigation = new GLargeMapControl();
	mapOverview = new GOverviewMapControl();
	map.addControl( mapTypeControl, mapType );
	map.addControl( mapNavigation );
	map.addControl( mapOverview );
	setCenter();
}

// Removes the controller on the map
function hideController(){
	map.removeControl( mapTypeControl );
	map.removeControl( mapNavigation );
	map.removeControl( mapOverview );
}

function htmlspecialchars_decode(str, quote_style) {
    str = str.toString();
    
    // Always encode
    str = str.replace(/&amp;/g, '&');
    str = str.replace(/&lt;/g, '<');
    str = str.replace(/&gt;/g, '>');
    
    // Encode depending on quote_style
    if (quote_style == 'ENT_QUOTES') {
        str = str.replace(/&quot;/g, '"');
        str = str.replace(/&#039;/g, '\'');
    } else if (quote_style != 'ENT_NOQUOTES') {
        // All other cases (ENT_COMPAT, default, but not ENT_NOQUOTES)
        str = str.replace(/&quot;/g, '"');
    }
    
    return str;
}

// load google maps using Google AJAX API Loader
google.load( "maps", "2.132", {"language" : "de"} );
// call init when DOM is loaded
google.setOnLoadCallback( init );
// unload google maps
//window.onunload = google.maps.Unload;

