
function loadTodayAnimeInfo(groupId)
{
	var gid = groupId;
	
	var path = "/xml/today_anime.xml";
	
	$(".today_anime").empty();
	
	$.ajax({
		url: path,
		dataType: "xml",
		success: function(data){
			var group;
			var child;
			var item;
			
			if(gid != undefined & gid != "")
			{
				group = $(data).find("group[id='" + gid + "']");
				
				if(group.size() > 0)
				{
					var n = 0;
					child = group.children();
					
					if(group.attr("random") != undefined && group.attr("random") == "true")n = Math.floor(Math.random() * child.length);
					
					item = $(child.get(n));
				}
				else
				{
					group = $(data).find("group[id='default']");
					child = group.children();
					item = $(child.get(0));
				}
			}
			else
			{
				group = $(data).find("group[id='default']");
				child = group.children();
				item = $(child.get(0));
			}
			
			var aTag = $('<a></a>');
			var url = item.find("url");
			if(url.attr("target") != undefined)
			{
				if(url.attr("target") == "thickbox")
				{
					aTag.attr("href", "#");
					aTag.click(function(){
						tb_show("", url.text());
						
						flashSoundMute();
						return false;
					});
				}
				else
				{
					aTag.attr("href", url.text());
					aTag.attr("target", utl.attr("target"));
				}
			}
			else
			{
				aTag.attr("href", url.text());
			}
			
			var imgSrc = $(item).find("img").text();
			var imgTag = $('<img src="' + imgSrc + '" width="221" height="165" alt="" />');
			imgTag.onmouseover = function() {
				this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));
			}
			imgTag.onmouseout = function() {
				this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));
			}
			
			aTag.append(imgTag);

			$(".today_anime").append(aTag);
			
			/*
			var items = $(data).find("item");
			var item = $(items).get(0);
			
			
			var href = $(item).find("url").text();
			
			var target = "";
			if($(item).find("url").attr("target") != undefined)target = 'target="' + $(item).find("url").attr("target") + '"';
			
			var imgSrc = $(item).find("img").text()
			
			
			var imgTag = $('<img src="' + imgSrc + '" width="221" height="165" alt="" />');
			imgTag.onmouseover = function() {
				this.setAttribute("src", this.getAttribute("src").replace("_off.", "_on."));
			}
			imgTag.onmouseout = function() {
				this.setAttribute("src", this.getAttribute("src").replace("_on.", "_off."));
			}
			
			var aTag = $('<a href="' + href + '" ' + target + '></a>');
			aTag.append(imgTag);
			
			$(".today_anime").append(aTag);
			*/
		}
	});
	
}


var minVolume = 0.1;
var maxVolume = 0.8;
var closeFlg = true;
function bgmInit(sub, scrolling)
{
	if(scrolling){
		$(window).scroll(function(){
			var vol = getVolume();
			
			var obj = getMovie();
			if(obj && typeof(obj.setVolume) == "function"){
				obj.setVolume(vol);
			}
		});
	}
	if(sub){
		window.onbeforeunload = function(){
			bgmSleep(true, true);
			if(closeFlg){
				if(window.opener && !window.opener.closed && typeof(window.opener.bgmSleep) == "function"){
					window.opener.bgmSleep(false, true);
				}
			}else{
				bgmSleep(true, true);
			}
			return;
		}
	}else{
		window.onbeforeunload = function(){
			obj = getMovie();
			if(obj && typeof(obj.clearData) == "function"){
				obj.clearData();
			}
			return;
		}	
	}

}
function getVolume()
{
	var sh;
	if(jQuery.browser.msie){
		sh = document.body.scrollHeight-document.body.clientHeight;
	}else{
		sh = document.height-window.innerHeight;
	}
		
	var rat = sh / $(document).height();
	var h = sh;
	
	var s = $(window).scrollTop();

	var targetPos = {top:$("#charaArea1").offset().top * rat, bottom:($("#charaArea2").offset().top + $("#charaArea2").height()) * rat};

	var vol = minVolume;
	if(s < targetPos.top){
		vol = s / targetPos.top * maxVolume;
		if(vol < minVolume)vol = minVolume;
		else if(vol > maxVolume)vol = maxVolume;
	
	}else if(targetPos.top >= s && s <= targetPos.bottom){
		vol = maxVolume;
	}else{
		
		var h2 = h - targetPos.bottom;
		var diff = h - s;
	
		vol = diff / h2 * maxVolume;
		if(vol < minVolume)vol = minVolume;
		else if(vol > maxVolume)vol = maxVolume;
		
	}
	
	console.log(vol);
	
	return vol;
}

function getMovie()
{
	var obj = document["externalSoundMgr"] || window["externalSoundMgr"];
	return obj;
}

function bgmSleep(arg0, arg1)
{
	var obj = getMovie();
	if(obj && typeof(obj.setSleep) == "function"){
		obj.setSleep(arg0, arg1);
	}
}


function getMovie2(id)
{
	var obj = document[id] || window[id];
	return obj;
}
//アニメ事典プレイヤーとその他Flashの音を制御
/**
 * thickbox.jsの読み込みよりもあとにこの関数を実行すること！
 */
function flashSoundControlInit()
{
	$('a.thickbox, area.thickbox, input.thickbox').bind("click", function(){
		flashSoundMute();
	});
	
	if(window.opener)
	{//サブウィンドウの場合
		var owner = window.opener;
		
		var urls = ["/play/01-naruhodo-game/alphabet/",
					"/play/01-naruhodo-game/kisekae/",
					"/anime/popup.php?code=ot&id=theme_song"];
		
		for(var i = 0; i < urls.length; i++){
			if(window.location.href.indexOf(document.domain + urls[i]) != -1){
				owner.flashSoundMute();
				break;
			}
		}
		
		$(window).bind("beforeunload", function(){
			if(!owner.closed && (typeof(owner.flashSoundUnmute) == "function" || typeof(owner.flashSoundUnmute) == "object")){
				//アニメ辞典プレイヤーが表示さているか判定
				var obj = owner.$("#TB_overlay");
				
				//アニメ辞典プレイヤーが表示さていなければ、親ウィンドウの音を元に戻す
				if(obj && obj.size() <= 0)owner.flashSoundUnmute();
			}
			return;
		});
	}
}
function flashSoundMute()
{
		var obj = getMovie2("categoryContent");
		if(obj && typeof(obj.mute) == "function"){
			obj.mute(true);
		}
		
		obj = getMovie2("cardContent");
		if(obj && typeof(obj.mute) == "function"){
			obj.mute(true);
		}
}
function flashSoundUnmute()
{
	var obj = getMovie2("categoryContent");
	if(obj && typeof(obj.mute) == "function"){
		obj.mute(false);
	}
	
	obj = getMovie2("cardContent");
	if(obj && typeof(obj.mute) == "function"){
		obj.mute(false);
	}
}

$(document).ready(function(){
	flashSoundControlInit();
});

