/* This script was created by Trevor Dowling for PhilWilUUs.org.
 * Usage without permission is expressly forbidden.
 */

var map; // this is the main Google Map object (initalized below)
var detailmap; // this is the mini detailed map for the InfoWindows
var churches = new Array(); // this is the data object array for the churches
var timer = null; // timer for mouseOver delay

/* The Church object data structure */
function Church(name, shortname, lat, lng, about, url, img, members, established, services, place, address, city, state, zip, m_address, m_city, m_state, m_zip, email, phone, fax, accessibility) {
  this.name = name;
  
  if (shortname)
    this.shortname = shortname;
  else
    this.shortname = name;
  
  this.lat = lat;
  this.lng = lng;
  this.about = about;
  this.url = url;
  
  this.members = members;
  this.established = established;
  this.services = services;
  
  if (img)
    this.img = img;
  else
    this.img = "http://www.uua.org/ads/square/images/banner.gif";

  this.place = place;
  this.address = address;
  this.city = city;
  this.state = state;
  this.zip = zip;
  
  this.m_address = m_address;
  if (m_city)  this.m_city = m_city;
  else         this.m_city = city;
  if (m_state) this.m_state = m_state;
  else         this.m_state = state;
  if (m_zip)   this.m_zip = m_zip;
  else         this.m_zip = zip;
  
  this.email = email;
  this.phone = phone;
  this.fax = fax;
  
  if (accessibility)
  	this.accessibility = accessibility.split(",");
  
  this.link = null;
  this.point = null;
  this.marker = null;
}

Church.prototype.createMarker = function() {
  if (!this.point)
    return null;

  this.marker = new GMarker(this.point, {title:this.name + " (click for more info)"});
  GEvent.bind(this.marker, "click", this, this.openInfoWindow);
  GEvent.bind(this.marker, "mouseover", this, this.highlightLink);
  GEvent.addListener(this.marker, "mouseout", clearHlLinks);
  
  return this.marker;
}

Church.prototype.highlightLink = function() {
  clearHlLinks();

  if (this.link.className.indexOf("sidebarLinks_sel") != -1)
    return;
    
  this.link.className = "sidebarLinks_hl";
}

Church.prototype.createLink = function(num) {
  this.link = document.createElement("TR");
  
  var cell = document.createElement("TD");
  this.link.appendChild(cell);
  
  if (this.url) {
    var a = document.createElement("A");
    cell.appendChild(a);
  
    a.innerHTML = this.shortname;
    a.setAttribute("href", this.url);
  } else
    cell.innerHTML = this.shortname;
    
  var cell = document.createElement("TD");
  this.link.appendChild(cell);
  
  if (this.state.indexOf("DC") != -1)
    cell.innerHTML = this.address;
  else
    cell.innerHTML = this.city;
  
  this.link.className = "sidebarLinks";
  eval("this.link.onmouseover = function() {openInfoWindowTimed("+num+");};");
  eval("this.link.onmouseout  = function() {clearInfoWindowTime("+num+");};");
  
  /* // The following does not work in IE for some reason, replaced with above
  this.link.setAttribute("onMouseOver", "openInfoWindowTimed("+num+")");
  this.link.setAttribute("onMouseOut", "clearInfoWindowTime("+num+")");
  */
  this.link.style['width'] = '100%';

  return this.link;
}

function openInfoWindowTimed(num) {
  if (timer)
    clearTimeout(timer);
  timer = setTimeout('churches['+num+'].openInfoWindow()', 500);
}

function clearInfoWindowTime(num) {
  clearTimeout(timer);
  timer = null;
}

