/**************************************************************************************
環境判別
***************************************************************************************/
var rootPath = "/"; //本番用

if(document.location.href.indexOf("file") != -1){ //ローカルチェック用
	rootPath = "";
	var dir = location.href.split("/");
	for(i=0; i<dir.length; i++){
		if(dir[i] != "07_HTML"){
			rootPath += dir[i] + "/";
		}
		else {
			rootPath += dir[i] + "/";
			break;
		}
	}
}
else if (document.location.href.indexOf("pre") != -1){ //テストアップ用
	rootPath = "/dreamusic/bbs/";
}



/**************************************************************************************
Google Analyticsコード書き出し
***************************************************************************************/
with(document){

	write('<script type="text/javascript">');
	
	write("var _gaq = _gaq || [];");
	write("_gaq.push(['_setAccount', 'UA-27924181-1']);");
	write("_gaq.push(['_trackPageview']);");
	
	write("(function() {");
	write("var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;");
	write("ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';");
	write("var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);");
	write('})();');
	write('</script>');
}


/**************************************************************************************
サイトコンストラクタ
***************************************************************************************/
var currentContent = "top"; //現在のコンテンツ名
var userAgent = window.navigator.userAgent.toLowerCase();
var appVersion = window.navigator.appVersion.toLowerCase();
var browser = "";
//crossFade不可のie6,7のみブラウザ検出
if (userAgent.indexOf('msie') != -1){
	if (appVersion.indexOf("msie 6.") != -1) {
		browser = "ie6";
	} else if (appVersion.indexOf("msie 7.") != -1) {
		browser = "ie7";
	}
}

//SOUND MANAGER2 設定
soundManager.flashVersion = 8; // Flashのバージョン(8 or 9)を指定
soundManager.url = 'swf/'; // .swfファイルを保存しているフォルダ
soundManager.debugMode = false; // trueの時、デバッグモード
soundManager.consoleOnly = false; // trueの時、Firebugなどのコンソールのみに出力。falseの時は画面にも出力
var bgm;
var bgmIsPlaying = 0;
soundManager.onready(function(oStatus) {
	if (!oStatus.success) {
			return false;	
	}
	soundManager.createSound({
			id: 'menu_over',
			url: 'se/menu_over.mp3',
			autoLoad: true,
			volume: 30
	});
	soundManager.createSound({
			id: 'change',
			url: 'se/change.mp3',
			autoLoad: true,
			volume: 60
	});
	bgm = soundManager.createSound({
			id: 'bgm',
			url: 'se/bgm1.mp3',
			autoLoad: true,
			autoPlay:false,
			volume: 0
	});
	
});

	function loopSound(sound) {
			sound.play({
					onfinish: function() {
							loopSound(sound);
					}
			});
	}



$(function(){
											
											//スマホ⇔タブレット・PC切り替え
var ua =navigator.userAgent;
if(ua.indexOf('iPhone') > -1  || ua.indexOf('iPod') > -1 || (ua.indexOf('Android') > -1 && ua.indexOf('Mobile') > -1)) {
 window.location.href = '/s/index.html';　//スマホサイトのURL
}
											/*
 if (navigator.userAgent.indexOf('iPhone') > 0 
 && navigator.userAgent.indexOf('iPad') == -1) 
 || navigator.userAgent.indexOf('iPod') > 0 
 || navigator.userAgent.indexOf('Android') > 0) 
   {     
         location.href = '/s/index.html';     
   } 
*/


	$("#sound").toggle(function(){
		if(bgmIsPlaying != 1){
				loopSound(bgm);
		}
		soundManager.setVolume("bgm", 60);
		bgmIsPlaying = 1;
		$("#sound img").attr("src", $("#sound img").attr("src").replace("_off", "_on"));
	},
	function(){
		soundManager.setVolume("bgm", 0);
		$("#sound img").attr("src", $("#sound img").attr("src").replace("_on", "_off"));
	});
	$("#sound img").hover(function(){
		$(this).attr("src", $(this).attr("src").replace(".", "_over."));
	},
	function(){
		$(this).attr("src", $(this).attr("src").replace("_over.", "."));
	});
											
											
	crossFade("#footer");
	$("#footer ul#menu li").hover(function(){
		play("menu_over");
	},
	function(){}
	);

	//URLにc=コンテンツ名、a=アンカー名のパラメータ付加で下層に自動移動
	$("#contents").css("display", "none").fadeTo(1000,1, "swing", function(){
		if(getParam("c")){
			goto(getParam("c"), getParam("a"));
		}
		else {
			rollOver();
			initLang();
		}
	});
	
	$("p#lang img").click(function(){
		changeLang();
	});
	
});

	
/**************************************************************************************
関数定義
***************************************************************************************/

