var last_tr;
var cur_tr
var last_tds;
var new_form;
var button;			
var cached_select = document.createElement("select");
var dragged;
var prev_dragged;

var drag_param = {
	revert:false,helper:"clone",start: 
	function( e, ui ){
		// desactive le parent clicable
		$(ui.draggable).parents("tr").unbind("click");
		$(ui.helper).attr("class", $(ui.helper).attr("class")+ " dragging" );
/*		var div = $(this);
		var next = div.nextAll(".ui-draggable");*/
//  		$(this).attr("class", $(this).attr("class")+ " dragging" );
// 		next.css("background-color","#555555");
// 		next.css("color","#FFFFFF");
// 		next.css("width","210px");
// 		next.css("height","22px");
// 		next.css("line-height","22px");
// 		next.css("text-align","center");
	},stop:
	function(){
		$(this).parents("tr").click( getFormAddPlaylist );
		$(this).parents("tr").css('opacity','1');
	}/*,drag:
	function(){
		$(this).parents("tr").css('opacity','0.6');
	}*/
	
	
// 	onStop:
// 	function() {		
// 		dragged.Draggable( drag_param );
// 	}
};	

/**
*	Récupère la liste des playlist du user
**/

function getFormAddPlaylist()
{
	var text_color = "white";
	var height = "30px";
	var background_color = "#005C8A";
	last_tr = cur_tr
	cur_tr = this;
	$(cur_tr).css('background-color', background_color);

	//s'il existe déjà une liste de playlist sur la page, on la retire
	if( last_tr )
	{
		//on retire le formulaire actuel
		$(last_tr).removeAttr('style');
		$(new_form).remove();

		//on remet la row clickable et draggable
		$(last_tr).find('div').draggableEnable();	
		$(last_tr).click( getFormAddPlaylist );

		$(last_tr).children("td").removeAttr('style');
	}

	

	$(cur_tr).children("td").css('color',text_color);
	//on retire à la ligne la possibilité d'etre draggé ou clickée
	$(cur_tr).find('div').draggableDisable();
	$(cur_tr).unbind("click");



	var form = new_form;
	$(new_form).children("button").click(
		function() {
			//au click on récupere l'id du titre et l'id de la playlist
			var title_id = $(cur_tr).attr('title_id');
			var playlist_id = $(new_form).children("select").val();
			if( playlist_id != "0" )
				addTitle(playlist_id, title_id);
		}
	);

	//on l'ajoute dans la cellule en cours
	$(cur_tr).children("td").eq(0).prepend(new_form);

	//on ne fait un appel au serveur que si globalPlaylist.cached est faux
	if( globalPlaylist.cached == "false" )
		$.getJSON('/services/json.php',{ _class: "Zaploop_Service_Playlist", method : "get_playlist_list", lang: Sloth.lang }, function(json) {
			if( json.error )
				alert( "error : "+json.error );
			else
			{	
				//vide le select caché
				$(cached_select).empty();
				$.each( json , function( i ) {
				//	remplit avec la liste de playliste acutelle
					var option = document.createElement('option');
					$(option).val(i);
					$(option).text(this.toString());
					$(cached_select).append(option);
				});
				//on retire à la ligne la possibilité d'etre draggé ou clickée
/*				$(cur_tr).DraggableDestroy();
				$(cur_tr).unbind("click");*/
				
				//la liste de playlist est désormais en cache
				globalPlaylist.cached = "true";
			}
		});
}



$(function drag()
{
	//les effets drag'n'drop ne sont accessibles que lorsque le membre est connecté

	$.getJSON('/services/json.php',{ _class: "Zaploop_Service_Membre", method : "is_logged", lang: Sloth.lang }, 
		function(json) {
			if(json.connected == "true")
			{
				//on crée un nouveau formulaire
				new_form = document.createElement('div');
				button = document.createElement('button');
				$(new_form).attr("class","form_select_playlist");						

				// on lui ajoute le noeud select et le bouton
				$(new_form).append(cached_select);
				$(new_form).append(button);
				$(button).attr("class","form_valid_playlist");
				$(button).text(globalPlaylist.textButton);
// 				$(button).click( function() {
// 					//au click on récupere l'id du titre et l'id de la playlist
// 					var title_id = $(cur_tr).attr('title_id');
// 					var playlist_id = $(new_form).children("select").val();
// 					if( playlist_id != "0" )
// 						addTitle(playlist_id, title_id);
// 				});

				$('.draggable').find('div').draggable( drag_param );
				$('.draggable').click( getFormAddPlaylist );
				$('.head_playlist').droppable( drop_param );
			}					
		});
});
