var AllSubNavButtons = 'normalbutton';

function AutoOpenItem(){
	var dropdown = document.forms[0].elements['plcRoot$Layout$ZonecenterBlock$pageplaceholder2$partPlaceholder$Layout$centerContainer$NeighbourhoodLandingPageContent$lbxNeighbourhood'];
	var tt = location.href.toLowerCase();
	var arrTT = tt.split("nid=");
	var nid = arrTT[1]
	if(arrTT[1].indexOf("&")>-1){
		var tmp = arrTT[1].split("&");
		nid =tmp[0]; 
	}	
	if(nid !=""){
		for(var i =0; i < dropdown.options.length;i++){
			if(dropdown.options[i].value==nid){
				dropdown.selectedIndex=i;
				CenterNeighbourhood(nid);
				DisplayNeighbourhoodDetails(nid);
				break
			}
		}
	}
}

function setRolloverControlButtons(ID, flag){
  var theButton = document.getElementById(ID);
  if(flag) theButton.style.background = "url(/App_themes/tourismtoronto/images/boxheader_on.gif) left top no-repeat";
  else if(ID!=AllSubNavButtons)theButton.style.background = "url(/App_themes/tourismtoronto/images/boxheader_off.gif) left top no-repeat";

}

function CustomButtonsOnMap() {
}

CustomButtonsOnMap.prototype = new GControl();

CustomButtonsOnMap.prototype.initialize = function(map) {
  var container = document.createElement("div");
  var map_controls = document.getElementById('map_controls');
    if(map_controls !=null&&map_controls !=undefined){
	container.innerHTML = map_controls.innerHTML;
	map_controls.innerHTML = "";
	}
  map.getContainer().appendChild(container);
  return container;
}


CustomButtonsOnMap.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
}


function GetNeighbourhoodObject(id) {
	    for (var p in points) {
	        if (points[p].neighbourhoodID == id) {
	            return points[p];
	        }
	    }
	    
	    return null;
	}

function toggleButtonsOn(id){
	var OLDID = AllSubNavButtons;
	setRolloverControlButtons(id, true);
	AllSubNavButtons = id;
	if(OLDID!=null&&OLDID!=id)setRolloverControlButtons(OLDID, false)
	
    
}
	
	function DisplayNeighbourhoodDetails(id) {
	    
	    // hide existing neighbourhood details container
	    if (currentNeighbourhoodId > -1) $("div[@id='neighbourhood_display_"+ currentNeighbourhoodId +"']").hide();
	    
	    // show container for all neighbourhood detail containers
	    $("div[@id='neighbourhood_display_container']").show();
	    currentNeighbourhoodId = id;
	    
	    // show new container
	    var oContainer = $("div[@id='neighbourhood_display_"+ id +"']");
	    oContainer.show();
	    
	    // thumb
	    var oParent = $("div[@id='neighbourhood_thumb_container']", oContainer);
	    var value = $("img", oParent).attr("src");
	    DisplayNeighbourhoodItem(oParent, value);
	    
	    // location
	    oParent = $("div[@id='neighbourhood_location_container']", oContainer);
	    value = $("span[@id='neighbourhood_location']", oParent).text();
	    DisplayNeighbourhoodItem(oParent, value);
	    
	    // transit
	    oParent = $("div[@id='neighbourhood_transit_container']", oContainer);
	    value = $("span[@id='neighbourhood_transit']", oParent).text();
	    DisplayNeighbourhoodItem(oParent, value);
	}
	
	function DisplayNeighbourhoodItem(parent, value) {
	    // check for value
	    if (value == "") { // ensure hidden
	        parent.hide();
	    } else {
	        parent.show();
	    }
	}
	
	function LookupByAddress() {
	    var address = "";
	    var a = utils.getRef("txtLookupAddress");
	    if (!utils.isNull(a)) address = a.value;
	    
	    var postalcode = "";
	    var p = utils.getRef("txtLookupPostalCode");
	    if (!utils.isNull(p)) postalcode = p.value;
	    
	    if (address == "" && postalcode == "") return;
	    if (address != "") address += ", Toronto, Ontario";
	    
	    var lookup = (postalcode == "") ? address : postalcode;
	    var content = (address == "") ? postalcode : address;
	    
	    // clear all existing points
	    tt_mapping.clear();
	    
	    // re-map all neighbourhoods
	    PlotNeighbourhoods();
	    
	    // map search address
	    var opts = {
	        "center": true,
	        "zoom": 2
	    };
	    tt_mapping.plotAddress(lookup, opts, content);
	}
	
	function CenterNeighbourhood(id) {
	    for (var p in points) {
	        if (points[p].neighbourhoodID == id) {
	            tt_mapping.setCenter(points[p], 13);
	           
	           points[p]["Marker"].openInfoWindow( points[p]["RollOverContent"]);
	            break;
	        }
	    }
	}
	

	

	function PlotNeighbourhoodsByAddress() {
	    var opts = {
			"center": true,
			"zoom": 10,
			"callback": DisplayNeighbourhoodDetails,
			"address": "mainIntersection+city"
		};
		
		tt_mapping.plotPointsByAddress(points, opts);	
	   //tt_mapping.plotPointsWithCallback(points, DisplayNeighbourhoodDetails);
	}

	//Rollover_InfoWindow_
	function PlotNeighbourhoods() {
		var ZOOM = 13;
		if(location.href.indexOf("Greater-Toronto-Region")>-1)ZOOM = 10;

	    var opts = {
			"center": true,
			"zoom": ZOOM,
			"infoWindowDomId": {
			    "prefix": "InfoWindow_",
			    "id": "neighbourhoodID"
			},
			"rolloverWindowDomId": {
			    "prefix": "InfoWindow_",
			    "id": "neighbourhoodID"
			},
			"icon": {
			    "image": "/App_Themes/tourismtoronto/images/neighbourhood_gmap_icon.png",
			    "width": 27,
			    "height": 35
			}
		};
		
		tt_mapping.plotPointsWithOpts(points, opts);
	}


