/**---------------------------------
 * common.js
 ---------------------------------*/

//
//ロールオーバー
//
function rolloverInit()
{
		$("a img[src*='_ov']").addClass("current");
		
		$("a img[class!='current'],:image").mouseover(function(){
			if ($(this).attr("src").match(/_ot./)){
				$(this).attr("src",$(this).attr("src").replace("_ot.", "_ov."));
				return;
			}
		}).mouseout(function(){
			if ($(this).attr("src").match(/_ov./)){
				$(this).attr("src",$(this).attr("src").replace("_ov.", "_ot."));
				return;
			}
		}).click(function(){
			if ($(this).attr("src").match(/_ov./)){
				$(this).attr("src",$(this).attr("src").replace("_ov.", "_ot."));
				return;
			}
		});

		
		//preload images
		var images = [];
		$("a img,:image").each(function(index){
				if($(this).attr("src").match(/_ot./)){
					 images[index]= new Image();
					 images[index].src = $(this).attr("src").replace("_ov.", "_ot.");
				}
		});
		
}


//
// 雲
//
function cloudInit(){
	if(userAgentCheck('iphone','ipad','ipod','android')){
		var c=$(".cloudWrap").remove();
		$("#wrap").prepend(c);
	}

  $(window).bind("resize",resize);
	var winW;
	var cw=$(".cloudWrap");
	function resize(){
    if(!$.support.noCloneEvent){
      winW=document.body.clientWidth-16;
    }else{
      winW=window.innerWidth-16;
    }
	}
	function init(){for(var i=0;i<cw.length;i++)movSet(cw,i)}
	function start(){for(var i=0;i<cw.length;i++)cw[i].movStart()}
	function movSet(jq,n){
		var si,px=0,delay=60/(n+1);
		jq[n].mov=function(){
			px++;
			if(px>1000)px=px-1000;
			jq.eq(n).css("background-position",px+"px 0");
		}
		jq[n].movStart=function(){clearInterval(si);si=setInterval(h,delay)}
		jq[n].movStop=function(){clearInterval(si)}
		function h(){jq[n].mov()}
	}
	init();
	resize();
	start();
}
//////////////////////////
///// UserAgentCheck /////
function userAgentCheck(){
	var ua=navigator.userAgent.toLowerCase();
	for(i=0;i<arguments.length;i++)
		if(ua.indexOf(arguments[i])>-1)
			return true;
	return false;
}
///////////////
///// Tab /////
function tabInit(content,_n){
	if(_n>=$(".positionMenu li").length)_n=0;
	$('.positionMenu li a[href]').click(function(e){e.preventDefault()});		
	$(".positionMenu li").hover(function(){
		$(this).css("cursor","pointer");
	},function(){
		$(this).css("cursor","default");
	});
	for(var i=0;i<$(".positionMenu li").length;i++){
		$(".positionMenu li > a").eq(i).click(tabClick(i));
		if(i==_n)$(".positionMenu li > a").eq(i).addClass("select");
		else $(className(i)).hide();
	}
	function tabClick(n){
		return function(){
			if(n==0)window.location.hash="";
			else window.location.hash=n;
			for(var i=0;i<$(".positionMenu li").length;i++){
				if(i==n){
					$(".positionMenu li > a").eq(i).addClass("select");
					$(className(i)).show();
				}else{
					$(".positionMenu li > a").eq(i).removeClass("select");
					$(className(i)).hide();
				}
			}
		}
	}
	function className(n){
		return "."+content+"Area"+(n<9?"0"+(n+1):(n+1));
	}
}
//////////////////////////////////////
///// DiscoBox (thickbox custom) /////
var DB_PATH_TO_IMAGE="discography/img/loadingAnimation.gif";
function dbInit(){
	db_init('a.discobox,area.discobox,input.discobox');
	imgLoader=new Image();
	imgLoader.src=DB_PATH_TO_IMAGE;
}
function db_init(domChunk){
	$(domChunk).click(function(){
		db_show(this.href);
		this.blur();
		return false;
	});
}
function db_show(url){//function called when the user clicks on a thickbox link
	try{
		if(typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height:"100%",width:"100%"});
			$("html").css("overflow","hidden");
			if(document.getElementById("DB_HideSelect")===null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='DB_HideSelect'></iframe><div id='DB_overlay'></div><div id='DB_window'></div>");
				$("#DB_overlay").click(db_remove);
			}
		}else//all others
			if(document.getElementById("DB_overlay") === null){
				$("body").append("<div id='DB_overlay'></div><div id='DB_window'></div>");
				$("#DB_overlay").click(db_remove);
			}
		if(db_detectMacXFF())
			$("#DB_overlay").addClass("DB_overlayMacFFBGHack");//use png overlay so hide flash
		else
			$("#DB_overlay").addClass("DB_overlayBG");//use background and opacity
		$("body").append("<div id='DB_load'><img src='"+imgLoader.src+"' /></div>");//add loader to the page
		$('#DB_load').show();//show loader
		var baseURL;
		if(url.indexOf("?")!==-1)baseURL=url.substr(0,url.indexOf("?"));else baseURL=url;		
		var params=db_parseQuery(url.replace(/^[^\?]+\??/,''));
		DB_WIDTH=491+30||630; //defaults to 630 if no paramaters were added to URL
		DB_HEIGHT=500+40||440; //defaults to 440 if no paramaters were added to URL
		ajaxContentW=DB_WIDTH-30;
		ajaxContentH=DB_HEIGHT-45;
		$("#DB_iframeContent").remove();
		$("#DB_window").append("<iframe allowtransparency='true' frameborder='0' hspace='0' src='"+url+"' id='DB_iframeContent' name='DB_iframeContent"+Math.round(Math.random()*1000)+"' onload='db_showIframe()' style='width:"+(0)+"px;height:"+(0)+"px;'> </iframe>");
		db_position();
		if($.browser.safari){
			$("#DB_load").remove();
			$("#DB_window").css({display:"block"});
		}
		document.onkeyup=function(e){ 	
			if(e==null) // ie
				keycode=event.keyCode;
			else // mozilla
				keycode=e.which;
			if(keycode==27)db_remove();
		};
		//DOMツリー完成待機kari
		setTimeout(function(){
			$("#DB_iframeContent").contents().find(".close").click(function(){db_remove()});
			$("#DB_window iframe").css("width",ajaxContentW+29).css("height",ajaxContentH+17);
		},200);
		
		$(window).bind("resize",function(){resize();});
		$("#DB_window").css("top",404);
		var winW,winH;
		function resize(){
			if(window.scrollWidth){
				winW=window.scrollWidth;
				winH=window.scrollHeight;
			}else{
				winW=document.documentElement.scrollWidth
				winH=document.documentElement.scrollHeight
			}
			var DB_left=194;
			if(userAgentCheck('safari')){
				DB_left=195;
			}else if(userAgentCheck('firefox')){
				DB_left=194;
			}else if(userAgentCheck('msie')){
				DB_left=194;
			}
			$("#DB_window").css("left",winW/2+DB_left);
		}
		resize();
	}catch(e){
		//nothing here
	}
}
function db_showIframe(){
	$("#DB_load").remove();
	$("#DB_window").css({display:"block"});
}
function db_remove(){
 	$("#DB_imageOff").unbind("click");
	$("#DB_closeWindowButton").unbind("click");
	$("#DB_window").fadeOut("fast",function(){$('#DB_window,#DB_overlay,#DB_HideSelect').trigger("unload").unbind().remove();});
	$("#DB_load").remove();
	if(typeof document.body.style.maxHeight=="undefined") {//if IE 6
		$("body","html").css({height:"auto",width:"auto"});
		$("html").css("overflow","");
	}
	document.onkeydown = "";
	document.onkeyup = "";
	return false;
}
function db_position() {
$("#DB_window").css({marginLeft:'-'+parseInt((DB_WIDTH/2),10)+'px',width:DB_WIDTH+'px'});
	if(!(jQuery.browser.msie&&jQuery.browser.version<7)) { // take away IE6
		$("#DB_window").css({marginTop:'-'+parseInt((DB_HEIGHT/2),10)+'px'});
	}
}
function db_parseQuery(query){
   var Params={};
   if(!query)return Params;// return empty object
   var Pairs=query.split(/[;&]/);
   for(var i=0;i<Pairs.length;i++){
      var KeyVal=Pairs[i].split('=');
      if(!KeyVal||KeyVal.length!=2)continue;
      var key=unescape(KeyVal[0]);
      var val=unescape(KeyVal[1]);
      val=val.replace(/\+/g,' ');
      Params[key]=val;
   }
   return Params;
}
function db_getPageSize(){
	var de=document.documentElement;
	var w=window.innerWidth||self.innerWidth||(de&&de.clientWidth)||document.body.clientWidth;
	var h=window.innerHeight||self.innerHeight||(de&&de.clientHeight)||document.body.clientHeight;
	arrayPageSize=[w,h];
	return arrayPageSize;
}
function db_detectMacXFF() {
  var userAgent=navigator.userAgent.toLowerCase();
  if(userAgent.indexOf('mac')!=-1&&userAgent.indexOf('firefox')!=-1)
    return true;
}
//////////////////
///// Scroll /////
function scrollInit(content){
	if(userAgentCheck('iphone','ipad','ipod','android'))
		if(content=="goods"||content=="links")
			$(".scroll ul li a").css("font-size","100%");
	else
		$("html").css("overflow-y","scroll");
	for(var i=0;i<$(".scroll").length;i++){
		$(".scroll")[i].txt=$(".scroll").eq(i).html();
		$(".scroll")[i].viewAll=$(".scroll").eq(i).next();
	}
	var baseH=$(".scroll").height();
	fleXenv.initByClass("scroll");
	for(i=0;i<$(".scroll").length;i++)
		if(baseH>$(".contentwrapper").eq(i).height())
			$(".scroll")[i].viewAll.hide();	
		else
			$(".scroll")[i].viewAll.click((function(n){
				return function(event){
					$(".scroll").eq(n).html($(".scroll")[n].txt);
					$(".scroll").eq(n).css("height","auto");
					$(".scroll")[n].viewAll.hide();
					if(content=="goods"||content=="links")
						$(".scroll").eq(n).find("ul li a").css("font-size","100%");

					event.preventDefault();
				}
			})(i));
}
function parseQuery(u){return db_parseQuery(u.replace(/^[^\?]+\??/,''))}
////////////////
///// Main /////
$(function(){
	rolloverInit();
	var p=parseInt(window.location.hash.substr(1))||0;
	var content=$("body").attr("id");
	if(content=="discography")content="disco";
	scrollInit(content);
	if (content != 'blog') tabInit(content,p);
	cloudInit();
	dbInit();
});


function cloudbnr01(){
	var windowW=$(window).width();
	$("#cloud_bnr01").animate({left:"+="+ windowW},40000,"linear")
	//elementを相対的に左1400pxまで20000ミリ秒かけて移動
	.animate({left:"-=1400px"}, 1)
	//elementを相対的に左から-1400pxまで1ミリ秒かけて移動
	setTimeout("cloudbnr01()",500);
	//500ミリ秒後に動作を繰り返します。
}
