ytdj.ui.initCoreControls = function()
{
		// Setup main fader
	$('#MixFader').slider( 
		{ 
			clicked: function( e, ui ){ ytdj.fader( ui.value ); },
			slide: function( e, ui ){ ytdj.fader( ui.value ); },
			minValue: 0, 
			maxValue: 100, 
			startValue: 50 
		} 
	);
	
	// Left channel fader
	ytdj.ui.Volume( ytdj.LEFT ).slider( { 
			slide: function( e, ui ){ ytdj.fader( $('#MixFader').slider("value") ); },
			change: function(e, ui)
			{ 
				ytdj.fader( $('#MixFader').slider("value") );
				PlaylistController.persistVolume( ytdj.LEFT );
			},
			minValue: 0, maxValue: 100, startValue: 0 } 
		);
	
	// Right channel fader
	ytdj.ui.Volume( ytdj.RIGHT ).slider( { 
			slide: function( e, ui ){ ytdj.fader( $('#MixFader').slider("value") ); },
			change: function(e, ui)
			{ 
				ytdj.fader( $('#MixFader').slider("value") );
				PlaylistController.persistVolume( ytdj.RIGHT );					
			},
			minValue: 0, maxValue: 100, startValue: 0 } 
		);
		
	// Left channel position
	$( '#' + ytdj.LEFT + '_Position' ).slider({
			start : function( e, ui ){ if(e){ytdj.userIsScrolling = true;} },
			stop : function( e, ui ){ 
				if(e){ytdj.userIsScrolling = false;} 
			},
			slide : function( e, ui )
			{
				if( ytdj.userIsScrolling )
				{
					ytdj.tube.seekTo( ytdj.LEFT, ytdj.tube.getDuration( ytdj.LEFT ) * ( ui.value / 100 ) );
				}
			}
	});
	// right channel position
	$( '#' + ytdj.RIGHT + '_Position' ).slider({
			start : function( e, ui ){ if(e){ytdj.userIsScrolling = true;} },
			stop : function( e, ui ){ 
				if(e){ytdj.userIsScrolling = false;} 
			},
			slide : function( e, ui )
			{
				if( ytdj.userIsScrolling )
				{
					ytdj.tube.seekTo( ytdj.RIGHT, ytdj.tube.getDuration( ytdj.RIGHT ) * ( ui.value / 100 ) );
				}
			}
	});
	
	// Youtube API function	
	$("#Chan1_Play").click( function(){
		if( ytdj.lastState[ ytdj.LEFT ] == ytdj.tube.STATE_UNDEFINED )
		{
			alert("You need to load a video from the playlist");
			return;
		}
		
		// is this kludgy?
		if (ytdj.tube.toggleVid(ytdj.LEFT)) {
			$(this).find("div").fadeIn(1000);
			$(this).unbind('mouseenter').unbind('mouseleave');
			ytdj.ui.buttonOver($(this));
		} else {
			$(this)
				.bind("mouseenter", function () { ytdj.ui.buttonOver($(this)) })
				.bind("mouseleave", function () { ytdj.ui.buttonOut($(this)) })
				.find("div").fadeOut(1000);
			ytdj.ui.buttonOut($(this));
		}
		
		// If we are in autoplay mode for the first time, set the channel
		if(ytdj.waitingOnAutoPlayChannel)
		{
			ytdj.autoplay_channel = ytdj.LEFT;
			ytdj.waitingOnAutoPlayChannel = false;
		};
		
	} );
	$("#Chan2_Play").click( function(){
		if( ytdj.lastState[ ytdj.RIGHT ] == ytdj.tube.STATE_UNDEFINED )
		{
			alert("You need to load a video from the playlist");
			return;
		}
		
		if (ytdj.tube.toggleVid(ytdj.RIGHT)) {
			$(this).find("div").fadeIn(1000);
			$(this).unbind('mouseenter').unbind('mouseleave');
			ytdj.ui.buttonOver($(this));
		} else {
			$(this)
				.bind("mouseenter", function () { ytdj.ui.buttonOver($(this)) })
				.bind("mouseleave", function () { ytdj.ui.buttonOut($(this)) })
				.find("div").fadeOut(1000);
			ytdj.ui.buttonOut($(this));
		}
		
		// If we are in autoplay mode for the first time, set the channel
		if(ytdj.waitingOnAutoPlayChannel)
		{
			ytdj.autoplay_channel = ytdj.RIGHT;
			ytdj.waitingOnAutoPlayChannel = false;
		};
	} );

	$("#Chan1_Mute").click( function() { 
		if (ytdj.tube.isMuted(ytdj.LEFT)) {
			ytdj.tube.mute(ytdj.LEFT, false);
			$(this).find("div").fadeOut(1000);
			$(this)
				.bind("mouseenter", function () { ytdj.ui.buttonOver($(this)) })
				.bind("mouseleave", function () { ytdj.ui.buttonOut($(this)) })
			ytdj.ui.buttonOut($(this));
		} else {
			ytdj.tube.mute(ytdj.LEFT, true);
			$(this).unbind('mouseenter').unbind('mouseleave')
			$(this).find("div").fadeIn(1000);
			ytdj.ui.buttonOver($(this));
		};
	});

	$("#Chan2_Mute").click( function() { 
		if (ytdj.tube.isMuted(ytdj.RIGHT)) {
			ytdj.tube.mute(ytdj.RIGHT, false);
			$(this).find("div").fadeOut(1000);
			$(this)
				.bind("mouseenter", function () { ytdj.ui.buttonOver($(this)) })
				.bind("mouseleave", function () { ytdj.ui.buttonOut($(this)) })
			ytdj.ui.buttonOut($(this));
		} else {
			ytdj.tube.mute(ytdj.RIGHT, true);
			$(this).unbind('mouseenter').unbind('mouseleave')
			$(this).find("div").fadeIn(1000);
			ytdj.ui.buttonOver($(this));
		};
	});
	
	$("#Chan1_Rewind").click( function() { 
		ytdj.tube.seekTo(ytdj.LEFT, 0);
	});

	$("#Chan2_Rewind").click( function() { 
		ytdj.tube.seekTo(ytdj.RIGHT, 0);
	});
	
};
	
	
	
