/**
 * @author Vincent POUILLEY
 */
iConcerts.UI.UserPlaylist = Class.create();
iConcerts.UI.UserPlaylist.prototype = {
	initialize: function(element){
		this.element = $(element);
		if (!this.element)
			return;
		$('uplaylist_new').onclick = this.CreateNew.bindAsEventListener(this);
		this.init();
	},
	
	init: function() {
//		alert('coucou init');
		ThePlaylistWrapper = $('upWrapper');
		ThePlaylistWrapper.select('.UserPlaylistTrack').each(function(elem) {
			elem.addClassName('draggable');
			new Draggable(elem.id, { ghosting : false, revert : true } );
			Droppables.add(elem.id, {
				accept: $w('UserPlaylistTrack UnitTrack'),
				hoverclass: 'dropHover',
				onDrop: this.DropOnTrack.bind(this)
			});
		}.bind(this));
		ThePlaylistWrapper.select('.UserPlaylistTitle').each(function(elem) {
			elem.addClassName('draggable');
			new Draggable(elem.id, { ghosting : false, revert : true } );
			Droppables.add(elem.id, {
				accept: $w('UserPlaylistTrack UnitTrack'),
				hoverclass: 'dropHover',
				onDrop: this.DropOnPlaylist.bind(this)
			});
			nid = elem.id.substr(2);
			new Ajax.InPlaceEditor(elem.id, 'index2.php?q=uprename/' + nid, {
				okControl: 'button',
				cancelControl: 'button',
				highlightcolor: '#00649c',
				highlightendcolor: '#000000',
				onComplete: doLoadMenu
			});
			PlusMoins = elem.up().select('.PlusMoins').first();
			PlusMoins.observe('click', this.PlusMoinsClick.bindAsEventListener(this, elem.up().select('.UserPlaylistTrackList').first().id, PlusMoins.id));
		}.bind(this));
		Droppables.add($('uplaylist_trash'), {
			accept: $w('UserPlaylistTrack UserPlaylistTitle'),
			hoverclass: 'dropHover',
			onDrop: this.DropOnTrash.bind(this)
		});
		$('up_loading').hide();
	},
	
	Refresh: function(data) {
//		alert('coucou Refresh, data=(' + data.responseText + ')');
		ThePlaylistWrapper = $('upWrapper');
		ThePlaylistWrapper.select('.UserPlaylistTrack').each(function(elem) {
			Droppables.remove(elem.id);
		});
		ThePlaylistWrapper.select('.UserPlaylistTitle').each(function(elem) {
			Droppables.remove(elem.id);
		});
		o_options = new Object();
		o_options = {method: 'get',evalScripts:true, onComplete: this.init.bind(this)};
		var laRequete = new Ajax.Updater($('upWrapper'),'index2.php?q=upgetlist', o_options);
	},
	
	CreateNew: function(event) {
//		alert('coucou CreateNew');
		if(event)
			Event.stop(event);
		$('up_loading').show();
		o_options = new Object();
		o_options = {method: 'get', onSuccess:  this.Refresh.bind(this), onComplete: doLoadMenu};
		var laRequete = new Ajax.Request('index2.php?q=upcreate/MyPlaylist1',o_options);
	},
	
	DropOnTrack: function(drag, drop) {
//		alert('coucou DropOnTrack');
		$('up_loading').show();
		var parid = drop.up().up().up().select('.UserPlaylistTitle').first().id.substr(2);
		var chid = drop.id.substr(5);
		o_options = new Object();
		o_options = {method: 'get', onSuccess: this.Refresh.bind(this)};
		if (drag.id.substr(0,3) == 'tra') {
			var pneid = drag.up().up().up().select('.UserPlaylistTitle').first().id.substr(2);
			var neid = drag.id.substr(5);
			var laRequete = new Ajax.Request('index2.php?q=upmovechild/' + pneid + '/' + neid + '/' + parid + '/' + chid, o_options);
		}
		else {
			var neid = drag.id.substr(6);
			var laRequete = new Ajax.Request('index2.php?q=upaddchildtrack/' + parid + '/' + chid + '/' + neid, o_options);
		}
	},
	
	DropOnPlaylist: function(drag, drop) {
//		alert('coucou DropOnPlaylist');
		$('up_loading').show();
		var parid = drop.id.substr(2);
		var chid = 0;
		o_options = new Object();
		o_options = {method: 'get', onSuccess: this.Refresh.bind(this)};
		if (drag.id.substr(0,3) == 'tra') {
			var pneid = drag.up().up().up().select('.UserPlaylistTitle').first().id.substr(2);
			var neid = drag.id.substr(5);
			var laRequete = new Ajax.Request('index2.php?q=upmovechild/' + pneid + '/' + neid + '/' + parid + '/' + chid, o_options);
		}
		else {
			var neid = drag.id.substr(6);
			var laRequete = new Ajax.Request('index2.php?q=upaddchildplaylist/' + parid + '/' + neid, o_options);
		}
	},
	
	DropOnTrash: function(drag, drop) {
//		alert('coucou DropOnTrash');
		$('up_loading').show();
		o_options = new Object();
		if (drag.id.substr(0,3) == 'tra') {
			o_options = {method: 'get', onSuccess: this.Refresh.bind(this)};
			var parid = drag.up().up().up().select('.UserPlaylistTitle').first().id.substr(2);
			var nid = drag.id.substr(5);
			var laRequete = new Ajax.Request('index2.php?q=updeletetrack/' + parid + '/' + nid, o_options);
		}
		else {
			o_options = {method: 'get', onSuccess: this.Refresh.bind(this), onComplete: doLoadMenu};
			var nid = drag.id.substr(2);
			var laRequete = new Ajax.Request('index2.php?q=updelete/' + nid, o_options);
		}
	},
	
	PlusMoinsClick: function(event, wrapperid, pmid) {
		if ($(wrapperid).visible()) {
//			$(wrapperid).hide();
			Effect.SlideUp($(wrapperid));
			$(pmid).innerHTML = '+';
		}
		else {
//			$(wrapperid).show();
			Effect.SlideDown($(wrapperid));
			$(pmid).innerHTML = '-';
		}
	}
};

iConcerts.UI.UnitTrack = Class.create();
iConcerts.UI.UnitTrack.prototype = {
	initialize: function(element, drop){
		if (!$(drop))
			return;
		this.element = $(element);
		if(!this.element)
			return;
		this.element.select('.UnitTrack').each(function(elem,i){
			elem.addClassName('draggable');
			new Draggable(elem.id, { ghosting : true, revert : true } );
		}.bind(this));
	}
}
