var drop_param = {accept:'.drag_helper',greedy:true,hoverClass:'head_playlist_hover',tolerance:'pointer',drop:
	function(e, ui){
		var playlist_id = $(this).parent().attr('playlist');
		var title_id = $(ui.helper).parents("tr").attr('title_id');
		addTitle( playlist_id, title_id );	
	}
};


function createPlaylist( name , id, hashed )
{
	var playlist = document.createElement('div');
	var link = document.createElement('a');
	var img_open = document.createElement('div');
	var img_delete = document.createElement('div');
	
 	$(link).attr('class', 'head_playlist');
	$(link).text(name+" (");
	var nb_titres = document.createElement('span');
	$(nb_titres).text("0");
	$(link).append(nb_titres);
	$(link).append(")");
	$(img_delete).attr('class', 'delete');
	$(link).append(img_delete);
	$(img_open).attr('class', 'open');
	$(link).append(img_open);
	
	var body_playlist = document.createElement('div');
	body_playlist.innerHTML = "<!---->";
	$(body_playlist).attr('class', 'body_playlist');
	$(body_playlist).css('height', '-200px');
	
	playlist.appendChild( link );
	playlist.appendChild( body_playlist );
	body_playlist.hidden = true;
	$(body_playlist).hide(true);
	$(playlist).attr('playlist', id);
	$(playlist).attr('link', hashed);
	$(playlist).attr('class', 'playlist');
	return $(playlist);
}

function createTitle( name, id )
{
	var title = document.createElement('div');
	$(title).attr( 'id_title', id );
	var img = document.createElement('img');
	$(img).attr( 'src' , '/lib/Zaploop/View/img/remove.png');
	$(img).attr( 'class' , 'delete_song');
	$(img).click( removeTitleClickFunction );
	$(title).text( name );
	$(title).append(img);
	return $(title);
}

function removeTitleClickFunction()
{
	var id_title = $(this).parent().attr('id_title');
	var id_playlist = $(this).parents('.playlist').attr('playlist');
	removeTitle( id_title, id_playlist );
}


function addTitle( idPlaylist, idTitle )
{
	$.getJSON('/services/json.php',{  _class: "Zaploop_Service_Playlist", method : "add_playlist_title", lang: Sloth.lang, arg1 : idPlaylist , arg2 : idTitle }, function(json) {
		if( json.error )
			alert( json.error );
		else
		{
			var playlist_id = json.playlist_id;
			var title = json.name;
			var body_playlist = $(".playlist[@playlist='"+playlist_id+"']").children('.body_playlist');
			var head_playlist = $(".playlist[@playlist='"+playlist_id+"']").children('.head_playlist_opened,.head_playlist');
			var new_title = createTitle( json.name, json.title_id );
			body_playlist.append( new_title );
			// incrémentation du nombre de playlist
			var n = new Number( $(head_playlist).children("span").text() );
			n = ""+ (n+1);
			$(head_playlist).children("span").text(n);

			// si la playliste est fermée, on l'ouvre
			if( body_playlist[0].hidden ) {
				action( head_playlist );
			}
		}													
	});

}


function removeTitle( title_id, playlist_id )
{
	
	$.getJSON('/services/json.php',{ _class:"Zaploop_Service_Playlist", method : "remove_title", arg1 : title_id ,arg2 : playlist_id, lang: Sloth.lang}, function(json) {
			if(json.error)
				alert(json.error);
			else {
				var playlist = $('div[playlist='+ json.playlist_id+']');
				var title = $(playlist).children('.body_playlist').children('div[id_title='+ json.title_id +']');
				$(title).remove();
				// décrémentation du nombre de playlist
				var textNumber = $(playlist).children('.head_playlist_opened').children("span").text();
		
				var n = new Number( textNumber );
				n = ""+ (n-1);

				$(playlist).children('.head_playlist, .head_playlist_opened').children("span").text( n );
			}					
		});
}

function removePlaylist(event)
{
	var playlist = $(this).parents('div[playlist]');
	var playlist_id = playlist.attr("playlist");
	var head_playlist = playlist.children("a");
	
	event.stopPropagation();
	
	if (confirm("Delete playlist ?"))
	{
		head_playlist.unbind("click");
	
		$.getJSON('/services/json.php',{ _class:"Zaploop_Service_Playlist", method : "remove_playlist", arg1 : playlist_id, lang: Sloth.lang}, function(json) {
				if(json.error)
					alert(json.error);
				else {
					var playlist = $('div[playlist='+ json.playlist_id+']');
					playlist.remove();
				}					
			});
	}
}

function openPlaylist(event)
{
	var playlist = $(this).parents('div[playlist]');
	var playlist_link = playlist.attr("link");
	var head_playlist = playlist.children("a");
	
	event.stopPropagation();
	
	popup.create(playlist_link, Sloth.lang, EMBED_MODE_PLAY);
}

function  clic_function(){
	action( this );
}

$( function accordion()
{
	$('.body_playlist').hide();		
	$('.body_playlist').each( function(){
		this.hidden = true ;
		});

	$('.head_playlist').click( function(){
		action( this );
		}
	);
});

function action( currentElement )
{
	var body = $( currentElement ).next();


	if( body[0] )
	{
		if( body[0].hidden )
		{
			body.show();
			body[0].hidden = false;
			$(currentElement).attr("class", "head_playlist_opened");
		}
		else
		{	
			body.hide();
			body[0].hidden = true;
			$(currentElement).attr("class", "head_playlist");
		}
	}	
}


$(function init() {
	$('.add_playlist_button').click(function(){
		$('.add_playlist_form').show();				
	});
	$('.playlist_ok').click( function(){
		var playlist_name = $('.playlist_name').val();
		$.getJSON('/services/json.php',{ _class:"Zaploop_Service_Playlist", method : "add_playlist", arg1 : playlist_name , lang: Sloth.lang}, function(json) {
			if(json.error)
				alert(json.error);
			else {
				var father = $('.playlist_content');
				var playlist = createPlaylist( json.name, json.id, json.link );

				father.append(playlist);

 				var droppableObject = playlist.children('.head_playlist');
				droppableObject.droppable( drop_param );
				
				playlist.children('.head_playlist').click( clic_function );
				droppableObject.children('div').filter('.open').click( openPlaylist );
				droppableObject.children('div').filter('.delete').click( removePlaylist );

				// lorsqu'on ajoute une playlist, on met la variable cache sur false
				globalPlaylist.cached = "false";
			}
			$('.playlist_name').val('');
		});
	});

	$('.delete_song').click( removeTitleClickFunction );

	$('.head_playlist, .head_playlist_opened').children('div').filter('.open').click( openPlaylist );
	$('.head_playlist, .head_playlist_opened').children('div').filter('.delete').click( removePlaylist );
});



