/**
	Function: toggles Map Types - used to reskin the control
	Global Vars:
	map
	streetviewcontrol
	streetOverlay
	**/
function customMapTypeControl() {
	this.id = "customMapTypeControl";
	this.polygonToggle = false;
	this.map; // map object
	this.last; 	// last clicked
}
customMapTypeControl.prototype = new GControl();

customMapTypeControl.prototype.printable = function() { return true; }
customMapTypeControl.prototype.selectable = function() { return true; }
customMapTypeControl.prototype.createContent = function() {
	var html = "";
	html += "<ul>";
	html += "<li id='normalMap'></li>";
	html += "<li id='SatelliteMap'></li>";
	html += "<li id='Hybrid'></li>";
	html += "<li id='toggle_polygon' title='Draw a polygon with your cursor. When done, click inside your polygon to search for a home in that area. Click the polygon icon again when done.'></li>";
	html += "<li id='toggle_streetview' title='toggle streetview overlay. Click on a street highlighted in blue to enter street view.'></li>";
	html += "</ul>";
	return html;
}
customMapTypeControl.prototype.initialize = function(map) { 
	this.mapContainer = map.getContainer();
	
	this.container = document.createElement("div");
	$(this.container).attr("id",this.id);
	
	$(this.container).append(this.createContent());
	
	$(this.mapContainer).append(this.container);
	
	var last;
	var toggle = this.polygonToggle;
	var polygon;
	
	// events
	$(this.container).find("li").bind("click", {map:map,last:last}, function() {
		if(last) $(last).attr("class","");
		last = $(this);
		$(this).attr("class","active");
		var id = $(this).attr("id");
		if(id=="normalMap") map.setMapType(G_NORMAL_MAP);
		else if(id=="SatelliteMap") map.setMapType(G_SATELLITE_MAP);
		else if(id=="Hybrid") map.setMapType(G_HYBRID_MAP);
		else if(id=='toggle_streetview') {
			if(streetviewcontrol==false) {
				streetviewcontrol = true;
				map.addOverlay(streetOverlay);
				$(this).attr("class","streetOverlayActive");
			}
			else if(streetviewcontrol==true) {
				streetviewcontrol = false;
				map.removeOverlay(streetOverlay);
				$(this).attr("class","");
			}
		}
		else if(id=='toggle_polygon') {
			if(toggle==false) {
				toggle = true;
				$(this).attr("class","active");
				polygon = new GPolygon([], "#008000", 2, 0.7, "#008000", 0.2);
				map.addOverlay(polygon);
				polygon.enableDrawing();
				polygon.enableEditing({onEvent: "mouseover"});
				polygon.disableEditing({onEvent: "mouseout"});
				GEvent.addListener(polygon, "endline", function() {
					GEvent.addListener(polygon, "click", function(latlng, index) {
						if (typeof index == "number") {
							polygon.deleteVertex(index);
						} 
					});
				});
				GEvent.addListener(polygon, "click", function(latlng, index) {
					var coords = "";
					for(var i = 0; i < polygon.getVertexCount(); i++) {
						var point = polygon.getVertex(i);
						if(i>0) coords += ",";
						coords += point.lat()+" "+point.lng();
					}
					var mapContainer = map.getContainer();
		
					var div = document.createElement("input");
					$(div).attr("name","polycoords");
					$(div).attr("type","hidden");
					$(div).attr("value",coords);
		
					$(mapContainer).append(div);
					submitSearch();
				});
			}
			else if(toggle==true) {
				toggle = false;

				polygon.disableEditing(); // disable cursor in case polygon doesn't exist
				map.removeOverlay(polygon);

				$(this).attr("class","");
				var val = $("input[name=polycoords]").attr("value");
				$("input[name=polycoords]").remove();
				if(val) {
					//alert(val);
					submitSearch();
				}
				for(var i = 0; i < polygon.getVertexCount(); i++) {
					polygon.deleteVertex(i);
				} // delete indexes
			}
		}
	});
	
	return this.container;
}

customMapTypeControl.prototype.getDefaultPosition = function() { 
	this.defaultPos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(6,10));
	return this.defaultPos;
}
