﻿var map, geocoder;
var baseIcon;
var locationList = new Array();

function showMap(largeControl)
{
	if (GBrowserIsCompatible())
	{
		baseIcon = new GIcon();
		baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		baseIcon.iconSize = new GSize(20, 34);
		baseIcon.shadowSize = new GSize(37, 34);
		baseIcon.iconAnchor = new GPoint(9, 34);
		baseIcon.infoWindowAnchor = new GPoint(9, 2);
		baseIcon.infoShadowAnchor = new GPoint(18, 25);
		
		map = new GMap2(document.getElementById("map"));
		if (largeControl) map.addControl(new GLargeMapControl());
		else map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		// When the info window closes, hide the venue's image
		var thePic = document.getElementById("thePic");
        if (thePic != null) GEvent.addListener(map, "infowindowclose", function() { document.getElementById("thePic").src = "../images/Venues/white.jpg"; });
		geocoder = new GClientGeocoder();
		
		return true;
	}
	else
	{
		return false;
	}
}


function Venue(iCode, iCoords, iName, iPopupText, iURL, iImage, iDraggable, iOnClick, iOnDragEnd)
{
	this.code = iCode;
	this.name = iName;
	this.popupText = iName + "<br>" + iPopupText;
	if (iURL != null && iURL != '') this.popupText = this.popupText + "<br><a href='http://" + iURL + "' target='venue'>" + iURL + "</a>";
	this.image = iImage;
	
	this.show = Venue_show;
	
	var index = locationList.length;
	locationList[index] = this;
	
	this.label = String.fromCharCode(65 + index);
	var icon = new GIcon(baseIcon);
	icon.image = "http://www.google.com/mapfiles/marker" + this.label + ".png";
	this.marker = new GMarker(iCoords, {icon:icon, draggable:iDraggable});
	map.addOverlay(this.marker);
	if (iOnClick == null) GEvent.addListener(this.marker, "click", function(){showLocation(iCode);});
	else GEvent.addListener(this.marker, "click", function() { iOnClick(iCode); });
	if (iOnDragEnd != null) GEvent.addListener(this.marker, "dragend", function() { iOnDragEnd(iCode); });
	var legend = document.getElementById("legend");
	if (legend) legend.innerHTML = legend.innerHTML + "<p>" + this.label + ": <a href=\"javascript:showLocation('" + this.code + "');\">" + iName + "</a></p>";
		//legend.innerHTML = legend.innerHTML + "<p>" + this.label + ": <a href=\"javascript:showLocation('" + this.code + "');\">" + iName + "</a></p><p class='printOnly'>" + this.popupText + "</p>";
}

function Venue_show()
{
    this.marker.openInfoWindowHtml(this.popupText);
    var thePic = document.getElementById("thePic");
    var details = document.getElementById("details");
	if (this.image != null && this.image != '')
	{
	    if (thePic != null) thePic.src = "../images/Venues/" + this.image + ".jpg";
		if (details != null) details.innerHTML = this.label + ": " + this.popupText;
	}
	else
	{
	    if (thePic != null) thePic.src = "../images/Venues/white.jpg";
		if (details != null) details.innerHTML = "";
	}
}


function City(iCode, iCoords, iName, iURL)
{
	this.code = iCode;
	this.url = iURL;
	
	this.show = City_show;
	
	var index = locationList.length;
	locationList[index] = this;
	
	var label = String.fromCharCode(65 + index);
	var icon = new GIcon(baseIcon);
	icon.image = "http://www.google.com/mapfiles/marker" + label + ".png";
	this.marker = new GMarker(iCoords, icon);
	map.addOverlay(this.marker);
	GEvent.addListener(this.marker, "click", function(){showLocation(iCode);});
	var legend = document.getElementById("legend");
	if (legend) legend.innerHTML = legend.innerHTML + "<p>" + label + ": <a href=\"" + iURL + "\">" + iName + "</a></p>";
}

function City_show()
{
	window.location.href = this.url;
}


function startMapCountry(name, setCenter)
{
	var legend = document.getElementById("legend");
	if (legend)
		legend.innerHTML = legend.innerHTML + "<h2><a href=\"javascript:map.setCenter(" + setCenter + ")\">" + name + "</a></h2>";
}


function findLocation(code)
{
    for (var i = 0; i < locationList.length; ++i)
    {
        if (locationList[i].code == code) return locationList[i];
    }
    return null;
}


function showLocation(code)
{
    map.closeInfoWindow();
    var location = findLocation(code);
    if (location != null) location.show();
}

