var cancel_open = false;

var current_box = '';

var current_drag_top = '';
var current_drag_left = '';

$(document).ready(function()
{
		
	$('#playlist_btn').click(function()
	{
		if (playlist != '') {
			window.location = urlbase + '/playlist?list=' + playlist;
		} else {
			alert('La playlist est vide!');
		}
	});
	
	$('.addplaylist a').click(function()
	{
		var postid = $(this).attr('class').substr(9);
		$.ajax(
		{
			url: urlbase + '/wp-content/add_playlist.php',
			type: 'POST',
			data: 'postid=' + postid,
			success: function(data)
			{
				if (data == 'IN') {
					if (playlist != '') {
						playlist += ',' + postid;
					} else {
						playlist = postid;
					}
					
					playlist_count++;
					$('#playlistcount').html(playlist_count.toString());
					
					/* Make the delete appear */
					$('#post_' + postid + ' .removeplaylist').each(function()
					{
						$(this).css('display', '');
					});
					
					$('#post_' + postid + ' .full_removeplaylist').each(function()
					{
						$(this).css('display', '');
					});
				}
				
				return false;
			}
		});
	});
	
	$('.out_addplaylist').click(function()
	{
		var postid = $('a', this).attr('class').substr(9);
		$.ajax(
		{
			url: urlbase + '/wp-content/add_playlist.php',
			type: 'POST',
			data: 'postid=' + postid,
			success: function(data)
			{
				if (data == 'IN') {
					if (playlist != '') {
						playlist += ',' + postid;
					} else {
						playlist = postid;
					}
					
					playlist_count++;
					$('#playlistcount').html(playlist_count.toString());
					
					/* Make the delete appear */
					$('#post_' + postid + ' .removeplaylist').each(function()
					{
						$(this).css('display', '');
					});
					
					$('#post_' + postid + ' .full_removeplaylist').each(function()
					{
						$(this).css('display', '');
					});
				}
				
				return false;
			}
		});
	});
	
	/* Remove from playlist buttons */
	$('.removeplaylist').click(function()
	{
		var postid = $(this).attr('id').substr(2);
		var parent = $(this).parent().parent();
		
		if (typeof(in_playlist) != "undefined") {
			parent.fadeOut(300);
		}
		
		$(this).css('display', 'none');
		$('.full_removeplaylist', parent).css('display', 'none');
		
		playlist_count--;
		$('#playlistcount').html(playlist_count.toString());
		
		if (playlist_count == 0) {
			playlist = '';
		} else {
			var plist = playlist.split(',');
			var plist_out = '';
			
			for (var i = 0; i < plist.length; i++) {
				if (plist[i].toString() != postid.toString()) {
					if (plist_out == '') {
						plist_out = plist[i];
					} else {
						plist_out += ',' + plist[i];
					}
				}	
			}

			playlist = plist_out;
		}
		
		$.ajax(
		{
			url: urlbase + '/wp-content/remove_playlist.php',
			type: 'POST',
			data: 'postid=' + postid,
			success: function(data) {
				if (data == 'empty') {
					if (uri != '') {
						window.location = urlbase + '/category/navigation/corpo/';
					}
				}
			}
		});
	});
	
	/* Remove from playlist buttons */
	$('.full_removeplaylist').click(function()
	{
		var postid = $(this).attr('id').substr(2);
		var parent = $(this).parent().parent();
		
		if (typeof(in_playlist) != "undefined") {
			parent.fadeOut(300);
		}
	
		$(this).css('display', 'none');
		$('.removeplaylist', parent).css('display', 'none');
		
		playlist_count--;
		$('#playlistcount').html(playlist_count.toString());
		
		if (playlist_count == 0) {
			playlist = '';
		} else {
			var plist = playlist.split(',');
			var plist_out = '';
			
			for (var i = 0; i < plist.length; i++) {
				if (plist[i].toString() != postid.toString()) {
					if (plist_out == '') {
						plist_out = plist[i];
					} else {
						plist_out += ',' + plist[i];
					}
				}	
			}

			playlist = plist_out;
		}
		
		$.ajax(
		{
			url: urlbase + '/wp-content/remove_playlist.php',
			type: 'POST',
			data: 'postid=' + postid,
			success: function(data) {
				if (data == 'empty') {
					if (uri != '') {
						window.location = urlbase + '/category/navigation/corpo/';
					}
				}
			}
		});
	});
	
	/* Remove All */
	$('.deleteall').click(function()
	{
		playlist_count = 0;
		$('#playlistcount').html('0');
		$('.removeplaylist').css('display', 'none');
		$('.full_removeplaylist').css('display', 'none');
				
		$.ajax(
		{
			url: urlbase + '/wp-content/clear_playlist.php',
			type: 'POST',
			success: function()
			{
				window.location = urlbase + '/category/navigation/corpo/';
			}
		});
		return false;
	});
});
