//var vgInerval;
var isIOS = AppGlobal.isIOS;
var contentsMode;
var vGrid;
var slideTime = 3000;
var slideOHBTime = 5000;
var isBGM = false;

jQuery(document).ready(function($){
	
	$("#contents").show();
	//watchTab();
	contentsMode = $("body").attr("id");
	if(contentsMode == "top" || contentsMode == "map" || contentsMode == "homebase"){
		setPageAnchor();
	}
	
	//ナビ開閉処理
	$("#navInner dl").each(function(){
		var elm = $(this);
		var h = elm.find("dd").height();
		setNavigation(elm, h);
  });
	
	function setNavigation(elm, h){
		elm.find("dt a").mouseover(function(){
			if(!elm.hasClass("active")){
				if(!elm.hasClass("over")){
					$("#navInner dl").each(function(){
						var tg = $(this);
						tg.removeClass("over");
						tg.find("dd").stop(true, false).animate({height : 0}, {duration : 400, easing : "easeInOutQuad", complete : function(){tg.removeClass("active");}});
						elm.addClass("over");
						elm.find("dd").css({display : "block", height : 0}).stop(true, false).animate({height : h}, {duration : 400, easing : "easeInOutQuad"});
					});
				}
			}
		});
	}
	
	setSoundData();
	setRollOver();
});

//ボタンオーバー処理
function setRollOver(){
	$("a, #soundBtn, p.btnClose, p#btnOpen, div.staffIcon").hover(
		function(){
			if(isIOS){
				playSound("html5", "play", "over");
			}else{
				playSound("flash", "play", "over");
			}
			$(this).find("img").stop(true, false).animate({opacity : 0.6}, 200);
		},
		function(){
			$(this).find("img").stop(true, false).animate({opacity : 1}, 200);
		}
	);
}

//音声データ処理

function setSoundData(){
	if(isIOS){
		$("#soundContainer").append('<audio src="' + AppGlobal.DOMAIN + 'common/audio/Mellifluous_4min.mp3" id="soundBGM" volume="1" onended="this.play()"></audio>');
		$("#overSoundContainer").append('<audio src="' + AppGlobal.DOMAIN + 'common/audio/over.wav" id="soundOver" volume="1"></audio>');
		$("#clickSoundContainer").append('<audio src="' + AppGlobal.DOMAIN + 'common/audio/click.wav" id="soundClick" volume="1"></audio>');
	}else{
		
		var swfVersionStr = "9";
		
		var flashvars1 = {
			path : AppGlobal.DOMAIN + "common/audio/Mellifluous_4min.mp3"
		};
		var flashvars2 = {
			path : AppGlobal.DOMAIN + "common/audio/over.mp3"
		};
		var flashvars3 = {
			path : AppGlobal.DOMAIN + "common/audio/click.mp3"
		};
		
		var params = {
			allowscriptaccess : "always",
			allowfullscreen : "false",
			wmode : "transparent"
		};
		
		var attr1 = {
			id : "external_sound1",
			name : "external_sound1"
		};
		var attr2 = {
			id : "external_sound2",
			name : "external_sound2"
		};
		var attr3 = {
			id : "external_sound3",
			name : "external_sound3"
		};
		
		swfobject.embedSWF(
				AppGlobal.DOMAIN + "common/audio/sound.swf",
				"soundContainer",
				"1", "1",
				swfVersionStr,
				null,
				flashvars1,
				params,
				attr1
		);
		
		swfobject.embedSWF(
				AppGlobal.DOMAIN + "common/audio/over.swf",
				"overSoundContainer",
				"1", "1",
				swfVersionStr,
				null,
				flashvars2,
				params,
				attr2
		);
		
		swfobject.embedSWF(
				AppGlobal.DOMAIN + "common/audio/click.swf",
				"clickSoundContainer",
				"1", "1",
				swfVersionStr,
				null,
				flashvars3,
				params,
				attr3
		);
		
	}
	/*$("#soundBtn").toggle(
		function(){
			//alert("on");
			$(this).find("img").hide();
			$(this).addClass("bgSound");
			if(isIOS){
				playSound("html5", "play", "BGM");
			}else{
				playSound("flash", "play", "BGM");
			}
			$.cookie("isBGM", 1, {expires:7});
		},
		function(){
			//alert("off");
			$(this).find("img").show();
			$(this).removeClass("bgSound");
			if(isIOS){
				playSound("html5", "pause", "BGM");
			}else{
				playSound("flash", "pause", "BGM");
			}
			$.cookie("isBGM", 0, {expires:7});
		}
	);*/
}

