var __geocoder_view__ = null;

function __cbGeocoder__(response) {
	__geocoder_view__.removeAll();
	switch (response.Status.code) {
		case G_GEO_SUCCESS :
			var nresults = response.Placemark.length;
			if (nresults > 1) {
				__geocoder_view__.cellNResults.innerHTML = 'Trovati ' + nresults + ' indirizzi';
			} else {
				__geocoder_view__.cellNResults.innerHTML = 'Trovato un indirizzo';
			}
			var place = null;
			var admarea = null;
			var subadm = null;
			var country = null;
			var locality = null;
			var streetAddress = '';
			var city = '';
			var state = '';
			var regione = '';
			var zip = '';
			var str = '';
			for (var i = 0; i < nresults; i++) {
				place = response.Placemark[i];
				streetAddress = '';
				city = '';
				state = '';
				zip = '';
				str = '';
				if (place.AddressDetails.Country != null) {
					country = place.AddressDetails.Country;
					state = country.CountryNameCode;
					if (country.AdministrativeArea != null) {
						admarea = place.AddressDetails.Country.AdministrativeArea;
						regione = admarea.AdministrativeAreaName;
						if (regione != '') {
							regione = '(' + regione + ')';
						}
						if (admarea.SubAdministrativeArea != null) {
							subadm = admarea.SubAdministrativeArea;
							city = subadm.SubAdministrativeAreaName;
							if (subadm.Locality != null) {
								locality = subadm.Locality;
								city = locality.LocalityName;
								if (locality.Thoroughfare != null) {
									streetAddress = locality.Thoroughfare.ThoroughfareName;
								}
								if (locality.PostalCode != null) {
									zip = locality.PostalCode.PostalCodeNumber;
								}
							}
						}
					}
				}
				var goZoom = 15;
				if (place.AddressDetails.Accuracy != null) {
					if (place.AddressDetails.Accuracy < 3) {
						goZoom = 15;
					}
					if ((place.AddressDetails.Accuracy < 4) && (place.AddressDetails.Accuracy > 2)) {
						goZoom = 15;
					}
					if ((place.AddressDetails.Accuracy > 3) && (place.AddressDetails.Accuracy < 6)) {
						goZoom = 15;
					}
					if (place.AddressDetails.Accuracy > 5) {
						goZoom = 15;
					}
				}
				if ((streetAddress != '') || (zip != '') || (city != '') || (regione != '')) {
					str = '<b>' + streetAddress + '</b><br>' + zip + ' ' + city + ' ' + regione + '<br>' + state;
				} else {
					str = '<b>' + place.address + '</b><br>';
				}
				__geocoder_view__.addItem(i + 1, str, new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]), goZoom);
			}
			break;
		case G_GEO_UNKNOWN_ADDRESS :
			__geocoder_view__.cellNResults.innerHTML = 'Indirizzo non trovato';
			break;
		default :
			__geocoder_view__.cellNResults.innerHTML = 'Impossibile processare l\'indirizzo';
	}
}

