﻿var MyLib = {
	version:'0.0.1'
}
	
// prototype.js っぽく
	MyLib.$ = function(id){
		return document.getElementById(id);
	}

// イベントハンドラの設定
	MyLib.setEvent = function(elem, func, type){
		MyLib.clearEvent(elem, func, type);// クリアしてから設定する
		if(elem.addEventListener){
			elem.addEventListener(type, func, true);
		} else if(elem.attachEvent){
			elem.attachEvent("on" + type, func);
		} else {
			alert("Not supported");
		}
	}

// イベントハンドラの解除
	MyLib.clearEvent = function(elem, func, type){
		if(elem.removeEventListener){
			elem.removeEventListener(type, func, true);
		} else if(elem.attachEvent){
			elem.detachEvent("on" + type, func);
		} else {
			alert("Not supported");
		}
	}

// イベントの起きた要素のID取得
	MyLib.getId = function(e){
		var tgt;
		if(!e){ var e = window.event; }
		if(e.srcElement){
			tgt = e.srcElement.id;
			if(tgt.nodetype == 3){
				tgt = tgt.repentNode;
			}
		}else if(e.target){
			tgt = e.target.id;
		}else {
			tgt = false;
		}
		return tgt;
	}

	MyLib.cElem = function(name){return document.createElement(name);} 
	MyLib.cText = function(text){return document.createTextNode(text);}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 以下クラス定義

// 継承用関数
	MyLib.inherbit = function(sub_class, super_class){
		for(var prop in super_class.prototype){
			sub_class.prototype[prop] = super_class.prototype[prop];
		}
	}

////////////////////////////////////////////////////////////////////////////////
// ManageClass
// setIntervalでのイベントをまとめて実行
// onLoad時に行う処理をまとめて実行
// 1, cycle			// setIntervalの間隔
// 2, list_span		// Menuの表示間隔
// 3, [file_list]	// 読み込むファイルのリスト

MyLib.ManageClass = function(){
	this.initialize.apply(this, arguments);
}

MyLib.ManageClass.prototype = {
	
	initialize:
		function(cycle, list_span, file_list){
			this.cycle = cycle || 50;
			this.event_list = new Array();
			this.data = new MyLib.DataClass();
			this.view = new MyLib.ViewClass(this);
			this.list_span = list_span || 3;
			this.file_list = file_list || [];
			this.selected_title = 0;
			this.selected_page = 0;

			var _this = this;
			// イベント設定
			MyLib.setEvent(window, function(){_this.setOnLoadEvent()}, "load");
		},

	setMenuCharImage:
		function(img_path){
			this.data.menu_char_img = img_path;
		},

	setOnLoadEvent:
		function(){
			var _this = this;
			setInterval(function(){_this.eventLoop()},this.cycle);
			for(var i=0; i<this.file_list.length; i++){
				this.data.loadDataFromXml(this.file_list[i]);
			}
			this.view.clearBody();
			if(this.file_list.length == 0){return;}// mascotのTestのため
			this.view.createTable();
			this.view.setMenuCharImage(this.data.menu_char_img);
			this.view.printTitleList(this.data.title_data);

			// 最後に更新されたPageを表示
			var latest_index = new Array(0,0);
			for(var i=0; i < this.data.title_data.length; i++){
				for(var j=0; j < this.data.title_data[i].pages.length; j++){
					if(this.data.title_data[latest_index[0]].pages[latest_index[1]].date < this.data.title_data[i].pages[j].date){
						latest_index[0] = i;
						latest_index[1] = j;
					}
				}
			}
			this.view.printPage(this.data.title_data, Number(latest_index[0]), Number(latest_index[1]));
			this.view.printList(this.data.title_data, Number(latest_index[0]));
			this.view.setSelectedTitleStyle(this.data.title_data, latest_index[0]);
			this.view.setSelectedPageStyle(this.data.title_data, latest_index[0], latest_index[1]);
			this.selected_title = latest_index[0];
			this.selected_page = latest_index[1];
		},

	setTimerEvent: 
		function(set_class){
			this.event_list.push(set_class);
		},

	clearTimerEvent:
		function(){
			while(this.event_list.length!=0)this.event_list.pop();
		},

	eventLoop:
		function(){
			for(var i=0; i<this.event_list.length; i++){
				this.event_list[i].timerAction();
			}
		},

	switchList:
		function(e){
			var id = MyLib.getId(e);
			var list_num = id.split("_")[1];
			this.view.clearList();
			this.view.printList( this.data.title_data, list_num );
			this.view.setSelectedTitleStyle(this.data.title_data, list_num);
			// 該当タイトルの最初の1pageを表示
			this.view.printPage(this.data.title_data, list_num, 0);
			this.view.setSelectedPageStyle(this.data.title_data, list_num, 0);
			this.selected_title = list_num;
			this.selected_page = 0;
		},

	printPageByEvent:
		function(e){
			var title_num = MyLib.getId(e).split("_")[1];
			var page_num = MyLib.getId(e).split("_")[2];
			this.view.printPage(this.data.title_data, Number(title_num), Number(page_num));
			this.view.setSelectedPageStyle(this.data.title_data, title_num, page_num);
			this.selected_title = title_num;
			this.selected_page = page_num;
		},

	mouseOverEvent:
			function(e){
				var title_num = MyLib.getId(e).split("_")[1];
				var page_num = MyLib.getId(e).split("_")[2];
				var title = this.data.title_data[title_num];
				var page = title.pages[page_num];
				this.view.char_text.clearString();
				// char_textの設定
				this.view.char_text.setString(title.name);
				if(typeof(page_num) != 'undefined'){
					if(page.sms != ""){
						this.view.char_text.setString(page.sms);
					} else {
						this.view.char_text.setString(page.page_caption);
						this.view.char_text.setString(page.author);
						this.view.char_text.setString(page.date.toLocaleDateString());
					}
				}
				document.body.style.cursor = "pointer";
			},

	mouseOutEvent:
			function(e){
				// char_textの設定
				var title = this.data.title_data[this.selected_title];
				var page = title.pages[this.selected_page];
				this.view.char_text.clearString();
				this.view.char_text.setString(title.name);
				if(typeof(this.selected_page) != 'undefined'){
					if(page.sms != ""){
						this.view.char_text.setString(page.sms);
					} else {
						this.view.char_text.setString(page.page_caption);
						this.view.char_text.setString(page.author);
						this.view.char_text.setString(page.date.toLocaleDateString());
					}
				}
				document.body.style.cursor = "default";
			}
}