function playSound(type, mode, triger){
	if(type == "flash") {
		try {
			if(mode == "play"){
				if(triger == "BGM"){
					swfTarget("external_sound1").playSound();
				}else if(triger == "over"){
					swfTarget("external_sound2").playSound();
				}else{
					swfTarget("external_sound3").playSound();
				}
			}else{
				swfTarget("external_sound1").stopSound();
			}
		} catch(e) {
			//trace(e.message);
		}
	}else if(type == "html5"){
		try {
			if(mode == "play"){
				if(triger == "BGM"){
					document.getElementById("soundBGM").play();
				}else if(triger == "over"){
					document.getElementById("soundOver").play();
				}else{
					document.getElementById("soundClick").play();
				}
			}else{
				document.getElementById("soundBGM").pause();
			}
		} catch(e) {
			//trace(e.message);
		}
	}
}


function swfTarget(str) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[str];
	} else {
		return document[str];
	}
}


//ページ内スクロール
function scrollPageInner(offset){
	$("html, body").stop().animate({scrollTop : offset}, 600, "easeInOutQuad");
}

//スタックコンテンツ展開
function onStartAnimation(){		
	$("#preloader").delay(1000).animate({opacity : 0},{duration : 600, easing : "easeInOutQuad", complete : function(){
		$("#preloader").hide();
		$("#stackContainer").show();
		vGrid.vgrefresh(null, null, null, null);
	}});
}

//リキッドグリッド初期化
function setVgrid(){
	$("#stackContainer").append('<div class="empty"></div>');
	vGrid = $("#stackContainer").vgrid({
		easeing: "easeInOutExpo",
		useLoadImageEvent: true,
		time: 350
	});
}

//スタック無しページのアンカー設定
function setPageAnchor(){
	var preBase = $("#preloaderBase");
	var cont = $("#contents");
	
	cont.hide();
	preBase.animate({opacity : 0},{duration : 1000, easing : "easeInOutQuad", complete : function(){
		$(this).hide();
		cont.show();
		var pt = 42;
		var ch = $("#contents").height();
		cont.css({top : -(pt + ch)}).animate({top : 0},{duration : 600, easing : "easeOutQuad", complete : function(){
			if(contentsMode == "map"){
				$("#mapContainer").append('<iframe width="636" height="636" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.jp/maps?q=%E6%B8%8B%E8%B0%B7%E5%8C%BA%E7%A5%9E%E5%AE%AE%E5%89%8D3-35-8&amp;ie=UTF8&amp;hq=&amp;hnear=%E6%9D%B1%E4%BA%AC%E9%83%BD%E6%B8%8B%E8%B0%B7%E5%8C%BA%E7%A5%9E%E5%AE%AE%E5%89%8D%EF%BC%93%E4%B8%81%E7%9B%AE%EF%BC%93%EF%BC%95%E2%88%92%EF%BC%98&amp;gl=jp&amp;z=14&amp;vpsrc=0&amp;brcurrent=3,0x60188c9865907f71:0x589e604290cd01a8,0,0x60188c9872074ec3:0x8996954529c48a9a&amp;ll=35.671862,139.712402&amp;output=embed"></iframe>');
			}else if(contentsMode == "homebase"){
				setHomebaseSlide();
			}
			
			$("a").click(function(e){
					if(isIOS){
						playSound("html5", "play", "click");
					}else{
						playSound("flash", "play", "click");
					}
					var disp = getWindowSize();
					var url = $(this).attr("href");
					var target = $(this).attr("target");
					if(target != "_blank" && url.indexOf("mailto") == -1){
						e.preventDefault();
						//$("body, html").css({overflowY : "hidden"});
						scrollPageInner(0);
						cont.animate({top : disp.height},{duration : 600, easing : "easeInOutQuad", complete : function(){
							location.href = url;
						}});
					}
			});
		}});
	}});
}

//スタック有りページのアンカー設定
function setAnchor(){
	$("a").click(function(e){
			if(isIOS){
				playSound("html5", "play", "click");
			}else{
				playSound("flash", "play", "click");
			}
			var disp = getWindowSize();
			var url = $(this).attr("href");
			var target = $(this).attr("target");
			if(target != "_blank" && url.indexOf("mailto") == -1){
				e.preventDefault();
				var ln = $(".stackSingle").length;
				var ind = 0;
				var delay = 0;
				scrollPageInner(0);
				if(contentsMode == "staff"){
					$("#staffTitle").animate({opacity : 0},{duration : 600, easing : "easeInOutQuad"});
				}
				$(".stackModuleA, .stackModuleB").each(function(){
					$(this).delay(delay).animate({top : disp.height},{duration : 600, easing : "easeInOutQuad", complete : function(){
						ind++;
						$(this).remove();
						if(ind >= ln){
							location.href = url;
						}
					}});
					delay += 25;
				});
			}
			
	});
}

