
var thegeocoder;

function Geocoder(map,loadElement)  {
	this.map = map;
	this.loadingElement = loadElement;
	this.caarr = [];
	this.usarr = [];
	this.options_dialog = null;
	this._semaphore = 0;
	thegeocoder = this;
}

Geocoder.prototype.start = function() {
	this._semaphore++;

	Element.show(this.loadingElement);
}

Geocoder.prototype.finish = function() {

	this._semaphore--;
	if (this._semaphore == 0) {

		Element.hide(this.loadingElement);
//		this.populate();
	}
	if (this._semaphore < 0) this._semaphore = 0;
}

Geocoder.prototype.goGeocoder = function(area) {
//	this.goUSGeocoder(area);
	this.goCaGeocoder(area);
}

Geocoder.prototype.goCaGeocoder = function(area) {
	var url = "/geocoder/geonames.php?q=" + area;
	this.start();

	var myAjax = new Ajax.Request(
		url,
		{
			method: 'get',			
			onComplete: gotCaPlaces
		});
}

Geocoder.prototype.goUSGeocoder = function(area) {
	var url = "/geocoder/yahoogeocoder.php?q=" + area;
	this.start();
	
	var myAjax = new Ajax.Request(
		url,
		{
			method: 'get',
			onComplete: gotUSPlaces
		});
}

Geocoder.prototype.handleJSON = function(request) {
	var obj = eval('('+request.responseText+')');
	this.populateWithJSON(obj);
}

Geocoder.prototype.populateWithJSON = function(obj) {
	var arr = obj.geonames;

	if (arr.length > 1) {
		this.show_options(this.buildJSONPlacesList(arr));
		}
	else if (arr.length == 1) { this.goto(arr[0].lng,arr[0].lat); }
}

Geocoder.prototype.buildJSONPlacesList = function(arr) {
	var str = '';
	var i = 1;
	arr.each(function(node) {
		str += i + '. <a href="#" onclick="javascript:thegeocoder.goto('+ node.lng + ',' + node.lat + ')" >';
		str += node.name + ', ';
		if (node.adminName1) str += node.adminName1 + ', ';
		if (node.countryName) str += node.countryName;
		str += '</a><br />';
		i++;
	});
	return str;
}


Geocoder.prototype.parseXML = function(request) {
	var arr = [];

	var xmlDoc = GXml.parse(request.responseText);
	
	if (request.responseText.match(/\w/)) {
		var results = (isIE) ? xmlDoc.childNodes[1].childNodes : xmlDoc.firstChild.childNodes;
		for (var i = 0; i < results.length; i++) {
			var obj = {};
			for (var j = 0; j < results[i].childNodes.length; j++) {
				var node = results[i].childNodes[j];
				if (node.firstChild) {
					obj[node.tagName] = node.firstChild.nodeValue;
				} else {
					obj[node.tagName] = '';
				}
			}
			arr.push(obj);
		}
	}
	return arr;
}

Geocoder.prototype.populate = function() {
	var newarr = this.caarr.concat(this.usarr);

	if (newarr.length > 1) {
		this.show_options(this.buildPlacesList(newarr));
		}
	else if (newarr.length == 1) { this.goto(newarr[0].Longitude,newarr[0].Latitude); }
}

Geocoder.prototype.buildPlacesList = function(arr) {
	var str = '';
	var i = 1;
	arr.each(function(node) {
		str += i + '. <a href="#" onclick="javascript:thegeocoder.goto('+ node.Longitude + ',' + node.Latitude + ')" >';
		str += node.City + ', ';
		if (node.State) str += node.State + ', ';
		if (node.Country == 'CA') str += 'CANADA';
		else str += node.Country;
		str += '</a><br />';
		i++;
	});
	return str;
}


Geocoder.prototype.goto = function(lng,lat) {
	if (this.options_dialog) this.close_dialog();

	$('area').innerHTML = lng + ',' + lat;
	
	this.map.panTo(new GLatLng(lat,lng));
}


Geocoder.prototype.show_options = function(text) {
	this.options_dialog = GeoBirdsBasicVeilDialog('<div style="height:200px;overflow:auto;">'+text+'</div>',
		'Choose from these options','VeilStyleDark');
}

Geocoder.prototype.close_dialog = function() {
	this.options_dialog.hide();
}


function gotCaPlaces(request) {	
//	thegeocoder.caarr = thegeocoder.parseXML(request);
	thegeocoder.handleJSON(request);
	thegeocoder.finish();
}

function gotUSPlaces(request) {
	thegeocoder.usarr = thegeocoder.parseXML(request);
	thegeocoder.finish();
}