///////////////////////////////////////////////////////////////////////////////////////////////////
//	DLibMod Google Map JavaScript Code - copyright davidviner.com 2009-2010
//
//	21.03.2010	2.0.0	DJV		Mods for using different marker pins. Fixed incorrect map type
//								handling.
//
///////////////////////////////////////////////////////////////////////////////////////////////////

//<![CDATA[

var	doDirs = 0,
	map,
	mapId = null,
	zoom = 16,
	mainPoint,
	directions,
	town,
	tm = null,
	win,
	dirTxt,
	locs,
	mapType = G_NORMAL_MAP,
	lat,
	lng,
	popupStyle = 0,
	mX = 0,
	mY = 0,
	mapPos = -1,
	namepop,
	mouseZoom = 0,
	baseIcon;

function createMarker (point, name, txt, pin)
{
	var marker;

	if (pin != "")
	{
		var mkIcon = new GIcon (baseIcon);
		mkIcon.shadow = "/images/gmmarkers/shadow.png";
		mkIcon.image = "/images/gmmarkers/" + pin;
		marker = new GMarker (point, mkIcon);
	}
	else
	{
		marker = new GMarker (point);
	}

	if (name != "" || txt != "")
	{
		if (popupStyle == 0)
		{
			GEvent.addListener (marker, "click", function ()
			{
				marker.openInfoWindowHtml ("<div class='gmtext'><b>" + name + "<\/b><br\/><br\/>" + txt + "<\/div>");
			});
		}
		else
		{
			GEvent.addListener (marker, "mouseover", function ()
			{
				if (popupStyle == 1 || popupStyle == 3)
				{
					mapPos = DLibUtilities.findPos (mapId);
					namepop = new NamePopup (name);
					map.addControl (namepop);
				}

				if (popupStyle > 1)
				{
					document.getElementById ("mapinfo").innerHTML = "<b>" + name + "<\/b> " + txt;
				}
			});

			GEvent.addListener (marker, "mouseout", function ()
			{
				if (popupStyle == 1 || popupStyle == 3)
				{
					map.removeControl (namepop);
				}

				if (popupStyle > 1)
				{
					document.getElementById ("mapinfo").innerHTML = "";
				}
			});
		}
	}

	return marker;
}

function load ()
{
	if (GBrowserIsCompatible ())
	{
		mapId = document.getElementById ("map");
		mapPos = DLibUtilities.findPos (mapId);
		mapId.onmousemove = mapMove;
		map = new GMap2 (mapId);
		map.addControl (new GLargeMapControl ());
		map.addControl (new GMapTypeControl ());
		map.addControl (new GOverviewMapControl ());

		baseIcon = new GIcon ();
		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);

		if (mouseZoom) map.enableScrollWheelZoom ();

		var mt = G_NORMAL_MAP;

		switch (mapType)
		{
			case 0 : mt = G_NORMAL_MAP; break;
//			case 1 : mt = G_NORMAL_MAP; break;
    		case 1 : mt = G_SATELLITE_MAP; break;
    		case 2 : mt = G_HYBRID_MAP; break;
			case 3 : mt = G_PHYSICAL_MAP; break;
		}

		map.addMapType (G_PHYSICAL_MAP);
    	map.setMapType (mt);

		mainPoint = new GLatLng (lat, lng);
		map.setCenter (mainPoint, zoom);
		//map.addOverlay (createMarker (mainPoint, "$mname"));

		for (i = 0; i < locs.length; i += 5)
		{
			map.addOverlay (createMarker (new GLatLng (locs [i], locs [i + 1]), locs [i + 2], locs [i + 3], locs [i + 4]));
		}

/*		if (doDirs)
		{
			directions = new GDirections (map, document.getElementById ("dirpanel"));
			GEvent.addListener (directions, "error", handleErrors);
		}*/
	}
}

function setMainCenter ()
{
	map.setCenter (mainPoint, zoom);
}

function mapMove (eo)
{
	if (mapPos < 0)
	{
		return;
	}

	mX = null;
	mY = null;

	if (eo != null)
	{
		mX = eo.clientX;
		mY = eo.clientY;
	}
	else
	{
		mX = event.clientX;
		mY = event.clientY;
	}

	mX -= mapPos [0];
	mY -= mapPos [1];

	var so = DLibUtilities.scrollOffset ();

	mX += so [0];
	mY += so [1];
}

// Pop up names

function NamePopup (txt)
{
	this.txt = txt;
}

NamePopup.prototype = new GControl ();

NamePopup.prototype.initialize = function (map)
{
	var container = document.createElement ("div");

	this.setButtonStyle_ (container);

	var e = document.createElement("div");
	e.innerHTML = this.txt;
	container.appendChild (document.createTextNode (e.innerHTML));
	map.getContainer ().appendChild (container);
	return container;
}

NamePopup.prototype.getDefaultPosition = function ()
{
	return new GControlPosition (G_ANCHOR_TOP_LEFT, new GSize (mX + 10, mY - 10));
}

NamePopup.prototype.setButtonStyle_ = function (button)
{
	button.style.color = "black";
	button.style.backgroundColor = "#ffc";
	button.style.font = "small Arial";
	button.style.fontSize = "7pt";
	button.style.border = "1px solid black";
	button.style.padding = "1px 3px";
	button.style.marginBottom = "2px";
	button.style.textAlign = "center";
	button.style.cursor = "pointer";
}

/*function doDirections ()
{
	town = document.getElementById ("dirs").value;

	if (town != "")
	{
		var btn = '<input type="button" ' +
			'value="Show In New Window For Printing" onclick="printWin()" />';

		if (window.clipboardData)
		{
			btn += '<input type="button" ' +
				'value="Copy to Clipboard" onclick="copyClip()" />';
		}
alert ("T: " + town + " to $mlat,$mlng");

		directions.load (town + " to $mlat,$mlng");
		document.getElementById ("clipbutton").innerHTML = "Directions: " + btn;
	}
}

function handleErrors ()
{
	alert ("Could not find " + town + ". Please try adding the county after it.");
}

function copyClip ()
{
	dirTxt = document.getElementById ("dirpanel").innerHTML;
	dirTxt = dirTxt.replace (/(<([^>]+)>)/ig, "");
	dirTxt = dirTxt.replace (/&nbsp;/ig, " ");
	window.clipboardData.setData ("Text", dirTxt);
}

function printWin ()
{
	dirTxt = document.getElementById ("dirpanel").innerHTML;

	win = window.open ("/popup.html", "directions", "width=550,height=600,scrollbars=yes,toolbar=no," +
		"location=no,resizable=yes");
	tm = setTimeout ("filldirs()", 2000);
}

function filldirs ()
{
	clearTimeout (tm);
	win.document.getElementById ("directions").innerHTML = dirTxt;
	win.document.getElementById ("from").innerHTML = town;
}
*/

//]]>