////////////////////////////////////////////////////////////////////////////////////////////////////
// MyLib.DataClass
MyLib.DataClass = function(){
	this.initialize.apply(this, arguments);
}

MyLib.DataClass.prototype = {
	initialize:
		function(){
			this.title_data = new Array();
			this.menu_char_img = "";
		},

	loadDataFromXml:
		function(file_name){
			var _this = this;
			sendRequest(function(oj){_this.getXmlData(oj)}, '', 'GET', file_name, false);
		},

	getXmlData:
		function(oj){
			var str = "";
			var xmlDoc = oj.responseXML;
			if(xmlDoc.documentElement.hasChildNodes()){
				var i = 0;
				var num_of_doc_child = xmlDoc.documentElement.childNodes.length;
				for(i=0; i < num_of_doc_child; i++){
					var doc_child = xmlDoc.documentElement.childNodes[i];
					if(doc_child.hasChildNodes()){
						this.title_data.push(this.getTitleInfo(doc_child));
					}
				}
			} else {
				alert("Don't have child nodes");
			}
		},

	// タイトル情報の取得
	getTitleInfo:
		function(doc_child){
			// TitleInfoに代入
			var title_info = new MyLib.TitleInfoClass();
			var i = 0;
			var page_num = 0;
			var num_of_title_child = doc_child.childNodes.length;
			// Attributeの取得
			if(doc_child.getAttribute("name") != null){
				title_info.name = doc_child.getAttribute("name");
			}
			if(doc_child.getAttribute("cols") != null){
				title_info.cols = doc_child.getAttribute("cols");
			}
			if(doc_child.getAttribute("type") != null){
				title_info.type = doc_child.getAttribute("type");
			}
			for(i=0; i < num_of_title_child; i++){
				var title_child = doc_child.childNodes[i];
				if(title_child.hasChildNodes()){
					switch(title_child.nodeName){
					case "page":
						// PageInfoの取得
						title_info.addPage(this.getPageInfo(title_child, page_num));
						page_num++;
						break;
					default:
						break;
					}
				}
			}
			return title_info;
		},

	getPageInfo:
		function(title_child, page_num){
			// PageInfoに代入
			var page_data = new MyLib.PageInfoClass(title_child.name, page_num + 1);
			var i = 0;
			var num_of_page_child = title_child.childNodes.length;
				for(i=0; i < num_of_page_child; i++){
					var page_child = title_child.childNodes[i];
					if(page_child.nodeName == "comment" && page_child.hasChildNodes()){
						// コメント内にまだタグがある場合
						page_data.comment = "";
						var j = 0;
						var num_of_comment_child = page_child.childNodes.length;
						for(j=0; j < num_of_comment_child; j++){
							var comment_child = page_child.childNodes[j];
							if(comment_child.hasChildNodes()){
								page_data.comment += this.toComment(comment_child);
							} else {
								page_data.comment += comment_child.nodeValue;
							}
						}
					} else {
						switch(page_child.nodeName){
							case "caption":
								page_data.page_caption = page_child.firstChild.nodeValue;
								break;
							case "img":
								page_data.image_url = page_child.firstChild.nodeValue;
								break;
							case "pagetitle":
								page_data.pagetitle = page_child.firstChild.nodeValue;
								break;
							case "author":
								page_data.author = page_child.firstChild.nodeValue;
								break;
							case "comment":
								page_data.comment = page_child.firstChild.nodeValue;
								break;
							case "url":
								page_data.url = page_child.firstChild.nodeValue;
								break;
							case "sms":
								page_data.sms = page_child.firstChild.nodeValue;
								break;
							case "date":
								var date_str = page_child.firstChild.nodeValue;
								var date_obj = date_str.split('/',3);
								if(date_obj.length == 3){
									page_data.date.setFullYear(date_obj[0],date_obj[1],date_obj[2]);
								}
								break;
							default:
								break;
						}
					}
				}
			return page_data;
		},

	toComment:
		function(comment_child){
			// コメント内タグの解釈
			var str = "";
			switch(comment_child.nodeName){
				case "link":
					str = ('<a href="' + 
							comment_child.getAttribute("href")
							+ '">'
							+ comment_child.firstChild.nodeValue
							+ '</a>');
					break;
				default:
					str = comment_child.firstChild.nodeValue;
					break;
			}
			return str;
		}

}