function GeocoderView(containerId, container, mapObject) {
	__geocoder_view__ = this;
	this.mapObject = mapObject;
	this.numresults = 0;
	this.HilightColor = '#FFFF00';
	this.BackgroundColor = 'transparent';
	this.CellBackgroundColor = '#F0E8E8';
	this.titleFont = 'Verdana, Arial, Helvetica, sans-serif';
	this.titleFontColor = '#FFFFFF';
	this.titleFontBold = 'normal';
	this.titleFontSize = '8pt';
	this.font = 'Verdana, Arial, Helvetica, sans-serif';
	this.fontColor = '#FFFFFF';
	this.fontBold = 'normal';
	this.fontSize = '8pt';
	this.SelectedIndex = -1;
	this.onItemClick = null;
	this.Box = document.createElement('DIV');
	this.Box.style.position = 'relative';
	this.Box.style.top = 0;
	this.Box.style.left = 0;
	this.Box.style.width = '99%';
	this.Box.style.height = '100%';
	this.Box.style.border = '1px inset silver';
	this.Box.style.overflow = 'auto';
	this.Box.style.fontSize = '9pt';
	this.Box.style.backgroundColor = this.BackgroundColor;
	this.Box.style.textAlign = 'left';
	if (container == null) {
		if (containerId != null) {
			container = document.getElementById(containerId);
		}
	}
	if (container == null) {
		container = document.body;
	}
	container.appendChild(this.Box);
	this.Table = document.createElement('TABLE');
	this.TableBody = document.createElement("TBODY");
	this.Table.appendChild(this.TableBody);
	this.Box.appendChild(this.Table);
	this.Table.border = 0;
	this.Table.bgColor = 'transparent';
	this.Table.style.position = 'relative';
	this.Table.style.margin = 0;
	this.Table.style.padding = 0;
	this.Table.style.backgroundColor = 'transparent';
	
	var oRow = document.createElement('TR');
	this.cellRicerca = document.createElement('TD');
	this.cellRicerca.colSpan = 2;
	this.cellRicerca.vAlign = 'middle';
	with (this.cellRicerca.style) {
		backgroundColor = 'transparent';
		margin = '0px';
		padding = '0px';
		border = '0px solid black';
		textAlign = 'left';
		verticalAlign = 'middle';
	}
	oRow.appendChild(this.cellRicerca);
	this.TableBody.appendChild(oRow);
	
	this.txtCerca = document.createElement('INPUT');
	this.txtCerca.type = 'text';
	this.txtCerca.style.border = '1px solid black';
	this.txtCerca.style.fontSize = '8pt';
	this.txtCerca.style.width = '180px';
	this.txtCerca.style.height = '18px';
	this.txtCerca.style.backgroundColor = '#D0D0D0';
	this.cellRicerca.appendChild(this.txtCerca);
	this.btnCerca = document.createElement('INPUT');
	this.btnCerca.type = 'submit';
	this.btnCerca.value = ' trova ';
	this.btnCerca.style.fontSize = '8pt';
	this.btnCerca.style.width = '71px';
	this.btnCerca.style.height = '18px';
	this.btnCerca.style.border = '1px solid black';
	this.cellRicerca.appendChild(this.btnCerca);
	this.btnCerca.owner = this;
	this.cellRicerca.appendChild(this.btnCerca);
	this.btnCerca.owner = this;
	GEvent.bindDom(this.btnCerca, "click", this, this.ricerca);
	
	var oRow = document.createElement('TR');
	this.cellNResults = document.createElement('TD');
	this.cellNResults.style.border = '0px solid black';
	this.cellNResults.style.backgroundColor = this.BackgroundColor;
	this.cellNResults.colSpan = 2;
	oRow.appendChild(this.cellNResults);
	this.TableBody.appendChild(oRow);
	this.allItems = new Array();
}


GeocoderView.prototype.ricerca = function(ev, obj) {
	if (obj != null) {
		var sSearch = this.txtCerca.value;
		if (sSearch != '') {
			this.mapObject.geocoder.getLocations(sSearch, __cbGeocoder__);
		}
	}
}

GeocoderView.prototype.resultitemClick = function(ev, obj) {
	if (obj != null) {
		var sId = obj.id;
		var sElement = sId.substr(0, 4);
		var iNodeId = parseInt(sId.substr(4));
		this.selectItem(iNodeId);
	}
}

GeocoderView.prototype.SetVisible = function(bVisible) {
	if (bVisible) {
		this.Box.style.display = 'block';
	} else {
		this.Box.style.display = 'none';
	}
}

GeocoderView.prototype.SetHeight = function(iHeight) {
	this.Box.style.height = iHeight; // + 'px';
}

GeocoderView.prototype.SetWidth = function(iWidth) {
	this.Box.style.width = iWidth; // + 'px';
}

GeocoderView.prototype.GetDOMContainer = function() {
	return this.Box;
}

GeocoderView.prototype.SetBackgroundColor = function(bgcol) {
	this.BackgroundColor = bgcol;
	this.Box.style.backgroundColor = this.BackgroundColor;
	this.Table.bgColor = this.BackgroundColor;
	this.cellNResults.style.backgroundColor = this.BackgroundColor;
}
GeocoderView.prototype.SetCellBackgroundColor = function(bgcol) {
	this.CellBackgroundColor = bgcol;
}

GeocoderView.prototype.SetHilightColor = function(hlcol) {
	this.HilightColor = hlcol;
}