Church.prototype.openInfoWindow = function() {
  if (!this.point)
    this.point = new GLatLng(39.1,-76.85);
  else {
    // if the infoWindow is already opened at this marker, don't re-open.
  
    var iw = map.getInfoWindow();
    if (iw && !iw.isHidden()) {
      pt = iw.getPoint();
      if (pt && pt.equals(this.point))
        return;
    }
  }

  var info = "<div id='information' class='infoWindow'>";
  info    += "\n<div class='infoWindow_name'>" + this.name + "</div>";
  info    += "\n<div class='infoWindow_about'>";
  if (this.about)
    info  +="&quot;" + this.about + "&quot;";
  info    += "</div>";
  info    += "\n<div class='infoWindow_right'>";
  info    += "\n<div class='infoWindow_image'><a href='" + this.url + "'><img src='" + this.img + "' /><br /><center style='font-size:8pt'>Visit our website</center></a></div>";
  if (this.accessibility) {
    info  += "\n<br /><div class='infoWindow_accessibility'>";
    for (var i = 0; i < this.accessibility.length; i++) {
      if (this.accessibility[i] == "blind")
        info += "<img src='images/accessibility/blind.png' alt='Access (other than large print or Braille)' title='Access (other than large print or Braille)' />";
      if (this.accessibility[i] == "wheelchair")
        info += "<img src='images/accessibility/wheelchair.png' alt='International Access Symbol' title='International Access Symbol' />";
      if (this.accessibility[i] == "audio")
        info += "<img src='images/accessibility/audio.png' alt='Live Audio Description' title='Live Audio Description' />";
      if (this.accessibility[i] == "tty_ttd")
        info += "<img src='images/accessibility/tty_ttd.png' alt='TTY/TTD' title='TTY/TTD' />";
      if (this.accessibility[i] == "phone")
        info += "<img src='images/accessibility/phone.png' alt='Volume Control Telephone' title='Volume Control Telephone' />";
      if (this.accessibility[i] == "listening")
        info += "<img src='images/accessibility/listening.png' alt='Assistive Listening Systems' title='Assistive Listening Systems' />";
      if (this.accessibility[i] == "sign")
        info += "<img src='images/accessibility/sign.png' alt='Sign Language Interpretation' title='Sign Language Interpretation' />";
      if (this.accessibility[i] == "print")
        info += "<img src='images/accessibility/SmallLargePrintSymbol.gif' alt='Accessible Print' title='Accessible Print' />";
      if (this.accessibility[i] == "info")
        info += "<img src='images/accessibility/info.png' alt='Information' title='Information' />";
      if (this.accessibility[i] == "cc")
        info += "<img src='images/accessibility/cc.png' alt='Closed Captioning' title='Closed Captioning' />";
      if (this.accessibility[i] == "oc")
        info += "<img src='images/accessibility/oc.png' alt='Open Captioning' title='Open Captioning' />";
      if (this.accessibility[i] == "braille")
        info += "<img src='images/accessibility/braille.png' alt='Braille' title='Braille' />";
				if (this.accessibility[i] == "welcoming")
        info += "<img src='images/accessibility/welcomingcongregation.gif' alt='Welcoming Congregation' title='GBLT Welcoming' />";
    }
    info  += "</div>";
  }
  info    += "\n</div>";
  if (this.established || this.members || this.services)
    info  += "\n<ul style='padding: 0px; padding-left: 14px; margin: 0px;'>"
  if (this.members)
    info  += "\n<li>" + this.members + " members.</li>"
  if (this.established)
    info  += "\n<li>Established in " + this.established + ".</li>";
  if (this.services)
    info  += "\n<li>" + this.services + "</li>";
  if (this.established || this.members || this.services)
    info  += "\n</ul>";
  info    += "\n<br /><u>Meeting Address</u>";
  if (this.place)
    info  += "\n<br />" + this.place;
  info    += "\n<br />" + this.address;
  info    += "\n<br />" + this.city + ", " + this.state + " " + this.zip + "<br />";
  if (this.m_address) {
    info  += "\n<br /><u>Mailing Address</u>";
    info  += "\n<br />" + this.m_address;
    info  += "\n<br />" + this.m_city + ", " + this.m_state + " " + this.m_zip + "<br />";
  }
  
  if (this.phone)
    info  += "\n<br />Phone: " + this.phone;
    
  if (this.fax)
    info  += "\n<br />Fax: " + this.fax;
  
  if (this.email) {
    var email = this.email.split("@");
    info  += "\n<br />E-Mail: <a href=" + '"' + "javascript:noSpam('" + email[0] + "','" + email[1] + "');" + '">' + email[0] + "<i>&#64;</i>" + email[1] + "</a>";
  }
  
  info    += "\n</div>";
  
  var dirs = "<div id='directions' class='infoWindow'>";
  
  var addr = this.address + ", " + this.city + ", " + this.state + " " + this.zip;
  
  dirs    += "\n<br /><br /><form method='get' action='http://maps.google.com/maps' target='_new'>";
  dirs    += "\nType your address here:<br />";
  dirs    += "\n<input type='text' id='saddr' name='saddr' style='width: 100%' /><br />";
  dirs    += "\n<center><input type='submit' target='_new' value='Get Directions' /><br /><br /><input type='image' target='_new' src='http://www.google.com/intl/en_us/mapfiles/poweredby.png' /></center>";
  dirs    += "\n<input type='hidden' name='daddr' value='" + addr + "' />";
  dirs    += "\n<input type='hidden' name='hl' value='en' />";
  dirs    += "\n</form>";
  
  dirs    += "\n<br /><center><a href='" + this.url + "'><i>For official directions to this church, visit their website.</i></a></center>";

  dirs    += "\n</div>";

  var infoTabs = [
    new GInfoWindowTab("Information", info),
    new GInfoWindowTab("Magnify", '<div id="detailmap" style="width: 350px; height: 225px"></div>'),
    new GInfoWindowTab("Directions", dirs)
  ];
  
  // remove all the style changes
  clearSelLinks();

  this.link.className = "sidebarLinks_sel";
  
  map.closeInfoWindow();
  
  //map.panTo(this.point);
  //this.marker.showMapBlowup();
  if (this.marker)
    this.marker.openInfoWindowTabsHtml(infoTabs, {maxWidth: 350});
  else
    map.openInfoWindowTabsHtml(this.point, infoTabs, {maxWidth: 350});
  GEvent.addListener(map.getInfoWindow(), "closeclick", clearSelLinks);
  var input = document.getElementById("saddr");
  if (input)
    input.focus();
  detailmap = new GMap2(document.getElementById("detailmap"));

  detailmap.setCenter(this.point, 15);
  detailmap.addControl(new GSmallZoomControl());
  detailmap.enableDoubleClickZoom();
  detailmap.enableContinuousZoom();
  detailmap.setMapType(G_HYBRID_MAP);
  //detailmap.addControl(new GMapTypeControl());

  detailmap.addOverlay(new GMarker(this.point));
}