//スタックスタッフコンテナ開閉処理
function setToggleStaff(inner, elm, openBtn, closeBtn){
	var staffH;
	openBtn.bind("click", openStaff);
	
	function openStaff(){
		if(isIOS){
			playSound("html5", "play", "click");
		}else{
			playSound("flash", "play", "click");
		}
		staffH = elm.height();
		
		elm.css({display : "block", height : 0}).stop(true, false).animate({height : staffH},{duration : 600, easing : "easeInOutQuad", complete : function(){
			openBtn.unbind("click", openStaff);
			closeBtn.bind("click", closeStaff);
			vGrid.vgrefresh(null, null, null, null);
			var offset = elm.offset();
			scrollPageInner(offset.top);
		}});
	}
	
	function closeStaff(){
		if(isIOS){
			playSound("html5", "play", "click");
		}else{
			playSound("flash", "play", "click");
		}
		elm.stop(true, false).animate({height : 0},{duration : 600, easing : "easeInOutQuad", complete : function(){
			$(this).css({display : "none", height : staffH})
			openBtn.bind("click", openStaff);
			vGrid.vgrefresh(null, null, null, null);
			var offset = inner.offset();
			scrollPageInner(offset.top);
		}});
	}
}

//スタック内サムネイルスライド
function setThumbsSlide(){
	if(AppGlobal.isIE7) $("div.thumb").css({position : "relative"});
	
	$("div.thumbInner").each(function(){
		var type = Math.round((Math.random() * 3) + 1);
		runThumbsSlide($(this), type);
	});
}

function runThumbsSlide(elm, type){
	var slide;
	var ln = elm.find("a").length;
	
	if(ln != 1){
		if(type == 3 || type == 4){
			var bH = elm.height();
			var eH = bH * ln;
			elm.height(eH);
			var vy = 0;
			var py = 0;
			if(type == 3){
				//py = bH;				
				elm.find("a").each(function(){
					$(this).css({top : vy});
					vy -= bH;
				});
				slide = function(){
					elm.stop(true, false).animate({top : py},{duration : 600, easing : "easeInOutQuad", complete : function(){
						py += bH;
						if(py >= eH){
							py = 0;
						}
					}});
					setTimeout(slide, slideTime);
				}
			}else{
				//py = -bH;				
				elm.find("a").each(function(){
					$(this).css({top : vy});
					vy += bH;
				});
				slide = function(){
					elm.stop(true, false).animate({top : py},{duration : 600, easing : "easeInOutQuad", complete : function(){
						py -= bH;
						if(py <= -eH){
							py = 0;
						}
					}});
					setTimeout(slide, slideTime);
				}
			}
		}else{
			var bW = elm.width();
			var eW = bW * ln;
			elm.width(eW);
			var vx = 0;
			var px = 0;
			if(type == 1){
				//px = -bW;
				elm.find("a").each(function(){
					$(this).css({left : vx});
					vx += bW;
				});
				slide = function(){
					elm.stop(true, false).animate({left : px},{duration : 600, easing : "easeInOutQuad", complete : function(){
						px -= bW;
						if(px <= -eW){
							px = 0;
						}
					}});
					setTimeout(slide, slideTime);
				}
			}else{
				//px = bW;
				elm.find("a").each(function(){
					$(this).css({left : vx});
					vx -= bW;
				});
				slide = function(){
					elm.stop(true, false).animate({left : px},{duration : 600, easing : "easeInOutQuad", complete : function(){
						px += bW;
						if(px >= eW){
							px = 0;
						}
					}});
					setTimeout(slide, slideTime);
				}
			}
		}
		slide();
	}
	
}