GeocoderView.prototype.SetFont = function(sFont) {
	this.font = sFont;
	this.cellNResults.style.fontFamily = sFont;
}
GeocoderView.prototype.SetFontColor = function(sFontCol) {
	this.fontColor = sFontCol;
	this.cellNResults.style.color = this.fontColor;
}
GeocoderView.prototype.SetFontBold = function(bBold) {
	if (bBold) {
		this.fontBold = 'bold';
	} else {
		this.fontBold = 'normal'; 
	}
	this.cellNResults.style.fontWeight = this.fontBold;
}
GeocoderView.prototype.SetFontSize = function(iFontSize) {
	this.fontSize = iFontSize + 'pt';
	this.cellNResults.style.fontSize = this.fontSize;
}
GeocoderView.prototype.SetTitleFont = function(sFont) {
	this.titleFont = sFont;
}
GeocoderView.prototype.SetTitleFontColor = function(sFontCol) {
	this.titleFontColor = sFontCol;
	this.cellNResults.style.color = this.titleFontColor;
}
GeocoderView.prototype.SetTitleFontBold = function(bBold) {
	if (bBold) {
		this.titleFontBold = 'bold';
	} else {
		this.titleFontBold = 'normal';
	}
}
GeocoderView.prototype.SetTitleFontSize = function(iFontSize) {
	this.titleFontSize = iFontSize + 'pt';
}
GeocoderView.prototype.setBorder = function(sBorderStyle) {
	this.Box.style.border = sBorderStyle;
}

GeocoderView.prototype.addItem = function(iOrd, sLabel, oPoint, iZoom) {
	var iId = parseInt(iOrd);
	var newItem = new GeoItem(this, iOrd, sLabel, oPoint, iZoom);
	if (newItem != null) {
		this.allItems[iId] = newItem;
		this.TableBody.appendChild(newItem.oRow);
	}
	return newItem;
}

GeocoderView.prototype.selectItem = function(sId) {
	var iId = parseInt(sId);
	if (this.SelectedIndex != iId) {
		if (this.SelectedIndex > 0) {
			this.allItems[this.SelectedIndex].Select(false);
		}
		this.SelectedIndex = iId;
		this.allItems[iId].Select(true);
	}
	this.mapObject.map.setCenter(this.allItems[this.SelectedIndex].oPoint, this.allItems[this.SelectedIndex].iZoom);
}


GeocoderView.prototype.removeItem = function(vId) {
	var iId = parseInt(vId);
	var oItem = this.allItems[iId];
	this.TableBody.removeChild(oItem.oRow);
	this.allItems[iId] = null;
	if (this.SelectedIndex != -1) {
		this.SelectedIndex = -1;
	}
}

GeocoderView.prototype.removeAll = function() {
	for (var i = 0; i < this.allItems.length; i++) {
		if (this.allItems[i]) {
			this.removeItem(i);
		}
	}
}



function GeoItem(oContainer, iOrd, sLabel, oPoint, iZoom) {
	this.Selected = false;
	this.cont = oContainer;
	this.id = iOrd;
	this.label = sLabel;
	this.oRow = document.createElement('TR');
	this.oRow.style.backgroundColor = this.cont.CellBackgroundColor;
	this.oRow.style.cursor = 'pointer';
	this.oCell0 = document.createElement('TD');
	this.oCell1 = document.createElement('TD');
	this.oText = document.createElement('SPAN');

	this.oCell0.style.verticalAlign = 'top';
	this.oCell0.style.color = this.cont.titleFontColor;
	this.oCell0.style.fontFamily = this.cont.titleFont;
	this.oCell0.style.fontSize = this.cont.titleFontSize;
	this.oCell0.style.fontWeight = 'bold';
	this.oCell0.style.border = '0px solid #A0A0A0';
	this.oCell0.innerHTML = iOrd;
	
	this.oCell1.style.verticalAlign = 'top';
	this.oCell1.style.border = '0px solid #A0A0A0';
	
	this.oText.style.color = this.cont.fontColor;
	this.oText.style.fontFamily = this.cont.font;
	this.oText.style.fontSize = this.cont.fontSize;
	this.oText.style.fontWeight = this.cont.fontBold;
	this.oText.innerHTML = sLabel;
	this.oText.id = 'text' + iOrd;
	GEvent.bindDom(this.oText, "click", this.cont, this.cont.resultitemClick);
	
	this.oRow.appendChild(this.oCell0);
	this.oRow.appendChild(this.oCell1);
	this.oCell1.appendChild(this.oText);
	this.oPoint = oPoint;
	this.iZoom = iZoom;
}

GeoItem.prototype.Select = function(isSelected) {
	if (isSelected) {
		this.oRow.style.backgroundColor = this.cont.HilightColor;
		this.Selected = true;
	} else {
		this.oRow.style.backgroundColor = this.cont.CellBackgroundColor;
		this.Selected = false;
	}
}

