//#########################//
//## 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);
		}
		
		var posX = 0;
		
		do {
			posX += target.offsetLeft || 0;
			if(!YAHOO.env.ua.opera && YAHOO.env.ua.ie < 8 && parseInt(this.style(target, "border-left-width"))) {
				posX += parseInt(this.style(target, "border-left-width"));
			}
			
			target = target.offsetParent;
		} while(target);
		
		return posX;
	},
	
	'posY' : function(IDorOBJ) {
		if(typeof IDorOBJ != 'string') {
			var target = IDorOBJ;
		} else {
			var target = document.getElementById(IDorOBJ);
		}
		
		var posY = 0;
		
		do {
			posY += target.offsetTop || 0;
			if(!YAHOO.env.ua.opera && YAHOO.env.ua.ie < 8 && parseInt(this.style(target, "border-top-width"))) {
				posY += parseInt(this.style(target, "border-top-width"));
			}
			
			target = target.offsetParent;
		} while(target);
		
		return posY;
	},
	
	'scrollX' : function() {
		return document.body.scrollLeft || document.documentElement.scrollLeft;
	},
	
	'scrollY' : function() {
		return document.body.scrollTop || document.documentElement.scrollTop;
	},
	
	'browserWidth' : function() {
		return document.documentElement.clientWidth ? document.documentElement.clientWidth : (window.innerWidth ? window.innerWidth : document.body.clientWidth)
	},
	
	'browserHeight' : function() {
		return document.documentElement.clientHeight ? document.documentElement.clientHeight : (window.innerHeight ? window.innerHeight : 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' : {
		'width' : 950,
		'borderWidth': 20
	},
	
	'force' : function() {
		var that = this;
		
		var main = document.getElementById("main");
		
		if(YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
			main.style.width = this.option.width + this.option.borderWidth - 1 + "px";
		} else {
			main.style.width = this.option.width - 1 + "px";
		}
		setTimeout(function() {
			if(YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
				main.style.width = that.option.width + that.option.borderWidth + "px";
			} else {
				main.style.width = that.option.width + "px";
			}
		}, 1);
	}
};


