// so far using the service of : google and maxmind 
// http://j.maxmind.com/app/geoip.js

	var visitor_lat;
	var visitor_lon;
	var visitor_city;
	var visitor_region_code;
	var visitor_region;
	var visitor_country;
	var visitor_countrycode;
	var visitor_postcode;

	var map;
	var geocoder;
	var zoom_init;
	function initGoogleMap(zoom, container) 
	{
		if (zoom === undefined)
			zoom = 5;
		
		zoom_init = zoom ;
		
		 var latlng = new google.maps.LatLng(40, 0);
		    var myOptions = {
		      backgroundColor:"#99B3CC",
		      zoom: zoom,
		      center: latlng,
		      disableDefaultUI: true,
		      navigationControl: true,
		      scaleControl: false,
		      mapTypeId: google.maps.MapTypeId.ROADMAP
		    };
		  
		  var containername = "map_canvas";
		  if (container != undefined)
		  	containername = container;
		  
		  map = new google.maps.Map(document.getElementById(containername), myOptions);
		  
		  // I love googlemap! this is the cleanest way i found to set the zoom min to 1 in order to not get the ugly view when zoom too small...
		  google.maps.event.addListener(map, "zoom_changed", function() { if (map.getZoom() < zoom_init) map.setZoom(zoom_init); });  
		  
		  return map;
	}
	
	var locations = [];
	var marker = null;
	var markers = [];
	
	function addMarker(latitude, longitude, name, pmap) 
	{ 
			var currentmap;
			if (pmap != undefined)
				currentmap = pmap;
			else
				currentmap = map;
			
			if (map ==null) return;
			
			myLatlng = new google.maps.LatLng(latitude,longitude);
			
			//var image = '/images/map/position.png'; bug in fucking google map when you set a zoom afterwards. 
			 
			marker = new google.maps.Marker({
											      position: myLatlng, 
											      map: currentmap, 
											      title:name
											     // icon: image
											  });
			
			currentmap.setCenter(myLatlng);
	}
	

	function addStepToItinerary(latitude, longitude, name, pmap) 
	{ 
			var currentmap;
			if (pmap != undefined)
				currentmap = pmap;
			else
				currentmap = map;
				
		if (currentmap ==null) return;
		
		myLatlng = new google.maps.LatLng(latitude,longitude);
		
		//var image = '/images/map/position.png'; //bug in fucking google map when you set a zoom afterwards. 
		image = new google.maps.MarkerImage("/images/map/position.png", new google.maps.Size(16, 16),  new google.maps.Point(0,0), new google.maps.Point(0, 16),  new google.maps.Size(16, 16) );
		 
		marker = new google.maps.Marker({
										      position: myLatlng, 
										      map: currentmap, 
										      title:name,
										      icon: image
										  });
		
		locations.push( { lat:latitude, long:longitude, label:name } );
		
		//map.setCenter(myLatlng);
	}
	
	function centerMap(latitude, longitude, zoom, pmap) 
	{ 
			var currentmap;
			if (pmap != undefined)
				currentmap = pmap;
			else
				currentmap = map;
				
				
		myLatlng = new google.maps.LatLng(latitude,longitude);
		currentmap.setCenter(myLatlng);
		
		if (zoom !== undefined)
			currentmap.setZoom(zoom);
	}

	// Deletes all markers in the array by removing references to them
	function removeMarker() 
	{
		 if (marker != null) 
		 {
			 marker.setMap(null);
			 marker = null;
		 }
	}
	
	function addMarkerToItinerary(zoom , pmap) 
	{ 
			var currentmap;
			if (pmap != undefined)
				currentmap = pmap;
			else
				currentmap = map;
				
		if (currentmap ==null || marker == null) return;
		
		locations.push( { lat:marker.getPosition().lat(), long:marker.getPosition().lng(), label:marker.getTitle() } );
		
		image = new google.maps.MarkerImage("/images/map/position.png", new google.maps.Size(20, 32),  new google.maps.Point(0,0), new google.maps.Point(0, 32),  new google.maps.Size(20, 32) );
		
		mark = new google.maps.Marker({
								      position: marker.getPosition(), 
								      map: currentmap, 
								      title:marker.getTitle(),
								      icon:image
								  });
		markers.push(mark);
		
		if (zoom === undefined)
		{
			if (currentmap.getZoom() < 3)
				currentmap.setZoom(3);
		}
		
		currentmap.setCenter(marker.getPosition());
		
		removeMarker();
	
	}
	
	var flightPath;
	
	function drawItinerary(pmap) 
	{ 
			var currentmap;
			if (pmap != undefined)
				currentmap = pmap;
			else
				currentmap = map;
				
		if (currentmap ==null) return;
		
		var flightPlanCoordinates = [];
		
		for (var i=0; i <= locations.length - 1; i++)
		{
			flightPlanCoordinates[i] = new google.maps.LatLng(locations[i].lat, locations[i].long);
		}
		
    flightPath = new google.maps.Polyline({
								          path: flightPlanCoordinates,
								          strokeColor: "#34B2CE",
								          strokeOpacity: 1.0,
								          strokeWeight: 2
								        });

    flightPath.setMap(currentmap);
	}
	
	function clearItinerary(pmap) 
	{ 
			var currentmap;
			if (pmap != undefined)
				currentmap = pmap;
			else
				currentmap = map;
	
		if (currentmap ==null) return;
		
		if (flightPath != null)
			flightPath.setMap(null);
		
		locations.length = 0;
		
		for (i in markers) 
		{
		      markers[i].setMap(null);
		}
		markers.length = 0;
	}
	
	
	function cleanPositions()
	{
		locations.length = 0;
	}

