var map;
var inc;
var my_map;
var geocoder;
var tab1,tab2,tab3,tab4;
var tmpmarker,tmpzoom;
var event_location;

/*******************************************************************************
 * This is the main function for the event,it load the map and adding the      *
 * control                                                                     *
 *******************************************************************************/

function load(my_parameters)
{
	var my_load=document.my_data.my_form_location.value+"empty";

  // If the location event isn't empty load the map and the parameters 

	if(my_load!="empty")
	{
		if(GBrowserIsCompatible())
		{
			var my_disable=0;
			var my_see_or_hide=1;
			var tmppoint;
			var tmpzoom;
	
			my_map=document.getElementById("map");
			map=new GMap2(my_map);

			// In my_parameters is present a string with 4 characters and every it 
			// rapresents the flag for add a new control in a google maps

			if(my_parameters[0]=='1')
				map.addControl(new GLargeMapControl());
			if(my_parameters[1]=='1')
				map.addControl(new GScaleControl());
			if(my_parameters[2]=='1')
				map.addControl(new GOverviewMapControl());
			if(my_parameters[3]=='1')
				map.addControl(new GMapTypeControl());

			// This block capture the mouse scrolling and with it zoomin or
			// Zoom out when the event is present only on map

			GMap2.prototype.wheelZoom = function(event)
			{ 
				if((event.detail || -event.wheelDelta) < 0)
				{
					map.zoomIn();
				}
				else
				{
					map.zoomOut();
				}
				return false;
			}
			GEvent.addDomListener(my_map, "DOMMouseScroll", map.wheelZoom);
			GEvent.addDomListener(my_map, "mousewheel", map.wheelZoom);
	
			geocoder=new GClientGeocoder();

			// Add in a google maps the table for the new menu

			inc = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(0,0));
			inc.apply(document.getElementById("my_table"));
			map.getContainer().appendChild(document.getElementById("my_table"));
		}
	}
	else
	{
		document.getElementById("main_div").removeChild(document.getElementById("map"));
		document.getElementById("main_div").removeChild(document.getElementById("my_table"));
	}
}

/*******************************************************************************
 * With this function through the address if it is correct a new marker is     *
 * create                                                                      *
 *******************************************************************************/

function markedMap(address,zoom,my_marker_color)
{
	if(geocoder)
	{
		geocoder.getLatLng(address,function(point)
		{
	
			// If the address isn't correct it not return a point and this block remove
			// from the page the map and the table

			if(!point)
			{
				document.getElementById("main_div").removeChild(document.getElementById("map"));
				document.getElementById("main_div").removeChild(document.getElementById("my_table"));
			}
		
			// If the address is correct

			else
			{
				
				// Generate the string tabs for include it in a popup marker

				generateTabs();

				// Create the array  tabs

				var infoTabs = [
          new GInfoWindowTab("Main",tab1+"<br>"+tab2+"<br>"),
          new GInfoWindowTab("Route",tab4)
        ];
				
				// Set the personal icon for the marker
				var fingerIcon=new GIcon();
				fingerIcon.image=my_marker_color+".png";
				fingerIcon.iconSize=new GSize(20, 34);
				fingerIcon.shadowSize=new GSize(37, 34);
				fingerIcon.iconAnchor=new GPoint(9, 34);
				fingerIcon.infoWindowAnchor=new GPoint(9, 2);
				fingerIcon.infoShadowAnchor=new GPoint(14, 25);
				fingerIcon.transparent=my_marker_color+".png";
				fingerIcon.printImage=my_marker_color+".png";
				fingerIcon.mozPrintImage=my_marker_color+".png";;

				// Set the temporary zoom and point

				tmppoint=point;
				tmpzoom=zoom;
				
				// In the maps is present a menu in a table,in this table
				// is present a form and this function compile it

				compilePoint(point);

				// Set the new center for the maps and create a marker
				// adding it in a maps and create a popup.
				// The map listen a click and if the click is on the marker
				// it open a popup,if is a generic point it set distance
				// with the marker and the point

				map.setCenter(point, zoom);
				var marker = new GMarker(point,fingerIcon);
				tmpmarker=marker;
				map.addOverlay(marker);
				marker.openInfoWindowTabsHtml(infoTabs);

				GEvent.addListener(map, "click", function(marker, point)
				{
					if(marker)
					{
						marker.openInfoWindowTabsHtml(infoTabs);
						map.setCenter(point, zoom);
					}
					if(point)
					{
						var km_and_m=""+tmpmarker.getPoint().distanceFrom(point)/1000;
						km_and_m=km_and_m.substring(0,km_and_m.indexOf(".")+4);
						km_and_m_splitted=km_and_m.split(".");
						var km=km_and_m_splitted[0];
						var meter=km_and_m_splitted[1];
						document.my_point.miadist.value=""+km+" km e "+meter+" m";
					}
				}
				);
			}
		}
		);
	}
}

