var map;
var my_index,index2;
var my_google_map;
var corrects=0;
var my_input=0;
var form_type_marker=0;
var my_title,my_description,my_event_start,	my_event_end,my_event_location,url;
var my_locations_array=new Array();
var my_url_array=new Array();
var my_title_array=new Array();
var my_desc_array=new Array();

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

function load(my_parameters)
{
	if(GBrowserIsCompatible())
	{
		my_google_map=document.getElementById("map");
		map=new GMap2(my_google_map);
		map.setCenter(new GLatLng(41.5643, 12.0000), 5);

		// In my_color 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_google_map, "DOMMouseScroll", map.wheelZoom);
		GEvent.addDomListener(my_google_map, "mousewheel", map.wheelZoom);
		geocoder=new GClientGeocoder();
	}
}

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

function markedEvent(address,url,my_title,desc,my_marker_color)
{
	if(geocoder)
	{ 
		geocoder.getLatLng(address,function(point)
		{
			if(!point)
			{
			}
			else
			{ 
        corrects++;
				// Create a personal icon with the correct color

				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 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

				var mark = new GMarker(point,fingerIcon);							
				GEvent.addListener(mark, "click", function() 
				{
					map.setCenter(point,9);
					mark.openInfoWindowHtml("<table width='200'><a href='"+url+"'>"+my_title+"<\/a><br>"+desc+"<\/table>");
				}
				);
				map.addOverlay(mark);
			}
      my_input++;
      if(my_input==form_type_marker)
        if(corrects==0)
          document.getElementById("main_div").removeChild(document.getElementById("map"));
		}
		);
	}
}

/*******************************************************************************
 * It preleve the contenute from the field forms create for the markers and    *
 * send it the string tab for the popup and the correct color                  *
 *******************************************************************************/

function SendForms(my_marker_color)
{
	my_index=0,index2=0;
	
	// Scan every form in a plone site
  // and obtain the number of miomarker
  while(index2<document.forms.length)
  {
    if(document.forms[index2].name=="marker")
    {
      my_title=document.forms[index2].elements[1].value;
			my_description=document.forms[index2].elements[2].value;
			my_event_start=document.forms[index2].elements[3].value;
			my_event_end=document.forms[index2].elements[4].value;
			my_event_location=document.forms[index2].elements[5].value;
			url=document.forms[index2].elements[6].value;
	
			desc=my_description+"<br>";
			desc=desc+my_event_location+"<br>";
			desc=desc+"dal "+my_event_start+" al "+my_event_end+"<br>";

      my_locations_array.push(my_event_location);
      my_url_array.push(url);
      my_title_array.push(my_title);
      my_desc_array.push(desc);
      
      form_type_marker++;
    }    
    index2++;
  }
  while(my_locations_array.length>0)
  {
    markedEvent(my_locations_array.pop(),
                my_url_array.pop(),
                my_title_array.pop(),
                my_desc_array.pop(),
                my_marker_color);
  }
  if(form_type_marker==0)
    document.getElementById("main_div").removeChild(document.getElementById("map"));
}