//###################################//
//## ヘッダとフッタをコントロール ###//
//###################################//
var CtrlHeaderFooter = {
	'option' : {
		'mainWidth' : 950,
		'borderWidth': 20
	},
	
	'init' : function() {
		var that = this;
		
		if(!YAHOO.env.ua.ie) {
			var body = document.getElementsByTagName("body").item(0);
			body.style.overflowY = "scroll";
		}
		
		this.resize();
		
		if (window.addEventListener) { 
			window.addEventListener("scroll", function() {
				that.resize();
			}, false);
			window.addEventListener("resize", function() {
				that.resize();
			}, false);
		} else if (window.attachEvent) {
			window.attachEvent("onscroll", function() {
				that.resize();
			});
			window.attachEvent("onresize", function() {
				that.resize();
			});
		}
	},
	
	'resize': function() {
		var header = document.getElementById("header");
		var footer = document.getElementById("footer");
		
		if(GET.browserWidth() < this.option.mainWidth + this.option.borderWidth) {
			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() {
						clearTimeout(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() {
							clearTimeout(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 posY =  GET.posY(id);
			var pageSize = GET.pageSize();
			var browserHeight = GET.browserHeight();
			
			var adjuster = 0;
			if(location.href.match(/sche/) && document.getElementById(id).nodeName != "BODY") adjuster = 25;
			
			var end = posY - adjuster;
			if(pageSize - posY - adjuster < browserHeight) {
				end = pageSize - browserHeight;
			}
			
			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;
			speed > 0 ? speed = Math.ceil(speed) : speed = Math.floor(speed);
			
			window.scrollBy(0, speed);
			
			return setTimeout(function() {
				that.option.intervalID = that.move(id, end);
			}, 20);
		} else {
			window.scrollTo(GET.scrollX(), 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("artist/");
		location.href.match(obj);
		var artist =  (RegExp.rightContext.split('/'))[0];
		obj = new RegExp(artist);
		location.href.match(obj);
		
		return RegExp.leftContext + artist;
	}
};

//####################//
//## スマートバック ##//
//####################//
var SmartBack  = {
	'option' : {
		'mainWidth' : 950,
		'borderWidth' : 20,
		'adjuster' : 5,  //固定時の余白
		'k': 0.2, // 運動係数
		'timeoutID' : setTimeout("", 500)
	},
	
	'init' : function() {
		var that = this;
		
		//画像先読み
		var iconObj = new Image();
		iconObj.src = this.getBase() + '/img/basic/icon_back.gif';
		
		var icon = document.createElement("img");
		icon.onload = function () {
			var parent = document.getElementsByTagName("BODY")[0];
			var target = document.createElement("div");
			
			var aTag = document.createElement("a");
			aTag.setAttribute("href", "#" + parent.getAttribute("id"));
			if(aTag.getAttribute("href").match(/^#/)) {
				aTag.targetID = aTag.getAttribute("href").substr(1);
				aTag.onclick = function() {
					clearInterval(FineScroll.option.intervalID);
					FineScroll.option.intervalID = FineScroll.setFunc(this.targetID);
					return false;
				}
			} else if(aTag.getAttribute("href").indexOf("#") != -1) {
				var temp = aTag.getAttribute("href").split("#");
				var bareURL = temp[0];
				aTag.targetID = temp[temp.length - 1];
				
				if((location.href).indexOf(bareURL) != -1) {
					aTag.onclick = function() {
						clearInterval(FineScroll.option.intervalID);
						
						FineScroll.option.intervalID = FineScroll.setFunc(this.targetID);
						return false;
					}
				}
			}
			
			aTag.appendChild(this);
			target.appendChild(aTag);
			parent.appendChild(target);
			
			
			target.setAttribute("id", "back-to-top");
			target.style.top = GET.scrollY() + GET.browserHeight() - target.offsetHeight - that.option.adjuster + "px";
			target.style.visibility = "visible";
			target.intervalIDX = setTimeout("", 20)
			target.intervalIDY = setTimeout("", 20)
			
			if(typeof window.addEventListener == 'function') {
				window.addEventListener('scroll', function() {
					clearTimeout(that.option.timeoutID);
					clearTimeout(target.intervalIDY);
					
					that.option.timeoutID = setTimeout(function() {
						target.intervalIDY = that.fixed(target);
					}, 500);
				}, false);
				window.addEventListener('resize', function() {
					clearTimeout(that.option.timeoutID);
					clearTimeout(target.intervalIDY);
					
					that.option.timeoutID = setTimeout(function() {
						target.intervalIDY = that.fixed(target);
					}, 500);
				}, false);
			} else if(typeof window.attachEvent == 'object'){
				window.attachEvent('onscroll', function() {
					clearTimeout(that.option.timeoutID);
					clearTimeout(target.intervalIDY);
					
					that.option.timeoutID = setTimeout(function() {
						target.intervalIDY = that.fixed(target);
					}, 500);
				});
				window.attachEvent('onresize', function() {
					clearTimeout(that.option.timeoutID);
					clearTimeout(target.intervalIDY);
					
					that.option.timeoutID = setTimeout(function() {
						target.intervalIDY = that.fixed(target);
					}, 500);
				});
			}
			
			that.fixed(target);
		}
		icon.setAttribute("src", iconObj.src);
	},
	
	'fixed' : function(obj) {
		//X軸計算
		var endX = GET.scrollX() + this.option.adjuster;
		
		//Y軸計算
		var footerPosY = GET.posY(document.getElementById("footer"));
		
		var endY = GET.scrollY() + GET.browserHeight() - obj.offsetHeight - this.option.adjuster;
		if(endY + obj.offsetHeight + this.option.adjuster > footerPosY) {
			endY = footerPosY - obj.offsetHeight - this.option.adjuster;
		}
		
		obj.intervalIDX = this.moveX(obj, GET.posX(obj), endX);
		obj.intervalIDY = this.moveY(obj, GET.posY(obj), endY);
	},
	
	'moveX': function(obj, start, end) {
		var that = this;
		var speed;
		var tempPos;
		
		tempPos = GET.posX(obj);
		
		if(Math.abs(tempPos - end) > 1) {
			speed = (end - GET.posX(obj)) * this.option.k;
			speed > 0 ? speed = Math.ceil(speed) : speed = Math.floor(speed);
			
			obj.style.left = tempPos + speed + "px";
			
			return setTimeout(function() {
				obj.intervalIDX = that.moveX(obj, parseInt(obj.style.left), end);
			}, 20);
		} else {
			obj.style.left = end + "px";
			clearTimeout(obj.intervalIDX);
		}
	},
	
	'moveY': function(obj, start, end) {
		var that = this;
		var speed;
		var tempPos;
		
		tempPos = GET.posY(obj);
		
		if(Math.abs(tempPos - end) > 1) {
			speed = (end - GET.posY(obj)) * this.option.k;
			speed > 0 ? speed = Math.ceil(speed) : speed = Math.floor(speed);
			
			obj.style.top = tempPos + speed + "px";
			
			return setTimeout(function() {
				obj.intervalIDY = that.moveY(obj, parseInt(obj.style.top), end);
			}, 20);
		} else {
			obj.style.top = end + "px";
			clearTimeout(obj.intervalIDY);
		}
	},
	
	'getBase' : function() {
		var re1 = new RegExp("catalog/");
		location.href.match(re1);
		
		var target =  (RegExp.rightContext.split('/'))[1];
		
		var re2 = new RegExp(target);
		location.href.match(re2);
		
		return RegExp.leftContext + target;
	}
};

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