/*
Charset: UTF-8（京雀）
Version: 20091124
Programing by Masao Takahashi.
*/

// callback routine (from chosunonline)
function chosunonline_request_done(list) { g_chosun.replaceNode(list); }

function ChosunOnline() {
	// list items per page (limit 50 items)
	this.items_per_page = 15;

	// maximum title length (ni-korea extension, 0=infinity)
	this.title_length = 0;

	// maximum contents length (limit 300 letter)
	this.contents_length = 300;

	// article date format string (ref: http://kr.php.net/manual/kr/function.date.php)
	this.dateformat = 'Y/m/d H:i';
}

ChosunOnline.prototype = {
onLoad : function()
{
	if (this.nMain) {
	//	this.executeScript();
		setTimeout(this.executeScript, 100);
	}
},
setConfig : function(items_per_page, title_length, contents_length)
{
	this.items_per_page = items_per_page;
	if (title_length != undefined) this.title_length = title_length;
	if (contents_length != undefined) this.contents_length = contents_length;
},
registElementById : function(id, page_numb)
{
	// check page_numb (1-100)
	if (page_numb == 0 || page_numb > 100) {
		alert("invalid parameter 'page_numb'");
		return;
	}
	this.page_numb = page_numb;
	this.nMain = document.getElementById(id);
	if (!this.nMain) {
		alert("not found '"+id+"'");
		return;
	}
	var list = getElementsByClass("js-format-first", this.nMain.parentNode);
	if (list.length) this.nFormat1st = list[0];
	var list = getElementsByClass("js-loading", this.nMain);
	if (list.length) this.nLoading = list[0];
	var list = getElementsByClass("js-format", this.nMain);
	if (list.length) this.nFormatTxt = list[0];

	removeAllChild(this.nMain);
	if (this.nLoading) this.nMain.appendChild(this.nLoading);
},
executeScript : function()
{
	var url = 'http://www.chosunonline.com/app/ArticleList_js.do?';
	url += '&listpage=' + g_chosun.page_numb;
	url += '&listitems=' + g_chosun.items_per_page;
	url += '&contentslength=' + g_chosun.contents_length;
	url += '&dateformat=' + escape(g_chosun.dateformat);
	var s = document.createElement('script');
	s.type = 'text/javascript'; s.charset = "utf-8";
	s.src = url;
	document.body.appendChild(s);
	setTimeout(g_chosun.timeoutMsg, 5000);//20090910: 2000->5000
},
timeoutMsg : function()
{
	var node = document.getElementById("js-chosun-msg");
	if (node) {
		node.innerHTML = "Chosun Onlineサーバーがメンテナンス中につき、現在ご利用になれません。";
	}
},
replaceNode : function(list)
{
	var nMain = this.nMain;
	var hash = location.hash;
	var insert = false;
	removeAllChild(nMain);

	for (var i = 0; i < list.length; i++) {
		var item = list[i];
		if (g_chosun.title_length) {
			if (item.title.length >= g_chosun.title_length) {
				item.title = item.title.substr(0, g_chosun.title_length-1) + "…";
			}
		}
		if (item.title.match(/^【動画】/)) {
			item.contents = "";
		}

		var nFormat = g_chosun.nFormatTxt;
		//if (item.image && g_chosun.nFormatImg) { nFormat = g_chosun.nFormatImg; }
		if (i == 0 && g_chosun.nFormat1st) { nFormat = g_chosun.nFormat1st; insert = true; }

		var node = nFormat.cloneNode(true);
		var html = node.innerHTML;
		var uniqueID = item.url.match(/\d+/g).join();
		if (!item.image) item.image = "http://ni-korea.jp/images/null.gif";//20091124
		html = html.replace(/%image%/g, item.image);
		html = html.replace(/%title%/g, item.title);
		html = html.replace(/%url%/g, item.url);
		html = html.replace(/%date%/g, item.date);
		html = html.replace(/%contents%/g, item.contents);
		html = html.replace(/%id%/g, uniqueID);
		node.innerHTML = html;
		if (insert) {
			nMain.parentNode.insertBefore(node, nMain);
			insert = false;
		}
		else {
			nMain.appendChild(node);
		}
		node.style.display = "";
	}
	if (hash) location.hash = hash;
}
};
var g_chosun = new ChosunOnline();
g_common.registInterface(g_chosun);
