//#########################//
//## Stringメソッド拡張 ###//
//#########################//
String.prototype.camelize = function() {
    return this.replace(/-([a-z])/g, function($0, $1) {
    	return $1.toUpperCase()
    });
}
String.prototype.deCamelize = function() {
    return this.replace(/[A-Z]/g, function($0) {
    	return "-" + $0.toLowerCase()
    });
}

//#####################################################################################//
//## アンカー付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;
			
			var terminal;
			YAHOO.env.ua.ie != 7 ? terminal = 'BODY' : terminal = 'HTML';
			
			while(target.offsetParent.nodeName != terminal) {
				target = target.offsetParent;
				posX += target.offsetLeft;
				if(!YAHOO.env.ua.opera && YAHOO.env.ua.ie < 8 && typeof parseInt(this.style(target, "border-left-width") == "number")) {
					posX += parseInt(this.style(target, "border-left-width"));
				}
			}
			target = target.offsetParent;
			posX += target.offsetLeft;
			if(!YAHOO.env.ua.opera && YAHOO.env.ua.ie < 8 && typeof parseInt(this.style(target, "border-left-width") == "number")) {
				posX += parseInt(this.style(target, "border-left-width"));
			}
			
			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;
			
			var terminal;
			YAHOO.env.ua.ie != 7 ? terminal = 'BODY' : terminal = 'HTML';
			
			while(target.offsetParent.nodeName != terminal) {
				target = target.offsetParent;
				posY += target.offsetTop;
				if(!YAHOO.env.ua.opera && YAHOO.env.ua.ie < 8 && typeof parseInt(this.style(target, "border-top-width") == "number")) {
					posY += parseInt(this.style(target, "border-top-width"));
				}
			}
			target = target.offsetParent;
			posY += target.offsetTop;
			if(!YAHOO.env.ua.opera && YAHOO.env.ua.ie < 8 && typeof parseInt(this.style(target, "border-top-width") == "number")) {
				posY += parseInt(this.style(target, "border-top-width"));
			}
			
			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 - 17 : (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;
	},
	
	'style' : function (element, property, pseudo) {
		if(element.currentStyle) {
			//IE or Opera
			if(property.indexOf("-") != -1) property = property.camelize();
			return element.currentStyle[property];
		} else if(getComputedStyle) {
			//Mozilla or Opera
			if(property.indexOf("-") == -1) property = property.deCamelize();
			return getComputedStyle(element, pseudo).getPropertyValue(property);
		}
		
		return "";
	},
	
	'mouse' : function(e) {
		var obj = new Object;
		
		if(e) {
			obj.x = e.pageX;
			obj.y = e.pageY;
		} else {
			obj.x = event.clientX + GET.scrollX();
			obj.y = event.clientY + GET.scrollY();
		}
		
		return obj;
	}
};

//###############################//
//## #main の幅をコントロール ###//
//###############################//
var CtrlMainWidth = {
	'option' : {
		'limit' : 970,
		'border': 20
	},
	
	'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 - CtrlMainWidth.option.border - 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 - CtrlMainWidth.option.border) + "px";
			}
		}, 1);
	}
};

//###################################//
//## ヘッダとフッタをコントロール ###//
//###################################//
var CtrlHeaderFooter = {
	'option' : {
		'limit' : 970
	},
	
	'init' : function() {
		if(YAHOO.env.ua.ie == 0) {
			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) {
			header.style.width = GET.scrollX() + GET.browserWidth() + "px";
			footer.style.width = GET.scrollX() + GET.browserWidth() + "px";
		} else {
			header.style.width = "100%";
			footer.style.width = "100%";
		}
	}
};

//#################//
//## FineScroll ###//
//#################//
var FineScroll = {
	'option' : {
		'k': 0.25,
		'intervalID' : setTimeout("", 20),
		'timeoutID' : setTimeout("", 500)
	},
	
	'init' : function() {
		var that = this;
		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(that.option.intervalID);
						that.option.intervalID = that.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(that.option.intervalID);
							that.option.intervalID = that.setFunc(this.targetID);
							return false;
						}
					}
				}
			}
		}
		if(YAHOO.env.ua.webkit) {
			window.addEventListener('mousewheel', function() {
    			clearTimeout(that.option.intervalID);
			}, false);
		} else if(YAHOO.env.ua.ie || YAHOO.env.ua.opera) {
			window.onmousewheel = document.onmousewheel = function() {
				clearTimeout(that.option.intervalID);
			}
		} else {
			window.addEventListener('DOMMouseScroll', function() {
    			clearTimeout(that.option.intervalID);
			}, false);
		}
		
		if(YAHOO.env.ua.webkit && typeof window.addEventListener == 'function') {
			window.addEventListener('scroll', function() {
				clearTimeout(that.option.intervalID);
			}, false);
		}
		
		if(location.href.match(/\?anchor=/)) {
			clearInterval(this.option.intervalID);
			var query = location.search.substring(1); 
			var temp = query.split("=");
			this.option.timeoutID = setTimeout(function() {
				clearTimeout(that.option.timeoutID);
				that.option.intervalID = that.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 = 25;
			
			var end = GET.posY(id) - adjuster;
			
			this.option.intervalID = this.move(id, end);
		}
	},
	
	'move': function(id, end) {
		var that = this;
		var speed;
		
		if(Math.abs(GET.scrollY() - end) > 1) {
			speed = (end - GET.scrollY()) * this.option.k;
			window.scrollBy(0, Math.round(speed));
			
			return setTimeout(function() {
				that.option.intervalID = that.move(id, end);
			}, 20);
		} else {
			window.scrollTo(0, end);
			clearTimeout(this.option.intervalID);
		}
	}
};

//################################//
//## リンクを別ウィンドウで開く ##//
//################################//
var Popup = {
	'init' : function() {
		var that = this;
		var temp;
		var aTagsA = document.getElementsByTagName("A");
		
		for(var i = 0; i < aTagsA.length; i++) {
			temp = GET.className(aTagsA[i]);
			if(aTagsA[i].href != "" && temp != null && temp.match(/popup/)) {
				aTagsA[i].onclick = function() {
					that.dialog(this);
					return false;
				}
			}
		}
	},
	
	'dialog' : function(obj) {
		if(confirm('リンク先を別ウィンドウで開きます（あらかじめポップアップを許可しておいて下さい）')) {
			window.open(obj.href, null);
		}
	}
};

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