
/* Initiate */

/*addLoadEvent(function() {
	var x = new GoogleUtility("mapWrapper", "mapCanvas", "mapList");
	x.Init();
});*/

addUnloadEvent(function() {
	GUnload();
});


/**************/
/* Google map */
/**************/

// Constructor

function GoogleUtility(containerId, canvasId, listContainerId) {
	this.MapContainerID = containerId;
	this.CanvasID = canvasId;
	this.ListContainerID = listContainerId;
	this.MapObject = null;
	this.MapMarkers = [];
	this.MapCenterLattitude = 0;
	this.MapCenterLongditude = 0;
	this.MapCenterMagnification = 0;
	this.MapMarkerImage = null;
}


// Public instance methods

GoogleUtility.prototype.Init = function()
{
  if(GBrowserIsCompatible())
  {
    // Make map Wrapper visible
    var mapWrapper = document.getElementById(this.MapContainerID)
    mapWrapper.style.display = "block";
    
    // Instantiate map
    this.MapObject = new GMap2(document.getElementById(this.CanvasID));
    
    // Find, set and apply map's center settings
    var settings = GoogleUtility.GetMapCoordText(mapWrapper);
    this.SetMapProperties(settings);
    this.ApplyMapProperties();
    
    // Find each point coords and add to the this.MapMarkers member
    var listWrapper = document.getElementById(this.ListContainerID)
    this.GetMarkerData(listWrapper);
    
    // Loop through markers and create points
    this.CreateMarkers();
  }
}

GoogleUtility.prototype.SetMapProperties = function(settings)
{
	var settingParts = settings.split(";");
	var hash = [];
	
	for(var i = 0; i < settingParts.length; i++)
	{
		hash = settingParts[i].split("|")
		switch(hash[0])
		{
			case "Lat":
				this.MapCenterLattitude = hash[1]; break;
			case "Lng":
				this.MapCenterLongditude = hash[1]; break;
			case "Mag":
				this.MapCenterMagnification = hash[1]; break;
			case "Marker":
				this.MapMarkerImage = hash[1]; break;
			default: break;
		}
	}
}

GoogleUtility.prototype.ApplyMapProperties = function()
{
	this.MapObject.setCenter(new GLatLng(this.MapCenterLattitude, this.MapCenterLongditude), this.MapCenterMagnification - 0);
	this.MapObject.addControl(new GSmallMapControl());
}


GoogleUtility.prototype.GetMarkerData = function(container)
{
	if(container.hasChildNodes())
	{
		var nodeList = container.childNodes;
		var mapMarker = null;
		for(var i=0; i<nodeList.length; i++)
		{
			if(nodeList[i].nodeType == document.ELEMENT_NODE || nodeList[i].nodeType == 1)
			{
				switch(nodeList[i].tagName.toLowerCase())
				{
					case "div":
						mapMarker = new MapMarker();
						mapMarker.SetMarkerProperties(nodeList[i].innerHTML);
						this.MapMarkers[this.MapMarkers.length] = mapMarker;
						break;
					default:
						this.GetMarkerData(nodeList[i]);
						break;
				}
			}
		}
	}
}

GoogleUtility.prototype.CreateMarkers = function()
{
	// Create icon object
	var icon = new GIcon();
	icon.image = this.MapMarkerImage;
	icon.iconSize = new GSize(11, 11);
	icon.iconAnchor = new GPoint(11, 11);
	icon.infoWindowAnchor = new GPoint(25, 7);

	var opts = { 
		"icon": icon,
		"clickable": true,
		"labelOffset": new GSize(-6, -10)
	};

	var point = null;
	var marker = null;

	for(var i=0; i < this.MapMarkers.length; i++)
	{
		point = new GLatLng(this.MapMarkers[i].Lattitude, this.MapMarkers[i].Longditude);
		marker = new GMarker(point, opts);
		GoogleUtility.AddLink(this.MapObject, marker, this.MapMarkers[i].Url, this.MapMarkers[i].Title)
		this.MapObject.addOverlay(marker);
	}
}


// Public static methods

GoogleUtility.GetMapCoordText = function(container)
{
	if(container.hasChildNodes())
	{
		var nodeList = container.childNodes;
		for(var i=0; i<nodeList.length; i++)
		{
			if(nodeList[i].nodeType === document.ELEMENT_NODE || nodeList[i].nodeType === 1)
			{
				switch(nodeList[i].tagName.toLowerCase())
				{
					case "div":
						if(nodeList[i].className === "coords")
							return nodeList[i].innerHTML;
						break;
					default:
						this.GetMapCoordText(nodeList[i]);
						break;
				}
			}
		}
	}
}

GoogleUtility.AddLink = function(map, marker, location, title)
{
	GEvent.addListener(marker, "click", function() {
		var html = "<div style=\"padding-right:10px;\"><b>" + title +
			"</b><br /><br /><a href=\"" + location + "\">View more info<\/a> ...<\/div>";
		marker.openInfoWindowHtml(html);
	});
}


/*********************/
/* Google map marker */
/*********************/

// Constructor

function MapMarker(lat, lng, url, target, title) {
	this.Title = title;
	this.Lattitude = lat;
	this.Longditude = lng;
	this.Url = url;
	this.Target = target;
}

// Public instance methods

MapMarker.prototype.SetMarkerProperties = function(settings)
{
	var settingParts = settings.split(";");
	var hash = [];
	
	for(var i = 0; i < settingParts.length; i++)
	{
		hash = settingParts[i].split("|")
		switch(hash[0])
		{
			case "Title":
				this.Title = hash[1]; break;
			case "Lat":
				this.Lattitude = hash[1]; break;
			case "Lng":
				this.Longditude = hash[1]; break;
			case "Url":
				this.Url = hash[1]; break;
			case "Target":
				this.Target = hash[1]; break;
			default: break;
		}
	}
	
}



