/*
 * 不正なPOI_CODEの場合
 * Googleマップへのリンクを無効にする *
*/
function GmapLink(){};

GmapLink.init = function(poi){

	GmapLink.pcode = poi;
	var url = '<scr' + 'ipt type="text/javascript" src="http://lbs.mapion.co.jp/map/uc/PoiInfo?grp=appleworld&poi_code=' + poi + '&vo=mbml&json=1&oe=sjis&callback=getJson"> </scr' + 'ipt>';
	document.write(url);
}
function getJson(data){

	if(!data.mbml.PoiList.Poi){
		document.write(" <span style='color: gainsboro;'>[ Google マップ ] </span>");
	} else {
		document.write("[ <a id='gmaplink' href='http://lbs.mapion.co.jp/map/uc/PoiInfo?grp=appleworld&amp;poi_code="+GmapLink.pcode+
		"&amp;vo=mbml&amp;scl=14&amp;mscl=17' onclick='openPopUpM(\"http://lbs.mapion.co.jp/map/uc/PoiInfo?grp=appleworld&amp;poi_code="+GmapLink.pcode+
		"&amp;vo=mbml&amp;scl=14&amp;mscl=17\");return false;'>Google マップ</a> ]");
	}
}

/*
 * josnscriptrequest
 */
function _JSONscriptRequest_(fullUrl, charset, noCacheIE) {
	this.fullUrl = fullUrl;
	this.charset = (charset == null || charset == '') ? "UTF-8" : charset;
	this.noCacheIE = noCacheIE;
	this.headLoc = document.getElementsByTagName("head").item(0);
	this.scriptId = 'JscriptId' + _JSONscriptRequest_.scriptCounter++;
	this.timeoutMSec=null;
	this.timeoutFunc=null;
	this.timerID=null;
}
_JSONscriptRequest_.scriptCounter = 1;
_JSONscriptRequest_.prototype.setTimeoutFunc=function(func, timeValue) {
	var jsonpr=this;
	var arg=[];
	for(var i=2;i<arguments.length;++i){
		arg.push(arguments[i]);
	}
	jsonpr.timeoutFunc=function(){func.apply(jsonpr,arg)};
	jsonpr.timeoutMSec=timeValue;
	jsonpr.timerID=window.setTimeout(jsonpr.timeoutFunc,jsonpr.timeoutMSec);
};
_JSONscriptRequest_.prototype.clearTimeoutFunc=function() {
	var jsonpr=this;
	if(jsonpr.timerID) jsonpr.timerID=window.clearTimeout(jsonpr.timerID);
	//jsonpr.removeScriptTag(); // このコメントアウトをはずすと、IEで落ちます！→removeしたscriptタグ内のデータアクセスがダメ！
	jsonpr.timerID=null;
	jsonpr.timeoutFunc=null;
	jsonpr.timeoutMSec=null;
};
_JSONscriptRequest_.prototype.buildScriptTag = function () {
	this.scriptObj = document.createElement("script");
	this.scriptObj.setAttribute("type", "text/javascript");
	this.scriptObj.setAttribute("charset", this.charset);
	this.scriptObj.setAttribute("src", this.fullUrl + (this.noCacheIE?"&noCacheIE="+this.getNoCacheParameterValue():""));
	this.scriptObj.setAttribute("id", this.scriptId);
};
_JSONscriptRequest_.prototype.getNoCacheParameterValue = function(){
	return "&"+new Date().getTime();
};
_JSONscriptRequest_.prototype.removeScriptTag = function () {
	try{
		this.headLoc.removeChild(this.scriptObj);
	}catch(e){}
};
_JSONscriptRequest_.prototype.addScriptTag = function () {

	this.headLoc.appendChild(this.scriptObj);
};

/*
 * eventdispatcher
 */
