// library.js
// InternetExplorer 6 ->
// Firefox 1.5 ->
// Safari 2 ->
// Opera 9 ->

var Library = function(eid){
	// 読み込みXML
	this.xml = "/support/ikimono/13/js/plans.xml";
	// コンテンツサイズ
	this.boxW = 722;
	this.boxH = 499;
	// 画像パス
	this.path = "/support/ikimono/image/";
	// ステップ○画像サイズ
	this.stepImgW = 127;
	this.stepImgH = 73;
	//　発問画像サイズ
	this.questionW = 74;
	this.questionH = 31;
	// 画像サムネールサイズ
	this.thumbW = 125;
	this.thumbH = 90;
	// ローディング画像（カスタマイズ用）
	this.loadImg = new Image();
	this.loadImg.src = this.path + "loading.gif";
	this.loadingW = 80;
	this.loadingH = 80;
	// その他
	this.eid = eid;
	this.na = navigator.userAgent;
	this.datas = null;
	this.num = null;
	this.contentsMax = 9;
};

var lb = new Library("lb");

window.onload = function() {
	if(lb.na.indexOf("MSIE") >= 0){
		document.body.onscroll = lb.setPos;
	}else{
		document.onscroll = lb.setPos;
	}
	window.onresize = lb.setPos;
}

Library.prototype.setBG = function(){
	document.getElementById(lb.eid).style.width = document.body.clientWidth + "px";
	if(lb.na.indexOf("MSIE 7") >= 0) {
		if(document.documentElement.clientHeight <= document.documentElement.scrollHeight){
			document.getElementById(lb.eid).style.height = document.documentElement.scrollHeight + "px";
		} else {
			document.getElementById(lb.eid).style.height = document.documentElement.clientHeight + "px";
		}
	} else if(lb.na.indexOf("MSIE") >= 0 || lb.na.indexOf("Opera") >= 0) {
		if(document.body.clientHeight <= document.body.scrollHeight){
			document.getElementById(lb.eid).style.height = document.body.scrollHeight + "px";
		} else {
			document.getElementById(lb.eid).style.height = document.body.clientHeight + "px";
		}
	} else {
		if(window.innerHeight >= document.body.clientHeight){
			document.getElementById(lb.eid).style.height = window.innerHeight + "px";
		} else {
			document.getElementById(lb.eid).style.height = document.body.clientHeight + "px";
		}
	}
};

Library.prototype.setPos = function(){
	lb.setBG();
	if(document.getElementById("lbloading")){
		if(window.innerHeight){
			if(window.scrollY){
				document.getElementById("lbloading").style.top = (window.innerHeight/2 - lb.loadingH/2) + window.scrollY + "px";
			} else {
				document.getElementById("lbloading").style.top = (window.innerHeight/2 - lb.loadingH/2) + document.body.scrollTop + "px";
			}
		} else {
			if(lb.na.indexOf("MSIE 7") >= 0) {
				document.getElementById("lbloading").style.top = (document.documentElement.clientHeight/2 - lb.loadingH/2) + document.documentElement.scrollTop +  "px";
			} else {
				document.getElementById("lbloading").style.top = (document.body.clientHeight/2 - lb.loadingH/2) + document.body.scrollTop +  "px";
			}
		}
		document.getElementById("lbloading").style.left = (document.body.clientWidth/2 - lb.loadingW/2) + "px";
	}
	if(document.getElementById("lbcontents").style.display == "block"){
		if(window.innerHeight){
			if(window.scrollY){
				document.getElementById("lbcontents").style.top = (window.innerHeight/2 - lb.boxH/2) + window.scrollY + "px";
			} else {
				document.getElementById("lbcontents").style.top = (window.innerHeight/2 - lb.boxH/2) + document.body.scrollTop + "px";
			}
		} else {
			if(lb.na.indexOf("MSIE 7") >= 0) {
				document.getElementById("lbcontents").style.top = (document.documentElement.clientHeight/2 - lb.boxH/2) + document.documentElement.scrollTop +  "px";
			} else {
				document.getElementById("lbcontents").style.top = (document.body.clientHeight/2 - lb.boxH/2) + document.body.scrollTop +  "px";
			}
		}
		document.getElementById("lbcontents").style.left = (document.body.clientWidth/2 - lb.boxW/2) + "px";
	}
}

