// ----------------------------------------------
// Deck
// ----------------------------------------------
function Deck(id)
{
	this.id		= id;
	this.player	= null;
	this.video	= "";

	this.setVideo = function(video)
	{
		this.player = null;
	

		this.video = video.replace(/http:\/\/www.youtube.com\/watch\?v=([a-zA-Z0-9]+)/g, "http://www.youtube.com/v/$1");
		this.embedPlayer();
	
//		console.log(this.video);
//		alert(video);
		
	}
	
	this.embedPlayer = function()
	{
		// Create the player, will invoke "onYouTubePlayerReady" callback
		var atts = { id: this.id };
		var params = { allowScriptAccess: "always", bgcolor: "#ffffff" };
		var w = "400";
		var h = "300";
		var f = "8";

		swfobject.embedSWF(this.video+"&amp;border=0&amp;enablejsapi=1&amp;playerapiid="+this.id+"&amp;rel=1", this.id, w, h, f, null, null, params, atts);
	}
	
	this.onReady = function()
	{

		this.player = document.getElementById(this.id);		
		console.log(this.id+".onReady()");
	}

	this.setVolume = function(val)
	{
		if (this.player)
		{
			this.player.setVolume(val);
//			console.log(this.id+".setVolume("+val+")");
		}
	}
	
	this.play = function()
	{
		if (this.player)
			this.player.playVideo();
	}

};


// ----------------------------------------------
// Playlist
// ----------------------------------------------
function Playlist(id)
{
	this.id = id;
//	$("#"+this.id).treeview( {url: "playlist.php"} );
//	$("#"+this.id).treeview( {toggle:function(){} } );
}

// ----------------------------------------------
// Mixer
// ----------------------------------------------
Mixer = 
{
	deckLeft:null,
	deckRight:null,
	crossFader:null,
	playlistLeft:null,
	playlistRight:null,
	
	init:function()
	{	
		this.deckLeft		= new Deck("deckLeft");		
		this.deckRight		= new Deck("deckRight");		

		this.playlistLeft	= new Playlist("deckLeftPlaylist");
		this.playlistRight	= new Playlist("deckRightPlaylist");

		this.setDeckVideo("deckLeft",		"http://www.youtube.com/watch?v=eGPhUr-T6UM");
		this.setDeckVideo("deckRight",		"http://www.youtube.com/watch?v=z33tH-JdPDg");

		$("#crossFader").slider(
									{
										animate:true,
										slide : function(e,ui)
												{
													Mixer.onCrossFaderSlide(e,ui);
												}
									}
								);
	},
	getDeck : function(id)
	{
		return id == "deckLeft" ? this.deckLeft : this.deckRight;
	},
	getVolumeForDeck : function(id)
	{
		if (id == "deckLeft")
			return 100-$("#crossFader").slider("value");
		else
		if (id == "deckRight")
			return $("#crossFader").slider("value");
		return 0;
	},
	getDeckForm : function(id)
	{
		return $("#"+id+"Form");
	},
	setDeckFormValue : function(id, value)
	{	
		this.getDeckForm(id).attr("value", value);	
	},
	getVideoURLFromInput : function(id)
	{
		var url = this.getDeckForm(id).attr("value");
		return url;
	},
	setDeckVideo: function(id,video)
	{
		this.setDeckFormValue(id,video);
		this.getDeck(id).setVideo(video);
//		this.setDeckVideo(id, video);

	},
	setDeckVideoFromInput: function(id)
	{
		this.setDeckVideo(id, this.getVideoURLFromInput(id));
		return false;
	},
	onCrossFaderSlide : function(e,ui)
	{
		this.setDecksVolume(100-ui.value, ui.value);
	},
	setDecksVolume : function(leftVol, rightVol)
	{
		this.getDeck("deckLeft").setVolume(leftVol);
		this.getDeck("deckRight").setVolume(rightVol);
		
		$("#crossFaderInfos").html(leftVol+"-"+rightVol);
	},
	onReady : function(playerId)
	{
		with(this.getDeck(playerId))
		{
			onReady();
			setVolume( this.getVolumeForDeck(playerId) );
			console.log( this.getVolumeForDeck(playerId) );
		}

		this.getDeck("deckLeft").play();
	},
	left : function()
	{
		$("#crossFader").slider("value", 0);
		this.setDecksVolume(100,0);
	},
	right : function()
	{
		$("#crossFader").slider("value", 100)
		this.setDecksVolume(0,100);
	},
	showHideSearchFrame : function()
	{
		$("#search").toggle();
	}
};

// ----------------------------------------------
// onYouTubePlayerReady
// ----------------------------------------------
function onYouTubePlayerReady(playerId) 
{
//	alert("onYouTubePlayerReady");
	Mixer.onReady(playerId);
}