//#####################################################################################//
//## アンカー付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();

//#######################//
//## クラス名取得関数 ###//
//#######################//
function getClassName(obj) {
	return obj.getAttribute('class') || obj.getAttribute('className');
}

//##############################//
//## #main に max-width 再現 ###//
//##############################//
var FlexMainWidth = {
	'init' : function () {
		if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
			window.attachEvent('onresize',  this.chkWidth);
			this.chkWidth();
		}
	},
	
	'chkWidth' : function() {
		var limit = 1024;
		var main = document.getElementById("main");
		var width = document.body.clientWidth;
		
		if(width < limit) {
			main.style.width = "99%";
		} else {
			main.style.width = "1024px";
		}
	},
	
	'force' : function() {
		var limit = 1024;
		var main = document.getElementById("main");
		var width = document.body.clientWidth;
		
		if(width < limit) {
			main.style.width = "98.9%";
			setTimeout(function() {	
				main.style.width = "99%";
			}, 1);
		} else {
			main.style.maxWidth = (limit - 1) + "px";
			if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
				main.style.width = (limit - 1) + "px";
			}
			setTimeout(function() {
				main.style.maxWidth = limit + "px";
				if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
					main.style.width = limit + "px";
				}
			}, 1);
		}
	}
}

//#################//
//## FineScroll ###//
//#################//
var FineScroll = {
	'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() {
						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() {
							FineScroll.setFunc(this.targetID);
							return false;
						}
					}
				}
			}
		}
		FineScroll.chkGET();
	},
	
	'setFunc' : function(id) {
		if(document.getElementById(id)) {
			var target = document.getElementById(id);
			var posY = target.offsetTop;
			if(YAHOO.env.ua.ie == 0 || (document.compatMode != 'CSS1Compat' && YAHOO.env.ua.ie < 7) || 8 <= YAHOO.env.ua.ie) {
				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;
			
			var speed = 0;
			var k = 0.25;
			
			var timerID = setInterval(function() {
				if(FineScroll.getScroll() != Math.floor(posY)) {
					speed = posY - FineScroll.getScroll();
					speed = speed * k >= 0 ? (speed * k > 1 ? speed * k : 1) : (speed * k > -1 ? -1 : speed * k);
					window.scrollBy(0, speed);
					if(FineScroll.getScroll() + FineScroll.getBrowserHeight() == FineScroll.getPageSize()) {
						clearInterval(timerID);
					}
				} else {
					clearInterval(timerID);
				}
			}, 20);
			
			if(0 < YAHOO.env.ua.ie || 0 < YAHOO.env.ua.opera) {
				window.onmousewheel = document.onmousewheel = function() {
					clearInterval(timerID);
				}
			} else {
				window.addEventListener('DOMMouseScroll', function() {
    				clearInterval(timerID);
				}, false);
			}
		}
	},
	
	'chkGET' : function() {
		if(location.href.match(/\?anchor=/)) {
			var query = location.search.substring(1); 
			var temp = query.split("=");
			setTimeout(function() {
				FineScroll.setFunc(temp[temp.length - 1]);
			}, 500);
    	}
	},
	
	'getScroll' : function() {
		return document.body.scrollTop || document.documentElement.scrollTop;
	},
	
	'getBrowserHeight' : function() {
		return window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
	},
	
	'getPageSize' : function() {
		return document.body.scrollHeight ? document.body.scrollHeight : document.documentElement.scrollHeight;
	}
}

//################################//
//## 閲覧人数カウントプログラム ##//
//################################//
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() {
	FlexMainWidth.init();
	FineScroll.init();
	WhoIsOnline.init();
});
