$("#playlist-form-btn").click(function(){
	if ($("#show_playlists").is(':hidden')) {
		var left = document.getElementById("playlist-form-btn").offsetLeft;
		var top = document.getElementById("playlist-form-btn").offsetTop + 15;
		$("#show_playlists").css({'left':left+'px','top':top+'px','color':'#555555','padding':'5px'}).show();
		show_playlists();
		return false;
	} else {
		$("#video_playlist_form").slideUp('fast');
		$("#show_playlists").hide();
	}
});

$(document).click(function(){
    $("#show_playlists").click(function(){ return false;});
	$("#show_playlists").hide();
});

function create_playlist() {
	var playlist_name = $("form#pl-frm input#playlist_name").val();
	
	if (playlist_name == '') {
		return false;
	}
	
	$("#show_playlists").hide();
	
	var sUrl = baseurl + "/ajax/playlist.php?action=create_playlist&playlist_name=" + playlist_name;
	
	$.ajax({
	    type: "GET",
	    url: sUrl,
	    dataType: 'json',
	    success: function(msg) {
			$("form#pl-frm input#playlist_name").val('');
			
			if (isNaN(parseInt(msg.message))) {
				$("#video-tools-result").html(msg.message).slideDown('fast');
			} else {
				add_video_playlist(msg.message);
			}
		},
	    error: function() {
			//$("#video-tools-result").html('connection failed').show();
		}
	});
}

function show_playlists() {
	$("#show_playlists").html('<img src="' + baseurl + '/templates/images/loading.gif" />');

	var form_only = 0;
	
	if (typeof(playlist_show_form) != "undefined") {
	    form_only = 1;
	}
	
	var sUrl = baseurl + "/ajax/playlist.php?action=show_playlist&user_id=" + user_id + "&form_only=" + form_only;
	
	$.ajax({
	    type: "GET",
	    url: sUrl,
	    dataType: 'json',
	    success: function(msg) {
			$("#show_playlists").html(msg.message).show();
		},
	    error: function() {
			//$("#video-tools-result").html('connection failed').show();
		}
	});
}

function add_video_playlist(pl_id) {
	var playlist_id = pl_id;
	
	if (playlist_id == 0) {
		playlist_id = $("form#show-pl-frm select#playlist_id").val();
	}
	
	var sUrl = baseurl + "/ajax/playlist.php";
	var postData = "action=add_playlist_video&video_id=" + vid + "&playlist_id=" + playlist_id;
	
	if (playlist_id == '') {
		return false;
	}
	
	$.ajax({
		type: "GET",
		url: sUrl,
		data: postData,
		dataType: 'json',
		success: function(msg) {
			$("#video-tools-result").html(msg.message).slideDown('fast');
			$("div[rel=dd-boxes]").remove();
			playlist_queue(playlist_id);
		},
		error: function() {
			//$("#video-tools-result").html('connection failed').show();
		}
	});
}

function playlist_queue(playlist_id)
{
    var sUrl = baseurl + "/ajax/playlist.php?action=video_queue&plid=" + playlist_id;
    $.ajax({
		type: "GET",
		url: sUrl,
		dataType: 'html',
		success: function(msg) {
		    $("#playlist_box").remove();
			$("body").append("<div id='playlist_box' class='quicklist_box quicklist_box_left'></div>");
			$("#playlist_box").html(msg).show();
		},
		error: function() {
			//alert('connection failed');
		}
	});
}

if (playlist_id > 0)
{
    playlist_queue(playlist_id);
}


