// REQUIRES THAT GOOGLEMAPS HAS BEEN LOADED.

var div_width = 220;

function ZoomBox( manager, map, load_fn ) {
	this.label = 'Loading coordinates';
	this.load_fn = load_fn;
	this.load_time = -1;
	this.manager = manager;
	this.map = map;
	this.window_open = false;
	this.zoom_loc;
	this.zoom_tit;
	
	//
	// all the fun stuff to open a DOM box.
	//
	ZoomBox.prototype.openZoom = function() {
		if ( this.window_open ) return;
		
		var z_div = document.createElement('div');
		var z_div_label = document.createElement('div');
		var z_div_content = document.createElement('div');
		z_div.setAttribute('class','window_content');
		z_div.className = 'window_content';
		z_div_label.setAttribute('class','window_content');
		z_div_label.className = 'window_content';
		z_div_content.setAttribute('class','window_content');
		z_div_content.className = 'window_content';
		
		var t_box = document.createElement('input');
		var b_zoom = document.createElement('input');
		t_box.setAttribute('type','text');
		b_zoom.setAttribute('type','button');
		b_zoom.setAttribute('value','zoom');
		b_zoom.onclick = this.makeSubmitClick();
		
		z_div_content.appendChild(t_box);
		z_div_content.appendChild(b_zoom);
		this.zoom_loc = t_box;
		this.zoom_tit = document.createElement('span');
		this.zoom_tit.innerHTML = 'City, State:';
		z_div_label.appendChild(this.zoom_tit);
		
		//z_div_label.style.width = (div_width-5)+'px';
		//z_div_content.style.width = (div_width-5)+'px';
		z_div.style.width = div_width+'px';
		z_div.appendChild(z_div_label);
		z_div.appendChild(z_div_content);
		this.close =
			this.manager.makeCloseWindow(this.manager.openWindow(z_div,'quick zoom',this.makeCloseAction()),this.makeCloseAction());
		t_box.onkeypress = this.makeSubmitter();
		
		t_box.focus();
		this.window_open = true;
	};
	
	ZoomBox.prototype.makeSubmitter = function() {
		var n_this = this;
		return function( event ) {
			if ( !event ) event = window.event;
			
			if ( event.keyCode == 13 ) n_this.zoom();
		};
	};
	
	ZoomBox.prototype.makeSubmitClick = function() {
		var n_this = this;
		return function( event ) {
			n_this.zoom();
		};
	};
	
	ZoomBox.prototype.zoom = function() {
		//this.zoom_tit.removeChild(this.zoom_tit.childNodes[0]);
		//this.zoom_tit.appendChild(document.createTextNode('Loading...'));
		var f_this = this;
		this.zoom_loc.style.backgroundColor = '#eee';
		this.zoom_loc.onkeypress = function() {return false;};
		this.load_time = 0;
		this.timeLoad();
		
		var success_fn = function( responseXML, responseText ) {
			var c = responseXML.getElementsByTagName('center');
			
			if ( c.length != 1 ) {
				f_this.stopLoad();
				
				if ( c.length == 0 )
					f_this.zoom_tit.innerHTML = '"'+f_this.zoom_loc.value+'" not found. Try another city.';
				else
					f_this.zoom_tit.innerHTML = 'Multiple results for "'+cs+'". Try another city.';
					
				f_this.unlockInput();
			} else {
				f_this.map.centerAndZoom(new GPoint(c[0].getAttribute('lng'),c[0].getAttribute('lat')),6);
				
				// now call the success function!
				if ( f_this.load_fn )
					f_this.load_fn();
					
				f_this.close();
				f_this.stopLoad();
			}
		}
		
		var ajax_request = new ajax('/xml/geocode.php?loc='+this.zoom_loc.value,success_fn,null);
		ajax_request.connect();
	};
	
	ZoomBox.prototype.makeCloseAction = function() {
		var n_this = this;
		return function() {
			n_this.window_open = false;
		};
	};
	
	ZoomBox.prototype.timeLoad = function() {
		if ( this.load_time < 0 ) return;
		
		//var time = this.load_time/10;
		//if ( time == parseInt(time) ) time += '.0';
		
		//this.zoom_tit.innerHTML = this.label+' '+time+'s';
		var dots = '';
		for ( var i = 0; i < (this.load_time/2)%3+1; ++i )
			dots += '.';
		this.zoom_tit.innerHTML = this.label + dots;
		this.load_time += 1;
		var n_this = this;
		
		setTimeout(function() {
			n_this.timeLoad();
		},100);
	};
	
	ZoomBox.prototype.stopLoad = function() {
		this.load_time = -1;
	};
	
	ZoomBox.prototype.unlockInput = function() {
		this.zoom_loc.style.backgroundColor = '#fff';
		this.zoom_loc.onkeypress = this.makeSubmitter();
	};
}

