var map = null;
var geocoder = null;

var blLocations = new Array(
	new Array("1121 Falconridge Dr. NE", "Calgary, AB T3J 3H4", "(403) 293-3001"),
	new Array("1624 16th Ave. NW", "Calgary, AB T2M 0L5", "(403) 289-5596"),
	new Array("3420 17th Ave. SE", "Calgary, AB T2A 0R4", "(403) 272-2266"),
	new Array("5808 Macleod Tr. S", "Calgary, AB T2H 0J8", "(403) 252-0171"),
	new Array("9250 Macleod Tr. S", "Calgary, AB T2J 0P5", "(403) 253-8004"),
	new Array("1708 37th St. SW", "Calgary, AB T3C 3R1", "(403) 249-1048")
);

function initialize() {
  if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"),
		{ size: new GSize(640,425) } );
	
	// ====== Restricting the range of Zoom Levels =====
      // Get the list of map types      
      var mt = map.getMapTypes();
      // Overwrite the getMinimumResolution() and getMaximumResolution() methods
      for (var i=0; i<mt.length; i++) {
        mt[i].getMinimumResolution = function() {return 10;}
        mt[i].getMaximumResolution = function() {return 15;}
      }
	
	map.setCenter(new GLatLng(51.0457, -114.0628), 11);
	map.setUIToDefault();
		
	geocoder = new GClientGeocoder();
	
	for (x in blLocations) {
		if (blLocations[x][0] == "1624 16th Ave. NW") {
			var blAddress = blLocations[x][1];		// override geocacheing error for 16th Ave location
		} else {
			var blAddress = blLocations[x][0] + ', ' + blLocations[x][1];
		}
		createMarker(blAddress, x);
	}
	
	// Add a move listener to restrict the bounds range
    GEvent.addListener(map, "move", function() {
        checkBounds();
    });

    // The allowed region which the whole map must be within
    var allowedBounds = new GLatLngBounds(new GLatLng(50.75,-114.5), new GLatLng(51.25,-113.5));
      
    // If the map position is out of range, move it back
    function checkBounds() {
        // Perform the check and return if OK
        if (allowedBounds.contains(map.getCenter())) {
        	return;
        }
        // It's not OK, so find the nearest allowed point and move there
        var C = map.getCenter();
        var X = C.lng();
        var Y = C.lat();

        var AmaxX = allowedBounds.getNorthEast().lng();
        var AmaxY = allowedBounds.getNorthEast().lat();
        var AminX = allowedBounds.getSouthWest().lng();
        var AminY = allowedBounds.getSouthWest().lat();

        if (X < AminX) {X = AminX;}
        if (X > AmaxX) {X = AmaxX;}
        if (Y < AminY) {Y = AminY;}
        if (Y > AmaxY) {Y = AmaxY;}
        //alert ("Restricting "+Y+" "+X);
        map.setCenter(new GLatLng(Y,X));
	}
	
  }
}
	
function createMarker(address, id) {
	
	geocoder.getLatLng(address, function(point) {
			var marker = new GMarker(point);
			map.addOverlay(marker);
			var html = '<div class="marker-content"><strong>Blaskin &amp; Lane Tire Centres</strong><br />' + blLocations[id][0] + '<br />' + blLocations[id][1] + '<br />' + blLocations[id][2] + '</div><div style="font-size:11px;"><a href="/pages/inquiry/'+(parseInt(id)+1)+'">Contact this location</a> | <a href="/pages/book-an-appointment/'+(parseInt(id)+1)+'">Book appointment</a></div>';
			GEvent.addListener(marker, 'click', function() {
				map.setCenter(point);
				marker.openInfoWindowHtml(html);
			});
			
			var linkId = "location" + id;
			GEvent.addDomListener(document.getElementById(linkId), 'click', function() {
				map.setCenter(point);
				marker.openInfoWindowHtml(html);
			});
		}
	);
}