//## 注意書きに追記 ##//
var AddNotice = {
	'init' : function() {
		
		var parent = document.getElementById("notice");
		var ulTags = parent.getElementsByTagName("UL")
		
		var li1 = document.createElement("li");
		li1.innerHTML = '「全てを表示」を選択すると、過去1年間に作品をリリースしたアーティストの名前が追加表示されます。（洋楽アーティストを除く）';
		
		var li2 = document.createElement("li");
		li2.innerHTML = '<img src="../img/basic/icon_photo.png" width="11" height="9" alt="写真表示アイコン" /> が表示されている項目は、マウスカーソルを合わせると関連した画像が表示されます。';
		
		ulTags[0].appendChild(li1);
		ulTags[0].appendChild(li2);
		
		FixedItem.init('jump');
	}
};

//#############################//
//## アーティストフォト追加 ###//
//#############################//
var AddPhoto = {
	'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 parent = document.getElementById("list");
		var aTags = parent.getElementsByTagName("A");
		
		for(var i = 0, max = aTags.length; i < max; i++) {
			if(aTags[i].parentNode.nodeName == "LI" && aTags[i].getAttribute("rel")) {
				if(YAHOO.env.ua.ie && YAHOO.env.ua.ie < 8) {
					aTags[i].setAttribute("className", "photo");
				} else {
					aTags[i].setAttribute("class", "photo");
				}
				
				aTags[i].onmouseover = function(e) {
					var self = this;
					
					that.option.timerID = setTimeout(function() {
						self.X = GET.posX(self);
						self.Y = GET.posY(self);
						
						var rel = self.getAttribute("rel");
						var widthA = self.offsetWidth;
						var heightA = self.offsetHeight;
						
						//ローディングアイコン生成
						if(!document.getElementById("photo")) {
							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 temp = document.createElement("img");
							temp.onload = 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", "photo");
									div.style.MozBorderRadius = "10px";
									div.style.WebkitBorderRadius = "10px";
									div.opacity = 0;
									div.intervalID = setTimeout("", 30);
									
									div.appendChild(this);
									
									var span = document.createElement("span");
									var text = document.createTextNode(self.innerHTML);
									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);
								}
								if(this.width < this.height) {
									if(this.width < 150) {
										img.setAttribute("src", 'http://www.teichiku.co.jp/script/original/ctrl_image.php?src=' + this.getAttribute("src") + '&width=' + this.width);
									} else {
										img.setAttribute("src", 'http://www.teichiku.co.jp/script/original/ctrl_image.php?src=' + this.getAttribute("src") + '&width=150');
									}
								} else {
									if(this.height < 150) {
										img.setAttribute("src", 'http://www.teichiku.co.jp/script/original/ctrl_image.php?src=' + this.getAttribute("src") + '&height=' + this.height);
									} else {
										img.setAttribute("src", 'http://www.teichiku.co.jp/script/original/ctrl_image.php?src=' + this.getAttribute("src") + '&height=150');
									}
								}
							}
							temp.setAttribute("src", rel);
						}, 300);
					}, 100);
				}
				
				aTags[i].onmouseout = function() {
					clearTimeout(that.option.timerID);
					
					if(document.getElementById("photo")) {
						var erase = document.getElementById("photo");
						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("\.co\.jp/|htdocs/");
		location.href.match(obj);
		var base =  (RegExp.rightContext.split('/'))[0];
		obj = new RegExp(base);
		location.href.match(obj);
		
		return RegExp.leftContext;
	}
};

var FixedItem  = {
	'option' : {
		'adjuster' : 5,  //固定時のWindow 最上部からの余白
		'k': 0.3, // 運動係数
		'timeoutID' : setTimeout("", 500)
	},
	
	'init' : function(id) {
		var that = this;
		
		var target = document.getElementById(id);
		target.posY = 0;
		target.defaultPosY = GET.posY(target);
		target.defaultTop = parseInt(GET.style(target, "top"));
		target.intervalID = setTimeout("", 20);
		
		if(typeof window.addEventListener == 'function') {
			window.addEventListener('scroll', function() {
				clearTimeout(that.option.timeoutID);
				clearTimeout(target.intervalID);
				
				that.chkPos(target);
				that.option.timeoutID = setTimeout(function() {
					that.fixed(target);
				}, 500);
			}, false);
		} else if(typeof window.attachEvent == 'object'){
			window.attachEvent('onscroll', function() {
				clearTimeout(that.option.timeoutID);
				clearTimeout(target.intervalID);
				
				if(YAHOO.env.ua.ie) target.style.removeAttribute('filter');
				
				that.chkPos(target);
				that.option.timeoutID = setTimeout(function() {
					that.fixed(target);
				}, 500);
			});
		}
		
		this.fixed(target);
	},
	
	'fixed' : function(obj) {
		obj.posY = GET.posY(obj);
		
		if(GET.scrollY() > obj.defaultPosY + obj.offsetHeight) {
		 	obj.footerHeight = document.getElementById("footer").offsetHeight;
		 	
			var start = obj.defaultTop + (GET.scrollY() - (obj.posY + obj.offsetHeight)) - 5;
		 	obj.style.top = start + "px";
		 	
		 	if(YAHOO.env.ua.ie) obj.style.removeAttribute('filter');
		 	obj.style.MozOpacity = 1; // Firefox
			obj.style.opacity = 1; // Safari
		 	
		 	obj.style.visibility = "visible";
		 	document.getElementById('return').style.display = "block";
		 	
		 	obj.intervalID = this.move(obj, GET.scrollY() + this.option.adjuster);
		} else {
			if(obj.defaultPosY != GET.posY(obj)) {
			 	obj.opacity = 0;
			 	
			 	obj.style.top = obj.defaultTop + "px";
		 		obj.style.visibility = "visible";
		 		
		 		obj.intervalID = this.fadeIn(obj);
			}
		 }
	},
	
	'move': function(obj, end) {
		var that = this;
		var speed;
		var tempPos;
		
		tempPos = parseInt(GET.style(obj, "top"));
		if(Math.abs(GET.posY(obj) - end) > 1) {
			if(end + obj.offsetHeight + obj.footerHeight < GET.pageSize()) {
				speed = (end - GET.posY(obj)) * this.option.k;
				
				obj.style.top = tempPos + Math.round(speed) + "px";
				
				return setTimeout(function() {
					obj.intervalID = that.move(obj, end);
				}, 20);
			} else {
				clearTimeout(obj.intervalID);
			}
		} else {
			clearTimeout(obj.intervalID);
		}
	},
	
	'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);
			}, 20);
		} else {
			clearTimeout(obj.intervalID);
			if(YAHOO.env.ua.ie) obj.style.removeAttribute('filter');
		}
	},
	
	'chkPos' : function(obj) {
		if(obj.defaultPosY != GET.posY(obj)) {
			obj.style.visibility = "hidden";
			document.getElementById('return').style.display = "none";
		}
	}
};