/*******************************************************************************
 * This function generate a string tab that the marker use                     *
 *******************************************************************************/

function  generateTabs()
{
	tab1=document.my_data.my_form_title.value;

	var description=document.my_data.my_form_description.value;
	var date_start=document.my_data.my_form_date_start.value;
	var date_end=document.my_data.my_form_date_end.value;
	event_location=document.my_data.my_form_location.value;
	var desc=description+"<br>"
	desc=desc+event_location+"<br>";
	desc=desc+"dal "+date_start+" al "+date_end+"<br>";
	tab2=desc;

	tab4="<div id='div4'>";
	tab4=tab4+"<form name='direction' method='get'>";
	tab4=tab4+"<div align='center' style='color:red;'>";
	tab4=tab4+"Calculate Route".bold()+"<\/div><br>";
	tab4=tab4+"<div align='center'>";
	tab4=tab4+"<input type=text style='background:silver;' name='city_one' value="+document.my_data.my_form_location.value+"><\/div><br>";
	tab4=tab4+"<div align='center'><input type=text name=city_two value='city'><\/div><br>";
	tab4=tab4+"<div align='center'><input type='radio' name='my_direction_mode' value='EvToCity' checked>City ========> City<\/div>";
	tab4=tab4+"<div align='center'><input type='radio' name='my_direction_mode' value='CityToEv'>City <======== City<\/div><br>";
	tab4=tab4+"<div align='center'><input type='button' value='GO!!!' onclick='calculates()'><\/div>";
	tab4=tab4+"<\/form>";
	tab4=tab4+"<\/div>";
}

/*******************************************************************************
 * It preleve the contenute from the field form create for the marker and      *
 * mark it                                                                     *
 *******************************************************************************/

function MarkedForm(my_marker_color)
{
 	event_location=document.my_data.my_form_location.value;
	markedMap(event_location,9,my_marker_color);
}

/*******************************************************************************
 * In a popup marker is present a tab for calculate the plain from 2 city.     *
 * This function set the correct link for open a google maps utility           *
 *******************************************************************************/

function calculates()
{
  var from,to;
	if(document.direction.my_direction_mode[0].checked==true)
	{
		from=document.direction.city_one.value;
		to=document.direction.city_two.value;
	}
	else
	{
		from=document.direction.city_two.value;
		to=document.direction.city_one.value;
	}
	window.open("http://maps.google.com/maps?saddr="+from+"&daddr="+to,"_blank");
}

/*******************************************************************************
 * In new menu present in a table,there are 2 button restore and center        *
 * Center with value 1 center the map from the coordinates from marker         *
 * Restore with value 2 center the map from the longitude and latitude fields  *
 *******************************************************************************/

function setNewCenter(my_value)
{
	if(my_value==1)
	{
		 map.setCenter(new GLatLng(document.my_point.lat.value,document.my_point.lon.value),map.getZoom());
	}
	else
	{
		map.setCenter(tmppoint,map.getZoom());
		document.my_point.lat.value=latlon[0];
		document.my_point.lon.value=latlon[1];
	}
}

/*******************************************************************************
 * This function compile the longitude and latitude fields from the marker     *
 *******************************************************************************/

function compilePoint(my_point)
{
	var punstring=my_point.toString();
	var latlon=punstring.split(",");
	latlon[0]=latlon[0].substring(1,latlon[0].length);
	latlon[1]=latlon[1].substring(0,latlon[1].length-1);
	document.my_point.lat.value=latlon[0];
	document.my_point.lon.value=latlon[1];
}

/*******************************************************************************
 * In the map is possible hide or see the optional menu and this function set  *
 * it                                                                          *
 *******************************************************************************/

function hide_my_menu()
{
	if(my_see_or_hide>0)
	{
		document.getElementById("my_table").style.visibility="visible";
		document.getElementById("lat").style.visibility="visible";
  	document.getElementById("center").style.visibility="visible";
		document.getElementById("lon").style.visibility="visible";
		document.getElementById("restore").style.visibility="visible";
		document.getElementById("dispuls").style.visibility="visible";
		document.getElementById("distext").style.visibility="visible";
	}
	if(my_see_or_hide<0)
	{
		document.getElementById("my_table").style.visibility="hidden";
		document.getElementById("lat").style.visibility="hidden";
		document.getElementById("center").style.visibility="hidden";
		document.getElementById("lon").style.visibility="hidden";
		document.getElementById("restore").style.visibility="hidden";
		document.getElementById("dispuls").style.visibility="hidden";
		document.getElementById("distext").style.visibility="hidden";
	}
	my_see_or_hide=my_see_or_hide*-1;
} 
