//#########################//
//## 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' : 950,
		'border': 2
	},
	
	'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' : 952
	},
	
	'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")) {
				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 DisplaySiteImage = {
	'option' : {
		'timerID' : setTimeout("", 300)
	},
	
	'init' : function() {
		var that = this;
		
		//ローディング画像先読み
		var iconObj = new Image();
		iconObj.src = this.getBase() + '/img/basic/icon_loading_s.gif';
		
		var target = document.getElementsByTagName("BODY");
		target = target[0];
		
		var aTags = target.getElementsByTagName("A");
		
		for(var i = 0, max = aTags.length; i < max; i++) {
			if(GET.className(aTags[i]) && GET.className(aTags[i]).match(/outside/)) {
				
				if(YAHOO.env.ua.ie && aTags[i].getAttribute("title")) {
					aTags[i].titleBackup = aTags[i].getAttribute("title");
					aTags[i].removeAttribute("title");
				}
				
				aTags[i].onmouseover = function(e) {
					var self = this;
					
					if(this.getAttribute("title")) {
						this.titleBackup = this.getAttribute("title");
						this.removeAttribute("title");
					}
					
					that.option.timerID = setTimeout(function() {
						self.X = GET.posX(self);
						self.Y = GET.posY(self);
						
						var href = self.getAttribute("href");
						var widthA = self.offsetWidth;
						var heightA = self.offsetHeight;
						
						//ローディングアイコン生成
						if(!document.getElementById("site-image")) {
							var icon = document.createElement("img");
							icon.onload = function () {
								var loading = document.createElement("div");
								loading.setAttribute("id", "loading_icon");
								loading.appendChild(this);
								loading.style.width = "16px";
								loading.style.height = "16px";
								
								target.appendChild(loading);
								
								if((GET.mouse(e).x - GET.scrollX()) > GET.browserWidth() / 2) {
									loading.style.left = GET.mouse(e).x - loading.offsetWidth - 15 + "px"; 
								} else {
									if(GET.mouse(e).x + loading.offsetWidth > GET.scrollX() + GET.browserWidth()) {
										loading.style.left = (GET.scrollX() + GET.browserWidth() - loading.offsetWidth - 5) + "px";
									} else {
										loading.style.left = GET.mouse(e).x + "px";
									}
								}
								
								if((GET.mouse(e).y - GET.scrollY()) > GET.browserHeight() / 2) {
									loading.style.top = self.Y - loading.offsetHeight - 5 + "px"; 
								} else {
									if(GET.mouse(e).y + loading.offsetHeight > GET.scrollY() + GET.browserHeight()) {
										loading.style.top = GET.scrollY() + GET.browserHeight() - loading.offsetHeight - 5 + "px"; 
									} else {
										loading.style.top = self.Y + self.offsetHeight + 5 + "px";
									}
								}
							}
							icon.setAttribute("src", iconObj.src);
						}
						
						//サイトイメージ生成
						that.option.timerID = setTimeout(function() {
							var img = document.createElement("img");
							img.onload = function() {
								if(document.getElementById("loading_icon")) {
									var erase = document.getElementById("loading_icon");
									erase.parentNode.removeChild(erase);
								}
								
								var div = document.createElement("div");
								div.setAttribute("id", "site-image");
								div.style.MozBorderRadius = "10px";
								div.style.WebkitBorderRadius = "10px";
								div.opacity = 0;
								div.intervalID = setTimeout("", 30);
								
								div.appendChild(this);
								if(self.titleBackup) {
									var span = document.createElement("span");
									var text = document.createTextNode(self.titleBackup);
									span.appendChild(text);
									div.appendChild(span);
								}
								
								if(document.getElementById("loading_icon")) {
									var erase = document.getElementById("loading_icon");
									erase.parentNode.removeChild(erase);
								}
								
								target.appendChild(div);
								
								if((GET.mouse(e).x - GET.scrollX()) > GET.browserWidth() / 2) {
									div.style.left = GET.mouse(e).x - div.offsetWidth - 5 + "px"; 
								} else {
									if(GET.mouse(e).x + div.offsetWidth > GET.scrollX() + GET.browserWidth()) {
										div.style.left = GET.scrollX() + GET.browserWidth() - div.offsetWidth - 5 + "px";
									} else {
										div.style.left = GET.mouse(e).x + 5 + "px";
									}
								}
								
								if((GET.mouse(e).y - GET.scrollY()) > GET.browserHeight() / 2) {
									div.style.top = self.Y - div.offsetHeight - 5 + "px"; 
								} else {
									if(GET.mouse(e).y + div.offsetHeight > GET.scrollY() + GET.browserHeight()) {
										div.style.top = GET.scrollY() + GET.browserHeight() - div.offsetHeight - 5 + "px"; 
									} else {
										div.style.top = self.Y + self.offsetHeight + 5 + "px";
									}
								}
								
								that.fadeIn(div);
							}
							img.setAttribute("width", 150);
							img.setAttribute("height", 180);
							img.setAttribute("src", 'http://capture.heartrails.com/150x180/delay=5?' + href);
						}, 300);
					}, 100);
				}
				
				aTags[i].onmouseout = function() {
					clearTimeout(that.option.timerID);
					
					if(!YAHOO.env.ua.ie && this.titleBackup) {
						this.setAttribute("title", this.titleBackup);
					}
					
					if(document.getElementById("site-image")) {
						var erase = document.getElementById("site-image");
						erase.parentNode.removeChild(erase);
					}
					if(document.getElementById("loading_icon")) {
						var erase = document.getElementById("loading_icon");
						erase.parentNode.removeChild(erase);
					}
				}
			}
		}
	},
	
	'fadeIn': function(obj) {
		var that = this;
		
		if(obj.opacity <= 10) {
			obj.style.filter = 'alpha(opacity=' + obj.opacity * 10 + ')'; // IE
			obj.style.MozOpacity = obj.opacity / 10; // Firefox
			obj.style.opacity = obj.opacity / 10; // Safari
			obj.opacity += 1;
			
			return setTimeout(function() {
				obj.intervalID = that.fadeIn(obj);
			}, 30);
		} else {
			clearTimeout(obj.intervalID);
			if(YAHOO.env.ua.ie) obj.style.removeAttribute('filter');
		}
	},
	
	'getBase' : function() {
		var obj = new RegExp("takumi/");
		location.href.match(obj);
		var artist =  (RegExp.rightContext.split('/'))[0];
		obj = new RegExp(artist);
		location.href.match(obj);
		
		return RegExp.leftContext + artist;
	}
};

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