//#########################//
//## 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 に max-width 再現 ###//
//##############################//
var FlexMainWidth = {
	'option' : {
		'limit' : 1024
	},
	
	'init' : function (min) {
		if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
			window.attachEvent('onresize',  function() {
				FlexMainWidth.chkWidth(min);
			});
			FlexMainWidth.force(min);
		}
	},
	
	'chkWidth' : function(min) {
		var main = document.getElementById("main");
		var width = document.body.clientWidth;
		
		if(min == null) min = 1;
		
		if(FlexMainWidth.option.limit < width) {
			main.style.maxWidth = FlexMainWidth.option.limit + "px";
			if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
				main.style.width = FlexMainWidth.option.limit + "px";
			}
		} else if(min < width && width <= FlexMainWidth.option.limit) {
			main.style.width = "100%";
		} else {
			main.style.maxWidth = min + "px";
			if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
				main.style.width = min + "px";
			}
		}
	},
	
	'force' : function(min) {
		var main = document.getElementById("main");
		var width = document.body.clientWidth;
		
		if(min == null) min = 1;
		
		if(FlexMainWidth.option.limit < width) {
			main.style.maxWidth = (FlexMainWidth.option.limit - 1) + "px";
			if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
				main.style.width = (FlexMainWidth.option.limit - 1) + "px";
			}
			setTimeout(function() {
				main.style.maxWidth = FlexMainWidth.option.limit + "px";
				if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
					main.style.width = FlexMainWidth.option.limit + "px";
				}
			}, 1);
		} else if(min < width && width <= FlexMainWidth.option.limit) {
			main.style.width = "99.9%";
			setTimeout(function() {	
				main.style.width = "100%";
			}, 1);
		} else {
			main.style.maxWidth = (min - 1) + "px";
			if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
				main.style.width = (min - 1) + "px";
			}
			setTimeout(function() {
				main.style.maxWidth = min + "px";
				if(0 < YAHOO.env.ua.ie && YAHOO.env.ua.ie < 7) {
					main.style.width = min + "px";
				}
			}, 1);
		}
	}
}

//#################//
//## 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 AniMenu = {
	'init' : function() {
		var div = document.getElementById("menu");
		div.style.visibility = "visible";
		
		var li = div.getElementsByTagName("LI");
		
		for(var i = 0; i < li.length; i++) {
			this.setOpacity(li[i], 320 * (i + 1));
		}
	},
	'setOpacity' : function(obj, timer) {
		obj.opacity = 0;
		
		obj.style.filter = 'alpha(opacity=0)';
		obj.style.MozOpacity = 0;
		obj.style.opacity = 0;
		
		obj.timer = setInterval("", 1);
		
		setTimeout(function() {
			obj.timer = setInterval(function() {
				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 += 0.5;
				} else {
					clearInterval(obj.timer);
				}
			}, 20);
		}, timer);
	}
}

//###############################//
//## ランダムに背景を設定する ###//
//###############################//
var RandomBack = {
	'init' : function() {
		var img = new Array();
		img[0] = "back_bodyA.gif";
		img[1] = "back_bodyB.gif";
		
		var num = Math.floor(Math.random() * img.length);
		
		if(YAHOO.env.ua.ie && YAHOO.env.ua.ie < 9) {
			document.styleSheets[0].addRule("body", "{ background: transparent url(../img/basic/" + img[num] +  ") 50% 0 repeat-y; }"); //for IE
		} else if( document.styleSheets[0].insertRule ) {
			document.styleSheets[0].insertRule("body { background: transparent url(../img/basic/" + img[num] +  ") 50% 0 repeat-y; }", document.styleSheets[0].cssRules.length); //for Mozilla
		}
	}
};

//################################//
//## リンクを別ウィンドウで開く ##//
//################################//
var Popup = {
	'init' : function() {
		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() {
					Popup.dialog(this);
					return false;
				}
			}
		}
	},
	
	'dialog' : function(obj) {
		if(confirm('リンク先を別ウィンドウで開きます（あらかじめポップアップを許可しておいて下さい）')) {
			window.open(obj.href, null);
		}
	}
}

//####################//
//## h1背景にFLASH ###//
//####################//
function CtrlH1(filename, width, height) {
	document.write('<h1>' + "\n");
	if(navigator.userAgent.indexOf("MSIE 5") != -1 && (navigator.userAgent.indexOf("PPC Mac") != -1 || navigator.userAgent.indexOf("PowerPC") != -1)) {
		document.write('テイチクエンタテインメント' + "\n");
	} else if(YAHOO.env.ua.ie == 0) {
		document.write('<embed src="'+ filename + '" quality="high" wmode="transparent" width="' + width + '" height="' + height + '" name="top" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' + "\n");
	} else {
		document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '">' + "\n");
		document.write('<param name="allowScriptAccess" value="sameDomain" />' + "\n");
		document.write('<param name="movie" value="' + filename + '" />' + "\n");
		document.write('<param name="quality" value="high" />' + "\n");
		document.write('<param name="wmode" value="transparent">' + "\n");
		document.write('</object>' + "\n");
	}
	document.write('</h1>' + "\n");
}

