//<![CDATA[

// Check to see if this browser can run the Google API
if (GBrowserIsCompatible()) {

	// A function to create a tabbed marker and set up the event window
	// This version accepts a variable number of tabs, passed in the arrays htmls[] and labels[]
	
	//Custom Icon
	var_icon = new GIcon();
	var_icon.image = "http://livadas.com/images/icons/marker.png";
	var_icon.iconAnchor = new GPoint(9, 36);
	var_icon.infoWindowAnchor = new GPoint(9, 36);

	//MAKE WINDOW
	// A function to create the marker and set up the event window	
	function createTabbedMarker(point,htmls,labels) {
		var marker = new GMarker(point, var_icon);
		

		GEvent.addListener(marker, "click", function() {
			// adjust the width so that the info window is large enough for this many tabs
			if (htmls.length > 2) {
			htmls[0] = '<div style="width:'+htmls.length*88+'px">' + htmls[0] + '</div>';
			}
			var tabs = [];
			for (var i=0; i<htmls.length; i++) {
				tabs.push(new GInfoWindowTab(labels[i],htmls[i]));
			}
			marker.openInfoWindowTabsHtml(tabs);
		});
		return marker;
	}

      // functions that open the directions forms
      function tohere(i) {
        gmarkers[i].openInfoWindowHtml(to_htmls[i]);
      }
      function fromhere(i) {
        gmarkers[i].openInfoWindowHtml(from_htmls[i]);
      }


   // Display the map, with some controls and set the initial location 
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(43.15874455351731,-77.60765790939331), 3);

		// OVERVIEW MINIMAP
		var omap = new GOverviewMapControl(new GSize(400,76));
		map.addControl(omap);
		var overview = document.getElementById("map_overview");
		document.getElementById("map").appendChild(overview); 
		
		// == restyling ==
		overview.firstChild.style.border = "1px solid gray";
		overview.firstChild.firstChild.style.left="0px";
		overview.firstChild.firstChild.style.top="4px";
		overview.firstChild.firstChild.style.width="400px";
		overview.firstChild.firstChild.style.height="76px";

      // ==================================================
      // Set up the tabbed markers


	// Read the data from locations.xml
	var request = GXmlHttp.create();
	request.open("GET", "http://livadas.com/about/location/locations.xml", true);


	//GET XML
	function decode(a) {
		var b = "";
		if (a.length > 0) {
			if (a[0]) {
				if (a[0].firstChild) {
				b = a[0].firstChild.nodeValue;
				}
			}
		}
		return b;
	}
  
		request.onreadystatechange = function() {

		if (request.readyState == 4) {
		var xmlDoc = request.responseXML;
		// obtain the array of markers and loop through it
		var 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("lng"));
			var point = new GLatLng(lat,lng);
			var tab = markers[i].getElementsByTagName("tab");
			var htmls = [];			
			var labels = [];
			for (var n = 0; n < tab.length; n++) {
				var infowindow = decode(tab[n].getElementsByTagName("infowindow"));
				var label = tab[n].getAttribute("label");
				htmls.push(infowindow);
				labels.push(label);
			}

			// create the marker

			var marker = createTabbedMarker(point,htmls,labels);
			map.addOverlay(marker);
		}

		}
	}

	request.send(null);

      // ======== get a reference to the GMap2 ===========
      var ovmap = omap.getOverviewMap();
      //  ======== change the overview map type AFTER the overview finisges initializing =====
      setTimeout("ovmap.setMapType(G_SATELLITE_MAP);",1);
}
    
    // display a warning if the browser was not compatible
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser");
    }

    // This Javascript is based on code provided by the
    // Blackpool Community Church Javascript Team
    // http://www.commchurch.freeserve.co.uk/   
    // http://www.econym.demon.co.uk/googlemaps/

    //]]>