//----------------------------------------------------------------------------------
// コンテンツ読み込み
//----------------------------------------------------------------------------------
function loadContent(url, target){
	
	//コンテンツ読み込み後の初期処理
	$(target).load(url, function(){
			initLang();
			crossFade("#contents");
			//rollOver();
			//smoothScroll();
			contentInit();
			$("#jScroll_" + currentContent).jScrollPane({showArrows:false, wheelSpeed:45, scrollbarWidth:6, animateTo:true, animateInterval:50, animateStep:3});

		//$(target).fadeTo(1000, 1);
	});
}

//ページ別の初期処理
function contentInit(){
	
	switch(currentContent){
		
		case "top":
			break;
			
		case "news":
			$("#content_" + currentContent + " h3 span").css("visibility", "hidden");
			typeTo("#content_" + currentContent + " h3 span", 4);
			break;
			
		case "live":
			$("#content_" + currentContent + " h3 span").css("visibility", "hidden");
			typeTo("#content_" + currentContent + "  h3 span", 4);
			break;
			
		case "discography":
			$("#content_" + currentContent + " .main ol li").css("visibility", "hidden");
			$("#content_" + currentContent + " .main h3").css("visibility", "hidden");
			$(".main li").addClass("clearfix");
			typeTo("#content_" + currentContent + " .main h3", 20);
			typeTo("#content_" + currentContent + " .main ol li", 5);
			break;
			
		case "biography":
			$("#content_" + currentContent + " div#profile p").css("visibility", "hidden");
			break;
	}
}


//----------------------------------------------------------------------------------
// コンテンツ切り替え
//----------------------------------------------------------------------------------
var menuLock = 0; //処理中フラグ