//## Yahooサーチ用 ##//
var YahooSearch = {
	'init' : function() {
		var input = document.getElementById("keyword");
		input.style.background = "#ffffff url(http://www.teichiku.co.jp/img/basic/back_input_search.gif) 2px 50% no-repeat";
		
		var brnd = Math.floor(1000000 * Math.random());
		document.write('<p><img src="http://img.yahoo-search.jp/img/bcn.gif?id=300094&rnd=' + brnd + '" width="1" height="1" /></p>' + "\n");
		
		this.ctrlTop();
	},
	
	'focus' : function(obj) {
		obj.style.background = "#ffffff";
	},
		
	'blur' : function(obj) {
		obj.style.background = "#ffffff url(http://www.teichiku.co.jp/img/basic/back_input_search.gif) 2px 50% no-repeat";
	},
	
	'ctrlTop' : function() {
		if(typeof window.addEventListener == 'function') {
			window.addEventListener('resize', function() {
				YahooSearch.chkWidth();
			}, false);
		} else if(typeof window.attachEvent == 'object'){
			window.attachEvent('onresize', function() {
				YahooSearch.chkWidth();
			});
		}
		
		YahooSearch.chkWidth();
	},
	
	'chkWidth' : function() {
		var width = document.body.clientWidth;
		var search = document.getElementById("search");
		
		if(width < 777) {
			search.style.top = "60px";
		} else {
			search.style.top = "5px";
		}
	}
}

//## サーチエンジンランキング表示 ##//
var SearchRank = {
	'init' : function(level) {
		SearchRank.timerID = setTimeout("", 5);
		SearchRank.timerID2 = setTimeout("", 300);
		var div = document.getElementById("search");
		
		div.onmouseover = function() {
			clearTimeout(SearchRank.timerID1);
			SearchRank.timerID2 = setTimeout(function() {
				SearchRank.showData(level);
			}, 300);
		}
		div.onmouseout = function() {
			clearTimeout(SearchRank.timerID2);
			SearchRank.timerID1 = setTimeout(function() {
				SearchRank.removeData();
			}, 50);
		}
	},
	
	'showData' : function(level) {
		if(!document.getElementById("ranking")) {
			var div = document.getElementById("search");
			var backup = div.innerHTML;
			
			if(window.XMLHttpRequest != null) {
				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) {
						div.innerHTML = backup + message.responseText;
						document.getElementById("ranking").onmouseover = function() {
							clearTimeout(SearchRank.timerID1);
							clearTimeout(SearchRank.timerID2);
						}
					} else {
						div.innerHTML = backup + '<ul id="ranking" class="ranking"><li>注目検索の読み込みに失敗しました。</li></ul>';
						document.getElementById("ranking").onmouseover = function() {
							clearTimeout(SearchRank.timerID1);
							clearTimeout(SearchRank.timerID2);
						}
					}
				} else {
					div.innerHTML = backup + '<ul id="ranking" class="ranking"><li>注目検索 読み込み中...</li></ul>';
					document.getElementById("ranking").onmouseover = function() {
						clearTimeout(SearchRank.timerID1);
						clearTimeout(SearchRank.timerID2);
					}
				}
			}
			message.open('GET', level + "script/original/get_keyword.php?t=" + new Date(), true);
			message.send(null);
		}
	},
	
	'removeData' : function() {
		if(document.getElementById("ranking")) {
			var div = document.getElementById("search");
			var ul = document.getElementById("ranking");
			div.removeChild(ul);
		}
	}
}

//## ローディング表示 ##//
var Loading = {
	'draw' : function() {
		if(!(navigator.userAgent.indexOf("PLAYSTATION 3") != -1) && !(navigator.userAgent.indexOf("MSIE 5") != -1 && (navigator.userAgent.indexOf("PPC Mac") != -1 || navigator.userAgent.indexOf("PowerPC") != -1))) {
			document.write('<p id="loading"><img src="http://www.teichiku.co.jp/img/basic/icon_loading.gif" width="32" height="32" alt="" /> Now Loading...<br /><span class="note">いつまでもコンテンツが表示されない場合は、一度ページを更新して下さい。</span></p>');
		}
	},
	
	'remove' : function() {
		if(document.getElementById("loading")) {
			var loading = document.getElementById("loading");
			loading.parentNode.removeChild(loading);
		}
	}
}

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