var xml = null;

function loadMap() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();
    map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 10)));
    map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10, 40)));
    map.setCenter(new GLatLng(53.45, -8.1), 7);

    // Create our "tiny" marker icon
    var baseIcon = new GIcon();
    baseIcon.image = "http://www.bitbuzz.com/images/map/marker_bitbuzz.png";
    baseIcon.shadow = "";
	baseIcon.iconSize = new GSize(32, 37);
    baseIcon.iconAnchor = new GPoint(16, 34);
    baseIcon.infoWindowAnchor = new GPoint(5, 1);

    initMarkers(map, baseIcon);

    GEvent.addListener(map, "click", function(marker, point) {
      if(marker) {
        var markerLocation = marker.getPoint();
        var hotspots = xml.documentElement.getElementsByTagName("hotspot");
        for(var i=0; i<hotspots.length; i++) {
          if(hotspots[i].getAttribute('lat') == markerLocation.lat() && hotspots[i].getAttribute('lng') == markerLocation.lng()) {
            var title, address, open, phone, link, info, type, banner;
            var childNodes = hotspots[i].childNodes;
            for(var j=0; j<childNodes.length; j++) {
              var childElement = childNodes[j];
              if(childElement.nodeName == '#text') {
                // ignore
              } else if(childElement.nodeName == 'title') {
                title = childElement.firstChild.nodeValue;
              } else if(childElement.nodeName == 'address') {
                var address = '';
                var firstElement = true;
                for(var k=0; k<childElement.childNodes.length; k++) {
                  if(childElement.childNodes[k].nodeType != childElement.TEXT_NODE) {
                    if(firstElement) {
                      firstElement = false;
                    } else {
                      address += '<BR/>';
                    }
                    address += childElement.childNodes[k].firstChild.nodeValue;
                  }
                }
              } else if(childElement.nodeName == 'open') {
                open = childElement.firstChild.nodeValue;
              } else if(childElement.nodeName == 'phone') {
                phone = childElement.firstChild.nodeValue;
              } else if(childElement.nodeName == 'link') {
                link = childElement.firstChild.nodeValue;
              } else if(childElement.nodeName == 'info') {
                info = childElement.firstChild.nodeValue;
              } else if(childElement.nodeName == 'type') {
                type = childElement.firstChild.nodeValue;
              } else if(childElement.nodeName == 'banner') {
                banner = childElement.firstChild.nodeValue;
              }
            }
            showInfoPanel(title, address, open, phone, link, info, type, banner);
          }
        }
      } else {
        hideInfoPanel();
      }
    });
  }
}

function showInfoPanel(title, address, open, phone, link, info, type, banner) {
  var infoPanelDiv = document.getElementById('infoPanel');

  var htmlText = '';
  htmlText += "<div class=\"infoBanner\"><img src=\"" + banner + "\" alt=\"Location banner\"></div>";
  htmlText += "<div class=\"infoTitle\">" + title + "</div>";
  htmlText += "<div class=\"infoAddress\">" + address + "</div>";
  htmlText += "<div class=\"infoHotspotType\">Type: " + type + "</div>";
  if(open) {
    htmlText += "<div class=\"infoOpen\">" + open + "</div>";
  }
  htmlText += "<div class=\"infoPhone\">" + phone + "</div>";
  if(open) {
    htmlText += "<div class=\"infoLink\"><a href=\"" + link + "\" target=\"_blank\">" + link + "</a></div>";
  }
  htmlText += "<div class=\"infoBlurb\">" + info + "</div>";

  infoPanelDiv.innerHTML = htmlText;
  infoPanelDiv.style.visibility = 'visible';
}

function hideInfoPanel() {
  document.getElementById('infoPanel').style.visibility = 'hidden';
}

function initMarkers(map, baseIcon) {
  var request = GXmlHttp.create();
  request.open('GET', '/datafeeds/locationsInfo.xml', false);
  request.send(null);
  xml = request.responseXML;
  var hotspots = xml.documentElement.getElementsByTagName("hotspot");
  var markerOptions = new Object();
  markerOptions.clickable = true;
  for(var i=0; i<hotspots.length; i++) {
    var point = new GLatLng(parseFloat(hotspots[i].getAttribute("lat")), parseFloat(hotspots[i].getAttribute("lng")));
    var childNodes = hotspots[i].childNodes;
    for(var j=0; j<childNodes.length; j++) {
      var childElement = childNodes[j];
      if(childElement.nodeName == 'title') {
        markerOptions.title = childElement.firstChild.nodeValue;
      }
      if(childElement.nodeName == 'type') {
        var type = parseFloat(childElement.getAttribute("typeInt"));
      }
    }
    icon = new GIcon(baseIcon);
    icon.image = "http://www.bitbuzz.com/images/map/marker_" + type + ".png";
    markerOptions.icon = icon;
    map.addOverlay(new GMarker(point, markerOptions));
  }
}

function parseMarkers(map) {
}