function clearSelLinks(which) {
  for (var i = 0; i < churches.length; i++) {
    if (churches[i].link.className.indexOf("sidebarLinks_sel") != -1)
      churches[i].link.className = "sidebarLinks";
  }
}

function clearHlLinks(which) {
  for (var i = 0; i < churches.length; i++) {
    if (churches[i].link.className.indexOf("sidebarLinks_hl") != -1)
      churches[i].link.className = "sidebarLinks";
  }
}

/* This is the "Reset" control object */
function GResetControl() {}
GResetControl.prototype = new GControl(false, false);

GResetControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(100, 7));
}

GResetControl.prototype.initialize = function(map) {
  var button = document.createElement("div");
  this.setButtonStyle(button);
  button.appendChild(document.createTextNode("Reset Map"));
  GEvent.addDomListener(button, "click", resetMap);

  map.getContainer().appendChild(button);
  return button;
}

GResetControl.prototype.setButtonStyle = function(button) {
//  button.style.textDecoration = "underline";
  button.style.fontSize = "9pt";
  button.style.color = "black";
  button.style.backgroundColor = "white";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "1px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "6em";
  button.style.cursor = "pointer";
}

function resetMap() {
  map.closeInfoWindow();
  map.setCenter(new GLatLng(39.9,-75.125), 9);
  map.setMapType(G_NORMAL_MAP);
  clearSelLinks();
  clearHlLinks()
}

/* Load the map */
function GLoad() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.enableContinuousZoom();
    map.setUIToDefault();
    //map.addControl(new GLargeMapControl());
    //map.addControl(new GMapTypeControl());
    map.addControl(new GResetControl());
    resetMap();
    
    // Read the data from the XML file
    var request = GXmlHttp.create();
    request.open("GET", "mapdata.xml", true);
    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        var xmlDoc = request.responseXML;
        
        var sidebar_pa = document.getElementById("sidebar_pa");
        var sidebar_de = document.getElementById("sidebar_de");
        var sidebar_nj = document.getElementById("sidebar_nj");
        
        // obtain the array of churches and loop through it
        var church_data = xmlDoc.documentElement.getElementsByTagName("church");
        
        for (var i = 0; i < church_data.length; i++) {
          // create the church object array
          churches[i] = new Church(
            church_data[i].getAttribute("name"),
            church_data[i].getAttribute("shortname"),
            parseFloat(church_data[i].getAttribute("lat")),
            parseFloat(church_data[i].getAttribute("lng")),
            church_data[i].getAttribute("about"),
            church_data[i].getAttribute("url"),
            church_data[i].getAttribute("img"),
            church_data[i].getAttribute("members"),
            church_data[i].getAttribute("established"),
            church_data[i].getAttribute("services"),
            church_data[i].getAttribute("place"),
            church_data[i].getAttribute("address"),
            church_data[i].getAttribute("city"),
            church_data[i].getAttribute("state"),
            church_data[i].getAttribute("zip"),
            church_data[i].getAttribute("m_address"),
            church_data[i].getAttribute("m_city"),
            church_data[i].getAttribute("m_state"),
            church_data[i].getAttribute("m_zip"),
            church_data[i].getAttribute("email"),
            church_data[i].getAttribute("phone"),
            church_data[i].getAttribute("fax"),
            church_data[i].getAttribute("accessibility")
          );
          if (churches[i].lat && churches[i].lng) {
            churches[i].point = new GLatLng(churches[i].lat, churches[i].lng);
          
            // create the marker
            map.addOverlay(churches[i].createMarker());
          }
          
          // create the link on the sidebar
          if (churches[i].state.indexOf("PA") != -1)
            sidebar_pa.appendChild(churches[i].createLink(i));
          if (churches[i].state.indexOf("DE") != -1)
            sidebar_de.appendChild(churches[i].createLink(i));
          if (churches[i].state.indexOf("NJ") != -1)
            sidebar_nj.appendChild(churches[i].createLink(i));
        }
      }
    }
    request.send(null);

  }
}
