/*
 * ロジック部分です。基本的にさわることはありません。
 */

// ロジック制御処理 部分
top.Controller =
{
    // 一度だけ XML を取得
    exec: function(id, xmlpath, callback){
		/* 09.01.06 h.adachi キャッシュクリア処理解除 */
        //new top.Model(id,  top.Controller.makePath(xmlpath), callback);
        new top.Model(id,  xmlpath, callback);
    },

    // タイマ設定により、定期的に XML を取得
    updateChecker: function(id, xmlpath, callback, interval){
        new PeriodicalExecuter(checkPeriodically, interval);
        function checkPeriodically(){
			/* 09.01.06 h.adachi キャッシュクリア処理解除 */
            //new top.Model(id, top.Controller.makePath(xmlpath), callback);
            new top.Model(id, xmlpath, callback);
        }
    },

    // キャッシュを利用させない様に、ダミーのクエリを(ついてなければ)つける。
    makePath: function(xmlpath){
        var urlPath;
        //xmlpath = encodeURIComponent(xmlpath);
        // 動的に作られるRSS (index.php?mode=rss など) に対応
        if (xmlpath.match(/.*\?.*/i)){
            urlPath = xmlpath;
        } else {
            var now = new Date();
            urlPath = xmlpath + '?'  + now.getMinutes().toString() + now.getSeconds().toString();
        }
        return urlPath;
    }
};


// XML取得処理 部分
top.Model = Class.create(
{
    // 初期化処理
    initialize: function(id, url, callback) {
        this.url = url;
		
		if(callback!=undefined){
			//this.httpRequest(id, callback);
			this.httpRequestASync(id, callback);
		}
    },

    // httpRequest処理 (同期処理)
    httpRequest: function(id, callback) {
        var http = new JKL.ParseXML(this.url);
        http.setOutputArrayAll();
		
		if(callback!=undefined){
			callback(id, http.parse());
		}
    },

    // httpRequest処理 (非同期処理)
    httpRequestASync: function(id, callback) {
        var http = new JKL.ParseXML(this.url);
        http.setOutputArrayAll();
		
		if(callback!=undefined){
			http.onerror_func = callback(id, null);
			http.async(function(data){callback(id, data)});
		}
        http.parse();
    }

}
);


