var windowsInternetExplorer = false;    /* the browser that is being used */
                                        /* internet explorer              */
var firefox = false;                    /* the browser that is being used */
                                        /* is firefox                     */
var browserVersion = 0;                 /* the version of the browser     */

var showingPic = true;
var picData;
var theNames = null;
var theMarkers = null;
var map = null;

function detectBrowser() {
        windowsInternetExplorer = false;
        var appVersion = navigator.appVersion;
        var temp;

        if ((appVersion.indexOf("MSIE") != -1) && (appVersion.indexOf("Macintosh") == -1)) {
                temp = appVersion.split("MSIE");

                browserVersion = parseFloat(temp[1]);
                windowsInternetExplorer = true;
        }
        else if (navigator.userAgent.indexOf("Firefox") != 1) {
                firefox = true;
        }
}

function fixBrowserSpecificProblems() {
	var	div;

	detectBrowser();
	if (windowsInternetExplorer || firefox) {
		div = document.getElementById('monumentsShadow');
		div.style.display = 'block';
	}

	if (windowsInternetExplorer) {
		div = document.getElementById('righttext');
		div.className = "lefttext";
		div.style.marginLeft = "60px";
	}
}

function setupMap() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(38.8973999479191, -77.02291488647461), 16);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
	}
	else {
		alert("DOH!");
	}
}

function toggleMap() {
	var	pic;
	var	button;

	pic = document.getElementById("mainpic");
	button = document.getElementById("toggleMapButton");
	if (showingPic) {
		picData = pic.innerHTML;
		map = new GMap2(pic);
		map.setCenter(new GLatLng(38.8973999479191, -77.02291488647461), 16);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		//makeMarkers();
		showSmithsonianMuseumOfAmericanArt();
		showingPic = false;
		button.value = "Show Picture";
	}
	else {
		pic.innerHTML = picData;
		showingPic = true;
		button.value = "Show Map";
	}
}

function createMarker(point, name) {
	var marker = new GMarker(point);

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(name);
	});
	return(marker);
}

function onEnter(i) {
	theMarkers[i].openInfoWindowHtml(theNames[i]);
}

function onLeave(m) {
	map.closeInfoWindow();
}

function makeMarkers() {
	map.clearOverlays();

	if (theMarkers != null) {
		for (i = 0; i < theMarkers.length; i++) {
			delete theMarkers[i];
			delete theNames[i];
		}

		delete theMarkers;
		delete theNames;

		theMarkers = null;
		theName = null;
	}

	theMarkers = new Array();
	theNames = new Array();

	GDownloadUrl("http://www.tourofdc.org/ajax/services/gp.php?category=3", function(data, responseCode) {
		var	xml = GXml.parse(data);
		var	markers = xml.documentElement.getElementsByTagName("marker");

		var	tt = null;

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

			var 	theName    = markers[i].getElementsByTagName("name")[0].firstChild.nodeValue;
			//var 	theAddress = markers[i].getElementsByTagName("address")[0].firstChild.nodeValue;
			//var 	theCity    = markers[i].getElementsByTagName("city")[0].firstChild.nodeValue;
			//var 	theState   = markers[i].getElementsByTagName("state")[0].firstChild.nodeValue;
			//var 	theZipcode = markers[i].getElementsByTagName("zipcode")[0].firstChild.nodeValue;

			//if (markers[i].getElementsByTagName("phonenumber") != null) {
			//	thePhoneNumber = markers[i].getElementsByTagName("phonenumber")[0].firstChild.nodeValue;
			//}
			//else {
			//	thePhoneNumber = "&nbsp;";
			//}


			theSN = markers[i].getElementsByTagName("serialnumber")[0].firstChild.nodeValue;
			//theData = theName + "<br>" + theAddress;
			theData = theName + "<br>";
			theURL = "<br><a target=\"new\" href=\"http://www.tourofdc.org/restaurants/detail.php?sn=" + theSN + "\">More Info</a>\n";
			theData = theName + "<br>" + theURL;
			theMarkers[i] = createMarker(point, theData);
			theNames[i] = theData;
			map.addOverlay(theMarkers[i]);
		}

	});
}

function showSmithsonianMuseumOfAmericanArt() {
	var	info;

	info = "";
	info = info + "<table style=\"width: 300px;\">\n";
	info = info + "<tr>\n";
	info = info + "<td>\n";
	info = info + "<img src=\"images/NationalMuseumOfAmericanArt-100.jpg\" width=\"156\" height=\"103\">\n";
	info = info + "</td>\n";
	info = info + "<td>\n";
	info = info + "<div class=\"mapheading\">Smithsonian Museum of American Art</div>\n";
	info = info + "<div class=\"mapbody\">8th &amp; F Streets NW</div>\n";
	info = info + "<div class=\"mapbody\">202.633.1000</div>\n";
	info = info + "<div class=\"mapbody\" style=\"margin-top: 3px;\"><b>Metro Stop</b>: Gallery Place/Chinatown</div>\n";
	info = info + "<td>\n";
	info = info + "</tr>\n";
	info = info + "</table>\n";
	point = new GLatLng(38.8973999479191, -77.02291488647461);
	marker = createMarker(point, info);
	map.addOverlay(marker);
}

