// USOverviewControl.js
//
//   This Javascript is written by Ben Cochran
//   http://bencochran.com
//
//   It is licensed under a Creative Commons License
//   http://creativecommons.org/licenses/by/3.0/us/
//
//   USOverviewControl is a GControl that displays an overview
//   map in the bottom right corner. It looks very similar to the GOverviewControl
//   but does not follow the main map. It is intended to be used to show a single point
//   on a static overview map. E.G. to show Carleton College's location in the
//   United States.
//
//   
//
////////////////////////////////////////////

function USOverviewControl(MOptions) {
	MOptions = MOptions ? MOptions : {};
	// Default bounds contain the Continental United States
	this.initialBounds = MOptions.bounds ? MOptions.bounds : new GLatLngBounds(new GLatLng(25.799891182088306,-124.453125),new GLatLng(49.95121990866204,-65.56640625));;
	this.size_ = MOptions.size ? MOptions.size : new GSize(200,200);
	this.latlng = MOptions.latlng;
	this.follow = MOptions.follow ? MOptions.follow : false;
	
	// Tiny Red Pin
	defaultIcon = new GIcon();
	defaultIcon.image = "/global_stock/js/admissions/map_pins/mm_20_red.png";
	defaultIcon.shadow = "/global_stock/js/admissions/map_pins/mm_20_shadow.png";
	defaultIcon.iconSize = new GSize(12, 20);
	defaultIcon.shadowSize = new GSize(22, 20);
	defaultIcon.iconAnchor = new GPoint(6, 20);
	defaultIcon.infoWindowAnchor = new GPoint(5, 1);
	
	
	this.icon = MOptions.icon ? MOptions.icon : defaultIcon;
}

USOverviewControl.prototype = new GControl(true, false);

// Create the divs, the map, and the marker if
// needed and attach them
USOverviewControl.prototype.initialize = function(map) {
		
	var container = document.createElement("div");
	var innerDiv = document.createElement("div");
	

	innerDiv.style.border = '1px solid #979797';
	innerDiv.style.width = (this.size_.width - 8) + 'px';
	innerDiv.style.height = (this.size_.height - 8) + 'px';
	innerDiv.style.marginTop = '6px';
	innerDiv.style.marginLeft = '6px';

	container.style.borderTop = '1px solid #979797';
	container.style.borderLeft = '1px solid #979797';
	container.style.width = this.size_.width + 'px';
	container.style.height = this.size_.height + 'px';
	container.style.backgroundColor = '#FFFFFF';
	
	container.appendChild(innerDiv);

	map.getContainer().appendChild(container);
	
	this.insetMap = new GMap2(innerDiv);
	
	this.insetMap.setCenter(this.initialBounds.getCenter());
	this.insetMap.setZoom(this.insetMap.getBoundsZoomLevel(this.initialBounds));
	
	/// Hide the copyright and Google logo
	var CopyrightDiv = innerDiv.firstChild.nextSibling;
	var CopyrightImg = innerDiv.firstChild.nextSibling.nextSibling;
	CopyrightDiv.style.display = "none"; 
	CopyrightImg.style.display = "none";

	// If we follow the map, put a marker and make it follow
	if (this.follow)
	{
		this.moveMarker(map.getCenter());
		//moveend()
		GEvent.bind(map, "moveend", this, function() {
		  this.moveMarker(map.getCenter());
		  //this.insetMap.setCenter(map.getCenter());
		});
	}
	// If there's a point, add a marker there
	else if (this.latlng)
	{
		this.moveMarker(this.latlng);		
	}

	return container;
}

USOverviewControl.prototype.moveMarker = function(latlng) {
	if (!this.marker)
	{
		this.marker = new GMarker(latlng, {icon:this.icon});
		this.insetMap.addOverlay(this.marker);
	}
	else
	{
		this.marker.setLatLng(latlng);
	}
}

USOverviewControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(-1,-1));
}
