
// extend google map

function geobirds_gmap(element, norbtns) {
	app.use('lib.scriptaculous.builder');
	
	var map = new GMap2(element);
	map.initKMap(norbtns);
	return map;
}



GMap2.BASE_LATITUDE = 42.6;
GMap2.BASE_LONGITUDE = -96.4;
GMap2.BASE_ZOOM = 8;

GMap2.CITY_ZOOM_LEVEL = 10;
GMap2.STATE_ZOOM_LEVEL = 7;
GMap2.COUNTRY_ZOOM_LEVEL = 3;

GMap2.CITY_LABEL = 'City';
GMap2.REGION_LABEL = 'Region';
GMap2.CONTINENT_LABEL = 'Continent';

GMap2.ZOOM_IN_LABEL = 'Zoom in';
GMap2.ZOOM_OUT_LABEL = 'Zoom out';

GMap2.SATELLITE_LABEL = 'Satellite';
GMap2.MAP_LABEL = 'Map';
GMap2.HYBRID_LABEL = 'Hybrid';



GMap2.prototype.initKMap = function(norbtns) {
	var map = this;
	
	if (!norbtns) {
		var jumpbtns = Builder.node('div',
					    {className:"gmnoprint", style: "color: black; font-family: Arial,sans-serif; font-size: small; -moz-user-select: none; position: absolute; right: 7px; top: 7px; width: 195px; height: 22px;" },
					    [
					    this.addButton(1, 12, 2,GMap2.CITY_LABEL, function() {map.setZoom(GMap2.CITY_ZOOM_LEVEL)}),
					    this.addButton(1, 6, 2,GMap2.REGION_LABEL, function() {map.setZoom(GMap2.STATE_ZOOM_LEVEL)}),
					    this.addButton(1, 0, 2,GMap2.CONTINENT_LABEL, function() {map.setZoom(GMap2.COUNTRY_ZOOM_LEVEL)})
					    ]);

		this.getContainer().appendChild(jumpbtns);

		var mtbuttons = Builder.node('div',
					    {className:"gmnoprint", style: "color: black; font-family: Arial,sans-serif; font-size: small; -moz-user-select: none; position: absolute; right: 7px; top: 7px; width: 195px; height: 22px;" },
					    [
					    this.addButton(1, 12, 0,GMap2.SATELLITE_LABEL, function() {map.setMapType(G_SATELLITE_MAP)}),
					    this.addButton(1, 6, 0,GMap2.MAP_LABEL, function() {map.setMapType(G_NORMAL_MAP)}),
					    this.addButton(1, 0, 0,GMap2.HYBRID_LABEL, function() {map.setMapType(G_HYBRID_MAP)})
					    ]);

		this.getContainer().appendChild(mtbuttons);
		
	}
	
	var zoombtns = Builder.node('div',
					{className:"gmnoprint", style: "color: black; font-family: Arial,sans-serif; font-size: small; -moz-user-select: none; position: absolute; left: 7px; top: 7px; width:65px; height: 52px;" },
					    [
					    this.addButton(0, 0, 0, GMap2.ZOOM_IN_LABEL, function() {map.zoomIn()}),
					    this.addButton(0, 0, 2, GMap2.ZOOM_OUT_LABEL, function() {map.zoomOut()})
					    ]);
					    
	this.getContainer().appendChild(zoombtns);					 

	GEvent.addListener(this, "zoomend", function(oldLevel, newLevel) {
		map.refreshButtonStates(newLevel);
	});

	this.addLoadingIcon();
	this.addGeocoder(this);	

}

GMap2.prototype.addButton = function(cn,x,y,label,clickAction,w,h) {
	var cnt = (cn) ? "right" : "left";
	var button = Builder.node('div',
			  {style:"border: 1px solid black; position: absolute; background-color: white; text-align: center; width: 5.5em; cursor: pointer; " + cnt + ": "+x+"em; top:"+y+"em;"},
			  [Builder.node('div',
			 		{className: "mapbutton unselectedbutton", id:'but'+label},
			  		label)
			  ]);

	if (clickAction) button.onclick = clickAction;
	
	return button;
}

