/////////////////////////////////////////////
//
// geocoder.js
// enables HTML to access geocoding.jp using JSON
// (C) TEDDY-G 2005
//
// 05/10/17 add Callback Function
// 05/10/18 add geocoderCsis
//
/////////////////////////////////////////////
var geocoderjpAPI="http://www.got2do.com/api/proxyJSON.php?uri_xml=http://www.geocoding.jp/api/&q=";
var geocodingCsisAPI='http://www.got2do.com/api/proxyJSON.php?uri_xml=http://saya.s145.xrea.com/x/geocode.cgi&mode=TKY&addr=';
var geoURI='';
var __gmap=false;
var now=new Date();
var __count=now.getYear()*3600*24*31*12+now.getMonth()*3600*24*31+now.getDate()*3600*24+now.getHours()*3600+now.getMinutes()*60+now.getSeconds()-5;
var __count_old=__count;
var __loadId="message";
var __loadMes="Loading...";
var __alertId="message";
var __funcSuccess=null;
var __funcFault=null;
var __service='geocodingjp';

/////////////////////////////////////////////
//
// moveMap(JSON)
// handle GMap according to JSON output
//
/////////////////////////////////////////////
var moveMap = function(arrayGeo){
	var html='';
	getComponentById(__loadId).innerHTML='';
	if (typeof arrayGeo.choices!="undefined") {
		html+='検索結果：' + arrayGeo.choices.choice.length + '<br/>';
		for (var i=0;i<arrayGeo.choices.choice.length;i++) {
			var choice=arrayGeo.choices.choice[i];
			html+='<a href="#" onClick="keyToGmap(\''+choice+'\')">'+choice+'</a><br>';
		}
		//document.getElementById(__alertId).innerHTML=html;
		getComponentById(__alertId).innerHTML=html;
	} else {
		if (geocoderErr(arrayGeo)==false) {
		//try {
			var _lat=(__service=="geocodingCsis")?arrayGeo.y:arrayGeo.coordinate.lat;
			var _long=(__service=="geocodingCsis")?arrayGeo.x:arrayGeo.coordinate.lng;
			//document.getElementById(__alertId).innerHTML= '('+_lat + ',' +_long+')';
			//getComponentById(__alertId).innerHTML= '検索結果：('+_lat + ',' +_long+')';
			if (__funcSuccess!=null) {
				__funcSuccess(_lat,_long,__alertId);
			}
		} else {
		//}catch(e) {
			//document.getElementById(__alertId).innerHTML=arrayGeo;
			//getComponentById(__alertId).innerHTML='検索結果：0';
			if (__funcFault!=null) {
				__funcFault(__alertId);
			}
		}
	}

	if (__gmap!=false) {
		_center=__gmap.getCenterLatLng();
		if (typeof _long=="undefined") {_long=_center.x;}
		if (typeof _lat=="undefined") {_lat=_center.y;}
		__gmap.centerAndZoom(new GPoint(parseFloat(_long), parseFloat(_lat)), __gmap.getZoomLevel());
		//__gmap.recenterOrPanToLatLng(new GPoint(parseFloat(_long), parseFloat(_lat)));

	}
}

function geocoderErr(arrayGeo) {
	return (__service=="geocodingCsis")?(arrayGeo.x=='fX'):(arrayGeo.error!=null);
}
/////////////////////////////////////////////
//
// handleGmap(gmap,loadId,loadMes,alertId,funcSuccess,funcFault,service)
// initialize data used for library
//
/////////////////////////////////////////////
function handleGmap(gmap,loadId,loadMes,alertId,funcSuccess,funcFault,service) {
	if (gmap!=null) 	{__gmap=gmap;}
	if (loadId!=null) 	{__loadId=loadId;}
	if (loadMes!=null) 	{__loadMes=loadMes;}
	if (alertId!=null) 	{__alertId=alertId;}
	if (funcSuccess!=null) {__funcSuccess=funcSuccess;}
	if (funcFault!=null){__funcFault=funcFault;}
	if (service!=null) 	{__service=service;}
}

/////////////////////////////////////////////
//
// keyToGmap(keyword)
// generate URI of got2do proxy from keyword
//
/////////////////////////////////////////////
function keyToGmap(keyword) {
	// check interval
	var now=new Date();
	__count_old=__count;
	__count=now.getYear()*3600*24*31*12+now.getMonth()*3600*24*31+now.getDate()*3600*24+now.getHours()*3600+now.getMinutes()*60+now.getSeconds();
	var wait=0;
	if ((__service!="geocodingCsis")&&(__count-__count_old<5)) {
		//alert("Do not submit the keyword within 5 seconds.");
		//document.getElementById(__loadId).innerHTML='Waiting...';
		getComponentById(__loadId).innerHTML='Waiting...';
		wait=(5-(__count-__count_old))*1000;
	};
	//document.getElementById(__loadId).innerHTML=__loadMes;
	geoURI = (__service=="geocodingCsis")?geocodingCsisAPI:geocoderjpAPI;
	geoURI+= (__service=="geocodingCsis")?keyword:encodeURI(keyword);
	geoURI+= '&name_f=moveMap';
	//setTimeout('document.getElementById(__loadId).innerHTML=__loadMes;loadJSON(geoURI);',wait);
	setTimeout('getComponentById(__loadId).innerHTML=__loadMes;loadJSON(geoURI);',wait);
}

/////////////////////////////////////////////
//
// loadJSON(uri)
// rereave JSON output from got2do proxy
//
/////////////////////////////////////////////
if (typeof loadJSON=="undefined") {
	var loadJSON = function(_uri){
		var s=document.createElement("script");
		s.setAttribute('type','text/javascript');
		s.setAttribute('src',_uri);
		s.setAttribute('charset','UTF-8');
		document.body.appendChild(s);
	}
}

if (typeof getComponentById=="undefined") {
	var getComponentById = function(_id) {
		var docElmt=document.getElementById(_id);
		if (docElmt!=null) {
			return docElmt;
		} else {
			var dummy=new Object();
			dummy.innerHTML='';
			return dummy;
		}
	}
}

if (typeof $_GET=="undefined") {
	var $_GET = function() {
		var args=new Array();
		if (location.search.length>1) {
			var strs = location.search.substr(1).split("&");
			for (str in strs) {
				var item=strs[str].split("=");
				args[item[0]]=item[1];
			}
		}
		return args;
	}
}
