//#########################//
//## 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' : {
		'touchID' : setTimeout("", 200), //A要素にタッチしてもすぐに反応しないように制御
		'siteID' : setTimeout("", 300) //サイトイメージ表示までの時差
	},
	
	'init' : function() {
		var that = this;
		
		//ローディング画像先読み
		var iconObj = new Image();
		iconObj.src = this.getBase() + '/img/basic/icon_loading_s.gif';
		
		//ローディングアイコン用 img要素
		var icon = document.createElement("img");
		icon.setAttribute("width", 16);
		icon.setAttribute("height", 16);
		
		//サイトイメージ用 img要素
		var thmbnail = document.createElement("img");
		thmbnail.setAttribute("width", 150);
		thmbnail.setAttribute("height", 180);
		
		//ローディングアイコン用 div要素
		var loading = document.createElement("div");
		loading.setAttribute("id", "loading-icon");
		loading.style.width = "16px";
		loading.style.height = "16px";
		
		//サイトイメージ用 div要素
		var site = document.createElement("div");
		site.setAttribute("id", "site-image");
		site.style.MozBorderRadius = "10px";
		site.style.MozBoxShadow = "2px 2px 8px #000000";
		site.style.WebkitBorderRadius = "10px";
		site.style.WebkitBoxShadow = "2px 2px 8px #000000";
		site.intervalID = setTimeout("", 30);
		
		//補足説明用の span要素
		var span = document.createElement("span");
		span.setAttribute("id", "site-title")
		site.appendChild(span);
		
		var bodyTag = document.getElementsByTagName("BODY")[0];
		
		var aTags = bodyTag.getElementsByTagName("A");
		
		for(var i = 0, max = aTags.length; i < max; i++) {
			if(GET.className(aTags[i]) && GET.className(aTags[i]).match(/outside/)) {
				
				//title属性の削除（for ie）
				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;
					
					clearTimeout(that.option.touchID);
					clearTimeout(that.option.siteID);
					
					//サイトイメージが残っていたら削除する（保険）
					if(document.getElementById("loading-icon")) {
						var erase = document.getElementById("loading-icon");
						erase.parentNode.removeChild(erase);
					}
					if(document.getElementById("site-image")) {
						var erase = document.getElementById("site-image");
						erase.parentNode.removeChild(erase);
					}
					
					if(!document.getElementById("loading-icon") && !document.getElementById("site-image")) {
						
						that.option.touchID = setTimeout(function() {
							//title属性の一時退避
							if(!YAHOO.env.ua.ie && self.getAttribute("title")) {
								self.titleBackup = self.getAttribute("title");
								self.removeAttribute("title")
							}
							
							self.X = GET.posX(self);
							self.Y = GET.posY(self);
							
							var href = self.getAttribute("href");
							var widthA = self.offsetWidth;
							var heightA = self.offsetHeight;
							
							//ローディングアイコンを読込み終えた時の動作
							icon.onload = function () {
								loading.appendChild(this);
								bodyTag.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";
									}
								}
								
								//引き続き、サイトイメージ生成（300ms後）
								that.option.siteID = setTimeout(function() {
									thmbnail.onload = function() {
										//ローディングアイコンの削除
										if(document.getElementById("loading-icon")) {
											icon.setAttribute("src", "");
											var erase = document.getElementById("loading-icon");
											erase.parentNode.removeChild(erase);
										}
										
										site.opacity = 0;
										site.insertBefore(this, span);
										
										//title属性がある場合はサムネイルに補足として表示させる
										if(self.titleBackup) {
											span.innerHTML = self.titleBackup;
										} else {
											span.innerHTML = "";
											span.style.display = "none";
										}
										
										bodyTag.appendChild(site);
										
										//サイトイメージにカーソル合わせると自身を消去（保険）
										site.onmouseover = function() {
											this.parentNode.removeChild(this);
										}
										
										if((GET.mouse(e).x - GET.scrollX()) > GET.browserWidth() / 2) {
											site.style.left = GET.mouse(e).x - site.offsetWidth - 5 + "px"; 
										} else {
											if(GET.mouse(e).x + site.offsetWidth > GET.scrollX() + GET.browserWidth()) {
												site.style.left = GET.scrollX() + GET.browserWidth() - site.offsetWidth - 5 + "px";
											} else {
												site.style.left = GET.mouse(e).x + 5 + "px";
											}
										}
										
										if((GET.mouse(e).y - GET.scrollY()) > GET.browserHeight() / 2) {
											site.style.top = self.Y - site.offsetHeight - 5 + "px"; 
										} else {
											if(GET.mouse(e).y + site.offsetHeight > GET.scrollY() + GET.browserHeight()) {
												site.style.top = GET.scrollY() + GET.browserHeight() - site.offsetHeight - 5 + "px"; 
											} else {
												site.style.top = self.Y + self.offsetHeight + 5 + "px";
											}
										}
										
										that.fadeIn(site);
									}
									thmbnail.setAttribute("src", 'http://capture.heartrails.com/150x180/delay=5?' + href);
								}, 300);
							}
							//ローディングアイコンを読み込む
							icon.setAttribute("src", iconObj.src);
						}, 200);
					}
				}
				
				aTags[i].onmouseout = function() {
					clearTimeout(that.option.touchID);
					clearTimeout(that.option.siteID);
					
					//span 要素の可視化
					span.style.display = "block";
					
					//title属性の復帰（for ie）
					if(!YAHOO.env.ua.ie && this.titleBackup) {
						this.setAttribute("title", this.titleBackup);
					}
					
					//画像ソースのリセット
					icon.setAttribute("src", "");
					site.setAttribute("src", "");
					
					//div要素の削除
					if(document.getElementById("loading-icon")) {
						var erase = document.getElementById("loading-icon");
						erase.parentNode.removeChild(erase);
					}
					if(document.getElementById("site-image")) {
						var erase = document.getElementById("site-image");
						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("artist/");
		location.href.match(re1);
		
		var artist =  (RegExp.rightContext.split('/'))[0];
		
		var re2 = new RegExp(artist);
		location.href.match(re2);
		
		return RegExp.leftContext + artist;
	}
};

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