function goto(contentName, anchorName){
	
	//処理のスタックまたは自カテゴリへの切り替えを禁止
	if(menuLock == 1){
		return;
	}
	else if((contentName == currentContent)){
		return;
	}
	else if((contentName == "shop")){
		return;
	}
	else if((contentName == "mailmagazine")){
		return;
	}
	
	//コンテンツ切替え初期処理
	menuLock = 1;
	currentContent = contentName; //選択されたコンテンツをカレントに設定
	if(currentContent != "top"){ 	play("change"); }
	
	//現在のコンテンツを閉じる
	$(".ani_top").css("display", "none");
	$("#contentBody").animate({height:"0px"}, 600, "easeInOutQuad");
	
	var left = "0px";
	
	/* ビーム
	var time = 600;
	switch(contentName){
		case "top": left = "160px"; time = 300; break;
		case "news": left = "230px"; break;
		case "live": left = "302px"; break;
		case "biography": left = "398px"; break;
		case "discography": left = "540px"; break;
		case "link": left = "792px"; break;
	}
	$("#line").css({display:"block", left:left}).animate({height:"420px",top:"-420px"}, time, "easeInOutQuad");
	$("#line").fadeOut(time, 0, function(){$("#line").css({height:"0px",top:"0px"});});
	*/
	$("h2").fadeOut(600, "easeInOutQuad");
	$("#contentBorder").fadeOut(600, "easeInOutQuad", function(){ //親ボックスをフェードアウト→見えない状態で内部を操作（小ボックスを全非表示→選択されたコンテンツの小ボックスを表示）
		$(".contentGroup").css("display", "none");
		$(".jScroll").jScrollPane().data().jsp.destroy(); //バグ回避のため、コンテンツ切り替え時にjScrollPaneを一旦すべて解除
		$("#content_" + currentContent).css("display", "block");
		$("body").attr("id", currentContent);
		
		if(currentContent == "top"){
			$("#wrapper").css("width", "993px")
			$("h1, .ani_top").fadeTo(1000,1, "swing");
			$("#contents").animate({	height:"455px"}, 500, "swing", function(){ menuLock = 0; });
		}
		else {
			switch(currentContent){
				/* NEWS -------------------------------------------------------------------------------------*/
				case "news":
				
					$("h1").fadeOut(500, "swing", function(){
						$("#wrapper").css("width", "950px")
						appearTitle();
						$("#contentBorder").fadeIn(500, "swing").find("#contentBody").animate({height:"420px"}, 1000, "easeInOutQuart");
					});
					$("#contents").animate({	height:"496px"}, 500, "swing", function(){
						loadContent('/news/2012.html?' + Math.random() , '#content_news .main');
						menuLock = 0;
					}); 
					
					break;
			
				/* LIVE -------------------------------------------------------------------------------------*/
				case "live":
					
					$("h1").fadeOut(500, "swing", function(){
						$("#wrapper").css("width", "950px")
						appearTitle();
						$("#contentBorder").fadeIn(500, "swing").find("#contentBody").animate({height:"420px"}, 1000, "easeInOutQuart");
					});
					$("#contents").animate({	height:"496px"}, 500, "swing", function(){
						loadContent('/live/2012.html?' + Math.random() , '#content_live .main')	;
						menuLock = 0;
					}); 
					
					break;
		
				/* DISCOGRAPHY ------------------------------------------------------------------------------*/
				case "discography":
					
					$("h1").fadeOut(500, "swing", function(){
						$("#wrapper").css("width", "950px")
						appearTitle();
						$("#contentBorder").fadeIn(500, "swing").find("#contentBody").animate({height:"420px"}, 1000, "easeInOutQuart");
					});
					$("#contents").animate({	height:"496px"}, 500, "swing", function(){
						loadContent('/discography/single.html?' + Math.random() , '#content_discography .main')	;
						menuLock = 0;
					}); 
					break;
			
				/* BIOGRAPHY --------------------------------------------------------------------------------*/
				case "biography":
					
					$("h1").fadeOut(500, "swing", function(){
						$("#wrapper").css("width", "950px")
						appearTitle();
						$("#contentBorder").fadeIn(500, "swing").find("#contentBody").animate({height:"420px"}, 1000, "easeInOutQuart", function(){	
							//記事タイトルタイプライター表示
							if($.cookie('lang') == "jp"){
								$("#content_" + contentName + " div#profile .en p").css("visibility", "visible")
								$("#content_" + contentName + " div#profile .jp p").eq(0).css("visibility", "visible").typewriter(60, function(){ 
									$("#content_" + contentName + " div#profile .jp p").eq(1).css("visibility", "visible").typewriter(60, function(){
										$("#content_" + contentName + " div#profile .jp p").eq(2).css("visibility", "visible").typewriter(60, function(){
											$("#content_" + contentName + " div#profile .jp p").eq(3).css("visibility", "visible").typewriter(60, function(){
												$("#content_" + contentName + " div#profile .jp p").eq(4).css("visibility", "visible").typewriter(60, function(){
												});
											});
										});
									});
								});
							}
							else if($.cookie('lang') == "en"){
								$("#content_" + contentName + " div#profile .jp p").css("visibility", "visible")
								$("#content_" + contentName + " div#profile .en p").eq(0).css("visibility", "visible").typewriter(60, function(){ 
									$("#content_" + contentName + " div#profile .en p").eq(1).css("visibility", "visible").typewriter(60, function(){
									});
								});
							}
						});
					});
					$("#contents").animate({	height:"496px"}, 500, "swing", function(){	$("#jScroll_" + currentContent).jScrollPane({showArrows:false, wheelSpeed:45, scrollbarWidth:6, animateTo:true, animateInterval:50, animateStep:3}); menuLock = 0;}); 
					
					break;
					
				/* LINK --------------------------------------------------------------------------------*/
				case "link":
					//初期表示調整
					$("#content_" + currentContent + " h3 span").css("visibility", "hidden");
					
					$("h1").fadeOut(500, "swing", function(){
						$("#wrapper").css("width", "950px")
						appearTitle();
						$("#contentBorder").fadeIn(500, "swing").find("#contentBody").animate({height:"420px"}, 1000, "easeInOutQuart");
					});
					
					$("#contents").animate({	height:"496px"}, 500, "swing", function(){
						loadContent('/link/index.html?' + Math.random() , '#content_link .main')	;
						menuLock = 0;
					}); 
					
					break;
			}
		}
	});
}


//----------------------------------------------------------------------------------
// コンテンツタイトル用アニメーション
//----------------------------------------------------------------------------------
function appearTitle(){
	//コンテンツタイトルを表示→srcをカレントコンテンツに変更→シャッフル
	$("h2").css("display", "block").find("img").attr("src", "images/" + currentContent + "/h2.gif");
	shuffle();
}

