//#####################################################################################//
//## アンカー付URLだったらアンカー情報をGETメソッドパラメータに変更してリダイレクト ###//
//#####################################################################################//
var ConvertURL = {
	'init' : function() {
		if(location.href.match(/#/)) {
			var temp = location.href.split("#");
			var baseURL = temp[0];
			var anchor = temp[temp.length - 1];
			location.replace(baseURL + "?anchor=" + anchor);
		}
	}
}
ConvertURL.init();

//###################//
//## 各種情報取得 ###//
//###################//
var GET = {
	'className' : function(obj) {
		return obj.getAttribute('class') || obj.getAttribute('className');
	},
	
	'posX' : function(IDorOBJ) {
		if(typeof IDorOBJ != 'string') {
			var target = IDorOBJ;
		} else {
			var target = document.getElementById(IDorOBJ);
		}
		
		if(target.nodeName == "BODY") {
			return 0;
		} else {
			var posX = target.offsetLeft;
			while(target.offsetParent.nodeName != "BODY") {
				target = target.offsetParent;
				posX += target.offsetLeft;
			}
			target = target.offsetParent;
			posX += target.offsetLeft;
			
			return posX;
		}
	},
	
	'posY' : function(IDorOBJ) {
		if(typeof IDorOBJ != 'string') {
			var target = IDorOBJ;
		} else {
			var target = document.getElementById(IDorOBJ);
		}
		
		if(target.nodeName == "BODY") {
			return 0;
		} else {
			var posY = target.offsetTop;
			if(YAHOO.env.ua.ie != 7) {
				while(target.offsetParent.nodeName != "BODY") {
					target = target.offsetParent;
					posY += target.offsetTop;
				}
			} else {
				while(target.offsetParent.nodeName != "HTML") {
					target = target.offsetParent;
					posY += target.offsetTop;
				}
			}
			target = target.offsetParent;
			posY += target.offsetTop;
			
			return posY;
		}
	},
	
	'scrollX' : function() {
		return document.body.scrollLeft || document.documentElement.scrollLeft;
	},
	
	'scrollY' : function() {
		return document.body.scrollTop || document.documentElement.scrollTop;
	},
	
	'browserWidth' : function() {
		return window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth);
	},
	
	'browserHeight' : function() {
		return window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
	},
	
	'pageSize' : function() {
		return document.body.scrollHeight ? document.body.scrollHeight : document.documentElement.scrollHeight;
	}
}


//###############################//
//## #main の幅をコントロール ###//
//###############################//
var CtrlMainWidth = {
	'option' : {
		'limit' : 950
	},
	
	'force' : function() {
		var main = document.getElementById("main");
		
		if(YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
			main.style.width = (CtrlMainWidth.option.limit - 1) + "px";
		} else {
			main.style.width = (CtrlMainWidth.option.limit - 1) + "px";
		}
		setTimeout(function() {
			if(YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
				main.style.width = CtrlMainWidth.option.limit + "px";
			} else {
				main.style.width = (CtrlMainWidth.option.limit - 2) + "px";
			}
		}, 1);
	}
}

//###################################//
//## ヘッダとフッタをコントロール ###//
//###################################//
var CtrlHeaderFooter = {
	'option' : {
		'limit' : 950
	},
	
	'init' : function() {
		if(!YAHOO.env.ua.ie) {
			var body = document.getElementsByTagName("body").item(0);
			body.style.overflowY = "scroll";
		}
		
		CtrlHeaderFooter.resize();
		
		if (window.addEventListener) { 
			window.addEventListener("scroll", function() {
				CtrlHeaderFooter.resize();
			}, false);
			window.addEventListener("resize", function() {
				CtrlHeaderFooter.resize();
			}, false);
		} else if (window.attachEvent) {
			window.attachEvent("onscroll", function() {
				CtrlHeaderFooter.resize();
			});
			window.attachEvent("onresize", function() {
				CtrlHeaderFooter.resize();
			});
		}
	},
	
	'resize': function() {
		var header = document.getElementById("header");
		var footer = document.getElementById("footer");
		
		if(GET.browserWidth() < CtrlHeaderFooter.option.limit) {
			if(YAHOO.env.ua.ie && YAHOO.env.ua.ie < 9) {
				header.style.width = GET.scrollX() + GET.browserWidth() + "px";
				footer.style.width = GET.scrollX() + GET.browserWidth() + "px";
			} else {
				header.style.width = GET.scrollX() + GET.browserWidth() - 17 + "px";
				footer.style.width = GET.scrollX() + GET.browserWidth() - 17 + "px";
			}
		} else {
			header.style.width = "100%";
			footer.style.width = "100%";
		}
	}
}

