//ABQIAAAAEgmj0qyXDL2IgcHfP2bW3RQKRiq8lNJaxupx8aaQXwDifYaUYRTkY7xsUTVMlJKqZ_VTIKP7QqpYIw

if (window.location.href.match("^http://colleges.usnews.rankingsandreviews.com")) {
	var key = "ABQIAAAAEgmj0qyXDL2IgcHfP2bW3RRC8LpXqa7dvOnGnzV1_MR_CWLS6RRSkhH_BZVHJpYjfhIZ6fXKOs9EPg"
	
}
else {
	// API key for usnews.com subdomains.
	var key = "ABQIAAAA55ZIcvCCnpEjX6zNN-1cHBQKRiq8lNJaxupx8aaQXwDifYaUYRSHXseeOfKcPLVV570Vp88gKmwcsA"
}

document.write('<script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=' + key + '"></script>');

USN.Map = {
	'coordinates': null,
	'address': null,
	'status': null,
	'size': null,
	'zoom': {
		'large': 12,
		'small': 11
	},
	
	markers: [],
	
	'initialize': function(size, container, options) {
		this.size = size;
		this.compatible =  !!GBrowserIsCompatible();
		this.container = $(container);	
		this.polylines = []
		
		if (!this.compatible) {
			this.container.update("<p>Your browser is not compatible with this service.</p>");
		}
		if (options) {
			if (options.zoom) { this.customZoom = options.zoom; }
			if (options.callback) { this.callback = options.callback; }
		}	
	},
	
	'setCoordinates': function(lat, lng) {
		this.coordinates = new GLatLng(lat, lng);
		if (this.compatible) {
			this.drawCallback();
		}
	},
	
	'setAddress': function(address, markerText) {
		this.geoCode(address, markerText);
	},
	
	'setPolyLine': function(polyline) {
		this.polylines.push(polyline)
	},

	'geoCode': function(address, markerText) {
		var geocoder = new GClientGeocoder();
		var wait = Math.floor(Math.random() * 10000)
		if (this.compatible) {
			window.setTimeout(function() { geocoder.getLatLng(address, this.geoCodeCallback.bind(this, markerText, address))}.bind(this), wait);
		}
	},
	
	'geoCodeCallback': function(markerText, address, GLatLng) {
		if (!GLatLng) {
			this.status = "Not Found.";
			this.debug("ERROR: Could not find address:" + address);
			return;
		}
        this.debug("Address found:" + address);
		this.coordinates = GLatLng;
		if (this.gmap) { //If gmap is already rendered, just add point to it.
			var marker = new GMarker(this.coordinates);
			if (markerText) {
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(markerText, {'maxWidth': 300});
				});
			}
			this.markers.push(marker)
			this.gmap.addOverlay(marker);
		}
		else {
			this.drawCallback(markerText);
		}
	},
	
	'drawCallback': function(markerText) {
		var gmap = new GMap2(this.container);
		var zoom = (this.customZoom) ? this.customZoom : this.zoom[this.size]
		if (this.size === 'large') {
			gmap.addControl(new GSmallMapControl());
			gmap.addControl(new GMapTypeControl());
		}
		gmap.setCenter(this.coordinates, zoom);
		var marker = new GMarker(this.coordinates);
		if (markerText) {
			GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(markerText, {'maxWidth': 300});
			});
		}
		this.markers.push(marker)
		gmap.addOverlay(marker);
		if (this.polylines.length > 0) {
			for (var i = 0; i < this.polylines.length; i++) {
				gmap.addOverlay(this.polylines[0]);
			}
		}
		if (this.callback) {
			gmap = this.callback(gmap);
		}
 		this.gmap = gmap;
	},
	'debug': function(message) {
	   try {
	       console.log(message);
	   }
	   catch (e) {
	       // 
	   }
	}
}