  var map = {
   lat: [],
   lng: [],
   addr: [],
   tel: [],
   title: [],
   container: null,
   mapFlag: false,
   eMap: null,
   hideLink: null,
   showLink: null,
   init: function(){
     map.container = document.getElementById("map");
     if(!map.container) return;

     var hiddens = document.getElementsByTagName("input");
     var length = hiddens.length;
     for(var i=0; i<length; i++){
	     if(hiddens[i].type == "hidden"){
          if(hiddens[i].id.match("mapLat")) map.lat.push(hiddens[i].value);
          if(hiddens[i].id.match("mapLng")) map.lng.push(hiddens[i].value);
          if(hiddens[i].id.match("mapAddress")) map.addr.push(hiddens[i].value);
          if(hiddens[i].id.match("mapTitle")) map.title.push(hiddens[i].value);
          if(hiddens[i].id.match("mapTel")) map.tel.push(hiddens[i].value);
        }      
     }
     var a = document.links;
     var length = a.length;
     for(var i=0; i<length; i++){
       if(a[i].id.match("mapID-")){
         a[i].onclick = map.showInfo;
       }
     }
    
     map.showLink = document.getElementById("showLink");
     map.hideLink = document.getElementById("hideLink");
     
     if(map.showLink) map.showLink.onclick = function() { map.toggle("open"); }
     if(map.hideLink) map.hideLink.onclick = function() { map.toggle("close"); }
     
     // control tabbed MapView on City Page
     var mapView = document.getElementById("mapShow");
     if(mapView && !map.mapFlag) addEvent(mapView, "click", map.constructMap);


     // Control Map hide/show on Page Load 
     if(map.container.className.match("show")) map.constructMap();
     
   },
   constructMap: function(){
    if(map.mapFlag) return;
    if(map.lat.length == 1 && !(document.getElementById("worldResults")) && !(document.getElementById("locationList"))) {
      var eracMap = new MQA.TileMap( document.getElementById('map'), 12,new MQA.LatLng(map.lat[0], map.lng[0]));
      var point = new MQA.Poi(new MQA.LatLng(map.lat[0], map.lng[0]));  
       path = eIconImagePath;         
       eIcon = new MQA.Icon(path ,20,20);
       point.icon = eIcon;
       point.setInfoTitleHTML(map.title[0]);
       point.setInfoContentHTML(map.addr[0] + "<br>" + map.tel[0]);
       eracMap.addShape(point);
     }
     else{
      var points = new MQA.ShapeCollection();
      var eracMap = new MQA.TileMap(document.getElementById('map'));
      var length = map.lat.length;
      for(var i=0; i<length; i++){	
         var point = new MQA.Poi(new MQA.LatLng(map.lat[i], map.lng[i]));         
         if(map.title[i] == "") point.setInfoTitleHTML(map.addr[i]);
         else point.setInfoTitleHTML('<span class="arrow">' + map.title[i] + '</span>');
        
         point.key = i;
         var listItem = document.getElementById("mapID-" + point.key);
        var src = listItem.getElementsByTagName("img")[0].src;     
        if(document.getElementById("listView")){
          var anchorText = listItem.parentNode.parentNode.children[1].getElementsByTagName("a")[0].href;
          var branchText = "View Branch Details";
          var startText = "Create a Reservation";
          var startAnchor = listItem.parentNode.parentNode.children[1].getElementsByTagName("a")[0].href;          	
        }       
        else{   
         var anchorText = listItem.parentNode.parentNode.children[1].getElementsByTagName("a")[0].href;
         var branchText = listItem.parentNode.parentNode.children[1].getElementsByTagName("a")[0].innerHTML;
         var startText = listItem.parentNode.parentNode.children[2].getElementsByTagName("strong")[0].innerHTML;
         var startAnchor = listItem.parentNode.parentNode.children[2].getElementsByTagName("a")[0].href;
         }
         point.setInfoContentHTML(map.addr[i] + '<br>' + map.tel[i] + '<br><br><a href="' + anchorText + '">' + branchText + '</a><br><a href="' + startAnchor + '">' + startText + '</a>');
         eIcon = new MQA.Icon(src,23,31);
         point.icon = eIcon;	
         points.add(point);
      }
       eracMap.getDeclutter().setDeclutterMode(2);
       eracMap.addShapeCollection(points);
       points.setDeclutter(true);
       eracMap.setBestFitMargin(10);
       eracMap.bestFit();
      }    
      eracMap.addControl(new MQA.ZoomControl());  
      map.eMap = eracMap;        
      map.mapFlag = true;
      map.container.style.display = "block";

  },
  toggle: function(direction){
    if(direction == "open"){
      if(!map.mapFlag) map.constructMap();
      map.container.style.display = "block";
      map.showLink.style.display = "none";
      if(map.hideLink) map.hideLink.style.display = "inline";
      return false;
    }
    if(direction == "close"){
      if(!map.mapFlag) map.constructMap();
      map.container.style.display = "none";
      map.hideLink.style.display = "none";
      if(map.showLink) map.showLink.style.display = "inline";
      return false;
    }

  },
  showInfo: function(){
    var index = this.id.split("-")[1];
    var elem = map.eMap.getByKey(index);
      elem.showInfoWindow();
    return false;

  }
	
}
 
addEvent(window, "load", map.init);


