function JukeboxAdd(addbtn, videoid)
{
	// prevent double click adding twice
	addbtn.onclick = function() { return false; };

	$(addbtn).fadeOut("slow", function() {
		$.ajax({
			type: "GET",
			url: "/jukebox.php",
			data: "action=add&vid=" + videoid,
			complete: function(XMLHttpRequest, textStatus) {
				if (textStatus == "error")
				{
					alert("Failed to add video to jukebox");
					$(addbtn).css("display", "block");					
				}
				else
				{
					//alert("Video added to jukebox successfully");
				}
			}
		});
	});
}

var videosPlayed = [];

function JukeboxPlay()
{
	// stop sorting of playlist
	$("#videos").sortable("disable");
	$("#videos").css("cursor", "default");
	
	// start playing
	JukeboxPlayNext();
}

// flash next video callback
function JukeboxPlayNext()
{
	var currentvideoid = '';
	
	$("#videos").children("li").each(function() {
		var videoid = this.id.split("_")[1];
		$(this).removeClass("selected");

		// if video hasnt been played before
		if (videosPlayed.join().indexOf(videoid) < 0)
		{
			videosPlayed[videosPlayed.length] = videoid;
			currentvideoid = videoid;
			
			// highlight in playlist
			$(this).addClass("selected");

			var flashVars="";
				
			flashVars+="vidid=" + videoid;
			flashVars+="&vid=%2Fvideos%2F" + videoid + ".flv";
			flashVars+="&id=" + videoid;
			flashVars+="&autoplay";
			
			// load video
			var flashcode = GenerateFlash("/swf/jukebox.swf?20081216", "746", "450", "#ffffff", "transparent", "VideoPlayer", flashVars);
			$("#jukebox .player").html(flashcode);
			
			return false; // break out
		}
	});
	
	// playlist is over, show play button and reset
	if (currentvideoid == '')
	{
		$("#jukebox .player").html('<a class="play" href="javascript:JukeboxPlay();" style="display: block;"><img class="pngfix" src="/images/buttons/play.png" alt="Play"/></a>');
		videosPlayed = [];
		
		$("#videos").sortable("enable");
		$("#videos").css("cursor", "move");
	}
}

function JukeboxList()
{	
	$("#videos").sortable({
	    placeholder: "placeholder", 
	    //revert: true,
		forcePlaceholderSize: true,
		tolerance: 'pointer',
		containment: 'parent',
		axis: 'y',
		stop: function() {
			// save new positions here
			$.ajax({
				type: "GET",
				url: "jukebox.php",
				data: "action=save&" + $(this).sortable('serialize')
			});
		}
	});
}