ytdj.ui.initPlaylistOptions = function()
{
	$("#Chan1_ExportCookie").click( function(){ 
		$("#ImportExportBox h2").text("Left Channel Playlist");
		$("#ImportExportArea").val( PlaylistController.getCookie( ytdj.LEFT ) );
		$("#ImportExportHidden").val( ytdj.LEFT );
		$("#ImportExportBox").fadeIn("slow"); 
		$("#Chan1_Display .options .toggle").click(); // close the options
		
	});
	$("#Chan2_ExportCookie").click( function(){ 
		$("#ImportExportBox h2").text("Right Channel Playlist");
		$("#ImportExportArea").val( PlaylistController.getCookie( ytdj.RIGHT ) );
		$("#ImportExportHidden").val( ytdj.RIGHT );
		$("#ImportExportBox").fadeIn("slow");
		$("#Chan2_Display .options .toggle").click(); // close the options 
	} );
	$("#ImportExportClose, #ImportExportBox .close").click( function(){ $("#ImportExportBox").fadeOut("slow") });
	$("#ImportExportClear").click( function()
	{
		$("#ImportExportArea").val( "" );
		$("#ImportExportBox h2").text( $("#ImportExportHidden").val() == ytdj.LEFT ? "Add to Left Playlist" : "Add to Right Playlist" );
	});
	$("#ImportExportAdd").click( function(){
		PlaylistHelper.addTrack( $("#ImportExportHidden").val(), $("#ImportExportArea").val(), null );
		$("#ImportExportBox").fadeOut("slow");
	});
	
	$("#Chan1_Clear").click( function(){ 
		if(confirm('delete entire playlist?'))
		{ 
			PlaylistController.emptyPlaylist( ytdj.LEFT ); 
			PlaylistController.update( ytdj.LEFT );
		}
		$("#Chan1_Display .options .toggle").click(); // close the options
	} );
	$("#Chan2_Clear").click( function(){ 
		if(confirm('delete entire playlist?')) 
		{
			PlaylistController.emptyPlaylist( ytdj.RIGHT );
			PlaylistController.update( ytdj.RIGHT );
		}
		$("#Chan2_Display .options .toggle").click(); // close the options
	} );	
};



ytdj.ui.initCueControls = function()
{
	$("#Chan1_SetCue1").click( function(){ ytdj.setCueToCurrentTime( ytdj.LEFT, 1 ) } );
	$("#Chan2_SetCue1").click( function(){ ytdj.setCueToCurrentTime( ytdj.RIGHT, 1 ) } );
	
	$("#Chan1_Arm").click( function()
	{
		ytdj.cueable[ ytdj.LEFT ] = !ytdj.cueable[ ytdj.LEFT ];
		var cueButton = $("#Chan1_Cue1");
		cueButton.toggleClass("armed");			
		if (cueButton.text() == "Set") {
			cueButton.text("Cue");
		} else {
			cueButton.text("Set");
		}
	})
	$("#Chan2_Arm").click( function()
	{
		ytdj.cueable[ ytdj.RIGHT ] = !ytdj.cueable[ ytdj.RIGHT ];
		var cueButton = $("#Chan2_Cue1");
		cueButton.toggleClass("armed");			
		if (cueButton.text() == "Set") {
			cueButton.text("Cue");
		} else {
			cueButton.text("Set");
		}
	})
	
	$("#Chan1_Cue1").click( function(){
		if( ytdj.cueable[ ytdj.LEFT ] )
		{
			ytdj.setCueToCurrentTime( ytdj.LEFT );
			$("#Chan1_Arm").click();
		} 
		else
		{
			ytdj.tube.playFromCue( ytdj.ui.cuePoints[ ytdj.LEFT ].val(), ytdj.LEFT ); 
		}
	} );
	
	$("#Chan2_Cue1").click( function(){ 
		if( ytdj.cueable[ ytdj.RIGHT ] )
		{
			ytdj.setCueToCurrentTime( ytdj.RIGHT );
			$("#Chan2_Arm").click();
		} 
		else
		{
			ytdj.tube.playFromCue( ytdj.ui.cuePoints[ ytdj.RIGHT ].val(), ytdj.RIGHT ); 
		}
	} );
};
	
	
	