//#################//
//## FineScroll ###//
//#################//
var FineScroll = {
	'option' : {
		'timerID' : setInterval("", 20),
		'timerID2' : setTimeout("", 500)
	},
	
	'init' : function() {
		var a = document.getElementsByTagName("A");
		var max = a.length;
		
		for(var i = 0; i < max; i++) {
			if(a[i].getAttribute("href") != null) {
				if(a[i].getAttribute("href").match(/^#/)) {
					a[i].targetID = a[i].getAttribute("href").substr(1);
					a[i].onclick = function() {
						clearInterval(FineScroll.option.timerID);
						FineScroll.option.timerID = FineScroll.setFunc(this.targetID);
						return false;
					}
				} else if(a[i].getAttribute("href").indexOf("#") != -1) {
					var temp = a[i].getAttribute("href").split("#");
					var bareURL = temp[0];
					a[i].targetID = temp[temp.length - 1];
					
					if((location.href).indexOf(bareURL) != -1) {
						a[i].onclick = function() {
							clearInterval(FineScroll.option.timerID);
							FineScroll.option.timerID = FineScroll.setFunc(this.targetID);
							return false;
						}
					}
				}
			}
		}
		if(YAHOO.env.ua.ie || 0 < YAHOO.env.ua.opera) {
			window.onmousewheel = document.onmousewheel = function() {
				clearInterval(FineScroll.option.timerID);
			}
		} else {
			window.addEventListener('DOMMouseScroll', function() {
    			clearInterval(FineScroll.option.timerID);
			}, false);
		}
		
		if(location.href.match(/\?anchor=/)) {
			clearInterval(FineScroll.option.timerID);
			var query = location.search.substring(1); 
			var temp = query.split("=");
			FineScroll.option.timerID2 = setTimeout(function() {
				clearTimeout(FineScroll.option.timerID2);
				FineScroll.option.timerID = FineScroll.setFunc(temp[temp.length - 1]);
			}, 500);
    	}
	},
	
	'setFunc' : function(id) {
		if(document.getElementById(id)) {
			var adjuster = 0;
			if(location.href.match(/sche/) && document.getElementById(id).nodeName != "BODY") adjuster = 30;
			
			var posY = GET.posY(id) - adjuster;
			var speed = 0;
			var k = 0.25;
			
			return setInterval(function() {
				if(GET.scrollY() != Math.floor(posY)) {
					speed = posY - GET.scrollY();
					speed = speed * k >= 0 ? (speed * k > 1 ? speed * k : 1) : (speed * k > -1 ? -1 : speed * k);
					window.scrollBy(0, speed);
					if(GET.scrollY() + GET.browserHeight() == GET.pageSize()) {
						clearInterval(FineScroll.option.timerID);
					}
				} else {
					clearInterval(FineScroll.option.timerID);
				}
			}, 20);
		}
	}
};

//################################//
//## 閲覧人数カウントプログラム ##//
//################################//
var WhoIsOnline = {
	'option' : {
		'repeatID' : setTimeout("", 30000)
	},
	
	'init' : function() {
		var that = this;
		
		if(window.XMLHttpRequest) {
			var message = new XMLHttpRequest;
		} else if(window.ActiveXObject) {
			try {
				var message = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				var message = new ActiveXObject("Microsoft.XMLHTTP");
			}
		} else {
			return null;
		}
		
		message.onreadystatechange = function() {
			if(message.readyState == 4) {
				if(message.status == 200) {
					//成功
				} else {
					//失敗
				}
			} else {
				//取得中
			}
		}
		
		if(location.href.match(/^http:/)) {
			this.option.repeatID = this.open(message);
		}
	},
	
	'open' : function(xmlhttp) {
		var that = this;
		
		try {
			var dummyTime = (new Date()).getTime();
			
			xmlhttp.open('GET', location.protocol + "//www.teichiku.co.jp/script/analyze/who_is_online.php?url=" + encodeURIComponent(location.href.replace(/index\.(html|php)/, "")) + "&t=" + dummyTime, true);
			xmlhttp.send(null);
			
			return setTimeout(function() {
				that.option.repeatID = that.open(xmlhttp);
			}, 30000);
		} catch(e) {
			
		}
	}
};

//####################//
//## イニシャライズ ##//
//####################//
YAHOO.util.Event.onDOMReady(function() {
	FineScroll.init();
	CtrlHeaderFooter.init();
	WhoIsOnline.init();
});