function EventDispatcher(){}
EventDispatcher.implement=function(obj){
	obj.__ed_eventContainer = new Object();
	obj.addEventListener = this._addEventListener;
	obj.removeEventListener = this._removeEventListener;
	obj.dispatchEvent = this._dispatchEvent;
}
EventDispatcher.initialize=EventDispatcher.implement;
EventDispatcher._addEventListener=function(_en,_obj){
	if(!_en||!_obj)return;
	if(typeof(_obj)=="object"||typeof(_obj)=="function"){
		if(this.__ed_eventContainer[_en]==null){
			this.__ed_eventContainer[_en]=new Array();
		}
		this.removeEventListener(_en,_obj);
		this.__ed_eventContainer[_en].push(_obj);
	}
}
EventDispatcher._removeEventListener=function(_en,_obj){
	if(!_en||!_obj)return;
	var a=this.__ed_eventContainer[_en];
	if(typeof(a)=="undefined")return;
	var l=a.length;
	for(var i=0;i<l;i++){
		if(a[i]==_obj){
			a.splice(i,1);
			return;
		}
	}
}
EventDispatcher._dispatchEvent=function(_ev){
	if(!_ev)return;
	if(typeof(_ev)=="string")_ev = new EventObject(_ev,this);
	if(_ev.target==null)_ev.target=this;
	var a=this.__ed_eventContainer[_ev.type];
	if(a==null)return;
	var l=a.length;
	for(var i=0;i<l;i++){
		var ln=a[i];
		if(typeof(ln)=="object"){
			ln[_ev.type].apply(ln,arguments);
		}else{
			ln.apply(ln,arguments);
		}
	}
}
function EventObject(_en,_tg){
	if(!_en)return;
	this.type=_en;
	this.target=_tg;
}

/*
 * json
 */
JSONCALLBACK = new Array();
function JSON(param){
	if(typeof(EventDispatcher)=="undefined"){alert("EventDispatcher has not been loaded !");return;}
	EventDispatcher.implement(this);
	this.numId = JSON.scriptCounter;
	this.responseData = null;
	var obj = this;
	JSONCALLBACK[this.numId]=function(){
		obj.clearTimeoutFunc();
		obj.responseData = arguments;
		obj.dispatchEvent(new EventObject("onload",obj));
	}
	this.scriptId = 'Jscript2Id' + JSON.scriptCounter++;
	this.callbackParamName = JSON.defaultCallbackParamName;
	var d = new Date();
	this.callbackFuncName = "JSONCallback"+this.scriptId+d.getTime();
	var makeCallback = ""+
	this.callbackFuncName+"=function(){\n"+
	"	JSONCALLBACK["+this.numId+"].apply(null,arguments);\n"+
	"}";
	eval(makeCallback);
	this.timeOutMS = 5000;
	this.noCacheIE = false;
	if(param){
		if(param["fullUrl"])this.fullUrl=param["fullUrl"];
		if(param["callbackParamName"])this.callbackParamName=param["callbackParamName"];
		if(param["charset"])this.charset=param["charset"];
		if(param["timeOutMS"])this.timeOutMS=param["timeOutMS"];
		if(param["noCacheIE"])this.noCacheIE=param["noCacheIE"];
	}
}

if(typeof(_JSONscriptRequest_)=="undefined"){alert("_JSONscriptRequest_ has not been loaded !")}
JSON.prototype = new _JSONscriptRequest_("");
JSON.scriptCounter = 0;
JSON.defaultCallbackParamName = "callback";

JSON.prototype.get = function(fullUrl,charset,callbackParamName){
	if(fullUrl)this.fullUrl=fullUrl;
	if(callbackParamName)this.callbackParamName=callbackParamName;
	if(charset)this.charset=charset;

	if(typeof(this.fullUrl)!="string"){
		alert(this.id+"\nrequestURL is not string!\ntype=>"+typeof(this.fullUrl));
		return;
	}
	if(typeof(this.callbackParamName)!="string"){
		alert(this.id+"\ncallbackParamName is not string!\ntype=>"+typeof(this.callbackParamName));
		return;
	}
	if(this.fullUrl.indexOf(this.callbackParamName)!=-1){
		this.fullUrl.match(new RegExp(".*("+this.callbackParamName+"=.*)"));;
		if(RegExp.$1.indexOf("&")!=-1){
			this.fullUrl=this.fullUrl.replace(new RegExp(this.callbackParamName+"=.*&"),"");
		}else{
			this.fullUrl=this.fullUrl.replace(new RegExp("&"+this.callbackParamName+"=.*"),"");
		}
	}
	this.fullUrl = this.fullUrl+"&"+this.callbackParamName+"="+this.callbackFuncName;

	this.clearTimeoutFunc();
	this.removeScriptTag();
	var obj = this;
	this.buildScriptTag();
	this.setTimeoutFunc(function(){
		obj.clearTimeoutFunc();
		obj.dispatchEvent(new EventObject("onerror",obj));
	},this.timeOutMS);
	this.addScriptTag();
}