ytdj.ui.initHelp = function()
{
	// ========  Help Dialogs
	// open-all or close-all help boxes with the help button
	$("#helpButton, #helpButtonDesc").click(function()
	{
		if(ytdj.help.numberOfOpenHelps() < ytdj.help.helpboxes.length)
		{
			// Hide the helpbar, if it's showing
			ytdj.help.hideHelpBar();
			
			//ytdj.help.openAll();
			ytdj.help.finessOpenAll();
		}
		else
		{
			ytdj.help.closeAll();
		}
	});
	
	$("#HelpBar .close").click(function()
	{
		ytdj.help.hideHelpBar();
	});

};



ytdj.ui.initPublishOptions = function()
{
	$("#publishButton").click(function()	{
		$(".publish").fadeIn("slow");
		return false;
	});
	
	$("#publishSave").click(function()
	{
		var cleanName = ytdj.helper.viewClean( $("#publishTitle").val() );
		var urlName = ytdj.helper.simpleClean( cleanName, true );
		
		if( urlName.length < ytdj.helper.minSetNameLength )
		{
			alert("The Url name must be greater than " + ( ytdj.helper.minSetNameLength - 1 ) + " characters.");
			return;
		}
		
		if(confirm("Save this set to the public URL:\nhttp://www.turntubelist.com/set/" + urlName + "?"))
		{
			ytdj.helper.saveSet( 
				cleanName, 
				false /* Save public */, 
				ytdj.ui.onSetSaving,
				function( setName, status ){
					ytdj.ui.onSetSaved( setName, status );
					if( status == 'success' )
					{
						var setUrl = 'http://www.turntubelist.com/set/' + setName;
						ytdj.help.showOurMessageToYou( 'Your set has been saved to <a href="' + setUrl + '>' + setUrl + '</a>', false );
					}
				}
			);
			
			$(".publish .close").click();
		}
	});
	$("#publishTitle").keyup(function()
	{
		var setName = $("#publishTitle").val();
		$("#publishUrl").val( "http:www//" + ytdj.baseUrl + "/" +  ytdj.helper.simpleClean( setName, true ) );
		$("#publishTitle").val( ytdj.helper.viewClean( setName ) );
	});

};



ytdj.ui.initServerSideOptions = function()
{
	// Feedback and register handlers
	$(".feedback .close, .register .close, .loginbox .close, .publish .close").click(function() { $(this).parent().fadeOut("slow") });
			
	// Register form
	$("#RegisterSend").click(function(){
		ytdj.user.register();
	});
	
	// Set up logged in message
	
	// Set up not logged in message
	
	// login
	$("#Login").click(function()
	{
		ytdj.server.login( $("#LoginName").val(), $("#LoginPass").val(), ytdj.server.onLogin );
		$(this).parent().fadeOut("slow");
	});

	// feedback form
	$("#FeedbackSend").click(function() { 
		var name = $("#FeedbackName").val();
		var from = $("#FeedbackEmail").val();
		var msg = $("#FeedbackMessage").val();
		$(".feedback.dialog h2").text("sending...");
		ytdj.helper.feedback(name, from, msg, function(data, status)
		{
			if( status == "validation" )
			{
				// no form fields filled in... 
				$(".feedback.dialog h2").text("Fill in the fields!");
				return;
			}
			
			if(status == "success" && data == "1")
			{
				// Sent.
				$(".feedback.dialog form").slideUp("slow");
				$(".feedback.dialog h2").text("Thank you for your feedback.");
				setTimeout('$(".feedback.dialog").fadeOut()',5000);
			}
			else
			{
				// Error sending.
				//alert(data); // TODO: uncomment this alert.
				$(".feedback.dialog h2").text("Oops... didn't work, please try again later.");
			}
		});
	});
	
	// Authenticated stuff		
	$("#authLoadSets").click(function(){
		$("#Chan1_ul").load("playlists/get_list");
	});
	
	$("#authSaveSet").click( function(){
		if( $('#SetName').val().length < ytdj.helper.minSetNameLength )
		{
			alert("Set name must be greater than " + ytdj.helper.minSetNameLength + " characters.");
			return;
		}
		ytdj.helper.saveSet( $('#SetName').val(), true, ytdj.ui.onSetSaving, ytdj.ui.onSetSaved );
		
	});
	
	$("#authDeleteSet").click( function()
	{
		if( $('#SetName').val().length < ytdj.helper.minSetNameLength )
		{
			alert("No set loaded.");
			return;
		}
		if( confirm("Are you sure you want to delete set '" + $('#SetName').val() +"'?"))
		{
			ytdj.helper.deleteSet( $('#SetName').val(), ytdj.ui.onSetSaving, ytdj.ui.onSetSaved );
		}
		
	});
		
};