var oLibertyMap = new LibertyMap();
    function LibertyMap() {
        this.bounds = null;
        this.init = function (iPageGUID, sGUIDs, sZip) {
            this.bounds = new GLatLngBounds();
            /* If pageGUID is found, we're using a branch, otherwise, full map */
            if (sGUIDs) {
                arrGUIDs = sGUIDs.split(",");
                this.initMap("map");
                this.addZipMarker(sZip);
                this.addLocations(arrGUIDs);
            } else {
                iArrayIndex = -1;
                for (i=0;i<branches.length;i++) {
                    if (branches[i]["id"] == iPageGUID) {
                        iArrayIndex = i;
                    }
                }
                if (iArrayIndex == -1) {
                    this.initMap("map");
                    this.addLocations();
                } else {
                    this.initMap("branchMap");
                    this.addLocation(iArrayIndex, iPageGUID, true);
                }
            }
            this.zoomAndCenterMap();
        };
        
        this.initMap = function (sMapDIV) {
            this.map = new GMap2(document.getElementById(sMapDIV));
            this.map.addControl(new GLargeMapControl());
            this.map.addControl(new GMapTypeControl());
            this.map.addControl(new GScaleControl());
            this.map.enableDoubleClickZoom();
            this.centerMap();            
        }
        
        this.addZipMarker = function(strTargetZip) {
            var geocoder = new GClientGeocoder();
            geocoder.getLatLng(strTargetZip,
                function(point) {
                    if (point) {
                        oLibertyMap.map.setCenter(point, 10);
                        var marker = new GMarker(point);
                        oLibertyMap.map.addOverlay(marker);
                        oLibertyMap.bounds.extend(point);
                    }
                }
            );
        }
        
        this.centerMap = function(strTargetZip) {
            this.map.setCenter(new GLatLng(0,0),1);
        };
        
        this.zoomAndCenterMap = function () {
            var maxlat = -180; var minlat = 180; var maxlon = -180; var minlon = 180;
            var czoo = this.map.getBoundsZoomLevel(this.bounds);
            var clat = (this.bounds.getNorthEast().lat() + this.bounds.getSouthWest().lat()) /2;
            var clng = (this.bounds.getNorthEast().lng() + this.bounds.getSouthWest().lng()) /2;            
            
            this.map.setCenter(new GLatLng(clat,clng),czoo);
        }            
        
        this.addLocations = function(arrGUIDs) {
            if (arrGUIDs) {
                for (var i=0; i<arrGUIDs.length; i++) {
                    this.addLocation(null, arrGUIDs[i], false);
                }
            } else {
                for (i=0;i<branches.length;i++) {
                    if (branches[i]["point"]) {
                        this.addLocation(i, null, false);
                    }
                }
            }
        }
        
        this.addLocation = function(iArrayIndex, iBranchID, blnShowMarker) {
            if (iBranchID) {
                for (i=0;i<branches.length;i++) {
                    if (branches[i]["id"] == iBranchID){
                        iArrayIndex = i;
                    }
                }
            }
            if (branches[iArrayIndex]["point"]) {
                var point = new GLatLng(parseFloat(branches[iArrayIndex]["point"][0]), parseFloat(branches[iArrayIndex]["point"][1]));
                var name = branches[iArrayIndex]["name"];
                var importance = branches[iArrayIndex]["importance"];
                var html = "<strong>" + name + "</strong><br />";
                html += branches[iArrayIndex]["address"] + "<br />";
                if (branches[iArrayIndex]["address2"]) {
                    html += branches[iArrayIndex]["address2"] + "<br />";
                }
                html += branches[iArrayIndex]["city"] + ", " + branches[iArrayIndex]["state"] + " " + branches[iArrayIndex]["zip"] + "<br />";
                html += "Phone: " + branches[iArrayIndex]["phone"] + "<br />";
                
                sFullAddress = branches[iArrayIndex]["address"] + ", " + branches[iArrayIndex]["city"] + ", " + branches[iArrayIndex]["state"] + " " + branches[iArrayIndex]["zip"];
                html += "<div id=\"directions" + i + "\"><a href=\"javascript:void(0)\" onclick=\"fShowAddress('directions" + i + "', '" + sFullAddress + "')\">Get Directions</a><br /><br /></div>";
                if (!iBranchID) {
                    html += "<a href=\"" + branches[iArrayIndex]["link"] + "\">Hours &amp; More Info &raquo;</a>";
                }
                
                thisMarker = this.createMarker(point, name, html, importance);
                this.map.addOverlay(thisMarker);
                this.bounds.extend(point);
                if (blnShowMarker) {
                    thisMarker.openInfoWindowHtml(html);
                }
            }
        }
      this.createMarker = function(point, sTitle, sHtml, iImportance) {
          markerIcon = new GIcon();
          markerIcon.image = "/img/locations/icon_torch.png";
          markerIcon.iconSize = new GSize(18,40);
          markerIcon.shadow = "/img/locations/icon_torch_shadow.png";
          markerIcon.shadowSize = new GSize(50, 40);
          markerIcon.iconAnchor = new GPoint(5, 39);
          markerIcon.infoWindowAnchor = new GPoint(5,2);
          var options = { 
              title : sTitle,
              icon : markerIcon,
              zIndexProcess : importanceOrder
          };
          
          var marker = new GMarker(point, options);
          marker.importance = iImportance;
          GEvent.addListener(marker, "click", function() {
              marker.openInfoWindowHtml(sHtml);
          });
          return marker;
      }
    }
    
    var iCounter = 0;
    function importanceOrder (marker,b) {
    return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
    }    
    
    function fShowAddress(oDiv, strAddress) {
        sForm = "<form id=\"frmDirections" + iCounter++ + "\" class=\"frmDirections\" action=\"https://maps.google.com/maps\" method=\"get\" target=\"blank\" onsubmit=\"return fConfirmDirectionsSubmit()\">";
        sForm += "<input type=\"hidden\" name=\"f\" value=\"d\" />";
        sForm += "<input type=\"hidden\" name=\"daddr\" value=\"" + strAddress + "\" />";
        sForm += "<label for=\"saddr\">Start Address</label>";
        sForm += "<input type=\"text\" id=\"saddr\" name=\"saddr\" value=\"\" />";
        sForm += "<input type=\"submit\" id=\"btnSubmit\" value=\"Go\" />";
        document.getElementById(oDiv).innerHTML = sForm;
    }
 
    function fConfirmDirectionsSubmit() {
        return confirm("Notice: you will be leaving Liberty Bank's web site and entering a web site hosted by another party. Liberty Bank has not approved this as a reliable partner site. Please be advised that you will no longer be subject to, or under the protection of, the privacy and security policies of Liberty Bank's web site. We encourage you to read and evaluate the privacy and security policies of the site you are entering, which may be different than those of Liberty Bank's.");
    }