var points = {};
//hashtable to hold each google map point
var markerArray = {};
var markerInfoArray = {};
var map;
var currentMarker;

var marker;

function goLocal(q, near) {
    if (q != "") {
        window.open("http://local.google.com/local?q=" + q + "&near=" + near);
    }
}


function openWindow(tofrom){
	   var html = '<div style="width: 220px;">' +
	   	   '<span class="bodytext" align="left">Directions' +
		   '<form class="directions" method="GET" action="http://maps.google.com/maps" target="_blank">' +
		   '<br>' +
		   '';
	   if(tofrom == 'to')
		   html += 'To Here - ';
	   else html += '<a href="javascript:void(0)" onClick="openWindow(\'to\'); return false;">To Here</a> - ';
	   
	   if (tofrom == 'from')
		   html += 'From Here<br>';
	   else html += '<a href="javascript:void(0)" onClick="openWindow(\'from\'); return false;">From Here</a><br>';

	   if(tofrom == 'to')
		{
		   html += 'Start Address:<br>' +
		   '<input name="daddr" value="151 Kent Ave., Brooklyn, NY" type="hidden">' +
		   '<input name="saddr" value="" style="width: 16em">' +
		   '<input name="hl" value="en" type="hidden">' +
		   '<br><input type=\"submit\" value=\"Get Directions\">';
		}
	   else if(tofrom == 'from')
	   {
		   html += 'End Address:<br>' +
		   '<input name="saddr" value="151 Kent Ave., Brooklyn, NY" type="hidden">' +
		   '<input name="daddr" value="" style="width: 16em">' +
		   '<input name="hl" value="en" type="hidden">' +
		   '<br><input type=\"submit\" value=\"Get Directions\">';
	   }
	   html += '</form></span></div>';
	   
	   if(tofrom != 'neither')
	   		currentMarker.openInfoWindowHtml(html);
			
	   return html;
}


function createMarker(marker) {
    var point = new GPoint(parseFloat(marker.getAttribute("lng")),
            parseFloat(marker.getAttribute("lat")));
    var type = marker.getAttribute("type");
    var name = marker.getAttribute("name");
    var adr = marker.getAttribute("adr");
    var description = marker.getAttribute("description");
    if (description == null) description = "";

    var infoHtml;

    infoHtml = "<div style=\"width: 220px;\">";
    //infoHtml = "<p id=\"zoom" + markerID + "\" align=\"right\" style=\"margin:-5px 0 0 0;padding-right:15px;z-index:1000;\">[<a href=\"javascript:zoomIn(" + markerID + ",1);\">zoom in</a>]</p>";
    infoHtml += "<strong>" + name + "</strong>" + "<br />" + description  + "<br />" + adr;
    //infoHtml += "<br />" + tmp;  //Lines/Schdule

    var icon = new GIcon();

    if (type == "station") {
        var line = marker.getAttribute("line");

        if (line == "A")
            icon.image = "img/a_16.gif";
        else if (line == "1")
            icon.image = "img/1_16.gif";
		else if (line == "L")
            icon.image = "img/l_16.gif";
		else if (line == "G")
            icon.image = "img/g_16.gif";
		else if (line == "J")
            icon.image = "img/j_16.gif";
		else if (line == "M")
            icon.image = "img/m_16.gif";
		else if (line == "Z")
            icon.image = "img/z_16.gif";

        icon.iconSize = new GSize(16, 16);
        icon.shadowSize = new GSize(0, 0);
        icon.iconAnchor = new GPoint(9, 34);
        icon.infoWindowAnchor = new GPoint(9, 2);
        icon.infoShadowAnchor = new GPoint(18, 25);
    }
    else if (type == "reference") {
        icon.image = "img/coldmarker.png";
        icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        icon.iconSize = new GSize(20, 34);
        icon.shadowSize = new GSize(37, 34);
        icon.iconAnchor = new GPoint(9, 34);
        icon.infoWindowAnchor = new GPoint(9, 2);
        icon.infoShadowAnchor = new GPoint(18, 25);
    }
    else if (type == "home") {
        icon.image = "img/marker_34_green.png";
        icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        icon.iconSize = new GSize(20, 34);
        icon.shadowSize = new GSize(37, 34);
        icon.iconAnchor = new GPoint(9, 34);
        icon.infoWindowAnchor = new GPoint(9, 2);
        icon.infoShadowAnchor = new GPoint(18, 25);
		
		infoHtml += openWindow("neither", 0);
        //infoHtml += "<p><form name=\"markerDropDown\">";
        //infoHtml += "<select name=\"local\" onchange=\"goLocal(this.value,'" + adr + ", New York, NY" + "');\"><option value=\"\">Find near this point:</option><option value=\"Museum of Modern Art\">MoMA</option><option value=\"Yankee Stadium\">Yankee Stadium</option><option value=\"Coffee\">Coffee Shop</option><option value=\"Free WiFi\">Free WiFi</option><option value=\"Cloisters Museum\">Cloisters Museum</option><option value=\"Pizza\">Pizza</option><option value=\"Thrift\">Thrift Stores</option></select>";
        //infoHtml += "</form></p>";
    }
    else if (type == "property") {
        icon.image = null;
    }

	infoHtml += "</div>";

    var newMarker = carveMarker(point, infoHtml, icon);
	//newMarker.openInfoWindowHtml(infoHtml);
	if (type == "home")
		currentMarker = newMarker;
		
    return newMarker;
}

function goLocal(q, near) {
    if (q != "") {
        window.open("http://local.google.com/local?q=" + q + "&near=" + near);
    }
}
function zoomIn(i, level) {
    map.centerAndZoom(points[i], level);
    document.getElementById("zoom" + i).innerHTML = "[<a href=\"javascript:zoomOut(" + i + ",3);\">zoom out</a>]";
}
function zoomOut(i, level) {
    map.centerAndZoom(points[i], level);
    document.getElementById("zoom" + i).innerHTML = "[<a href=\"javascript:zoomIn(" + i + ",1);\">zoom in</a>]";
}
function carveMarker(point, text, icon) {
    // Create a marker whose info window displays the given number.
    if (icon.image == null)
        var marker = new GMarker(point);
    else
        var marker = new GMarker(point, icon);

    // Show this marker's index in the info window when it is clicked.
    var html = text;
    GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(html);
    });

    return marker;
}

function parseXMLfile() {
    var dataFileDoc = this.req.responseXML;
    var markerElements = dataFileDoc.documentElement.getElementsByTagName("group");

    for (var i = 0; i < markerElements.length; i++) {
        //heading1 = markerElements[i].getAttribute("title");

        var markers = markerElements[i].getElementsByTagName("marker");
        for (var j = 0; j < markers.length; j++) {
            var newMarker = createMarker(markers[j]);
            map.addOverlay(newMarker);
        }
    }
}

function onLoad(zoomFactor, long, lat) {
    // Using XML and Asynchronous RPC ("AJAX") with Maps

    map = new GMap(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.centerAndZoom(new GPoint(long, lat), zoomFactor);

    var request = GXmlHttp.create();
    request.open("GET", "files.xml", true);
    request.onreadystatechange = function() {
        if (request.readyState == 4) {
            var filesXmlDoc = request.responseXML;
            var files = filesXmlDoc.documentElement.getElementsByTagName("file");
            //var xmlRequest;
            //hash table for city name/tree node pairs
            for (var i = 0; i < files.length; i++) {     //iterate files in files.xml
                var fileName = files[i].getAttribute("name");
                var loader = new net.ContentLoader(fileName, parseXMLfile);
            }
        }
    }
    request.send(null);
}