
	var	list		= null,
		panel		= null,
		sidebar		= null,
		map		= null,
		showAll		= null,
		address		= null,
		radius		= null,
		geocoder	= null,
		listTitles	= null,
		listData	= null;


	function initMap ()
	{
		if (!GBrowserIsCompatible ())
		{
			alert ("Your browser does not appear to be compatible with this utility.");
			return;
		}


		showAll			= 			 document.getElementById ("lf_showall");
		address			= 			 document.getElementById ("lf_postcode");
		radius			= 			 document.getElementById ("lf_range");
		list			= 			 document.getElementById ("lf_listcanvas");
		sidebar			= 			 document.getElementById ("lf_details");
		panel			= 			 document.getElementById ("lf_detailscanvas");
		map			= new GMap2		(document.getElementById ("lf_googlecanvas"));
		geocoder		= new GClientGeocoder	();

		geocoder.setBaseCountryCode	("au");

		map.setCenter			(new GLatLng (-35.281693, 149.132155), 13);
		map.setMapType			(G_NORMAL_MAP);
		map.setUIToDefault		();

		//GEvent.addListener		(map, "click",		function () { this.refresh (); });
		//GEvent.addListener		(map, "onblur",		function () { this.refresh (); });


		// show all locations by default
		searchAllLocations		();
	}


	function searchAllLocations ()
	{
		searchLocationsNear (new GLatLng (0, 0), 0);
	}


	function searchLocations ()
	{
		if (showAll.checked)	searchAllLocations ();
		else geocoder.getLatLng (address.value, function (latlng)
		{
			if (latlng) searchLocationsNear	(latlng, radius.value);
			else
			{
				map.clearOverlays ();

				generateList_EntriesNone ();
			}
		});

		return false;
	}


	function searchLocationsNear (center, radius)
	{
		var searchUrl	= '_locations.php?lat='
				+ center.lat ()
				+ '&lng='
				+ center.lng ()
				+ '&rng=' + radius;

		GDownloadUrl (searchUrl, function (data)
		{
			var markers		=	GXml.parse	(data).documentElement.getElementsByTagName ('marker');
			var bounds		= new	GLatLngBounds	();


			while (panel.hasChildNodes ()) panel.removeChild	(panel.firstChild);

			sidebar.style.display	= "none";

			map.clearOverlays	();

			if (!markers.length)
			{
				generateList_EntriesNone ();
				return;
			}

			killList_Entries ();

			for (var i = 0; i < markers.length; i++)
			{
				var distance	= parseFloat	(markers[i].getAttribute ("distance"));
				var lat		= parseFloat	(markers[i].getAttribute ("lat"));
				var lng		= parseFloat	(markers[i].getAttribute ("lng"));

				var s		= parseInt	(markers[i].getAttribute ("side"));
				var t		= parseInt	(markers[i].getAttribute ("type"));

				var area	=		 markers[i].getAttribute ("area");
				var category	=		 markers[i].getAttribute ("category");
				var name	=		 markers[i].getAttribute ("name");
				var address	=		 markers[i].getAttribute ("address");
				var products	=		 markers[i].getAttribute ("products");
				var phone	=		 markers[i].getAttribute ("phone");
				var fax		=		 markers[i].getAttribute ("fax");
				var img		=		 markers[i].getAttribute ("img");

				var times	= new Object ();

				for (var j = 0; j < markers[i].childNodes.length; j++)
				{
					if (markers[i].childNodes[j].nodeType != 1) continue;	// ELEMENT_NODE

					var n		= markers[i].childNodes[j].getAttribute ("name");

					times[n]	= new Object ();
					times[n]["o"]	= markers[i].childNodes[j].getAttribute ("open");
					times[n]["c"]	= markers[i].childNodes[j].getAttribute ("close");
				}

				var point	= new GLatLng (lat, lng);
				var marker	= createMarker (point, name, address, phone, fax, img, products, times, t);

				createEntry_List	(marker, s, t, area, category, name, address);
				map.addOverlay		(marker);

				bounds.extend		(point);
			}

			generateList_Entries ();

			map.checkResize		();
			map.setCenter		(bounds.getCenter (), map.getBoundsZoomLevel (bounds));
			refreshBodyStyle	();
		});
	}


	function createMarker (point, name, address, phone, fax, img, products, times, subagent)
	{
		var marker	= new GMarker (point);

		var html	= '<p>'
					+ '<strong>' + name + '</strong><br />'
					+ address + '<br />'
				+ '</p>'
//+ '<!-- ' + subagent + '-->'
				+ '<p><img src="' + (img ? img : '/_lib/img/lf_nopic.jpg') + '" alt="' + name + '" /></p>';



//				+ '<p>'
//				+ '<strong>Ph:</strong> ' + phone + '<br />'
//					+ '<strong>Fax:</strong> ' + fax + '<br />'
//				+ '</p>'

		html = html		+ '<p>'
					+ '<strong>Products:</strong><br />'
					+ products + '<br />'
				+ '</p>';

		if (subagent == '2')
		{
			html = html	+ '<p>Opening and closing times are set by the individual sub-agencies</p>';
		}
		else
		{
			html = html	+ '<dl>'
					+ '<dt class="title">Opening hours</dt>'

					+ '<dt>Mon:</dt>		<dd>'		+ times['Mon']['o'] + ' &ndash; ' + times['Mon']['c'] + '</dd>'
					+ '<dt>Tue:</dt>		<dd>'		+ times['Tue']['o'] + ' &ndash; ' + times['Tue']['c'] + '</dd>'
					+ '<dt>Wed:</dt>		<dd>'		+ times['Wed']['o'] + ' &ndash; ' + times['Wed']['c'] + '</dd>'
					+ '<dt>Thu:</dt>		<dd>'		+ times['Thu']['o'] + ' &ndash; ' + times['Thu']['c'] + '</dd>'
					+ '<dt>Fri:</dt>		<dd>'		+ times['Fri']['o'] + ' &ndash; ' + times['Wed']['c'] + '</dd>'
					+ '<dt>Sat:</dt>		<dd>'		+ times['Sat']['o'] + ' &ndash; ' + times['Sat']['c'] + '</dd>'
					+ '<dt>Sun:</dt>		<dd>'		+ times['Sun']['o'] + ' &ndash; ' + times['Sun']['c'] + '</dd>'
					+ '<dt>Public Holidays:</dt>	<dd><br />'	+ times['Hld']['o'] + ' &ndash; ' + times['Hld']['c'] + '</dd>'
					// + '<dt>Melbourne Cup Day:</dt>	<dd><br />'	+ times['Mlb']['o'] + ' &ndash; ' + times['Mlb']['c'] + '</dd>'
					+ '</dl>';
		}

		//html = html	+ '<p>Opening and closing times are set by each venue.</p>';

		GEvent.addListener (marker, "click", function ()
		{
			sidebar.style.display	= "";
			panel.innerHTML		= html;

			map.checkResize		();
			map.setCenter		(point, 17);
		});

		return marker;
	}


	function createEntry_List (marker, s, t, area, category, name, address)
	{
		if (!listData[s])
		{
			listTitles[s]		= [];
			listData[s]		= [];
		}

		if (!listData[s][t])
		{
			listTitles[s][t]	= area + " " + category;
			listData[s][t]		= [];
		}

		var tr				= document.createElement ('tr');
		var td1				= document.createElement ('td');
		var td2				= document.createElement ('td');

		td1.innerHTML			= name;
		td2.innerHTML			= address;

		tr.appendChild		(td1);
		tr.appendChild		(td2);

		tr.style.cursor			= "pointer";
		tr.style.marginBottom		= "5px";

		GEvent.addDomListener	(tr, "click",		function () { window.location.hash='top';GEvent.trigger (marker, "click"); });

		listData[s][t].push (tr);
	}


	function killList_Entries ()
	{
		listTitles		= [];
		listData		= [];

		while (list.hasChildNodes ())
			list.removeChild (list.firstChild);
	}

	function generateList_Entries ()
	{
		var table	= document.createElement ("table");

		for (var x = 0; x < listData.length; x++)
		{
			if (!listData[x])	continue;

			for (var y = 0; y < listData[x].length; y++)
			{
				if (!listData[x][y])	continue;

				var tb		= document.createElement ("tbody");
				var tr		= document.createElement ("tr");
				var th		= document.createElement ("th");
				var cs		= document.createAttribute ("colspan");

				th.innerText	=
				th.textContent	= listTitles[x][y];
				cs.nodeValue	= 3;

				th.setAttributeNode	(cs);
				tr.appendChild		(th);
				tb.appendChild		(tr);

				for (var z = 0; z < listData[x][y].length; z++)
					tb.appendChild (listData[x][y][z]);

				table.appendChild	(tb);
			}
		}

		table.className		= "locationlist";
		list.appendChild	(table);
	}

	function generateList_EntriesNone ()
	{
		killList_Entries ();

		var message	= [
					"We probably couldn't find the address you entered. Try checking the spelling and entering it again.",
					"You could also try increasing the search Radius, or clicking \"Show all Locations\" to view all ACTTAB locations."
				  ];

		var table	= document.createElement ("table");
		var tb		= document.createElement ("tbody");
		var tr		= document.createElement ("tr");
		var th		= document.createElement ("th");

		th.innerText	=
		th.textContent	= "No results were found.";

		tr.appendChild		(th);
		tb.appendChild		(tr);

		for (var z = 0; z < message.length; z++)
		{
			var tx		= document.createElement ("tr");
			var td		= document.createElement ("td");

			td.innerText	=
			td.textContent	= message[z];

			tx.appendChild	(td);
			tb.appendChild	(tx);
		}

		table.appendChild	(tb);
		table.className		= "locationlist";
		list.appendChild	(table);
	}

	DomLoaded.load (initMap);
	document.onunload	= GUnload;