GMap2.prototype.refreshButtonStates = function(zoomLevel) {
	document.getElementsByClassName('mapbutton').each(function(node) {
		Element.removeClassName(node,'selectedbutton');
		Element.addClassName(node,'unselectedbutton');
	});

	var label = null;
	if (zoomLevel == GMap2.CITY_ZOOM_LEVEL) label = GMap2.CITY_LABEL;	
	else if (zoomLevel == GMap2.STATE_ZOOM_LEVEL) label = GMap2.REGION_LABEL;
	else if (zoomLevel == GMap2.COUNTRY_ZOOM_LEVEL) label = GMap2.CONTINENT_LABEL;
	
	if (label) {
		Element.removeClassName($('but'+label),'unselectedbutton');
		Element.addClassName($('but'+label),'selectedbutton');
	}	
}

GMap2.prototype.addGeocoder = function(gloc) {	
	var map = this;
		
	var timestamp = new Date().getTime();
	var geocodernode = Builder.node('div',
					{className:"gmnoprint", style: "color: black; font-family: Arial,sans-serif; font-size: small; position: absolute; left: 100px; top: 7px; width: 190px; height: 22px;" },
					    [Builder.node('div',
			  			{style:"border: 1px solid black; position: absolute; background-color: white; text-align: center; cursor: pointer;"},
			  			[Builder.node('div',
			  			 	      {className: "unselectedbutton"},
			  			 	      [Builder.node('input', {type:"text", id:"geotext"+timestamp, size:"17", value:"Enter city or state"}),
	                              	     		       Builder.node('a', {id:"geook"+timestamp, href:"#"}, 'Go')
	                              	     		      ])
					    	])
					    ]);
	   
	this.getContainer().appendChild(geocodernode); 
	
//	this.addButton('l', 120, 7, [buttonText], null, 146, 24);

//	map.geocoder.onStart = function() { Element.show(map.iconnode); }
//	map.geocoder.onEnd = function() { Element.hide(map.iconnode); }

	$('geotext'+timestamp).select();	
	$('geotext'+timestamp).onkeydown = function(event) {
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (event) keycode = event.which;

		if (keycode == 13) this.doGeocode($('geotext'+timestamp).value);

	}.bind(this);
	$('geook'+timestamp).onclick = function() { this.doGeocode($('geotext'+timestamp).value); return false;}.bind(this);
}

GMap2.prototype.addLoadingIcon = function() {
	this.iconnode = Builder.node('div', {style: 'position:absolute; top:10px; left:234px; z-index:5; display:none;'},
				   [Builder.node('img', {src:"/images/loading.gif"}) ]); 
	this.getContainer().appendChild(this.iconnode);
}

GMap2.prototype.doGeocode = function(text, onSuccess, onFailure)
{
	if (!this.geocoder) this.geocoder = new GClientGeocoder();

	this.geocoder.getLocations(text, function(response)
	{
		if (!response || response.Status.code != 200) 
		{
			alert('Location not found!  Try to be more specific or add state / province / country.');
			if (onFailure) onFailure();
		}
		else 
		{
			if (onSuccess || this.onSuccess)
			{

				place = response.Placemark[0];
				
				var locstr = '', country = '', state = '', city = '';
				if (place.AddressDetails && place.AddressDetails.Country)
				{
					var c_node = place.AddressDetails.Country;
					country = c_node.CountryNameCode;
					if (c_node.AdministrativeArea)
					{
						var s_node = c_node.AdministrativeArea;
						state = s_node.AdministrativeAreaName;
						if (s_node.SubAdministrativeArea)
						{
							var a_node = s_node.SubAdministrativeArea;
							city = a_node.SubAdministrativeAreaName;
							if (a_node.Locality)
							{
								city = a_node.Locality.LocalityName;
							}
						}
						else if (s_node.Locality)
						{
							city = s_node.Locality.LocalityName;
						}
					}
					else if (c_node.Locality)
					{
						city = c_node.Locality.LocalityName;
					}
				}
/*				
				if (city && state) locstr = city + ', ' + state;
				else if (state && country) locstr = state + ', ' + country;
				else if (country) locstr = country;
*/
				locstr = place.address;
								
				if (onSuccess) onSuccess(place.Point.coordinates[0], place.Point.coordinates[1],locstr,city,state,country);
				else if (this.onSuccess) this.onSuccess(place.Point.coordinates[0], place.Point.coordinates[1],locstr,city,state,country);
				
			}
			else
			{
				place = response.Placemark[0];
				this.setCenter(new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]));
			}	
		}	
	}.bind(this))
	
}





	


