
var currentSceneSound;

$(function(){
	
	// scene slector
	$("ul.flat").hide();
	$("ul#currentScene #close").hide();
	$("ul#currentScene").click(function () {
		$("ul.flat").slideToggle(200);
		$("ul#currentScene #close").toggle();
	});
	//外側クリックで閉じる
	$("#wrapper").live("click", function(event){
		$("ul.flat").slideUp(200);
		$("ul#currentScene #close").hide();
	});
	$("#scene_selector").live("click", function(){
		return false;
	});
	
	
	$("#sceneBtn").click(function () {
		if(currentSceneSound.paused)
		{
			currentSceneSound.resume();
		}
		else
		{
			currentSceneSound.pause();
		}
	});
	
	
	
	// artist info box
	$("a[rel]").overlay({effect: 'apple'});
	
	// global menu
	$("#globalmenu").tabs("#mainContents > div", { history: true , effect: 'fade', fadeOutSpeed: 400});
	//$("#scrmenu").tabs("#mainContents > div", { history: true , effect: 'fade', fadeOutSpeed: 400}); // こうするとNG。同じUL内にないと駄目みたい
	
	// auto play
	if($("#tones").hasClass('autoplay')) 
	{
		threeSixtyPlayer.config.autoPlay=true;
		
		//threeSixtyPlayer.config.autoPlayID=$("#tones").attr('tone');
		//console.log(">>>"+$("#tones").attr('tone'));
	}
	
	
	
	// ------------------------------- FILTER -------------------------------------
	
	
	
	// 読込後最初の状態のTWEET ボタンのリンク先書き換え
	var url = document.URL.replace("#","");
	var txt = "";
	if ( url.indexOf( "artist" ) != -1 ) 
	{
		txt = 'like this artist "'+url+' #lifetones';
	}
	else if ( url.indexOf( "tone" ) != -1 ) 
	{
		txt = 'like this tone "'+url+' #lifetones';
	}
	else
	{
		txt = 'like this site "'+url+' #lifetones';
	}
	txt = encodeURIComponent(txt);
	$("#filterTweetBtn").attr('href','http://twitter.com/?status='+txt);
	
	
	
	$("#filter :radio").change(function()
	{
		//console.log("radio change");
		if ($(this+':checked').val() == 'all') 
		{
			// showall
			//console.log("show all");
			
			$("#tones").animate({opacity: 0},300,'linear',function(){
				$('.toneUnit').not('.indexUnit').show();
				$('.toneUnit.indexUnit.coverUnit').show();
				$('.toneUnit.indexUnit.artistUnit').hide();
				fadeInTones();
			});
			
			var txt = 'LiFETONES http://lifetones.net #lifetones';
			txt = encodeURIComponent(txt);
			
			// TWEET ボタンのリンク先書き換え
			$("#filterTweetBtn").attr('href','http://twitter.com/?status='+txt);
			
		} else {
			var checked = $('input[name="artist"]:checked');
			var selectedArtistID 	= checked.val();
			var selectedArtistName	= checked.next().html();
			//console.log(selectedArtistName);
			
			$("#tones").animate({opacity: 0},300,'linear',function(){
				filterByArtist(selectedArtistID);
				fadeInTones();
			});
			
			var lang = $('body').attr('lang');
			var txt = 'like this artist "'+selectedArtistName+'" tones at http://lifetones.net/'+lang+'/artist.'+selectedArtistID+'/ #lifetones';
			txt = encodeURIComponent(txt);
			
			// TWEET ボタンのリンク先書き換え
			$("#filterTweetBtn").attr('href','http://twitter.com/?status='+txt);
			//$("#filterTweetBtn").attr('href',);
			
		}
	});
	
	
	$('.toneUnit.indexUnit.artistUnit').hide();
	
	// GETからアーティスト名を取得して描画
	var select_artist = $("#filter").attr("select");
	//console.log(select_artist);
	if(select_artist!=null)
	{
		var radios = $('input[name="artist"]');
		//console.log("アーティスト指定あり");
		for(var i=0; i<radios.length; i++)
		{
			if( $(radios[i]).val() == select_artist )
			{
				//console.log("該当するアーティストがいた");
				
				filterByArtist(select_artist);
				
				// 手動でチェックを入れる
				$(radios[i]).attr("checked","checked");
				
			}
		}
	}
	// 並び順(途中：2010/9/27)
	//$('#tones').quicksand( $('#destination li') );
	
});

function fadeInTones()
{
	$("#tones").animate({opacity: 1},600);
}
function filterByArtist(artistID)
{
	var toneUnits = $('.toneUnit');
	for(var i=0; i<toneUnits.length; i++)
	{
		var toneUnit = $(toneUnits[i]);
		if(toneUnit.attr('artist') == artistID)
		{
			toneUnit.show();
		}
		else
		{
			toneUnit.hide();
		}
	}
}

// GETパラメータを取得する「vars.パラメータ名」で値を取得
function getGETS()
{
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	
	for(var i = 0; i < hashes.length; i++)
	{ 
		hash = hashes[i].split('='); 
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	
	return vars;
}

function changeBackGroundImage(obj)
{
	var image = $(obj).css("background-image");
	var title = $(obj).text();
	$("#currentScene li a span#title").text(title);
	$("body").css("background-image",image);
}

function onSceneSoundPlay(obj)
{
	currentSceneSound = obj;
}

function setSceneBtnOn()
{
	$("#sceneBtn img").attr("src","http://lifetones.net/img/sceneBtn-on.png");
	$("#sceneBtn").removeClass("pausing");
	$("#sceneBtn").addClass("playing");
}
function setSceneBtnOff()
{
	$("#sceneBtn img").attr("src","http://lifetones.net/img/sceneBtn-off.png");
	$("#sceneBtn").removeClass("playing");
	$("#sceneBtn").addClass("pausing");
}

