/*
*	Copyright: Matthew Hummon
*	Date: 2.21.06
*   Map v0.3 - 
*
*/

//Custom JumpToControls - Adds a text field for a centering the map
function JumpToControls() { }
//Extends GControl() printable = false, selectable = true
JumpToControls.prototype = new GControl(false, true);
JumpToControls.prototype.initialize = function(map){
	//Main Container
	var container = document.createElement("div");
	
	//Container for controls
	var iContainer = document.createElement("div");
	
	//postition of iContainer
	iContainer.style.position = "absolute";
	iContainer.style.top = "5px";
	iContainer.style.left = "3px";
	
	//TextField for recentering the map
	var txtJump = document.createElement("input");
	txtJump.setAttribute("type", "text");
	txtJump.style.width = "235px";
	txtJump.style.padding = "3px";
	//Pre-load with greyed out text
	txtJump.value = "Jump to an address";
	txtJump.style.color = "#ccc";
	txtJump.style.fontStyle = "italic";
	//Focus event for clearing txtJump
	txtJump.onclick = function(){
		this.style.color = "#000";
		this.style.fontStyle = "normal";
		this.value = "";
		//Remove the onclick event
		txtJump.onclick = null;
	}
	
	/**** Add "Enter" keydown event to txtJump ****/
	
	//Button to call centerMap()
	var btnJump = document.createElement("input");
	btnJump.setAttribute("type", "button");
	btnJump.value = "Go!";
	btnJump.style.marginLeft = "3px";
	
	//btnJump onclick event
	btnJump.onclick = function(){
		centerMap(txtJump.value);
	}
	
	iContainer.appendChild(txtJump);
	iContainer.appendChild(btnJump);
	
	container.appendChild(iContainer);
    
	this.setDivStyle_(container);
	map.getContainer().appendChild(container);
	return container;
}
//Setting control position "roughly" centered
JumpToControls.prototype.getDefaultPosition = function() {
	var q = document.gmap.getSize().width/4;
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(q, 0));
}
// Sets the proper CSS for the given div
JumpToControls.prototype.setDivStyle_ = function(div){
	div.style.height ="35px";
	div.style.width = "290px";
	div.style.border = "1px solid #ccc";
	div.style.borderTop = "none";
	div.style.backgroundColor = "#fff";
	div.style.font = "small Arial";
}

//Real Estate Control - changed layout to vertical and satellite to aerial 
function REMapTypeControl() { }
//Extends GControl() printable = false, selectable = false
REMapTypeControl.prototype = new GControl(false, false);

REMapTypeControl.prototype.initialize = function(map){
	var container = document.createElement("div");
	
	//Normal map type custom div
	var mapDiv = document.createElement("div");
	this.setButtonStyle_(mapDiv);
	container.appendChild(mapDiv);
	mapDiv.appendChild(document.createTextNode("Map"));
	GEvent.addDomListener(mapDiv, "click", function() {
		var x= map.getMapTypes();    	map.setMapType(x[0]); 
	});
	
	//Satellite map type custom div
	var satDiv = document.createElement("div");
	this.setButtonStyle_(satDiv);
	container.appendChild(satDiv);
	satDiv.appendChild(document.createTextNode("Aerial"));
	GEvent.addDomListener(satDiv, "click", function() {
		var x= map.getMapTypes();    	map.setMapType(x[1]); 
	});
	
	//Hybrid map type custom div
	var hyDiv = document.createElement("div");
	this.setButtonStyle_(hyDiv);
	container.appendChild(hyDiv);
	hyDiv.appendChild(document.createTextNode("Hybrid"));
	GEvent.addDomListener(hyDiv, "click", function() {
		var x= map.getMapTypes();    	map.setMapType(x[2]);
	});
	
	map.getContainer().appendChild(container);
	return container;
}

//Setting position top right
REMapTypeControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 7));
}

// Sets the proper CSS for the given input["button"]
REMapTypeControl.prototype.setButtonStyle_ = function(button) {
	button.style.textDecoration = "underline";
	button.style.color = "#093E82";
	button.style.backgroundColor = "#fff";
	button.style.font = ".8em Arial";
	button.style.border = "1px solid #333";
	button.style.padding = "3px";
	button.style.marginBottom = "2px";
	button.style.textAlign = "center";
	button.style.width = "6em";
	button.style.cursor = "pointer";
}

//centerMap takes an address as an argument
//if the point exists, it re-centers the map on that point
function centerMap(address){
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(address, function(point) {		if (!point){			alert("Could not locate the following address\n\t-" + address);		}else{			document.gmap.setCenter(point, 11);		}	});
}

function createIcon(type){
	var icon = new GIcon();	icon.image = type;	icon.iconSize = new GSize(22, 22);	icon.shadowSize = new GSize(22, 22);	icon.iconAnchor = new GPoint(6, 20);	icon.infoWindowAnchor = new GPoint(5, 1);
	return icon;
}

function getMap(){
	return document.gmap;
}	

//loadMap takes a boolean specifying that listeners should be added.
function loadGmap(type){
	//if browser is compatible then create GMap2 object
	if(!GBrowserIsCompatible()){
	   alert("No google maps for you");
	   return;		
	}
	document.gmap = new GMap2(document.getElementById("gmap"));
	document.AerialControl = new REMapTypeControl();
	document.gmap.addControl(document.AerialControl);
	if(type == "search"){
		// add controls to GMap2 object
		document.gmap.addControl(new GLargeMapControl());
		document.gmap.addControl(new GScaleControl());
		/* removed document.JumpControls = new JumpToControls();
		document.gmap.addControl(document.JumpControls);*/
	}else{
		document.gmap.addControl(new GSmallMapControl());
	}
}