function TTSimpleMapWithPoint_initialize(latp,longp) {
      if (GBrowserIsCompatible()) {
	  //	var latp =43.64677344;
       //	var longp =-79.37437284;
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(latp, longp), 13);
      
        var baseIcon = new GIcon();
        baseIcon.iconSize = new GSize(27, 33);
        baseIcon.iconAnchor = new GPoint(0, 0);
    
        function createMarkerTTpoint(point, index) {
  
          var letteredIcon = new GIcon(baseIcon);
          letteredIcon.image = "/App_Themes/tourismtoronto/images/neighbourhood_gmap_icon.png";
          markerOptions = { icon:letteredIcon };
          var marker = new GMarker(point, markerOptions);
          return marker;
        }

        
        var lngSpan = longp;
        var latSpan = latp;
  
        var latlng = new GLatLng(latSpan,lngSpan)
 
        map.addOverlay(createMarkerTTpoint(latlng, 0));
      }
    }


var utils = function() {
	var typeOf = function(value) {
		var s = typeof value;
		if (s === 'object') {
			if (value) {
				if (typeof value.length === 'number' &&
						!(value.propertyIsEnumerable('length')) &&
						typeof value.splice === 'function') {
					s = 'array';
				}
			} else {
				s = 'null';
			}
		}
		return s;
	};
	
	var isNull = function(v) {
		if (v == null || typeof(v) == "undefined") return true;
		return false;
	};
	
	var getRef = function(id) {
    	return (document.all) ? document.all[id] : (document.getElementById) ? document.getElementById(id) : null;
	};
	
	var showHideById = function(id, show) {
	    var e = utils.getRef(id);
		if (e == null) return;
		var display = (show) ? "block" : "none";
		e.style.display = display;
	};
	
	var showHideElement = function(e, show) {
	    if (e == null) return;
		var display = (show) ? "block" : "none";
		e.style.display = display;
	};
	
	return {
		typeOf: function(value) {
			return typeOf(value);	
		},
		
		isNull: function(value) {
			return isNull(value);	
		},
		
		getRef: function(id) {
		    return getRef(id);
		},
		
		showHideById: function(id, show) {
		    showHideById(id, show);
		},
		
		showHide: function(e, show) {
		    showHideElement(e, show)
		}
		
	};
	
	
}();

