//
// 楽天地図API 共通ライブラリ
//


//
// キャッシュサーバ設定
//
if (ZdcCommon) {
	ZdcCommon.ZdcTileServer="http://image.map.rakuten.co.jp/";
} else {
	alert('ERROR:zdccommon.jsを読み込んでください');
}


//
// ログ生成クラス
//
var logSkip = false;
//var logSkip = true;
var rakutenMapC_log = {
	append : function (arg) {
		var str = '';
		if (arg instanceof ZdcMap || arg instanceof ZdcSubMap) {
			str = this._logText4Map(arg, "Create_Map");
			this._sendLog(str);
			this._setEvent(arg, 'dragmapend');
			this._setEvent(arg, 'scrollmapend');
			this._setEvent(arg, 'changezoomend');
		} else {
			this._setEvent(arg, 'end');
		}
	},
	//ログサーバURL
	_logUrl : 'http://t.map.rakuten.co.jp/?',
	_logscript : null,
	_sendLog : function(str) {
try {
var _count=0;
_count++;
		if (logSkip) {
			return;
		}
		if (this._logscript != null) {
			var _headTag = document.getElementsByTagName('head')[0];
			_headTag.removeChild(_headTag.lastChild);
			this._logscript = null;
		}
_count++;
		this._logscript = document.createElement('script');
		this._logscript.type = "text/javascript";
		this._logscript.charset = "UTF-8";
		this._logscript.id = "logger";
		this._logscript.src = this._logUrl + str;
		var _head = document.getElementsByTagName('head')[0];
		_head.appendChild(this._logscript);
		_head.removeChild(_head.lastChild);
		this._logscript = null;
} catch (e) {
	alert("_sendLog:"+e+":count:"+_count);
}
	},
	_setEvent : function (object, eventName) {
		if (this._reduceObject(object, eventName)) {
			return;
		}
		this._set(object,eventName);
	},
	writeLog : function (mapObject,eventName) {
		try {
			var str;
			if (eventName == 'end') {
				str = this._logText4Search(mapObject,eventName);
			} else if (eventName == 'error') {
				str = this._logText4Error(mapObject,eventName);
			} else if (eventName == null || eventName == undefined) {
//				alert("eventName:"+eventName)
			} else {
				str = this._logText4Map(mapObject,eventName);
			}
//			alert("eventName:"+eventName)
			this._sendLog(encodeURI(str));
		} catch (e) {
			alert("writeLog:"+e);
		}
	},
//
//  zdcMap及びzdcSubMapクラスの地図操作系イベントのログ生成関数
//    -引数：
//　　     mapObject：appendメソッドで指定された監視するオブジェクト
//　　     eventName：監視するイベント名
//　　-戻り値:
//         String
//
	_logText4Map : function (obj,eventName) {
		var lon = obj.getMapLocation().mx;
		var lat = obj.getMapLocation().my;
		var str = "";
		if (obj instanceof ZdcMap) {
			str = "event:" + eventName +" MapScale:" + obj.getMapScale() + " MapLocation[lon/lat]:" + lon + "," + lat;
		}
		else if (obj instanceof ZdcSubMap) {
			str = "event:" + eventName +" SubScale:" + obj.getMapScale() + " SubLocation[lon/lat]:" + lon + "," + lat;
		}
		return str;
	},
//
//  検索系イベントのログ生成関数
//    -引数：
//　　     obj：appendメソッドで指定された監視するオブジェクト
//　　     eventName：監視するイベント名
//　　-戻り値:
//         String
//
	_logText4Search : function(obj,eventName) {
		var str="";
		
		if ((window.ZdcSearchPoi != undefined && obj instanceof ZdcSearchPoi) || (window.ZdcSearchAddress != undefined && obj instanceof ZdcSearchAddress) || (window.ZdcSearchStation != undefined && obj instanceof ZdcSearchStation) || (window.ZdcSearchPoiGenre != undefined && obj instanceof ZdcSearchPoiGenre)) {
			str = "event:"+ eventName + " searchType:" + obj.type  + " freeword:" + obj.result.options.freeWord;
		} else {
			str = "event:"+ eventName + " searchType:" + obj.type;
		}
		return str;
	},
//
//  処理が失敗時のログ生成関数
//　　     obj：appendメソッドで指定された監視するオブジェクト
//　　     eventName：監視するイベント名
//　　-戻り値:
//         String
//
	_logText4Error : function(obj,eventName) {
		var str="";
		if (obj instanceof ZdcSearchPoiResult || obj instanceof ZdcSearchAddressResult || obj instanceof ZdcSearchStationResult || obj instanceof ZdcSearchPoiGenreResult) {
			str = "error status:" + obj.status + " event:"+ eventName + " searchType:" + obj.type  + " freeword:" + obj.options.freeWord;
		} else {
			str = "error status:" + obj.status +  " event:"+ eventName + " searchType:" + obj.type;
		}
		return str;
	},
	_SPoiBox:null,
	_SAddressBox:null,
	_SStationBox:null,
	_SPoiGenreBox:null,
	_GetAddrStrBox:null,
//
//　オブジェクトがセット済みかどうか判断する関数
//　　　　obj:apendで指定されたオブジェクト
//　　-戻り値：
//　　　　　　Object
//
	_reduceObject:function (obj, eName) {
		var isBool = false;
		if (window.ZdcSearchPoi != undefined && obj instanceof ZdcSearchPoi) {
			if (this._SPoiBox != null) {
				ZdcEvent.removeListener(this._SPoiBox);
			}
			this._SPoiBox = this._set(obj,eName);
			isBool = true;
		} else if(window.ZdcSearchAddress != undefined && obj instanceof ZdcSearchAddress){
			if (this._SAddressBox != null) {
				ZdcEvent.removeListener(this._SAddressBox);
			}
			this._SAddressBox = this._set(obj,eName);
			isBool = true;
		} else if(window.ZdcSearchStation != undefined && obj instanceof ZdcSearchStation){
			if (this._SStationBox != null) {
				ZdcEvent.removeListener(this._SStationBox);
			}
			this._SStationBox = this._set(obj,eName);
			isBool = true;
		} else if(window.ZdcSearchPoiGenre != undefined && obj instanceof ZdcSearchPoiGenre) {
			if (this._SPoiGenreBox != null) {
				ZdcEvent.removeListener(this._SPoiGenreBox);
			}
			this._SPoiGenreBox = this._set(obj,eName);
			isBool = true;
		} else if(window.ZdcGetAddressString != undefined && obj instanceof ZdcGetAddressString) {
			if (this._GetAddrStrBox != null) {
				ZdcEvent.removeListener(this._GetAddrStrBox);
			}
			this._GetAddrStrBox = this._set(obj,eName);
			isBool = true;
		}
		return isBool;
	},
	_set:function(obj,eName) {
		return ZdcEvent.addListener(obj, eName, function(){
			try {
				rakutenMapC_log.writeLog(this, eName);
			}catch(e) {
				alert(e);
			}
		});		
	}
};

