var MG = {
	'Util': {},
	'Event': {}
};


MG.Event.addEvent = function( obj, type, fn )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
};


MG.Util.loadExternalJs = function(url)
{
	var e = document.createElement("script");
	e.setAttribute('url', url);
	e.setAttribute('type', 'text/javascript');
	
	var head = document.getElementsByTagName('head').item(0);
	head.insertBefore(e, head.firstChild);
};



// ----------------------------------------------------------------------------
// HasClassName
//
// Description : returns boolean indicating whether the object has the class name
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
MG.Util.HasClassName = function(objElement, strClass)
{

   // if there is a class
   if ( objElement.className )
      {

      // the classes are just a space separated list, so first get the list
      var arrList = objElement.className.split(' ');

      // get uppercase class for comparison purposes
      var strClassUpper = strClass.toUpperCase();

      // find all instances and remove them
      for ( var i = 0; i < arrList.length; i++ )
         {

         // if class found
         if ( arrList[i].toUpperCase() == strClassUpper )
            {

            // we found it
            return true;

            }

         }

      }

   // if we got here then the class name is not there
   return false;

};
// 
// HasClassName
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// AddClassName
//
// Description : adds a class to the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to add
//
MG.Util.AddClassName = function (objElement, strClass, blnMayAlreadyExist)
{
	// if there is a class
	if ( objElement.className )
	{
		// the classes are just a space separated list, so first get the list
		var arrList = objElement.className.split(' ');
		
		// if the new class name may already exist in list
		if ( blnMayAlreadyExist )
		{
			// get uppercase class for comparison purposes
			var strClassUpper = strClass.toUpperCase();
			
			// find all instances and remove them
			for ( var i = 0; i < arrList.length; i++ )
			{
				// if class found
				if ( arrList[i].toUpperCase() == strClassUpper )
				{
					// remove array item
					arrList.splice(i, 1);
					
					// decrement loop counter as we have adjusted the array's contents
					i--;
				}
			}
		}
		
		// add the new class to end of list
		arrList[arrList.length] = strClass;
		
		// add the new class to beginning of list
		//arrList.splice(0, 0, strClass);
		
		// assign modified class name attribute
		objElement.className = arrList.join(' ');
	}
	// if there was no class
	else
	{
		// assign modified class name attribute      
		objElement.className = strClass;
	}
}
// 
// AddClassName
// ----------------------------------------------------------------------------


// ----------------------------------------------------------------------------
// RemoveClassName
//
// Description : removes a class from the class attribute of a DOM element
//    built with the understanding that there may be multiple classes
//
// Arguments:
//    objElement              - element to manipulate
//    strClass                - class name to remove
//
MG.Util.RemoveClassName = function(objElement, strClass)
{
	// if there is a class
	if ( objElement.className )
	{
		// the classes are just a space separated list, so first get the list
		var arrList = objElement.className.split(' ');
		
		// get uppercase class for comparison purposes
		var strClassUpper = strClass.toUpperCase();
		
		// find all instances and remove them
		for ( var i = 0; i < arrList.length; i++ )
		{
			// if class found
			if ( arrList[i].toUpperCase() == strClassUpper )
			{
				// remove array item
				arrList.splice(i, 1);
				
				// decrement loop counter as we have adjusted the array's contents
				i--;
			}
		}
		
		// assign modified class name attribute
		objElement.className = arrList.join(' ');
	}
}
// 
// RemoveClassName
// ----------------------------------------------------------------------------
  




var Map = {
	init: function()
	{
		if ( !document.getElementById("map") )
			return;
		
		if ( GBrowserIsCompatible() )
		{
			Map.map = new GMap2(document.getElementById("map"));
			
			var mapCenter = new GLatLng( 42.94838139765314, 11.23077392578125);
			Map.map.setCenter(mapCenter, 10);
			
			Map.map.addControl( new GSmallZoomControl() );
			Map.map.addControl( new GMapTypeControl() );
			
			var mapMarkerCoords = new GLatLng( 42.959924, 11.189489);
			var mapMarker = new GMarker(mapMarkerCoords);
			
			GEvent.addListener(mapMarker, 'click', 
				function() {
					mapMarker.openInfoWindowHtml('<div id="MapInfo"><strong>Agriturismo Le Lupinaie</strong><br />via della Stazione<br />58036 Roccastrada (GR)<br />Tel. +39 0564/563114</div>', {maxWidth:160});
				}
			);
			Map.map.addOverlay(mapMarker);
			
			MG.Util.RemoveClassName(document.getElementById('mapwrap'), 'loading');
			
			MG.Event.addEvent(window, 'unload', GUnload);
		}
	}
};

MG.Event.addEvent(window, 'load', Map.init);

