//<![CDATA[

var markers = new Array;

if (GBrowserIsCompatible()) {

	var gmarkers = [];

	// A function to create the marker and set up the event window
	function createMarker(point,name,html,category,permalink) {

		var marker = new GMarker(point, gicons[category]);
		// === Store the category and name info as a marker properties ===
		marker.mycategory = category;                                 
		marker.myname = name;
		marker.draggable = false;

		if (html.length > 0) {
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(html);
				//document.location.href='http://www.google.com';
			});
		}
		else if (permalink.length > 0) {
			GEvent.addListener(marker, "click", function() {
				document.location.href=permalink;
			});
		}


		gmarkers.push(marker);
		return marker;
	}

	// create the map
	var map = new GMap2(document.getElementById("map"));

	//simpler pan/zoom controls
	map.addControl(new GSmallMapControl());

	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(startingLat,startingLong),startingZoom);

	// Read the data
	GDownloadUrl(mapDataLocation, function(doc) {
		var xmlDoc = GXml.parse(doc);
		markers = xmlDoc.documentElement.getElementsByTagName("marker");
			
		for (var i = 0; i < markers.length; i++) {
			// obtain the attribues of each marker
			var lat = parseFloat(markers[i].getAttribute("lat"));
			var lng = parseFloat(markers[i].getAttribute("long"));
			var point = new GLatLng(lat,lng);
			var info = markers[i].getAttribute("info");
			var name = markers[i].getAttribute("name");
			var permalink = markers[i].getAttribute("permalink");
			var category = markers[i].getAttribute("category");
			// create the marker
			var marker = createMarker(point,name,info,category,permalink);
			map.addOverlay(marker);
		}
		
		//conditionally show listing type menu is this variable has been preset
		if (showListingTypeButtons == true) {
			addListingMenu();
		}

	});
	
	/*
	* Wrapper function for all listing type button functionality 
	*/
	function addListingMenu() {


		// We define the function first
		function CustomListingControl() {
		}

		// To "subclass" the GControl, we set the prototype object to
		// an instance of the GControl object
		CustomListingControl.prototype = new GControl();

		// Creates a one DIV for each of the buttons and places them in a container
		// DIV which is returned as our control element. We add the control to
		// to the map container and return the element for the map class to
		// position properly.


		CustomListingControl.prototype.initialize = function(map) {
			var container = document.createElement("div");

			this.setContainerStyle_(container);

			//build an array of used listing types, so we dont make buttons that we dont need
			var usedListingTypes = new Array;
			for (var i = 0; i < markers.length; i++) {
				if (is_numeric(markers[i].getAttribute("category"))) {
					usedListingTypes[i] = markers[i].getAttribute("category");
				}
			}

			var AllDiv = document.createElement("div");
			this.setButtonStyle_(AllDiv);
			this.setButtonStyleClicked_(AllDiv);
			container.appendChild(AllDiv);
			AllDiv.appendChild(document.createTextNode("VIEW ALL"));
			GEvent.addDomListener(AllDiv, "click", function() {

				hideAll();

				show('5');
				show('6');
				show('7');
				show('8');
				show('9');
				show('10');

				CustomListingControl.prototype.setButtonStyleClicked_(AllDiv);
			});

			//hotels
			if (in_array(5, usedListingTypes)) {
				var hotelDiv = document.createElement("div");
				this.setButtonStyle_(hotelDiv);
				container.appendChild(hotelDiv);
				hotelDiv.appendChild(document.createTextNode("HOTELS"));
				GEvent.addDomListener(hotelDiv, "click", function() {

					hideAll();

					CustomListingControl.prototype.setButtonStyleClicked_(hotelDiv);
					show('5');
				});
			}

			//restaurants
			if (in_array(6, usedListingTypes)) {
				var restaurantDiv = document.createElement("div");
				this.setButtonStyle_(restaurantDiv);
				container.appendChild(restaurantDiv);
				restaurantDiv.appendChild(document.createTextNode("RESTAURANTS"));
				GEvent.addDomListener(restaurantDiv, "click", function() {

					hideAll();

					CustomListingControl.prototype.setButtonStyleClicked_(restaurantDiv);
					show('6');
				});
			}

			//bars
			if (in_array(8, usedListingTypes)) {
				var barsDiv = document.createElement("div");
				this.setButtonStyle_(barsDiv);
				container.appendChild(barsDiv);
				barsDiv.appendChild(document.createTextNode("BARS"));
				GEvent.addDomListener(barsDiv, "click", function() {

					hideAll();

					CustomListingControl.prototype.setButtonStyleClicked_(barsDiv);
					show('8');
				});
			}

			//sights
			if (in_array(9, usedListingTypes)) {
				var sightsDiv = document.createElement("div");
				this.setButtonStyle_(sightsDiv);
				container.appendChild(sightsDiv);
				sightsDiv.appendChild(document.createTextNode("SIGHTS"));
				GEvent.addDomListener(sightsDiv, "click", function() {

					hideAll();

					CustomListingControl.prototype.setButtonStyleClicked_(sightsDiv);
					show('9');
				});
			}
			

			if (in_array(10, usedListingTypes)) {
				var eventsDiv = document.createElement("div");
				this.setButtonStyle_(eventsDiv);
				container.appendChild(eventsDiv);
				eventsDiv.appendChild(document.createTextNode("EVENTS"));
				GEvent.addDomListener(eventsDiv, "click", function() {

					hideAll();

					CustomListingControl.prototype.setButtonStyleClicked_(eventsDiv);
					show('10');
				});	
			}

			if (in_array(7, usedListingTypes)) {
				var shoppingDiv = document.createElement("div");
				this.setButtonStyle_(shoppingDiv);
				container.appendChild(shoppingDiv);
				shoppingDiv.appendChild(document.createTextNode("SHOPPING"));
				GEvent.addDomListener(shoppingDiv, "click", function() {

					hideAll();

					CustomListingControl.prototype.setButtonStyleClicked_(shoppingDiv);
					show('7');
				});
			}

			function hideAll() {

				CustomListingControl.prototype.setButtonStyle_(hotelDiv);
				hide('5');

				CustomListingControl.prototype.setButtonStyle_(restaurantDiv);
				hide('6');

				CustomListingControl.prototype.setButtonStyle_(shoppingDiv);
				hide('7');

				CustomListingControl.prototype.setButtonStyle_(barsDiv);
				hide('8');

				CustomListingControl.prototype.setButtonStyle_(sightsDiv);
				hide('9');

				CustomListingControl.prototype.setButtonStyle_(eventsDiv);
				hide('10');

				CustomListingControl.prototype.setButtonStyle_(AllDiv);
			
			}

			map.getContainer().appendChild(container);
			return container;
		}

		// By default, the control will appear in the top left corner of the
		// map with 7 pixels of padding.
		CustomListingControl.prototype.getDefaultPosition = function() {
			return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(7, 20));
		}

		//wrapper for all divs
		CustomListingControl.prototype.setContainerStyle_ = function(button) {
		}

		// Sets the proper CSS for the given button element.
		CustomListingControl.prototype.setButtonStyle_ = function(button) {
			
			if (button) {
				button.style.backgroundImage="url(http://www.tripoutgaytravel.com/wp-content/themes/tripout/images/togt-gmaps.png)";
				button.style.backgroundPosition="0px -50px";
				button.style.margin="0";
				button.style.padding="3px 8px 2px 8px";
				button.style.border="1px solid #ababab";
				button.style.height="20px";
				button.style.display = "inline";
				button.style.textAlign = "center";
				button.style.font = "bold 10px Arial, sans-serif";
				button.style.color="#0b72bf";
				button.style.cursor="pointer";
			}
		}

		CustomListingControl.prototype.setButtonStyleClicked_ = function(button) {
			if (button) {
				button.style.backgroundPosition="0px -75px";
				button.style.color="#333";
				button.style.cursor="pointer";
			}
		}

		// == shows all markers of a particular category, and ensures the checkbox is checked ==
		function show(category) {
			for (var i=0; i<gmarkers.length; i++) {
				if (gmarkers[i] && gmarkers[i].mycategory == category) {
					gmarkers[i].show();
				}
			}
			// == check the checkbox ==
			//document.getElementById(category+"box").checked = true;
		}

		// == hides all markers of a particular category, and ensures the checkbox is cleared ==
		function hide(category) {
			for (var i=0; i<gmarkers.length; i++) {
				if (gmarkers[i] && gmarkers[i].mycategory == category) {
					gmarkers[i].hide();
				}
			}
			// == clear the checkbox ==
			//document.getElementById(category+"box").checked = false;
			// == close the info window, in case its open on a marker that we just hid
			map.closeInfoWindow();
		}

		function is_numeric( mixed_var ) {
			// Returns true if value is a number or a numeric string  
			// 
			// version: 904.317
			// discuss at: http://phpjs.org/functions/is_numeric
			// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
			// +   improved by: David
			// +   improved by: taith
			// +   bugfixed by: Tim de Koning

			if (mixed_var === '') {
				return false;
			}

			return !isNaN(mixed_var * 1);
		}

		function in_array(needle, haystack, argStrict) {
			// http://kevin.vanzonneveld.net
			// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

			var found = false, key, strict = !!argStrict;
		 
			for (key in haystack) {
				if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
					found = true;
					break;
				}
			}
		 
			return found;
		}

		//add custom stuff to this google map
		map.addControl(new CustomListingControl()); //custom listing control


	}
}

//]]>