
function GSGeocoder(params){this.map=GSUtil.getCaseInsensitiveProperty(params,"map");this.map.centerOnDblClick=false;this.resultContainer=GSUtil.getCaseInsensitiveProperty(params,"resultContainer");this.noResultsHtml=GSUtil.getCaseInsensitiveProperty(params,"noResultsHtml");this.onGeocodeAccepted=GSUtil.getCaseInsensitiveProperty(params,"onGeocodeAccepted");this.onResults=GSUtil.getCaseInsensitiveProperty(params,"onResults");this.onResultClicked=GSUtil.getCaseInsensitiveProperty(params,"onResultClicked");this.result=[];this.mapLayer=this.map.createLayer("geocoder");this.map.addListener(this);};GSGeocoder.ADDRESS_WS_BASEURL="http://www.geosmartlbs.co.nz/smartfind/address-0.3";GSGeocoder.COMMON_BASEURL="http://www.geosmartlbs.co.nz/smartfind/common-0.1";GSGeocoder.prototype.find=function(address,limit){var number=parseInt(address.split(" ")[0]);var params={"address":address};if(limit){params.limit=limit;}
if(number>0){GSJsonServiceRequest.send(GSGeocoder.ADDRESS_WS_BASEURL+"/address.php",params,this,this.handleAddressList);}else{GSJsonServiceRequest.send(GSGeocoder.ADDRESS_WS_BASEURL+"/street.php",params,this,this.handleStreetList);}};GSGeocoder.prototype.handleStreetList=function(jsonArray){this.result=jsonArray;if(jsonArray.length==1){this.getAddressList(jsonArray[0].id);return;}
var html=document.createElement('div');var odd=true;var cssClass="";var len=jsonArray.length;if(len>0){for(var i=0;i<len;i++){var poi=jsonArray[i];cssClass=odd?"result-odd":"result-even";odd=!odd;var t=document.createElement('div');t.className=cssClass;var d=document.createElement('div');t.appendChild(d);var l=document.createElement('a');d.appendChild(l);l.href='javascript:void(0)';l.innerHTML=poi.name;l.onclick=this.getAddressListFunction(poi.id);l=document.createElement('a');d=document.createElement('div');t.appendChild(d);d.innerHTML=poi.suburb+', '+poi.region;html.appendChild(t);}}else{if(this.noResultsHtml){html=this.noResultsHtml;}}
this.resultContainer.innerHTML='';this.resultContainer.appendChild(html);if(this.onResults){this.onResults("street",jsonArray);}};GSGeocoder.prototype.getAddressListFunction=function(id){var geocoder=this;return function(){geocoder.getAddressList(id);}};GSGeocoder.prototype.handleAddressList=function(jsonArray){this.result=jsonArray;var html=document.createElement('div');var odd=true;var cssClass="";var len=jsonArray.length;var geocoder=this;if(len>0){for(var i=0;i<len;i++){var poi=jsonArray[i];cssClass=odd?"result-odd":"result-even";odd=!odd;var t=document.createElement('div');t.className=cssClass;var d=document.createElement('div');t.appendChild(d);var l=document.createElement('a');d.appendChild(l);l.href='javascript:void(0)';l.innerHTML=poi.number+', '+poi.name;l.onclick=this.getDisplayAddressOnMapFunction(poi.id);l=document.createElement('a');d=document.createElement('div');t.appendChild(d);d.innerHTML=poi.suburb+', '+poi.region;html.appendChild(t);}}else{if(this.noResultsHtml){html=this.noResultsHtml;}}
this.resultContainer.innerHTML='';this.resultContainer.appendChild(html);if(this.onResults){this.onResults("address",jsonArray);}};GSGeocoder.prototype.getDisplayAddressOnMapFunction=function(id){var geocoder=this;return function(){geocoder.displayAddressOnMap(id);}};GSGeocoder.prototype.handleLocalityRequest=function(data){var address={x:this.selectedPoint.x,y:this.selectedPoint.y,suburb:data.suburb,district:data.district,region:data.region,label:this.selectedPoint.label};this.onGeocodeAccepted(address);this.map.closeInfoWindow();var feature=new GSPointFeature({coordinate:new GSPoint(this.selectedPoint.x,this.selectedPoint.y),name:this.selectedPoint.label});this.mapLayer.addFeature(feature);};GSGeocoder.prototype.getAddressList=function(id){var params={streetId:id};GSJsonServiceRequest.send(GSGeocoder.ADDRESS_WS_BASEURL+"/addressOnStreet.php",params,this,this.handleAddressList);if(this.onResultClicked){this.onResultClicked(id);}};GSGeocoder.acceptAddressPopupHtml='<div>{number} {name}</div><div>{suburb}, {region}</div>';GSGeocoder.acceptAddressPopupHtml+='<a href="javascript:{thisRef}.acceptAddress({id})">Accept</a> | ';GSGeocoder.acceptAddressPopupHtml+='<a href="javascript:{mapRef}.closeInfoWindow()">Cancel</a>';GSGeocoder.prototype.displayAddressOnMap=function(id){var geocoder=this;this.mapLayer.clear();var address=this.getAddress(id);var coordinate=new GSPoint(address.x,address.y);var html=document.createElement('div');var d=document.createElement('div');d.innerHTML=address.number+' '+address.name;html.appendChild(d);d=document.createElement('div');d.innerHTML=address.suburb+', '+address.region;html.appendChild(d);var l=document.createElement('a');html.appendChild(l);l.href='javascript:void(0)';l.onclick=function(){geocoder.acceptAddress(id);};l.innerHTML='Accept';html.appendChild(document.createTextNode(' | '));l=document.createElement('a');html.appendChild(l);l.href='javascript:void(0)';l.onclick=function(){geocoder.map.closeInfoWindow();};l.innerHTML='Cancel';this.map.centerAndZoom(coordinate,0);this.map.infoWindow.open(coordinate,html,new GSPoint(0,0));if(this.onResultClicked){this.onResultClicked(id);}};GSGeocoder.prototype.getAddress=function(id){var len=this.result.length;for(var i=0;i<len;i++){if(this.result[i].id==id){return this.result[i];}}
throw Error("Missing address id: "+id);};GSGeocoder.prototype.mapDblClicked=function(map,coordinate){this.displayPointOnMap(map,coordinate,null,true);};GSGeocoder.prototype.displayPointOnMap=function(map,coordinate,name,edit){this.mapLayer.clear();var content=document.createElement("div");var field=null;if(edit){var label=document.createElement("div");label.innerHTML="Name this place:";content.appendChild(label);field=document.createElement("input");field.style.width="160px";field.style.margin="5px 0 5px 0";field.name="name";field.type="text";if(name){field.value=name;}
content.appendChild(field);}else{content.appendChild(document.createTextNode(name));}
content.appendChild(document.createElement("br"));var link=document.createElement("a");link.appendChild(document.createTextNode("Accept"));link.setAttribute("href","#");content.appendChild(link);GSEventManager.bind(link,"click",this,function(){this.acceptManual(coordinate.x,coordinate.y,field.value);});content.appendChild(document.createTextNode(" | "));link=document.createElement("a");link.appendChild(document.createTextNode("Cancel"));link.setAttribute("href","#");content.appendChild(link);this.map.infoWindow.open(coordinate,content,new GSPoint(0,0));GSEventManager.bind(link,"click",this,function(){map.closeInfoWindow();});};GSGeocoder.prototype.acceptAddress=function(id){var params={};params.uid=_globals.clientId;params.category="geocode-address";params.message=id;GSJsonServiceRequest.send(GSGeocoder.COMMON_BASEURL+"/log.php",params,this,function(){});var address=this.getAddress(id);address.label=address.number+" "+address.name;this.onGeocodeAccepted(address);this.map.closeInfoWindow();var feature=new GSPointFeature({coordinate:new GSPoint(address.x,address.y),name:address.label});this.mapLayer.addFeature(feature);};GSGeocoder.prototype.acceptManual=function(x,y,label){var params={};params.uid=_globals.clientId;params.category="geocode-point";params.message=x+":"+y;GSJsonServiceRequest.send(GSGeocoder.COMMON_BASEURL+"/log.php",params,this,function(){});params={};params.x=x;params.y=y;this.selectedPoint={};this.selectedPoint.x=x;this.selectedPoint.y=y;this.selectedPoint.label=label;GSJsonServiceRequest.send("http://www.geosmartlbs.co.nz/smartfind/address-0.6/getlocality.asp",params,this,this.handleLocalityRequest);};GSGeocoder.prototype.registerPoi=function(poi,func){var params={};params.poiId=poi.id;params.source=_globals.clientId;params.delta=JSON.stringify(poi);GSJsonServiceRequest.send(_globals.POI_MANAGER_WS_BASEURL+"/add.php",params,this,func);};