function location_point(pAddress, pIndex) {
	this.attempts = 0;
	this.index = pIndex;
	this.address = pAddress;
	this.point = "";
	this.initPoint = function() {
		var callback = this.setPointWrapper.apply(this);
		this.attempts++;
        geocoder.getLatLng(this.address,
			callback
        );
	};
	this.setPointWrapper = function() {
		var loc = this;
		return function() {
			return loc.setPoint.apply(loc, arguments);
		}
	},
	this.setPoint = function(pPoint) {
	    if (pPoint) {
	    	try {
		    	this.point = pPoint;
		        marker = new GMarker(this.point, location_map.blueMarker());
		        if (document.getElementById('branch_info_' + this.index))
		        	marker.bindInfoWindowHtml(document.getElementById('branch_info_' + this.index).innerHTML);
		        location_map.map.addOverlay(marker);
	        }
	        catch(err) {
	        	if (this.attempts < 10) {
	        		this.initPoint();
	        	}
	        }
	    }
	    else {
	    	if (this.attempts < 10) {
        		this.initPoint();
        	}
		}
	};
}

var location_map = {
    map: "",
    locations: [],
    points: [],
    getIndex: function(address) {
		for (var i = 0; i < location_map.locations.length; i++) {
			if (address == location_map.locations[i])
				return i;
		}
	},
    blueMarker: function(){
        blueIcon = new GIcon(G_DEFAULT_ICON);
        blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
        blueIcon.iconSize = new GSize(32,  32);
        blueMarkerOptions = { icon:blueIcon };
        return blueMarkerOptions;
    },
    redMarker: function(){
        redIcon = new GIcon(G_DEFAULT_ICON);
        redIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png";
        redIcon.iconSize = new GSize(32,  32);
        redMarkerOptions = { icon:redIcon };
        return redMarkerOptions;
    },
    greenMarker: function(){
        greenIcon = new GIcon(G_DEFAULT_ICON);
        greenIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png";
        greenIcon.iconSize = new GSize(32,  32);
        greenMarkerOptions = { icon:greenIcon };
        return greenMarkerOptions;
    },
    init: function(index) {
        if (GBrowserIsCompatible()) {
            location_map.map = new GMap2(document.getElementById("map"));
            if (index > -1 && location_map.locations.length > 1)
                location_map.centerOnArea(index);
            else if (index > -1)
                location_map.centerOnOnePoint(index);
            else
                location_map.map.setCenter(new GLatLng(31.14, -100.1419), 5);
            location_map.map.addControl(new GSmallMapControl());
            location_map.placeAllMarkers(); 
        }
    },
    placeAllMarkers: function() {
    	geocoder = new GClientGeocoder();
        for (var i = 0; i < location_map.locations.length; i++) {
            point = new location_point(location_map.locations[i], i);
            point.initPoint();
            location_map.points.push(point);
        }
    },
    centerOnOnePoint: function(index) {
        geocoder = new GClientGeocoder();
        address = location_map.locations[index];
        geocoder.getLatLng(address,
            function(point) {
                if (point) {
                    location_map.map.setCenter(point, 15);
                    location_map.map.openInfoWindowHtml(point, document.getElementById('branch_info_' + location_map.getIndex(address)).innerHTML, {pixelOffset:new GSize(0,-32)});
                }
                else {
                    alert("That location cannot be found by Google maps.");
                }
            }
        );
    },
    centerOnArea: function(index) {
        geocoder = new GClientGeocoder();
        if (index > 11) { index = 0; }
        geocoder.getLatLng(location_map.locations[index],
            function(point) {               
                if (point) {
                    location_map.map.setCenter(point, 11);
                }
            }
        );
    }
};

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}