//## IE6で透明PNG使用 ##//
var ChangePNG = {
	'init' : function() {
		if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7 && document.getElementById("h2_artist")) {
			var h2 = document.getElementById("h2_artist");
			h2.runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="../img/basic/back_h2_artist.png", sizingmethod="scale")';
			h2.style.background = "none";
		}
	}
};

//## 注意書きに追記 ##//
var AddNotice = {
	'init' : function() {
		var parent = document.getElementById("artist-list");
		var target = document.getElementById("insertPointArtist")
		
		var div = document.createElement("div");
		div.setAttribute("id", "notice");
		
		var h3 = document.createElement("h3");
		h3.innerHTML = 'information';
		
		var ul = document.createElement("ul");
		ul.innerHTML = '<li>「全てを表示」を選択すると、過去に作品をリリースしたアーティスト（Catalog Artist）の名前が追加表示されます。</li><li><img src="../img/basic/icon_photo.png" width="11" height="9" alt="写真表示アイコン" /> が表示されている項目は、マウスカーソルを合わせると関連した画像が表示されます。</li>';
		
		div.appendChild(h3);
		div.appendChild(ul);
		
		parent.insertBefore(div, target);
	}
}

//#############################//
//## アーティストフォト追加 ###//
//#############################//
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 == "DT" && 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("imp/");
		location.href.match(obj);
		var base =  (RegExp.rightContext.split('/'))[0];
		obj = new RegExp(base);
		location.href.match(obj);
		
		return RegExp.leftContext;
	}
};