function shuffle(){
	$("h2").css({left:0,top:0});
	var no  = Math.ceil(Math.random()*100%5);
	switch(no){
	case 1:
		$("h2").animate({left:-40,top:-14},16).animate({left:94,top:-40},16).fadeTo(18,0.4).delay(300).animate({left:10,top:132},16).fadeTo(18,1).animate({left:10,top:132},16).animate({left:0,top:-34},16).animate({left:0,top:0},16);
		break;
	case 2:
		$("h2").animate({left:94,top:-140},16).delay().animate({left:-40,top:-34},16).animate({left:-120,top:-3},16).fadeTo(18,0.7).delay(120).animate({left:-60,top:182},16).fadeTo(18,1).animate({left:-12,top:-14},16).animate({left:0,top:0},16);
		break;
	case 3:
		$("h2").animate({left:-40,top:-14},16).delay(100).animate({left:94,top:-40},16).fadeTo(18,0.4).animate({left:-140,top:432},16).fadeTo(18,1).animate({left:10,top:132},16).animate({left:0,top:-34},16).delay(300).animate({left:120,top:234},16).delay(100).animate({left:0,top:0},16);
		$("h2 img").delay(140).animate({height:222},16).animate({height:47},16);
		break;
	case 4:
		$("h2").animate({left:94,top:-140},16).animate({left:-40,top:-34},16).animate({left:-120,top:-3},16).fadeTo(18,0.7).delay(210).animate({left:-60,top:182},16).fadeTo(18,1).animate({left:-12,top:-14},16).animate({left:0,top:0},16);
		break;
	case 5:
		$("h2").animate({left:40,top:34},16).animate({left:-40,top:-34},60).delay(210).animate({left:-120,top:-3},16).fadeTo(18,0.7).animate({left:-60,top:182},16).fadeTo(18,1).animate({left:-12,top:-14},16).parent().animate({left:0,top:0},16);
		$("h2 img").delay(240).animate({height:127},16).animate({height:47},16);
		break;
	}
}


//----------------------------------------------------------------------------------
// サウンド再生
//----------------------------------------------------------------------------------
function play(id) {
  soundManager.play(id);
}


//----------------------------------------------------------------------------------
// タイプライター
//----------------------------------------------------------------------------------
function typewriter(e, time){
	e.each(function(id){
		var txt = $(this).html();
		$(this).html("").css("display", "block");
		var tmp = "";
		var char = "";
		var tag = "";
		for(i=0; i<txt.length; i++){
			char = txt.substring(i, i+1);
			//タグの開始かどうかを判別
			if(char == "<"){
				switch(txt.substring(i+1, i+3)){
					case "BR": tag = "br"; break;
					case "br": tag = "br"; break;
					case "A ": tag = "a"; break;
					case "a ": tag = "a"; break;
					case "IM": tag = "img"; break;
					case "im": tag = "img"; break;
					case "sp": tag = "span"; break;
					case "P>": tag = "p"; break;
					case "p>": tag = "p"; break;
				}
			}
			
			//テキストの場合は出力、タグの場合閉じタグの出現までキャッシュ
			if(tag != ""){
				tmp += char;
				
				if((tag == "br") && (char == ">")) {
					setTimeout(function(){ write(tmp) }, time);
					tag = "";
				}
				else if((tag == "img") && (char == ">")) {
					setTimeout(function(){ write(tmp) }, time);
					tag = "";
				}
				else if(txt.substring(i-3, i) == "/a>") {
					setTimeout(function(){ write(tmp) }, time);
					tag = "";
				}
				else if(txt.substring(i-3, i) == "/p>") {
					setTimeout(function(){ write(tmp) }, time);
					tag = "";
				}
				else if(txt.substring(i-6, i) == "/span>") {
					setTimeout(function(){ write(tmp) }, time);
					tag = "";
				}
				else {
					//alert("タグが閉じられていません\n" + tmp);
				}
			}
			else {
				setTimeout(function(){ alert("write:" + char); }, time);
			}
		}
	});
	
}


//----------------------------------------------------------------------------------
// 記事タイトルタイプライター表示（負荷低減のため最新3件のみ）
//----------------------------------------------------------------------------------
function typeTo(target, limit){	
	$(target).each(function(i){
		if(i < limit){
			$(this).typewriter(800);
		}
		$(this	).css("visibility", "visible");
	});
}


//----------------------------------------------------------------------------------
// スムーススクロール
//----------------------------------------------------------------------------------
function smoothScroll() {
	jQuery.easing.quart = function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}
	$('.pageTop a').attr("href", "#" + $("body").attr("id")).click(function() {
		if(location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = jQuery(this.hash);
			$target = $target.length && $target || jQuery('[name=' + this.hash.slice(1) +']');
			if($target.length) {
				var targetOffset = $target.offset().top;
				jQuery('html,body').animate({ scrollTop: targetOffset }, 1000, 'quart');
				return false;
			}
		}
	});
}