//Our Home Baseスライド
function setHomebaseSlide(){
	if(AppGlobal.isIE7) $("#homebaseSlideInner").css({position : "relative"});
	var slide;
	var elm = $("#homebaseSlideInner")
	var ln = elm.find("img").length;
	var type = Math.round((Math.random() * 3) + 1);
	elm.css({display : "block", opacity : 0});
	
	if(ln != 1){
		if(type == 3 || type == 4){
			var bH = elm.height();
			var eH = bH * ln;
			elm.height(eH);
			var vy = 0;
			var py = 0;
			if(type == 3){
				//py = bH;				
				elm.find("img").each(function(){
					$(this).css({top : vy});
					vy -= bH;
				});
				slide = function(){
					elm.stop(true, false).animate({top : py},{duration : 600, easing : "easeInOutQuad", complete : function(){
						py += bH;
						if(py >= eH){
							py = 0;
						}
					}});
					setTimeout(slide, slideOHBTime);
				}
			}else{
				//py = -bH;				
				elm.find("img").each(function(){
					$(this).css({top : vy});
					vy += bH;
				});
				slide = function(){
					elm.stop(true, false).animate({top : py},{duration : 600, easing : "easeInOutQuad", complete : function(){
						py -= bH;
						if(py <= -eH){
							py = 0;
						}
					}});
					setTimeout(slide, slideOHBTime);
				}
			}
		}else{
			var bW = elm.width();
			var eW = bW * ln;
			elm.width(eW);
			var vx = 0;
			var px = 0;
			if(type == 1){
				//px = -bW;
				elm.find("img").each(function(){
					$(this).css({left : vx});
					vx += bW;
				});
				slide = function(){
					elm.stop(true, false).animate({left : px},{duration : 600, easing : "easeInOutQuad", complete : function(){
						px -= bW;
						if(px <= -eW){
							px = 0;
						}
					}});
					setTimeout(slide, slideOHBTime);
				}
			}else{
				//px = bW;
				elm.find("img").each(function(){
					$(this).css({left : vx});
					vx -= bW;
				});
				slide = function(){
					elm.stop(true, false).animate({left : px},{duration : 600, easing : "easeInOutQuad", complete : function(){
						px += bW;
						if(px >= eW){
							px = 0;
						}
					}});
					setTimeout(slide, slideOHBTime);
				}
			}
		}
		elm.show().delay(600).animate({opacity : 1},{duration : 600, easing : "easeInOutQuad", complete : function(){
			slide();
		}});
	}
}

//戻るボタン時のリロード処理
if(!AppGlobal.isIE){
	window.onbeforeunload = function(){location.reload(true);}
}

//ExternalInterface
function callBackFlash(){
	trace("isBGM : " + $.cookie("isBGM"));
	if($.cookie("isBGM")){
		if($.cookie("isBGM") == 1){
			isBGM = true;
			if(isIOS){
				playSound("html5", "play", "BGM");
			}else{
				playSound("flash", "play", "BGM");
			}
			$("#soundBtn").find("img").hide();
			$("#soundBtn").addClass("bgSound");
		}
	}
	$("#soundBtn").toggle(
		function(){
			if(isBGM){
				$(this).find("img").show();
				$(this).removeClass("bgSound");
				if(isIOS){
					playSound("html5", "pause", "BGM");
				}else{
					playSound("flash", "pause", "BGM");
				}
				$.cookie("isBGM", 0, {expires:7,path: '/'});
			}else{
				$(this).find("img").hide();
				$(this).addClass("bgSound");
				if(isIOS){
					playSound("html5", "play", "BGM");
				}else{
					playSound("flash", "play", "BGM");
				}
				$.cookie("isBGM", 1, {expires:7,path: '/'});
			}
		},
		function(){
			if(isBGM){
				$(this).find("img").hide();
				$(this).addClass("bgSound");
				if(isIOS){
					playSound("html5", "play", "BGM");
				}else{
					playSound("flash", "play", "BGM");
				}
				$.cookie("isBGM", 1, {expires:7,path: '/'});
			}else{
				$(this).find("img").show();
				$(this).removeClass("bgSound");
				if(isIOS){
					playSound("html5", "pause", "BGM");
				}else{
					playSound("flash", "pause", "BGM");
				}
				$.cookie("isBGM", 0, {expires:7,path: '/'});
			}
		}
	);
}


//タブ移動監視
var watch_tabObj = new Object();
watch_tabObj.date = new Date();
watch_tabObj.getTime = 0;
watch_tabObj.disabled = false;
function watchTab(){
	watch_tabObj.date = new Date();
	if((watch_tabObj.date.getTime() - watch_tabObj.time) >= 1000 && !watch_tabObj.disabled){
		watch_tabObj.disabled = true;
		otherTabOpen();
	}
	else if((watch_tabObj.date.getTime() - watch_tabObj.time) < 1000 && watch_tabObj.disabled){
		watch_tabObj.disabled = false;
		returnTabOpen();
	}
	watch_tabObj.time = watch_tabObj.date.getTime();
	setTimeout("watchTab()",500);
}
function returnTabOpen(){
	
}
function otherTabOpen(){
}