Library.prototype.showBox = function(num){
	lb.num = num;
	document.getElementById(lb.eid).style.display = "block";
	lb.setBG();
	document.getElementById(lb.eid).onclick = function(){
		lb.hideBox();
		lb.close();
	};
	lb.loadXML();
};

Library.prototype.hideBox = function(){
	document.getElementById(lb.eid).style.width = "0";
	document.getElementById(lb.eid).style.height = "0";
	document.getElementById(lb.eid).style.display = "none";
};

Library.prototype.showLoading = function(){
	if(document.getElementById("lbcontents").style.display != "block"){
		document.getElementById("lb").innerHTML = '<div id="lbloading"><img src="' + lb.loadImg.src + '" width="' + lb.loadingW + '" height="' + lb.loadingH + '" /></div>';
		document.getElementById("lbloading").style.width = lb.loadingW + "px";
		document.getElementById("lbloading").style.height = "80px";
		document.getElementById("lbloading").style.position = "absolute";
		if(window.innerHeight){
			document.getElementById("lbloading").style.top = (window.innerHeight/2 - 80/2) + "px"
		} else {
			document.getElementById("lbloading").style.top = (document.body.clientHeight/2 - 80/2) + "px";
		}
		document.getElementById("lbloading").style.left = (document.body.clientWidth/2 - 80/2) + "px";
	}
}

Library.prototype.hideLoading = function(){
	document.getElementById("lb").innerHTML = "";
}