//## リストコントロール ##//
var CtrlList = {
	'init' : function() {
		var ctrl = document.getElementById("ctrl");
		var ul = document.createElement("ul");
		ul.setAttribute("id", "selector");
		ul.innerHTML = '<li><input id="selectorA" type="radio" name="type" value="A" checked="checked" /><label for="selectorA">標準リスト</label></li><li><input id="selectorB" type="radio" name="type" value="B" /><label for="selectorB">全てを表示</label></li>'
		
		ctrl.appendChild(ul);
		
		var limit = new Date();
		limit.setTime(limit.getTime() + (1000 * 60 * 60 * 24 * 30));
		
		var div = document.getElementById("list");
		
		var A = document.getElementById("selectorA");
		A.onclick = function() {
			document.cookie = "ArtistListSelector=A;expires=" + limit.toGMTString() + ";";
			CtrlList.change('A');
		}
		
		var B = document.getElementById("selectorB");
		B.onclick = function() {
			document.cookie = "ArtistListSelector=B;expires=" + limit.toGMTString() + ";";
			CtrlList.change('B');
		}
		
		if(document.cookie.indexOf("ArtistListSelector=B") != -1) {
			B.checked = "checked";
			CtrlList.change('B');
		} else {
			A.checked = "checked";
			CtrlList.change('A');
		}
		
		Loading.remove();
		div.style.visibility = "visible";
	},
	
	'change' : function(flag) {
		var parent = document.getElementById("list");
		var liTags = parent.getElementsByTagName("LI");
		var max = liTags.length;
		
		for(var i = 0; i < max; i++) {
			if(GET.className(liTags[i]) != '' && GET.className(liTags[i]) != null) {
				switch(flag) {
					case 'A':
						if(GET.className(liTags[i]).match(/^release_\d\d\d\d-\d\d-\d\d/)) {
							var parts = GET.className(liTags[i]).split(" ");
							
							parts[0] = parts[0].replace("release_", "");
							var YMD = parts[0].split("-");
							var d = new Date(YMD[0], YMD[1]-1, YMD[2]);
							var d = new Date(YMD[0], YMD[1]-1, YMD[2]);
						}
						
						if(serverTime - d.getTime() < 1000 * 60 * 60 * 24 * 90) {
							liTags[i].style.display = "block";
							liTags[i].style.borderColor = "#ffffff";
							liTags[i].style.background = "transparent url(../img/basic/back_line.gif) 50% 100% no-repeat";
						} else {
							liTags[i].style.display = "none";
						}
						break;
					case 'B':
						if(GET.className(liTags[i]).match(/^release_\d\d\d\d-\d\d-\d\d/)) {
							var parts = GET.className(liTags[i]).split(" ");
							
							parts[0] = parts[0].replace("release_", "");
							var YMD = parts[0].split("-");
							var d = new Date(YMD[0], YMD[1]-1, YMD[2]);
							var d = new Date(YMD[0], YMD[1]-1, YMD[2]);
						}
						if(serverTime - d.getTime() < 1000 * 60 * 60 * 24 * 90) {
							liTags[i].style.display = "block";
							liTags[i].style.borderColor = "#ffffff";
							liTags[i].style.background = "transparent url(../img/basic/back_line.gif) 50% 100% no-repeat";
						} else if(serverTime - d.getTime() < 1000 * 60 * 60 * 24 * 365) {
							liTags[i].style.display = "block";
							liTags[i].innerHTML += '<span title="最新リリース：' + parts[0] + '">' + parts[0] + '</span>';
						}
						break;
					default:
						break;
				}
			}
		}
		if(YAHOO.env.ua.webkit == 0 || YAHOO.env.ua.webkit >= 522) AddPhoto.init();
		if(YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) setTimeout(function() { FlexMainWidth.force(); }, 500);
	}
}

//## イニシャライズ ##//
YAHOO.util.Event.onDOMReady(function() {
	//if(YAHOO.env.ua.webkit == 0) AniMenu.init();
	//SearchRank.init('../');
	CtrlList.init();
	AddNotice.init();
	RandomBack.init();
});