////////////////////////////////////////////////////////////////////////////////////////////////////
// ViewClass
MyLib.ViewClass = function(){
	this.initialize.apply(this, arguments);
}

MyLib.ViewClass.prototype = {

	NormalBgColorClassName:".normal",
	SelectedBgColorClassName:".selected",

	//default
	NormalBgColorStyle:"background-color:#fff",
	SelectedBgColorStyle:"background-color:#aaa",

	initialize:
		function(manager){
			this.manager = manager;
			this.loadSelectedBgColorStyle();
		},

	loadSelectedBgColorStyle:
		function(){
			if(document.styleSheets.length != 1){
				//とりあえず、StyleSheetは一つだけを想定
				alert("please set one styleSheet");
				return;
			}
			var i=0;
			var stylesheet = document.styleSheets[0];
			var rule;
			// IEとFireFoxでプロパティが違う
			if(typeof(stylesheet.cssRules) != "undefined"){
				for(i=0; i<stylesheet.cssRules.length; i++){
					rule = stylesheet.cssRules[i];
					if(rule.selectorText == this.NormalBgColorClassName){
						this.NormalBgColorStyle = rule.style.cssText;
					}
					if(rule.selectorText == this.SelectedBgColorClassName){
						this.SelectedBgColorStyle = rule.style.cssText;
					}
				}
			}else{
				for(i=0; i<stylesheet.rules.length; i++){
					rule = stylesheet.rules[i];
					if(rule.selectorText == this.NormalBgColorClassName){
						this.NormalBgColorStyle = rule.style.cssText;
					}
					if(rule.selectorText == this.SelectedBgColorClassName){
						this.SelectedBgColorStyle = rule.style.cssText;
					}
				}
			}
		},

	clearBody:
		function(){
			var body_node = document.body;
			while(body_node.childNodes.length != 0){
				body_node.removeChild(body_node.firstChild);
			}
		},

	createTable:
		function(){
			var body_node = document.body;
				var table = MyLib.cElem("table");

					var tbody = MyLib.cElem("tbody");
						var tr = MyLib.cElem("tr");

							var tr_td1 = MyLib.cElem("td");
								tr_td1.setAttribute("id", "menu_colom");
								tr_td1.setAttribute("valign", "top");
							tr_td1.appendChild(this.createMenuTable());

							var tr_td2 = MyLib.cElem("td");
								tr_td2.setAttribute("id", "page_colom");
								var tr_td2_div = MyLib.cElem("div");
									tr_td2_div.setAttribute("id", "page_area");
							tr_td2.appendChild(tr_td2_div);


							var tr_td3 = MyLib.cElem("td");
								tr_td3.setAttribute("id", "list_colom");
								tr_td3.setAttribute("valign", "top");
								var tr_td3_div = MyLib.cElem("div");
									tr_td3_div.setAttribute("id", "list_area");
							tr_td3.appendChild(tr_td3_div);

						tr.appendChild(tr_td1);
						tr.appendChild(tr_td2);
						tr.appendChild(tr_td3);

					tbody.appendChild(tr);
				table.appendChild(tbody);

			body_node.appendChild(table);
		},

	createMenuTable:
		function(){
			var table = MyLib.cElem("table")
				table.setAttribute("id", "menu_table");
				var tbody = MyLib.cElem("tbody");
					var tr1 = MyLib.cElem("tr");

						var tr1_td = MyLib.cElem("td");
							tr1_td.setAttribute("id", "char_text_colom");
							var tr1_td_div_char_text = MyLib.cElem("div");
								tr1_td_div_char_text.setAttribute("id", "char_text");
						tr1_td.appendChild(tr1_td_div_char_text);
					tr1.appendChild(tr1_td);

					var tr2 = MyLib.cElem("tr");
						var tr2_td = MyLib.cElem("td");
							tr2_td.setAttribute("id", "char_img_colom");
							var tr2_td_img = MyLib.cElem("img");
								tr2_td_img.setAttribute("id", "char_img");
								tr2_td_img.setAttribute("src", "img");
						tr2_td.appendChild(tr2_td_img);
					tr2.appendChild(tr2_td);

					var tr3 = MyLib.cElem("tr");
						var tr3_td = MyLib.cElem("td");
							tr3_td.setAttribute("id", "menu_list_colom");
							var tr3_td_div = MyLib.cElem("div");
								tr3_td_div.setAttribute("id", "menu_area");
						tr3_td.appendChild(tr3_td_div);
					tr3.appendChild(tr3_td);

				tbody.appendChild(tr1);
				tbody.appendChild(tr2);
				tbody.appendChild(tr3);
			table.appendChild(tbody);

			// char用のText設定
			this.char_text = new MyLib.TextRollClass("char_text", 20);
			this.manager.setTimerEvent(this.char_text);
			return table;
		},

	setMenuCharImage:
		function(img_path){
			MyLib.$("char_img").src = img_path;
		},

	printTitleList:
		function(title_data){
			var i = 0;
			for(i=0; i < title_data.length; i++){
				var title = MyLib.cElem("div");
					var id = title_data[i].name + "_" + i;
					title.setAttribute("id", id);
					var title_text = MyLib.cText(title_data[i].name);
				title.appendChild(title_text);


				// list_areaに追加
				MyLib.$("menu_area").appendChild(title);
				MyLib.$("menu_area").appendChild(MyLib.cElem("br"));

				// eventの設定
				var manager = this.manager;
				MyLib.setEvent(MyLib.$(id), function(e){manager.switchList(e)}, "click");
				MyLib.setEvent(MyLib.$(id), function(e){manager.mouseOverEvent(e)}, "mouseover");
				MyLib.setEvent(MyLib.$(id), function(e){manager.mouseOutEvent(e)}, "mouseout");
			}
		},

	setSelectedTitleStyle:
		function(title_data, list_num){
			for(i=0; i < title_data.length; i++){
				var id = title_data[i].name + "_" + i;
				var doc = MyLib.$(id);
				var style = "";
				var bgclass = "";
				if(i == list_num){ // selected
					style = this.SelectedBgColorStyle;
				} else { // not selected
					style = this.NormalBgColorStyle;
				}
				doc.setAttribute('class',bgclass);
				if(typeof(doc.style.cssText) != 'undefined'){
					doc.style.cssText = style;
				} else {
					doc.setAttribute('style',style);
				}
			}
		},

	setSelectedPageStyle:
		function(title_data, list_num, page_num){
			for(var i=0; i<title_data[list_num].pages.length; i++){
				var id = title_data[list_num].name + "_" + list_num + "_" + i + "_captions";
				var doc = MyLib.$(id);
				var style = "";
				var bgclass = "";
				if(i == page_num){ // selected
					style = this.SelectedBgColorStyle;
				} else { // not selected
					style = this.NormalBgColorStyle;
				}
				doc.setAttribute('class',bgclass);
				if(typeof(doc.style.cssText) != 'undefined'){
					doc.style.cssText = style;
				} else {
					doc.setAttribute('style',style);
				}
			}
		},

	printList:
		function(title_data, list_num){
			var line = 0;
			var str = "";
			var num_of_col = title_data[list_num].cols;
			// 描画領域の取得
			var list_area = MyLib.$("list_area");
			
			// 既にtableがあった場合には何もしない( MEMO:チェック関数作ってここでは処理しないようにするかも)
			if(list_area.hasChildNodes()){return;}

			// table の追加
			var table = MyLib.cElem("table");
			var tbody = MyLib.cElem("tbody");
			for(var i=0; i<title_data[list_num].pages.length;){
				var tr = MyLib.cElem("tr");
				for(var j=0; (j<num_of_col) && (i<title_data[list_num].pages.length); j++,i++){
					var td = MyLib.cElem("td");
						var div = MyLib.cElem("div");
							div.setAttribute("class","normal");
							div.setAttribute("id", title_data[list_num].name + "_" + list_num + "_" + i + "_captions");
							var text = MyLib.cText(title_data[list_num].pages[i].page_caption);
						div.appendChild(text);
					td.appendChild(div);
					tr.appendChild(td);
				}
				tbody.appendChild(tr);
				line++;
			}
			table.appendChild(tbody);
			list_area.appendChild(table);

			// eventの設定
			if(list_area){
				var manager = this.manager;
				for(var i=0; i<title_data[list_num].pages.length; i++){
					var id = title_data[list_num].name + "_" + list_num + "_" + i + "_captions";
					MyLib.setEvent(MyLib.$(id), function(e){manager.printPageByEvent(e)}, "click" );
					MyLib.setEvent(MyLib.$(id), function(e){manager.mouseOverEvent(e)}, "mouseover");
					MyLib.setEvent(MyLib.$(id), function(e){manager.mouseOutEvent(e)}, "mouseout");
				}
			}
		},

	clearList:
		function(){
			var list_area = MyLib.$("list_area");
			while(list_area.childNodes.length > 0){list_area.removeChild(list_area.firstChild);}
		},

	pageTemplate:
		{
			standard:
			function(title_data, title_num, page_num, _this){
				var page_colom = MyLib.$("page_colom");
					var table = MyLib.cElem("table");
						table.setAttribute("id", "page_table");

						var tbody = MyLib.cElem("tbody");
							var tr1 = MyLib.cElem("tr");

								var tr1_td1 = MyLib.cElem("td");
									tr1_td1.setAttribute("id", "title_colom");
									tr1_td1.setAttribute("align", "center");
									var tr1_td1_div = MyLib.cElem("div");
										tr1_td1_div.setAttribute("id", "title_area");
								tr1_td1.appendChild(tr1_td1_div);

							tr1.appendChild(tr1_td1);

							var tr2 = MyLib.cElem("tr");
								var tr2_td1 = MyLib.cElem("td");
									tr2_td1.setAttribute("id", "image_colom");
									var tr2_td1_div = MyLib.cElem("div");
										tr2_td1_div.setAttribute("id", "main_window");
										var tr2_td1_div_img = MyLib.cElem("img");
											tr2_td1_div_img.setAttribute("id", "image_area");
									tr2_td1_div.appendChild(tr2_td1_div_img);
								tr2_td1.appendChild(tr2_td1_div);
							tr2.appendChild(tr2_td1);

							var tr3 = MyLib.cElem("tr");
								var tr3_td1 = MyLib.cElem("td");
									tr3_td1.setAttribute("id", "comment_colom");
									var tr3_td1_div = MyLib.cElem("div");
										tr3_td1_div.setAttribute("id", "comment_area");
										tr3_td1_div.setAttribute("align", "center");
								tr3_td1.appendChild(tr3_td1_div);
							tr3.appendChild(tr3_td1);

							var tr4 = MyLib.cElem("tr");
								var tr4_td1 = MyLib.cElem("td");
									tr4_td1.setAttribute("id", "fotter_colom");
									var tr4_td1_div = MyLib.cElem("div");
										tr4_td1_div.setAttribute("id", "fotter_area");
										tr4_td1_div.setAttribute("align", "center");
										tr4_td1_div.setAttribute("valign", "bottom");
								tr4_td1.appendChild(tr4_td1_div);
							tr4.appendChild(tr4_td1);

						tbody.appendChild(tr1);
						tbody.appendChild(tr2);
						tbody.appendChild(tr3);
						tbody.appendChild(tr4);
					table.appendChild(tbody);

				page_colom.appendChild(table);

				var page_data = title_data[title_num].pages[page_num];
				_this.printTitleArea(page_data.pagetitle);
				_this.printImageArea(page_data.image_url);
				_this.printCommentArea(page_data.comment);
				_this.printFotterArea(title_data, title_num, page_num, 1);
			},

			link:
			function(title_data, title_num, page_num, _this){
				var page_colom = MyLib.$("page_colom");
					var table = MyLib.cElem("table");
						table.setAttribute("id", "page_table");

						var tbody = MyLib.cElem("tbody");
							var tr1 = MyLib.cElem("tr");

								var tr1_td1 = MyLib.cElem("td");
									tr1_td1.setAttribute("id", "title_colom");
									tr1_td1.setAttribute("align", "center");
									var tr1_td1_div = MyLib.cElem("div");
										tr1_td1_div.setAttribute("id", "title_area");
								tr1_td1.appendChild(tr1_td1_div);
							tr1.appendChild(tr1_td1);

							var tr2 = MyLib.cElem("tr");
								var tr2_td1 = MyLib.cElem("td");
									tr2_td1.setAttribute("id", "link_colom");
									var tr2_td1_div = MyLib.cElem("div");
										tr2_td1_div.setAttribute("id", "link_area");
										tr2_td1_div.setAttribute("align", "center");
								tr2_td1.appendChild(tr2_td1_div);
							tr2.appendChild(tr2_td1);

							var tr3 = MyLib.cElem("tr");
								var tr3_td1 = MyLib.cElem("td");
									tr3_td1.setAttribute("id", "fotter_colom");
									var tr3_td1_div = MyLib.cElem("div");
										tr3_td1_div.setAttribute("id", "fotter_area");
										tr3_td1_div.setAttribute("align", "center");
										tr3_td1_div.setAttribute("valign", "bottom");
								tr3_td1.appendChild(tr3_td1_div);
							tr3.appendChild(tr3_td1);

						tbody.appendChild(tr1);
						tbody.appendChild(tr2);
						tbody.appendChild(tr3);
					table.appendChild(tbody);

				page_colom.appendChild(table);

				var page_data = title_data[title_num].pages[page_num];
				var interval = 10;
				_this.printTitleArea(page_data.pagetitle);
				_this.printLinkArea(title_data, title_num, page_num, interval);
				_this.printFotterArea(title_data, title_num, page_num, interval);
			},
			script:
			function(){
			},
			readme:
			function(){
			}
		},

	fotterType:
		{
			standard:
				function(page_num, length, interval){
					return(MyLib.cText('(' + ((page_num) + 1) + '/' + length + ')'));
				},
			link:
				function(page_num, length, interval){
					var page_num_last = page_num + interval;
					if( page_num_last > length){ page_num_last = length;}
					return(MyLib.cText((page_num + 1) + ' - ' + (page_num_last)));
				}
		},

	printPage:
		function(title_data, title_num, page_num){
			this.clearPage();
			this.pageTemplate[title_data[title_num].type](title_data, title_num, page_num, this);

			var callscene_flag = false;
			if(title_data[title_num].pages[page_num].image_url.match(/xml$/)){ callscene_flag = true; }
			if(callscene_flag){ readScene(title_data[title_num].pages[page_num].image_url);}
		},

	clearPage:
		function(){
			var page_colom = MyLib.$("page_colom");
			while(page_colom.childNodes.length > 0){page_colom.removeChild(page_colom.firstChild);}
		},

	printTitleArea:
		function(page_title){
			var title_colom = MyLib.$("title_colom");
			var page_title = MyLib.cText(page_title);
			title_colom.replaceChild(page_title, title_colom.firstChild);
		},

	printImageArea:
		function(image_url){
			var image_area = MyLib.$("image_area");
			image_area.setAttribute("src",image_url); 
		},

	printCommentArea:
		function(comment){
			var comment_colom = MyLib.$("comment_colom");
			var comment_area = MyLib.$("comment_area");

			var comment_tmp = MyLib.cElem("div");
				comment_tmp.setAttribute("id", "comment_area");
				comment_tmp.setAttribute("align", "center");
			var pre = MyLib.cElem("pre");
				pre.innerHTML = comment.replace(/\t/g,"").replace(/\n/g,"<br/>");
			comment_tmp.appendChild(pre);
			comment_colom.replaceChild(comment_tmp, comment_area);
		},

	printLinkArea:
		function(title_data, title_num, page_num, interval){
			var link_colom = MyLib.$("link_colom");
			var link_area = MyLib.$("link_area");

			var link_tmp = MyLib.cElem("div");
				link_tmp.setAttribute("id", "link_area");
				link_tmp.setAttribute("align", "center");
		
			for(var i=0; i<interval; i++){
				// 選択されたlinkからinterval分の表示
				if(page_num + i >= title_data[title_num].pages.length){break;}
					var table = MyLib.cElem("table");
						var tbody = MyLib.cElem("tbody");

							var tr1 = MyLib.cElem("tr");
								var tr1_td1 = MyLib.cElem("td");
									tr1_td1.setAttribute("colspan","2");
									var tr1_td1_div = MyLib.cElem("div");
										var tr1_td1_div_text = MyLib.cText(title_data[title_num].pages[page_num+i].page_caption);
									tr1_td1_div.appendChild(tr1_td1_div_text);
								tr1_td1.appendChild(tr1_td1_div);
							tr1.appendChild(tr1_td1);

							var tr2 = MyLib.cElem("tr");
								var tr2_td1 = MyLib.cElem("td");
									var tr2_td1_a = MyLib.cElem("a");
										tr2_td1_a.setAttribute("href", title_data[title_num].pages[page_num+i].url);
										var tr2_td1_img = MyLib.cElem("img");
											tr2_td1_img.setAttribute("src", title_data[title_num].pages[page_num+i].image_url); 
									tr2_td1_a.appendChild(tr2_td1_img);
								tr2_td1.appendChild(tr2_td1_a);

								var tr2_td2 = MyLib.cElem("td");
									var tr2_td2_div = MyLib.cElem("div");
										var tr2_td2_div_text = MyLib.cText(title_data[title_num].pages[page_num+i].author);
									tr2_td2_div.appendChild(tr2_td2_div_text);
								tr2_td2.appendChild(tr2_td2_div);

							tr2.appendChild(tr2_td1);
							tr2.appendChild(tr2_td2);

							var tr3 = MyLib.cElem("tr");
								var tr3_td1 = MyLib.cElem("td");
									tr3_td1.setAttribute("colspan","2");
									var tr3_td1_div = MyLib.cElem("div");
										var tr3_td1_div_text = MyLib.cText(title_data[title_num].pages[page_num+i].comment);
									tr3_td1_div.appendChild(tr3_td1_div_text);
								tr3_td1.appendChild(tr3_td1_div);
							tr3.appendChild(tr3_td1);

						tbody.appendChild(tr1);
						tbody.appendChild(tr2);
						tbody.appendChild(tr3);

					table.appendChild(tbody);
				link_tmp.appendChild(table);
			}
			link_colom.replaceChild(link_tmp, link_area);
		},

	printFotterArea:
		function(title_data, title_num, page_num, interval){
			var prev_flag = false;
			var next_flag = false;
			var prev_id = "";
			var next_id = "";
			var fotter_colom = MyLib.$("fotter_colom");
			var fotter_area = MyLib.$("fotter_area");

			var fotter_tmp = MyLib.cElem("div");
				fotter_tmp.setAttribute("id", "fotter_area");
				fotter_tmp.setAttribute("align", "center");
		
			if(page_num > 0){
				var page_num_prev = 0;
				if(page_num > interval - 1){ page_num_prev = page_num - interval;}
				var span = MyLib.cElem("span");
					prev_id = title_data[title_num].name + "_" + title_num + "_" + page_num_prev + "_in_page";
					span.setAttribute("id",prev_id);
					var span_text = MyLib.cText("prev  ");
					span.appendChild(span_text);
				fotter_tmp.appendChild(span);
				prev_flag = true;
			} else {
				var span = MyLib.cElem("span");
					span.setAttribute("id","id_prev_over");
					var span_text = MyLib.cText("over  ");
					span.appendChild(span_text);
				fotter_tmp.appendChild(span);
				prev_flag = false;
			}

			var pages = this.fotterType[title_data[title_num].type](page_num, title_data[title_num].pages.length, interval);
			fotter_tmp.appendChild(pages);

			if( (Number(page_num) + interval) < title_data[title_num].pages.length){
				var span = MyLib.cElem("span");
					next_id = title_data[title_num].name + "_" + title_num + "_" + (page_num + interval) + "_in_page";
					span.setAttribute("id", next_id);
					var span_text = MyLib.cText("  next");
					span.appendChild(span_text);
				fotter_tmp.appendChild(span);
				next_flag = true;
			} else {
				var span = MyLib.cElem("span");
					span.setAttribute("id","id_next_over");
					var span_text = MyLib.cText("  over");
					span.appendChild(span_text);
				fotter_tmp.appendChild(span);
				next_flag = false;
			}

			fotter_colom.replaceChild(fotter_tmp, fotter_area);
			var manager = this.manager;
			if(prev_flag){
				MyLib.setEvent(MyLib.$(prev_id), function(e){manager.printPageByEvent(e)}, "click" );
				MyLib.setEvent(MyLib.$(prev_id), function(e){manager.mouseOverEvent(e)}, "mouseover");
				MyLib.setEvent(MyLib.$(prev_id), function(e){manager.mouseOutEvent(e)}, "mouseout");
			} else {
				MyLib.setEvent(MyLib.$("id_prev_over"), function(e){manager.mouseOutEvent(e)}, "mouseout");
			}
			if(next_flag){
				MyLib.setEvent(MyLib.$(next_id), function(e){manager.printPageByEvent(e)}, "click" );
				MyLib.setEvent(MyLib.$(next_id), function(e){manager.mouseOverEvent(e)}, "mouseover");
				MyLib.setEvent(MyLib.$(next_id), function(e){manager.mouseOutEvent(e)}, "mouseout");
			} else {
				MyLib.setEvent(MyLib.$("id_next_over"), function(e){manager.mouseOutEvent(e)}, "mouseout");
			}
		}

}

