/**********************************************************************************
 * JSONPライブラリ(コンストラクタ)
 * @constructor
 * @class
 * 		JSONPを呼び出すためのライブラリ
 * @param {String}fullUrl 呼び出しURL
 * @param {String}charset 文字コード
 **********************************************************************************/
function JSONscriptRequest(fullUrl, charset) {

	/** 呼び出しURL @type String  */
	this.fullUrl = fullUrl;
	/** 文字コード(デフォルト： "EUC-JP") @type String */
	this.charset = (charset == null || charset == '') ? "EUC-JP" : charset;
	/** キャッシュ防止タイムスタンプ @type String */
	this.noCacheIE ="";// '&noCacheIE=' + (new Date()).getTime();
	/** &lt;head&gt;タグの参照 @type HTML Object */
	this.headLoc = document.getElementsByTagName("head").item(0);
	/** 呼び出しID(カウンター) @type String */
	this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
	/** タイムアウト秒数 @type Number */
	this.timeoutMSec=null;
	/** タイムアウトで実行する関数（メソッド）名 @type String */
	this.timeoutFunc=null;
	/** タイマーID @type Number */
	this.timerID=null;

}

/** カウンターの値 @type Number*/
JSONscriptRequest.scriptCounter = 1;


/**
 * タイムアウト処理を設定する
 * @param {String}func 実行する関数
 * @param {Number}timeValue 秒数
 * @param {String}  渡したいパラメータをカンマ区切りで記述する
 * @public
 */
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);
};

/**
 * タイムアウト処理を解除する
 * @public
 */
JSONscriptRequest.prototype.clearTimeoutFunc=function() {
	var jsonpr=this;
	if(jsonpr.timerID) jsonpr.timerID=window.clearTimeout(jsonpr.timerID);
	//タグを消す必要はない？？（abort、という意味合いでは消した方がよさげ）=> 消さないとOperaで？な動き
	//jsonpr.removeScriptTag();
	jsonpr.timerID=null;
	jsonpr.timeoutFunc=null;
	jsonpr.timeoutMSec=null;
};

/**
 * &lt;script&gt;タグを生成する
 * @public
 */
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);
	this.scriptObj.setAttribute("id", this.scriptId);
};

/**
 * &lt;script&gt;タグを削除(removeChild)する
 * @public
 */
JSONscriptRequest.prototype.removeScriptTag = function () {

	try{
		this.headLoc.removeChild(this.scriptObj);
	}catch(e){}
};

/**
 * &lt;script&gt;タグを追加(appendChild)する
 * @public
 */
JSONscriptRequest.prototype.addScriptTag = function () {

	this.headLoc.appendChild(this.scriptObj);
};

