function MInsetMapControl(MOptions) {
	MOptions = MOptions ? MOptions : {};
	this.position = MOptions.position ? MOptions.position : new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(0,0));
	this.center = MOptions.center ? MOptions.center : new GLatLng(0,0);
	this.zoom = MOptions.zoom ? MOptions.zoom : 3;
}

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

MInsetMapControl.prototype.initialize = function(map) {
	this.map = map;
	this.self = this;
	this.map.overlays = Array();
	this.overlays = Array();
	this.requestSent = false;

	this.st2;
	this.loadStateList();

	var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(200, 5));
	this.progressControl = new MProgressControl({position:pos});
	this.map.addControl(this.progressControl);

	this.container = document.createElement('div');
	this.innerDiv = this.createInnerDiv();
	this.container.appendChild(this.innerDiv);
	this.map.getContainer().appendChild(this.container);
	return this.container;

}	

	

MInsetMapControl.prototype.loadStateList = function() {
	this.st2 = Array('MN','MT','ID','ND','WA','OR','SD','NH','MI','WI','VT','WY','ME','IA','NE','IL','PA','CT','NV','UT','IN','OH','MA','NJ','RI','CO','NY','WV','MO','KS','DE','KY','MD','CA','VA','AZ','OK','NM','TN','AR','NC','SC','GA','LA','AL','MS','FL','TX');
	//AK, HI, DC and PR not included
}
	
MInsetMapControl.prototype.createInnerDiv = function() {
	var self = this.self;

	var oDiv = document.createElement('div');
	this.insetMap = new GMap2(oDiv);
	this.insetMap.setCenter(this.center,this.zoom);
	
	var CopyrightDiv = oDiv.firstChild.nextSibling;
	var CopyrightImg = oDiv.firstChild.nextSibling.nextSibling;
	CopyrightDiv.style.display = "none"; 
	CopyrightImg.style.display = "none";

	this.blankMap = this.createBlankMap();
	this.insetMap.addMapType(this.blankMap);
	this.insetMap.setCenter(this.center,this.zoom,this.blankMap);

	GEvent.addListener(this.insetMap,'drag',function(){
		var d = (self.map.getCenter().distanceFrom(self.insetMap.getCenter()) / 1609).toFixed(2);
//		GLog.write(d);

		if (d > 1000) {
			self.setPolyColor('#C00000');
		}
		else if (d > 200) {
			self.setPolyColor('#aaaa00');
		}
		else if (d > 80) {
			self.setPolyColor('#00aa00');
		}
		else if (d > 30) {
			self.stateFound();
		}
	})

	GEvent.addListener(this.insetMap,'dragstart',function(){
		self.setSize(self.map.getContainer().clientWidth,self.map.getContainer().clientHeight,false);
	})


	oDiv.style.background = '';
	return oDiv;
}


MInsetMapControl.prototype.setPolyColor = function(color) {
	for (var n = 0 ; n < this.overlays.length ; n++ ) {
		this.overlays[n].setFillStyle({color:color});
		this.overlays[n].setStrokeStyle({color:color});
	}
}