var tt_mapping = function() {
	
	 var config = {
		 target: "map_canvas",
		 center: {lat: 43.670233, lng: -79.386755},
		 zoom: 10,
		 icon: {
			size: { width: 27, height: 33 },
			shadow: "/App_Themes/tourismtoronto/images1/textmarker.png",
			shadowSize: { width: 27, height: 33 },
			anchor: { x: 0, y: 37},
			windowAnchor: { x: 14, y: -2 }, 
			shadowAnchor: { x: 18, y: 5 }
		 },
		 overlay: "overlay.png",
		 boundary: {
			 sw: {lat: 37.4419, lng: -122.1419 },
			 ne: {lat: 36.4819, lng: -122.1019 }
		 }
	 };
	 

     var setSatelliteButton = function(){


  	map.setMapType(G_SATELLITE_MAP);
	}

	var setHybridButton = function(){

	
  	map.setMapType(G_HYBRID_MAP);
	}

	var setNormalButton = function(){

	
  	map.setMapType(G_NORMAL_MAP);
	}

	 var map = null;
	 
	 var baseIcon = null;
	 
	 /**
	  *	Creates the default map (GMap2) object and baseIcon (GIcon) objects.  <br/>
	  *	Both map and baseIcon are properties of tt_mapping and not returned by the initialize function.
	  *	
	  * @author Drew Ashby
	  */
	var initialize = function() {
		if (!GBrowserIsCompatible()) return;
		
		map = new GMap2(document.getElementById(config.target));
		map.setCenter(new GLatLng(config.center.lat, config.center.lng), config.zoom);
		map.addControl(new GSmallMapControl());
	//	map.addControl(new GMapTypeControl());
		map.addControl(new CustomButtonsOnMap());

		
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		baseIcon = new GIcon();
		baseIcon.shadow = config.icon.shadow;
		baseIcon.iconSize = new GSize(config.icon.size.width, config.icon.size.height);
		baseIcon.shadowSize = new GSize(config.icon.shadowSize.width, config.icon.shadowSize.height);
		baseIcon.iconAnchor = new GPoint(config.icon.anchor.x, config.icon.anchor.y);
		baseIcon.infoWindowAnchor = new GPoint(config.icon.windowAnchor.x, config.icon.windowAnchor.y);
		baseIcon.infoShadowAnchor = new GPoint(config.icon.shadowAnchor.x, config.icon.shadowAnchor.y);
	
	};
	
	/**
	 * Creates a GMarker object that can be ovelayed on a map
	 * 	
	 * @param 	point		        A GLatLng object specifying the latitude and longitude position of the marker
	 * @param 	opts		        A JSON-styled object containing all the properties of the marker
							        See http://code.google.com/apis/maps/documentation/reference.html#GMarkerOptions for reference
	 * @param 	contents	        HTML code segment containing the contents of the marker overlay	
	 * @param   rolloverContents    HTML code segment containing the contents of the rollover infowindow
	 * @return	A GMarker object
	 * @see		<a href="http://code.google.com/apis/maps/documentation/reference.html#GMarkerOptions">GMarkerOptions API reference</a>
	 * @author Drew Ashby
	 */
	var createMarker = function(point, opts, contents, rolloverContents) {
		if (utils.isNull(baseIcon)) return;
		
		// Set up our GMarkerOptions object
		var marker = new GMarker(point, opts);
		
		GEvent.addListener(marker, "click", function() {
			//alert(point.neighbourhoodID)
			marker.openInfoWindowHtml(contents);
		});
		
		if (!utils.isNull(rolloverContents) && rolloverContents != "") {
		    GEvent.addListener(marker, "mouseover", function() {
		        marker.openInfoWindowHtml(rolloverContents);
		    });
		} 

		return marker;
	};
	
	/**
	 * Creates a GMarker object that can be ovelayed on a map
	 * 	
	 * @param 	point		        A GLatLng object specifying the latitude and longitude position of the marker
	 * @param 	opts		        A JSON-styled object containing all the properties of the marker
							        See http://code.google.com/apis/maps/documentation/reference.html#GMarkerOptions for reference
	 * @param 	contentNode	        DOM node containing the content for the infowindow
	 * @param   rolloverContentNode DOM node containing the content for the rollover infowindow (optional)
	 * @return	A GMarker object
	 * @see		<a href="http://code.google.com/apis/maps/documentation/reference.html#GMarkerOptions">GMarkerOptions API reference</a>
	 * @author Drew Ashby
	 */
	var createMarkerWithDom = function(point, opts, contentNode, rolloverContentNode) {
		if (utils.isNull(baseIcon)) return;
		
		// Set up our GMarkerOptions object
		var marker = new GMarker(point, opts);
		
		// add listeners
		var mouseoutHandler = null;
		var mouseoutEnabled = false; 
		
		GEvent.addListener(marker, "click", function() {
	    	var showBoxID = contentNode.id.replace("InfoWindow_","");
			DisplayNeighbourhoodDetails(showBoxID);
			window.scrollTo(0,500);
			marker.openInfoWindow(contentNode);
			
			GEvent.removeListener(mouseoutHandler);
			mouseoutHandler = false;
		});
		
		if (!utils.isNull(rolloverContentNode)) {
		    function AddMouseoutListener() {
		        var handler = GEvent.addListener(marker, "mouseout", function() {
		           //marker.closeInfoWindow();

					//Felix  removed rollout...
		        });
		        mouseoutHandler = true;
		        return handler;
		    }
		    
		    
		    GEvent.addListener(marker, "mouseover", function() {
		        marker.openInfoWindow(rolloverContentNode);
		        
		        if (!mouseoutHandler) {
		            GEvent.addListener(marker, "mouseout", function() {
		                marker.closeInfoWindow();
		            });
		        }
		    });
		    
		    mouseoutHandler = AddMouseoutListener();
		    
		    GEvent.addListener(marker, "infowindowclose", function() {
		        if (!mouseoutHandler) mouseoutHandler = AddMouseoutListener();
		    });
		} 

		return marker;
	};
	
	
	/**
	 * Creates a GMarker object that can be ovelayed on a map and attaches a custom callback function to it
	 * 	
	 * @param 	point		A GLatLng object specifying the latitude and longitude position of the marker
	 * @param 	opts		A JSON-styled object containing all the properties of the marker
							See http://code.google.com/apis/maps/documentation/reference.html#GMarkerOptions for reference
	 * @param 	contents	HTML code segment containing the contents of the marker overlay	
	 * @return	A GMarker object
	 * @see		<a href="http://code.google.com/apis/maps/documentation/reference.html#GMarkerOptions">GMarkerOptions API reference</a>
	 * @author Drew Ashby
	 */
	var createMarkerWithCallback = function(point, opts, oData, callback) {
		if (utils.isNull(baseIcon)) return;
		
		// Set up our GMarkerOptions object
		var marker = new GMarker(point, opts);
		
		GEvent.addListener(marker, "click", function() {
//alert("b");
			callback(oData);
		});

		return marker;
	};
	
	var createTestMarker = function(point, index) {
		if (utils.isNull(baseIcon)) return;
		
		// Create a lettered icon for this point using our icon class
		var letter = String.fromCharCode("A".charCodeAt(0) + index);
		var letteredIcon = new GIcon(baseIcon);
		letteredIcon.image = config.icon.shadow;
		
		// Set up our GMarkerOptions object
		markerOptions = { icon:letteredIcon };
		var marker = new GMarker(point, markerOptions);
		
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml("Marker <b>" + letter + "</b>");
		});

		return marker;
	};
	
	/**
	 * Uses default boundary parameters (from config object) and inputted image to create a new GGroundOverlay object for the map.
	 *
	 * @param	mapImage	A png graphic to be used as the new overlay
	 * @author Drew Ashby
	 */
	var addOverlayMapImage = function(mapImage) {
		if (utils.isNull(mapImage) || utils.isNull(map)) return;
		
		var boundaries = new GLatLngBounds(new GLatLng(config.boundary.sw.lat, config.boundary.sw.lng), new GLatLng(config.boundary.ne.lat, config.boundary.ne.lng));
		var oldmap = new GGroundOverlay(mapImage,map.getBounds());
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addOverlay(oldmap)
		
	};
	
	/**
	 * Overlays a series of given points onto the map
	 *
	 * @param	points	An array of generic objects.  Each object has the following 4 elements; <br/>
	 *					<ul><li>latitude</li>
	 *						<li>longitude</li>
	 *						<li>an image name to be used as the overlay image in place of defaults </li>
	 *						<li>an HTML code segment containing the contents of the overlay </li>
	 *					</ul>
	 * @author Drew Ashby
	 */
	var plotLatLngPoints = function(points) {
		if (utils.typeOf(points) != "array") return;
		
		for (var p in points) {
			var latlng = new GLatLng(points[p].lat, points[p].lng);
			var content = points[p].content;
			var opts = {};
			if (!utils.isNull(points[p].image) && !utils.isNull(baseIcon)) {
				var pIcon = new GIcon(baseIcon);
				pIcon.image = points[p].image;
				opts = {icon:pIcon};
			}
			
			map.addOverlay(createMarker(latlng, opts, content));
		}
	};
	
	



	/**
	 * Overlays a series of given points onto the map configurable by options
	 *
	 * @param	points	An array of generic objects.  Each object has the following 4 elements; <br/>
	 *					<ul><li>latitude</li>
	 *						<li>longitude</li>
	 *						<li>an image name to be used as the overlay image in place of defaults </li>
	 *						<li>an HTML code segment containing the contents of the overlay </li>
	 *					</ul>
	 * @param   opts    A JSON object detailing basic configurable options
	 * @author Drew Ashby
	 */
	var plotLatLngPointsWithOpts = function(points, opts) {
		if (utils.typeOf(points) != "array") return;
		
		if (!utils.isNull(opts.center)) {
		    var zoom = (!utils.isNull(opts.zoom)) ? opts.zoom : config.zoom;
		    if (opts.center) setCenter(points[0], zoom);
		}
		
		for (var p in points) {
			var latlng = new GLatLng(points[p].lat, points[p].lng);
			
			var pIcon = new GIcon(baseIcon);
			if (!utils.isNull(opts.icon)) {
			    if (!utils.isNull(opts.icon.image)) pIcon.image = opts.icon.image;
			    if (!utils.isNull(opts.icon.width) && !utils.isNull(opts.icon.height)) {
			        pIcon.iconSize = new GSize(opts.icon.width, opts.icon.height);
			    }
			    
			}
			
			var markerOpts = {};
			markerOpts = {icon:pIcon};
			
			var contentNode = null;
			if (!utils.isNull(opts.infoWindowDomId)) {
			    var nodeId = opts.infoWindowDomId.prefix + points[p][opts.infoWindowDomId.id];
			    contentNode = utils.getRef(nodeId);
			}
			
			var rolloverNode = null;
			if (!utils.isNull(opts.rolloverWindowDomId)) {
			    var nodeId = opts.rolloverWindowDomId.prefix + points[p][opts.rolloverWindowDomId.id];
			    rolloverNode = utils.getRef(nodeId);
			}
			
			if (!utils.isNull(contentNode)) {
				var mkerret =createMarkerWithDom(latlng, markerOpts, contentNode, rolloverNode)
			points[p]["Marker"] = mkerret;
			points[p]["RollOverContent"] =rolloverNode;
		//	alert(rolloverNode);
			map.addOverlay(mkerret);
			   // map.addOverlay();
			}else{
			//	alert("not found");
			}
			
			/*  code to create html content segments, deprecated in favour of passing content nodes into createmarker
			var overlayContent = "";
			if (!utils.isNull(opts.infoWindowFields)) {
			    overlayContent = "<div class=\"overlay-content-container\">\n";
			    
			    if (utils.typeOf(opts.infoWindowFields) == "array") {
			        for (var f in opts.infoWindowFields) {
			            overlayContent += "<div class=\""+ opts.infoWindowFields[f].field +"\">";
			            if (!utils.isNull(points[p][opts.infoWindowFields[f].field]) && points[p][opts.infoWindowFields[f].field] != "") {
			                if (opts.infoWindowFields[f].caption != "") overlayContent += opts.infoWindowFields[f].caption +" ";
			                switch (opts.infoWindowFields[f].type) {
			                    case "image":
			                        overlayContent += "<img src=\""+ points[p][opts.infoWindowFields[f].field] +"\"/>";
			                        break;
    			                    
			                    case "string":
			                        overlayContent += points[p][opts.infoWindowFields[f].field];
			                        break;
			                }
			            }
			            overlayContent += "</div>\n";
			        }
			    } else {
			        overlayContent += "<div>";
			        if (opts.infoWindowFields.caption != "") overlayContent += opts.infoWindowFields.caption +" ";
			        overlayContent += points[p][opts.infoWindowFields.field];
			        overlayContent += "</div>\n";
			    }
			
			    if (!utils.isNull(opts.cta)) {
			        if (opts.cta.type == "function") {
			            overlayContent += "<div class=\"cta\">";
			            overlayContent += "<a href=\"Javascript:"+ opts.cta.name +"("+ p +");\">";
			            overlayContent += opts.cta.caption +"</a>";
			            overlayContent += "</div>\n";
			        }
			    }
			    overlayContent += "</div>\n";
    		}
			
			var rolloverContent = "";
			if (!utils.isNull(opts.rolloverWindowFields)) {
			    rolloverContent = "<div class=\"rollover-content-container\">\n";
			    
			    if (utils.typeOf(opts.rolloverWindowFields) == "array") {
			        for (var f in opts.rolloverWindowFields) {
			            rolloverContent += "<div class=\""+ opts.rolloverWindowFields[f].field +"\">";
			            if (!utils.isNull(points[p][opts.rolloverWindowFields[f].field]) && points[p][opts.rolloverWindowFields[f].field] != "") {
			                if (opts.infoWindowFields[f].caption != "") rolloverContent += opts.rolloverWindowFields[f].caption +" ";
			                switch (opts.infoWindowFields[f].type) {
			                    case "image":
			                        rolloverContent += "<img src=\""+ points[p][opts.rolloverWindowFields[f].field] +"\"/>";
			                        break;
    			                    
			                    case "string":
			                        rolloverContent += points[p][opts.rolloverWindowFields[f].field];
			                        break;
			                }
			            }
			            rolloverContent += "</div>\n";
			        }
			    } else {
			        rolloverContent += "<div>";
			        if (opts.rolloverWindowFields.caption != "") rolloverContent += opts.rolloverWindowFields.caption +" ";
			        rolloverContent += points[p][opts.rolloverWindowFields.field];
			        rolloverContent += "</div>\n";
			    }
			    
			    rolloverContent +=  "</div>\n";
			}
			var mkerret = createMarker(latlng, markerOpts, overlayContent, rolloverContent)
			points[p]["Marker"] = mkerret;
			points[p]["RollOverContent"] =rolloverContent;
			map.addOverlay(mkerret);
		    */
		}
	};
	
	
	/**
	 * Overlays a series of given points onto the map and implements custom callback for each point
	 *
	 * @param	points		An array of generic objects.  Each object has the following 4 elements; <br/>
	 *						<ul><li>latitude</li>
	 *							<li>longitude</li>
	 *							<li>an image name to be used as the overlay image in place of defaults </li>
	 *							<li>an HTML code segment containing the contents of the overlay </li>
	 *						</ul>
	 * @param	callback	A custom callback function to be triggered by each point overlay
	 * @author Drew Ashby
	 */
	var plotLatLngPointsWithCallback = function(points, callback) {
		if (utils.typeOf(points) != "array") return;
		
		for (var p in points) {
			var latlng = new GLatLng(points[p].lat, points[p].lng);
			var content = points[p].markerLabel;
			var opts = {};
			if (!utils.isNull(points[p].image) && !utils.isNull(baseIcon)) {
				var pIcon = new GIcon(baseIcon);
				pIcon.image = points[p].image;
				opts = {icon:pIcon};
			}
			
			map.addOverlay(createMarkerWithCallback(latlng, opts, points[p], callback));
		}
	};
	
	var plotRandomLatLngPoints = function() {
		// Add 10 markers to the map at random locations
		var bounds = map.getBounds();
		var southWest = bounds.getSouthWest();
		var northEast = bounds.getNorthEast();
		var lngSpan = northEast.lng() - southWest.lng();
		var latSpan = northEast.lat() - southWest.lat();
		for (var i = 0; i < 10; i++) {
			var latlng = new GLatLng(southWest.lat() + latSpan * Math.random(),
			southWest.lng() + lngSpan * Math.random());
			map.addOverlay(createMarker(latlng, {}, "Latitude: "+ latlng.lat() +"<br/>Longitude: "+ latlng.lng()));
		}
	};
	
	
	/**
	 * Given a list of data points, uses GClientGeocoder to look up lat. and lng. for each, then plots the point on the map.
	 * <p>
	 * The Actual parsing of the input variables and plotting the map is handled by the callback function callback_plotPointsByAddress.
	 *
	 * @param	points  A Javascript array of JSON objects
	 * @param	opts	Object containing properties of the point to be plotted
	 * @author Drew Ashby
	 */
	var plotPointsByAddress = function(points, opts) {
		if (utils.typeOf(points) != "array") return;
		
		for (var p in points) {
		    var addressField = (utils.isNull(opts.address)) ? "address" : opts.address;
		    //if (utils.isNull(points[p][addressField])) continue;
		    
		    var address = "";
		    if (addressField.indexOf("+") > -1) {
		        var arrAddressFields = addressField.split("+");
		        for (var a=0; a<arrAddressFields.length; a++) {
		            if (!utils.isNull(points[p][arrAddressFields[a]]) && points[p][arrAddressFields[a]] != "") {
		                if (a > 0) address += ", ";
		                address += points[p][arrAddressFields[a]];        
		            }
		        }
		    } else {
		        if (!utils.isNull(points[p][addressField]) && points[p][addressField] != "") {
		            address = points[p][addressField];
		        }
		    }
		    
		    if (!utils.isNull(address)) {
		        var geocoder = new GClientGeocoder();
		        geocoder.getLatLng(
						           address, 
						           function(point) {
						                if (!point) return;
								        callback_plotPointsByAddress(point, points[p], opts);
						           });
			}
		}
	};
	
	var callback_plotPointsByAddress = function(point, pointProperties, opts) {
	    if (utils.isNull(map)) return;
		
		if (!utils.isNull(opts.center)) {
			var zoom = (!utils.isNull(opts.zoom)) ? opts.zoom : (!utils.isNull(config.zoom)) ? config.zoom : 13;
			if (opts.center) map.setCenter(point, zoom);
		}

        pointProperties.lat = point.lat();
        pointProperties.lng = point.lng();
        
        // add pointProperties into an array as that's what plotLatLng methods are expecting
        var pArray = new Array();
        pArray[0] = pointProperties;
        
		if (!utils.isNull(opts.callback)) {
		    plotLatLngPointsWithCallback(pArray, opts.callback);
		} else {
		    plotLatLngPoints(pArray);
		}

	}
	
	
	/**
	 * Takes an address as input, uses GClientGeocoder to look up lat. and lng., then plots the point on the map.
	 * <p>
	 * The Actual parsing of the input variables and plotting the map is handled by the callback function callback_plotPointByAddress.
	 *
	 * @param	address	A string representing a valid street address
	 * @param	opts	Object containing properties of the point to be plotted
	 * @param	content	HTML code segement containing the content of the overlay that will be placed on the map
	 * @author Drew Ashby
	 */
	var plotSinglePointByAddress = function(address, opts, content) {
		if (utils.isNull(address)) return;
		
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(
						   address, 
						   function(point) {
						   		if (!point) return;
								callback_plotPointByAddress(point, opts, content, address);
						   });
		
	};
	
	var callback_plotPointByAddress = function(point, opts, content, address) {
		if (utils.isNull(map)) return;
		
		if (!utils.isNull(opts.center)) {
			var zoom = (!utils.isNull(opts.zoom)) ? opts.zoom : (!utils.isNull(config.zoom)) ? config.zoom : 13;
			if (opts.center) map.setCenter(point, zoom);
		}

		if (!utils.isNull(opts.contentPlusAddress)) {
			if (opts.contentPlusAddress) content += address;	
		}
		
		var points = new Array();
		points[0] = {
			lat: point.lat(),
			lng: point.lng(),
			content: content,
			image: opts.image	
		};
		
		if (!utils.isNull(opts.callback)) {
		    plotLatLngPointsWithCallback(points, opts.callback);
		} else {
		    plotLatLngPoints(points);
		}
		
	};
	
	/**
	 * Returns the latitude and longitude to a defined callback function for a given address
	 *
	 * @param	address     A string representing a valid street address or major intersection
	 * @param	city        City for the address
	 * @param   province    The province/state for the address
	 * @param   country     The country for the address
	 * @param	callback    The callback function that will be used to handle the GLatLng point returned from the Geocoder
	 * @author Drew Ashby
	 */
	var returnGPoint = function(address, city, province, country, callback) {
		if (address == "" || city == "") return;
		if (province == "") province = "Ontario";
		if (country == "") country = "Canada";
		
		var completeAddress = address +", "+ city +", "+ province +", "+ country;
		
		var geocoder = new GClientGeocoder();
		geocoder.getLatLng(
						   completeAddress, 
						   function(point) {
						   		if (!point) return;
								callback(point, completeAddress);
						   });
		
	};
	
		
	/**
	 * Centers the map on a given point and to a given zoom level
	 *
	 * @param	point   a GLatLng object showing the desired latitude and longitude to center on
	 * @param	zoom    the desired zoom level
	 * @author Drew Ashby
	 */
	var setCenter = function(point, zoom) {
		if (utils.isNull(map)) return;
		
		var latlng = null;
		if (!utils.isNull(point)) {
			latlng = new GLatLng(point.lat, point.lng);
		} else {
			latlng = new GLatLng(config.center.lat, config.center.lng);
		}
		
		if (zoom == "" || utils.isNull(zoom)) zoom = config.zoom;
		
		map.setCenter(latlng, zoom);
	}
	
	/**
	 * Zooms the map to a given zoom level
	 *
	 * @param	zoom    the desired zoom level
	 * @author Drew Ashby
	 */
	var setZoom = function(zoom) {
		if (utils.isNull(map)) return;
		map.setZoom(zoom);
	}
	
	/**
	 * Removes all overlay points from the map
	 *
	 * @author Drew Ashby
	 */
	var clearAllPoints = function() {
		if (utils.isNull(map)) return;
		map.clearOverlays();
	};
		
	/**
	 * Appends the specified function to the existing window.onload stack
	 *
	 * @param	func	The function to be added to the stack
	 * @author Drew Ashby
	 */
	var addLoadEvent = function(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			};
		}
	};
	
	/**
	 * Appends the specified function to the existing window.onunload stack
	 *
	 * @param	func	The function to be added to the stack
	 * @author Drew Ashby
	 */
	var addUnLoadEvent = function(func) {
		var oldonunload = window.onunload;
		if (typeof window.onunload != 'function') {
			window.onunload = func;
		} else {
			window.onunload = function() {
				if (oldonunload) {
					oldonunload();
				}
				func();
			};
		}
	};
	
	return {
		init: function() {
			addLoadEvent(initialize);
			addUnLoadEvent(GUnload);
		},
		
		plotRandomPoints: function() {
			plotRandomLatLngPoints();	
		},
		
		plotPoints: function(points) {
			plotLatLngPoints(points);	
		},
		
		plotPointsWithCallback: function(points, callback) {
			plotLatLngPointsWithCallback(points, callback);	
		},
		
		plotAddress: function(address, opts, content) {
			plotSinglePointByAddress(address, opts, content);
		},
		
		plotPointsByAddress: function(points, opts) {
		    plotPointsByAddress(points, opts)
		},
		
		plotPointsWithOpts: function(points, opts) {
		    plotLatLngPointsWithOpts(points, opts)
		},
		
		getLocationPropertiesByAddress: function(address, city, province, country, callback) {
		    returnGPoint(address, city, province, country, callback);
		},
		
		setCenter: function(point, zoom) {
			setCenter(point, zoom);	
		},
		
		setZoom: function(zoom) {
			setZoom(zoom);	
		},
		
		clear: function() {
			clearAllPoints();	
		},
		
		addLoadEvent: function(func) {
		    addLoadEvent(func);
		},

     setSatelliteButton : function(){


  		setSatelliteButton()
	},

	setHybridButton : function(){

	
  		setHybridButton();
	},

	setNormalButton:function(){

	setNormalButton();
	}
		
	};
}();

tt_mapping.init();