//## アーティストフォト追加 ##//
var _AddPhoto = {
	'option' : {
		'timerID1' : setTimeout("", 300),
		'timerID2' : setTimeout("", 50)
	},
	
	'init' : function() {
		//ローディング画像先読み
		var iconObj = new Image();
		iconObj.src = '../img/basic/icon_loading_s.gif';
		
		var target = document.getElementById("imperial");
		var parent = document.getElementById("list");
		var aTags = parent.getElementsByTagName("A");
		var max = aTags.length;
		
		for(var i = 0; i < max; i++) {
			if(aTags[i].parentNode.nodeName == "DT" && (aTags[i].getAttribute("rel") != null && aTags[i].getAttribute("rel") != "")) {
				if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 8) {
					aTags[i].setAttribute("className", "photo");
				} else {
					aTags[i].setAttribute("class", "photo");
				}
				
				aTags[i].onmouseover = function() {
					clearTimeout(AddPhoto.option.timerID2);
					
					if(document.getElementById("photo")) {
						var erase = document.getElementById("photo");
						erase.parentNode.removeChild(erase);
						delete erase;
					}
					if(document.getElementById("loading_icon")) {
						var erase = document.getElementById("loading_icon");
						erase.parentNode.removeChild(erase);
						delete erase;
					}
					
					var pos = new Object(); 
					pos.X = GET.posX(this);
					pos.Y = GET.posY(this);
					
					var rel = this.getAttribute("rel");
					var widthA = this.offsetWidth;
					var heightA = this.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(pos.X > GET.browserWidth() / 2) {
								loading.style.left = (pos.X - parseInt(loading.style.width) - 5) + "px"; 
							} else {
								loading.style.left = (pos.X + widthA + 5) + "px"; 
							}
							loading.style.top = pos.Y + "px";
						}
						icon.setAttribute("src", iconObj.src);
					}
					
					//アーティストフォト生成
					AddPhoto.option.timerID1 = 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);
									delete erase;
								}
								
								if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
									var margin = 12;
								} else {
									var margin = 10;
								}
								
								var div = document.createElement("div");
								div.setAttribute("id", "photo");
								div.style.width = (this.width + margin) + "px";
								div.style.height = (this.height + margin) + "px";
								
								div.appendChild(this);
								target.appendChild(div);
								
								if(pos.X > GET.browserWidth() / 2) {
									div.style.left = (pos.X - parseInt(div.style.width) - 5) + "px"; 
								} else {
									div.style.left = (pos.X + widthA + 5) + "px"; 
								}
								
								if(pos.Y - 25 <= GET.scrollY()) {
									div.style.top = (GET.scrollY() + 10) + "px";
								} else if (pos.Y + this.offsetHeight >= GET.scrollY() + GET.browserHeight()) {
									div.style.top = (GET.scrollY() + GET.browserHeight() - this.height - 25) + "px";
								} else if(pos.Y > GET.browserHeight() / 2 + GET.scrollY()) {
									if(GET.scrollY() + this.height > pos.Y) {
										div.style.top = (GET.scrollY() + 10) + "px";
									} else {
										div.style.top = (pos.Y - parseInt(div.style.height) + 35) + "px";
									}
								} else {
									if(pos.Y + this.height > GET.scrollY() + GET.browserHeight()) {
										div.style.top = (GET.scrollY() + GET.browserHeight() - this.height - 20) + "px";
									} else {
										div.style.top = (pos.Y - 20) + "px";
									}
								}
								if(document.getElementById("loading_icon")) {
									var erase = document.getElementById("loading_icon");
									erase.parentNode.removeChild(erase);
									delete erase;
								}
							}
							if(this.width < this.height) {
								if(this.width < 150) {
									img.setAttribute("src", '../script/original/ctrl_image.php?src=' + this.getAttribute("src") + '&width=' + this.width);
								} else {
									img.setAttribute("src", '../script/original/ctrl_image.php?src=' + this.getAttribute("src") + '&width=150');
								}
							} else {
								if(this.height < 150) {
									img.setAttribute("src", '../script/original/ctrl_image.php?src=' + this.getAttribute("src") + '&height=' + this.height);
								} else {
									img.setAttribute("src", '../script/original/ctrl_image.php?src=' + this.getAttribute("src") + '&height=150');
								}
							}
						}
						temp.onerror = function() {
							clearTimeout(AddPhoto.option.timerID1);
							if(document.getElementById("loading_icon")) {
								var erase = document.getElementById("loading_icon");
								erase.parentNode.removeChild(erase);
								delete erase;
							}
						}
						temp.setAttribute("src", rel);
					}, 300);
				}
				
				aTags[i].onmouseout = function() {
					clearTimeout(AddPhoto.option.timerID1);
					
					if(document.getElementById("photo")) {
						var erase = document.getElementById("photo");
						erase.parentNode.removeChild(erase);
						delete erase;
					}
					if(document.getElementById("loading_icon")) {
						var erase = document.getElementById("loading_icon");
						erase.parentNode.removeChild(erase);
						delete erase;
					}
					
					AddPhoto.option.timerID2 = setTimeout(function() {
						if(document.getElementById("photo")) {
							var erase = document.getElementById("photo");
							erase.parentNode.removeChild(erase);
							delete erase;
						}
						if(document.getElementById("loading_icon")) {
							var erase = document.getElementById("loading_icon");
							erase.parentNode.removeChild(erase);
							delete erase;
						}
					}, 50);
				}
			}
		}
	}
}

//## リストコントロール ##//
var CtrlList = {
	'init' : function() {
		var parent = document.getElementById("artist-list");
		var target = document.getElementById("insertPointArtist")
		
		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>'
		
		parent.insertBefore(ul, target);
		
		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 = "ArtistListSelectorImperial=A;expires=" + limit.toGMTString() + ";";
			CtrlList.change('A');
		}
		
		var B = document.getElementById("selectorB");
		B.onclick = function() {
			document.cookie = "ArtistListSelectorImperial=B;expires=" + limit.toGMTString() + ";";
			CtrlList.change('B');
		}
		
		if(document.cookie.indexOf("ArtistListSelectorImperial=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(/sub/)) {
							liTags[i].style.display = "none";
						}
						break;
					case 'B':
						liTags[i].style.display = "block";
						liTags[i].innerHTML += '<span>Catalog Artist</span>';
						break;
					default:
						break;
				}
			}
		}
		if(YAHOO.env.ua.webkit == 0 || YAHOO.env.ua.webkit >= 522) AddPhoto.init();
	}
}

//## イニシャライズ ##//
YAHOO.util.Event.onDOMReady(function() {
	ChangePNG.init();
    if(YAHOO.env.ua.webkit == 0 || YAHOO.env.ua.webkit >= 522) Nifty("dl", "transparent");
	AddNotice.init();
	CtrlList.init();
});