Library.prototype.createHttpRequest = function(){
	var XHR = null;
	if(window.XMLHttpRequest){
		XHR = new XMLHttpRequest();
	} else if(window.ActiveXObject){
		try {
			XHR = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			XHR = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return XHR;
}

Library.prototype.loadXML = function(){
	var XHR = lb.createHttpRequest();
	var uri = lb.xml + "?" + Math.random();
	XHR.open('GET',uri,true);
	XHR.send(null);
	XHR.onreadystatechange = function(){
		if(XHR.readyState != 4){
			lb.showLoading();
		}
		if(XHR.readyState == 4 && XHR.status == 200){
			lb.hideLoading();
			lb.datas = XHR.responseXML;
			lb.setXML();
		}
	}
}

Library.prototype.setXML = function(){
	var plans = lb.datas.getElementsByTagName("plan");
	var questions = lb.datas.getElementsByTagName("question");
	var thumbs = lb.datas.getElementsByTagName("thumb");
	lb.changeNavi();
	document.getElementById("lbcontents").style.display = "block";
	document.getElementById("lbcontents").style.width = lb.boxW + "px";
	document.getElementById("lbcontents").style.height = lb.boxH + "px";
	document.getElementById("lbcontent").style.width = lb.boxW + "px";
	document.getElementById("lbcontent").style.height = lb.boxH + "px";
	document.getElementById("lbstepnum").innerHTML = '<img src="' + lb.path + 'step' + lb.num + '.gif" width="' + lb.stepImgW + '" height="' + lb.stepImgH + '" alt="step' + lb.num + '" />';
	if(parseInt(questions.item(parseInt(lb.num)-1).firstChild.nodeValue)){
		document.getElementById("lbquestion").innerHTML = '<img src="' + lb.path + 'icon-question.gif" width="' + lb.questionW + '" height="' + lb.questionH + '" alt="発問" title="発問" / >';
	}
	if(isNaN(thumbs.item(parseInt(lb.num)-1).firstChild.nodeValue)!=0){
		var thumb = thumbs.item(parseInt(lb.num)-1).firstChild.nodeValue;
		document.getElementById("lbthumb").innerHTML = '<a onclick="lb.zoomImg();" title=""><img src="' + thumb + '" width="' + lb.thumbW + '" height="' + lb.thumbH + '" alt="" /></a>';
	} else {
		document.getElementById("lbthumb").innerHTML = "";
	}
	lb.setPos();
	lb.loadHTML();
}

Library.prototype.loadHTML = function(){
	var XHR = lb.createHttpRequest();
	var uri = "step" + lb.num + ".html?" + Math.random();
	XHR.open('GET',uri,true);
	XHR.send(null);
	XHR.onreadystatechange = function(){
		if(XHR.readyState != 4){
			lb.showLoading();
		}
		if(XHR.readyState == 4 && XHR.status == 200){
			lb.hideLoading();
			document.getElementById("lbbody").innerHTML = XHR.responseText;
		}
	}
}

Library.prototype.zoomImg = function() {
	var imgs = lb.datas.getElementsByTagName("image");
	var img = imgs.item(parseInt(lb.num)-1).firstChild.nodeValue
	document.getElementById("lbimage").style.display = "block";
	document.getElementById("lbimage").innerHTML = '<a onclick="lb.clearImg();" title=""><img src="' + img + '" class="main-image' + lb.num + '" alt="" /></a>';
	lb.setPos();
}

Library.prototype.clearImg = function() {
	document.getElementById("lbimage").style.display = "none";
	document.getElementById("lbimage").innerHTML = "";
}

Library.prototype.changeStep = function(num){
	lb.clearImg();
	lb.num = num;
	document.getElementById("lbquestion").innerHTML = "";
	document.getElementById("lbstepnum").innerHTML = '<img src="' + lb.path + 'step' + lb.num + '.gif" width="' + lb.stepImgW + '" height="' + lb.stepImgH + '" alt="step' + lb.num + '" />';
	document.getElementById("lbthumb").innerHTML = "";
	var questions = lb.datas.getElementsByTagName("question");
	if(parseInt(questions.item(parseInt(lb.num)-1).firstChild.nodeValue)){
		document.getElementById("lbquestion").innerHTML = '<img src="' + lb.path + 'icon-question.gif" width="' + lb.questionW + '" height="' + lb.questionH + '" alt="発問" title="発問" / >';
	}
	var thumbs = lb.datas.getElementsByTagName("thumb");
	if(isNaN(thumbs.item(parseInt(lb.num)-1).firstChild.nodeValue)!=0){
		document.getElementById("lbthumb").innerHTML = '<a onclick="lb.zoomImg();" title=""><img src="' + thumbs.item(parseInt(lb.num)-1).firstChild.nodeValue + '" width="' + lb.thumbW + '" height="' + lb.thumbH + '" alt="" /></a>';
	}
	lb.loadHTML();
	lb.changeNavi();
}

Library.prototype.changeNavi = function(){
	var plans = lb.datas.getElementsByTagName("plan");
	var naviCode = "<ul>";
	for(var i = 1;i <= lb.contentsMax;i++){
		if(i<=plans.length){
			if(i == lb.num){
				naviCode += '<li id="current"><a href="step' + i + '.html" onclick="lb.changeStep(' + i + ');return false;" title="step' + i + '" id="step' + i + '">' + i + '</a></li>'
			} else {
				naviCode += '<li><a href="step' + i + '.html" onclick="lb.changeStep(' + i + ');return false;" title="step' + i + '" id="step' + i + '">' + i + '</a></li>'
			}
		} else {
			naviCode += '<li class="off"><a title="step' + i + '" id="step' + i + '">' + i + '</a></li>'
		}
	}
	naviCode += "</ul>";
	document.getElementById("lbnavi").innerHTML = naviCode;
}

Library.prototype.close = function(){
	document.getElementById("lbbody").innerHTML = "";
	document.getElementById("lbquestion").innerHTML = "";
	document.getElementById("lbcontents").style.display = "none";
	lb.clearImg();
	lb.hideBox();
}