MInsetMapControl.prototype.stateFound = function() {
	var self = this.self;
	self.insetMap.disableDragging();

	if (!document.all) {
		var evt = document.createEvent("MouseEvents");
		evt.initMouseEvent("mouseup", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
		self.insetMap.getContainer().dispatchEvent(evt);
	}

	self.insetMap.panTo(this.map.getCenter());
	self.blinkAndRemove();
}


MInsetMapControl.prototype.blinkAndRemove = function(up,cnt) {
	var self = this.self;
	up = up ? up : false;
	cnt = cnt ? cnt : 0;

	if (up) {
		for (var n = 0 ; n < this.overlays.length ; n++ ) {
			this.overlays[n].setStrokeStyle({weight:5});
		}
	}
	else {
		for (var n = 0 ; n < this.overlays.length ; n++ ) {
			this.overlays[n].setStrokeStyle({weight:2});
		}
	}

	cnt++;
	up = up ? false : true;
	if ((cnt < 5) && (!document.all)) {
		window.setTimeout(function(){self.blinkAndRemove(up,cnt)},500);
	}
	else {
		for (var n = 0 ; n < this.overlays.length ; n++ ) {
			this.overlays[n].setStrokeStyle({color:'#C000C0'});
			this.overlays[n].setFillStyle({color:'#C000C0'});
		}
		self.getRandomState();
	}
}



MInsetMapControl.prototype.reset = function() {
	this.clearOverlays();

	while (this.map.overlays.length) {
		var ol = this.map.overlays.shift();
		this.map.removeOverlay(ol);
		ol = null;
	}
	this.map.overlays = null;
	this.map.overlays = Array();
	this.loadStateList();
	this.getRandomState();
}

MInsetMapControl.prototype.getRandomState = function() {
	if (this.requestSent) {
		return;
	}
	var self = this.self;
	this.clearOverlays();
	this.setSize(200,200,true);
	if (this.st2.length) {
		this.requestSent = true;
		var url = '/cgi-bin/randomState.pl?st=' + this.st2.join(',');
//GLog.write(url);
		this.progressControl.show();
		this.makeRequest(url,this.readPoly);
	}
	else {
		this.setSize(1,1,false);
		window.setTimeout(function(){
			alert('Good job! :-)')
		},200);
	}
}


MInsetMapControl.prototype.readPoly = function(http_request,self) {
	var str = http_request.responseText;
	eval(str);
	self.requestSent = false;
	self.addOverlay(state);
	self.progressControl.hide();
	for (var n = 0 ; n < self.st2.length ; n++ ) {
		if (self.st2[n] == state.name) {
			self.st2.splice(n,1);
			continue;
		}
	}
}


MInsetMapControl.prototype.addOverlay = function(olObj) {
	var bounds = new GLatLngBounds();
	for (var p = 0 ; p < olObj.polys.length ; p++ ) {
		var polyObj = olObj.polys[p];
		this.overlays.push(polyObj);
		this.insetMap.addOverlay(polyObj);
		bounds.extend(polyObj.getBounds().getSouthWest());
		bounds.extend(polyObj.getBounds().getNorthEast());
	}
	this.insetMap.setCenter(bounds.getCenter());
	this.insetMap.enableDragging();
}


MInsetMapControl.prototype.clearOverlays = function() {
	while (this.overlays.length) {
		var ol = this.overlays.shift();
		this.insetMap.removeOverlay(ol);
		this.map.overlays.push(ol);
		this.map.addOverlay(ol);
	}
	this.overlays = null;
	this.overlays = Array();
}


MInsetMapControl.prototype.setSize = function(w,h,border) {
	this.innerDiv.style.border = border ? '3px dashed navy' : 'none';
	var deltaX = this.insetMap.getContainer().clientWidth - w;
	var deltaY = this.insetMap.getContainer().clientHeight - h;
	var pt = this.insetMap.getCurrentMapType().getProjection().fromLatLngToPixel(this.insetMap.getCenter(),this.insetMap.getZoom());
	var newpt = new GPoint(pt.x + Math.floor(deltaX/2),pt.y + Math.floor(deltaY/2));
	var newCenter = this.insetMap.getCurrentMapType().getProjection().fromPixelToLatLng(newpt,this.insetMap.getZoom());
	this.innerDiv.style.width = w + 'px';
	this.innerDiv.style.height = h + 'px';
	this.insetMap.checkResize();
	this.insetMap.setCenter(newCenter); 
}


MInsetMapControl.prototype.getDefaultPosition = function() {
	return this.position;
}


MInsetMapControl.prototype.createBlankMap = function() {
	var normalProj = G_NORMAL_MAP.getProjection();	
	var cMap = new GMapType([], normalProj, 'Blank', {maxResolution:20, minResolution:0, errorMessage:'Boom!'}); 
	return cMap;
}


MInsetMapControl.prototype.makeRequest = function(url,callback,data,typeXml) {
	var self = this.self;
//GLog.write(url);return;
	var http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType &! typeXml) {
			http_request.overrideMimeType('text/plain');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {};
		}
	}

	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}

	http_request.onreadystatechange =  function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				eval(callback(http_request,self));
			}
			else {
				alert('Request Failed: ' + http_request.status);
			}
		}
	};

	if (data) {
		http_request.open('POST', url, true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_request.setRequestHeader("Content-length", data.length);
		http_request.send(data);
	}
	else {
		http_request.open('GET', url, true);
		http_request.send(null);
	}
};


