/*
 * This function simply displays the church map.
 */
function createChurchMap()
{
  myMap = new Map("/maps/test.xml");
}


function Map(inUrl)
{
  /*
   * Load in the XML data from the URL provided. Then send it out to the right
   * places to be processed.
   */
  GDownloadUrl(inUrl, function(inData, inResponseCode)
  {
    var objXml = GXml.parse(inData);

    var objMap = objXml.documentElement;

    this._objMap = new GMap2(document.getElementById("map"));
    this._objMap.addControl(new GSmallMapControl());
    this._objMap.addControl(new GMapTypeControl());
    var tmpPoint = new GLatLng(objMap.getAttribute("lat"), objMap.getAttribute("lng"));
    var tmpZoom = objMap.getAttribute("zoom");
    this._objMap.setCenter(tmpPoint, Number(tmpZoom));

    this._arrPlaces = new Array();
    var arrPlaces = objMap.getElementsByTagName("place");
    for (var i = 0; i < arrPlaces.length; i++)
    {
      this._arrPlaces.push(new Place(this._objMap, arrPlaces[i]));
    }
  });
}

//==============================================================================


function Place(inMap, inData)
{
  /*
   * Set the map this place is for.
   */
  this._objMap = inMap;
  /*
   * Read out the latitude and longitude for the place.
   */
  this._objLocation = new GLatLng(
    parseFloat(inData.getAttribute("lat")),
    parseFloat(inData.getAttribute("lng"))
  );
  this._objPlace = new GMarker(this._objLocation);
  this._objMap.addOverlay(this._objPlace);
  /*
   * Read in and add the information for this place.
   */
  var tmpInformation = inData.getElementsByTagName("information")[0];
  this._objInformation = Information(this._objPlace, tmpInformation);
}


function Information(inPlace, inData)
{
  /*
   * Set the place this information is for.
   */
  this._objPlace = inPlace;
  /*
   * Set up our data storage.
   */
  this._arrInformation = new Array();
  /*
   * Search for tabs defined.
   */
  var tmpTabs = inData.getElementsByTagName("tab");
  /*
   * Find out how much information we have.
   */
  if (tmpTabs.length > 1)
  {
    for (var j = 0; j < tmpTabs.length; j ++)
    {
      this._arrInformation.push(new GInfoWindowTab(tmpTabs[j].getAttribute("name"), domToHtml(tmpTabs[j])));
    }
    this._objPlace.openInfoWindowTabsHtml(this._arrInformation);
  }
  else if (inData.length > 0)
  {
    this._arrInformation.push(inData[0].textContent);
    this._objPlace.openInfoWindowHtml(this._arrInformation[0]);
  }
}

function domToHtml(inDom) {
  /***/
  var outHtml = "";
  /***/
  var arrNodes = inDom.childNodes;
  /***/
  for (var i = 0; i < arrNodes.length; i ++)
  {
    /*
     * If this node is a text node...
     */
    if (arrNodes[i].nodeType == 3)
    {
      outHtml += arrNodes[i].textContent;
    }
    /*
     * If this node is a any other node...
     */
    else
    {
      switch (arrNodes[i].tagName)
      {
        case "a":
          outHtml += "<a href='" + arrNodes[i].getAttribute("href") + "'>" + arrNodes[i].childNodes[0].textContent + "</a>";
          break;

        case "br":
          outHtml += "<br>";
          break;

        case "img":
          outHtml += "<img src='" + arrNodes[i].getAttribute("src") +"'>";
          break;
      }
    }
  }
  /***/
  return outHtml;
}

/*
 * Add this script to load on start up.
 */
start_up.add(createChurchMap);