//----------------------------------------------------------------------------------
// マウスオーバー：単純切替え（ボタン画像にclass="rollOver"を追加）
//----------------------------------------------------------------------------------
function rollOver() {
	var preLoad = new Object();
	$('img.rollOver, input.rollOver').not("[src='_over.']").each(function(){
		var imgSrc = this.src;
		var fType = imgSrc.substring(imgSrc.lastIndexOf('.'));
		var imgName = imgSrc.substr(0, imgSrc.lastIndexOf('.'));
		var imgOver = imgName + '_over' + fType;
		preLoad[this.src] = new Image();
		preLoad[this.src].src = imgOver;
		$(this).hover(
			function (){ this.src = imgOver; },
			function (){ this.src = imgSrc;  }
		).click(function(){ this.src = imgSrc; });
	});
}


//----------------------------------------------------------------------------------
// マウスオーバー：クロスフェード
//（ボタン画像をa要素で括ってclass="crossFade"を追加。targetで要素指定）
//----------------------------------------------------------------------------------
function crossFade(target){
	if(browser != ""){
		$(target + " " + ".crossFade").removeClass(".crossFade").find("img").addClass("rollOver");
		rollOver();
		return;
	}
	$(target + " " + ".crossFade").each(function(i){
		$(this).css({display:"block",	position:"relative"});
		var baseObj = $(this).find("img").css({position:"absolute", left:"0px", top:"0px"});
		var width = baseObj.attr("width");
		var height = baseObj.attr("height");
		var a = baseObj.attr("width");
		var b = baseObj.attr("height");
		$(this).css({width:a,height:b});
		$(this).attr("id", "crossFade_" + currentContent + "_" + i);
		//ホバーイメージの生成（ファイル名_over.拡張子）
		var hoverObj = baseObj.clone();
		var src = hoverObj.attr("src")
		var fType = src.substring(src.lastIndexOf('.'));
		var name = src.substr(0, src.lastIndexOf('.'));
		var hoverSrc = name + '_over' + fType;
		baseObj.addClass("off").css("z-index", 100 + i);
		hoverObj.appendTo(this).addClass("over").css("z-index", i).attr("src", hoverSrc);
		
		$(target + " " + ".crossFade").hover(
			function(){
				$(this).find(".off").fadeTo(500, 0.01);
			},
			function(){
				$(this).find(".off").stop().queue([]).fadeTo(300, 1);
			}
		).click(function(){ $(this).find(".off").stop().queue([]).fadeTo(3000, 1); });
	});
}


//----------------------------------------------------------------------------------
// 言語切り替え
//----------------------------------------------------------------------------------
function initLang(){
	//alert("クッキー確認："+ $.cookie('lang'));
	if($.cookie('lang') == null){
		$(".en").css("display", "none");
		$.cookie('lang', 'jp', { expires: 360, domain:"bbs-net.com", path:"/" });
		//alert("クッキー初期設定："+ $.cookie('lang'));
	}
	else {
		switch($.cookie('lang')) {
			case "jp":
				$(".en").css("display", "none");
				break;
			case "en":
				$(".jp").css("display", "none");
				break;
		}
	}
}


//-------------------------------------------------------------------------------
// 言語切り替え（クッキーの保持期限：360日）
//-------------------------------------------------------------------------------
function changeLang(){

	switch($.cookie('lang')) {
		case "jp":
			$(".jp").css("display", "none");
			$(".en").fadeIn(200);
			$.cookie('lang', 'en', { expires: 360, domain:"bbs-net.com", path:"/" });
			//alert("クッキー確認（英語に切替）："+ $.cookie('lang'));
			break;
		case "en":
			$(".en").css("display", "none");
			$(".jp").fadeIn(200);
			$.cookie('lang', 'jp', { expires: 360, domain:"bbs-net.com", path:"/" });
			//alert("クッキー確認（日本語に切替）："+ $.cookie('lang'));
			break;
		default:
			initLang()
			break;
	}
	$("#jScroll_" + currentContent).jScrollPane({showArrows:false, wheelSpeed:45, scrollbarWidth:6, animateTo:true, animateInterval:50, animateStep:3});

	isLoad = {"top":0,"news":0,"live":0,"biography":0,"discography":0,"link":0};
}

function del(){
	$.cookie('lang', null, { expires: 360, domain:"pre.draw4.co.jp", path:"/" });
	//alert("クッキー削除："+ $.cookie('lang'));
}


//-------------------------------------------------------------------------------
// URLパラメータ取得
//-------------------------------------------------------------------------------
function getParam(key) {
	var str = location.search.split("?");
	if (str.length < 2) {
		return "";
	}
	
	var params = str[1].split("&");
	for (var i = 0; i < params.length; i++) {
		var keyVal = params[i].split("=");
		if (keyVal[0] == key && keyVal.length == 2) {
			return decodeURIComponent(keyVal[1]);
		}
	}
	return "";
}