///////////////////////////////////////////////////////////////////////////////
// TextRollClass
MyLib.TextRollClass = function(){
	this.initialize.apply(this, arguments);
}

MyLib.TextRollClass.prototype = {

	initialize:
		function(id,round){
			this.id = id;
			this.msg = new Array();
			this.count = 0;
			this.msg_num = 0;
			this.round = round || 10;
			this.round_count = 1;

			// 引数を配列に格納
			if(arguments.length < 3){
				this.msg.push("");
			} else {
				this.msg.push(arguments[2]);
			}
		},

	setString:
		function(str){
			this.msg.push(String(str));
		},

	clearString:
		function(str){
			MyLib.$(this.id).innerHTML = "";
			while(this.msg.length != 0){this.msg.pop();}
			this.count = 0;
			this.round_count = 1;
			this.msg_num = 0;
		},

	timerAction:
		function(){
			if(MyLib.$(this.id) == null){return;}
			if(this.msg.length == 0){return;}
			if(!(this.count>=this.msg[this.msg_num].length)){
				if(this.count>=this.round_count * this.round){
					MyLib.$(this.id).innerHTML += "<br>";
					this.round_count++;
				}
				else{
					var printstr = this.msg[this.msg_num].substring(this.count,this.count+1);
					MyLib.$(this.id).innerHTML += printstr;
					this.count++;
				}
			} else {
				if(this.msg_num < this.msg.length-1){
					MyLib.$(this.id).innerHTML += "<br>";
					this.round_count = 1;
					this.count = 0;
					this.msg_num++;
				}
			}
		},

	finalize:
		function(){
			// イベント解除
			var _this = this;
			MyLib.clearEvent(MyLib.$(this.id), function(){_this.Clicked()}, "click");
		}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
// TitleInfoClassの定義
MyLib.TitleInfoClass = function(){
	this.initialize.apply(this, arguments);
}

MyLib.TitleInfoClass.prototype = {

	initialize:
		function(){
			this.name = "No name";
			this.cols = 1;
			this.pages = new Array;
			this.type = "standard";
		},

	addPage:
		function(page){
			if( page instanceof MyLib.PageInfoClass ){
				// 型チェック
				this.pages.push(page);
			} else {
				alert("page is not PageInfo");
			}
		}
}

///////////////////////////////////////////////////////////////////////////////
// PageInfoClassの定義
MyLib.PageInfoClass = function(){
	this.initialize.apply(this, arguments);
}

MyLib.PageInfoClass.prototype = {

	initialize:
		function(page_title, page_caption){
			this.pagetitle = page_title;
			this.page_caption = page_caption;
			this.date = new Date("1/1/2000");
			this.image_url = "./noimage.jpg";
			this.url = "./";
			this.sms = "";
			this.comment = "no comment";
			this.author = "no author";
		}
}
