if (typeof ZdcCommon != "undefined") {
    ZdcCommon.ZdcTileServer = (window.location.href.indexOf('https://') == 0) ? "https://image.map.rakuten.co.jp/": "http://image.map.rakuten.co.jp/";
} else {
    rakutenMapC_lib_OnloadCallback('ERROR:zdccommon.jsを読み込んでください');
}
var logSkip = false;
var rakutenMapC_log = {
    _count: 0,
    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');
        }
    },
    _logUrl: (window.location.href.indexOf('https://') == 0) ? 'https://t.map.rakuten.co.jp/?': 'http://t.map.rakuten.co.jp/?',
    _logscript: null,
    _sendLog: function(str) {
        try {
            if (logSkip) {
                return;
            }
            if (this._logscript != null) {
                var _headTag = document.getElementsByTagName('head')[0];
                _headTag.removeChild(_headTag.lastChild);
                this._logscript = null;
            }
            var _head = document.getElementsByTagName('head')[0];
            var l = $$$("logger" + this._count);
            if (l) _head.removeChild(l);
            this._logscript = document.createElement('script');
            this._logscript.type = "text/javascript";
            this._logscript.charset = "UTF-8";
            this._logscript.id = "logger" + this._count;
            if (this._count != 9) {
                this._count++;
            } else {
                this._count = 0;
            }
            this._logscript.src = this._logUrl + str;
            _head.appendChild(this._logscript);
            this._logscript = null;
        } catch(e) {}
    },
    _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) {} else {
                str = this._logText4Map(mapObject, eventName);
            }
            this._sendLog(encodeURIComponent(str));
        } catch(e) {
            alert("writeLog:" + e.message);
        }
    },
    _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 + " time:" + new Date().getTime();
        } else if (obj instanceof ZdcSubMap) {
            str = "event:" + eventName + " SubScale:" + obj.getMapScale() + " SubLocation[lon/lat]:" + lon + "," + lat + " time:" + new Date().getTime();
        }
        return str;
    },
    _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 + " time:" + new Date().getTime();;
        } else if ((window.ZdcRouteSearch != undefined && obj instanceof ZdcRouteSearch)) {
            str = "event:" + eventName + " searchType:ZdcRouteSearch" + " time:" + new Date().getTime();
        } else if ((window.ZdcPRouteSearch != undefined && obj instanceof ZdcPRouteSearch)) {
            str = "event:" + eventName + " searchType:ZdcPRouteSearch" + " time:" + new Date().getTime();
        } else if ((window.ZdcViRouteSearch != undefined && obj instanceof ZdcViRouteSearch)) {
            str = "event:" + eventName + " searchType:ZdcViRouteSearch" + " time:" + new Date().getTime();
        } else {
            str = "event:" + eventName + " searchType:" + obj.type + " time:" + new Date().getTime();
        }
        return str;
    },
    _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 + " time:" + new Date().getTime();
        } else {
            str = "error status:" + obj.status + " event:" + eventName + " searchType:" + obj.type + " time:" + new Date().getTime();
        }
        return str;
    },
    _SPoiBox: null,
    _SAddressBox: null,
    _SStationBox: null,
    _SPoiGenreBox: null,
    _GetAddrStrBox: null,
    _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);
            }
        });
    }
};
