/*playlist management
====================================================================*/
$(function(){
    $('.edit-controls').hide();

    if($('#play-list-inner ul').length>0){
        var playlistId = $('#play-list-inner ul').attr('id').replace('playlist-','');
    }

    $('#edit-playlist a').click(function(){
        if($(this).hasClass('editing')){
            //show normal controls
            $(this).removeClass('editing').text('Edit Playlist');
            $('.edit-controls').slideUp(function(){
                $('.standard-controls').slideDown()
            });
            saveMyPlaylistTitle();
        }else{
            //show edit controls
            $(this).addClass('editing').text('Done');
           
            $('.standard-controls').slideUp(function(){
                $('.edit-controls').slideDown();
            });
            editMyPlaylistTitle();
        }
        return false;
    });
    
    //delete button
    $('.edit-controls .delete').click(function(){
        var trackId = '#' + 'track-' + $(this).attr('rel');        
        var playlistId = $(this).parent().attr('id');                    
        
        $.get($(this).attr('href'), function(){
            $(trackId).slideUp(function(){
                $(this).remove();
                if($('.track-list li').size() == 0){
                    var deleted = confirm("This playlist is empty. Shall we delete it?");
                    if(deleted){
                        $.post('/do/songbank/playlist/deleteplaylist/', { 'deleted-playlists[]' : [playlistId] }, function(){
                            //reload page?
                        });
                    }
                }else{
                    reloadXml(playlistId);                
                }
            });
        });
        return false;
    });
    
    //delete whole playlists
    $('#delete_playlists button').click(function(){
        $(this).text('Deleting...');
        $.post(
            $('#delete_playlists').attr('action'),
            $('#delete_playlists').serialize(),
            function(data){
                $('#delete_playlists input:checked').parent().slideUp(function(){$(this).remove()});
                $('#delete_playlists button').text('Delete selected playlists');
            });
        return false;
    });
    
    //sortable playlist
    try {
        $('#play-list-inner ul').sortable({
            handle : '.move',
            update : function(){
                var order = 'existing-playlist='+$('#play-list-inner ul').attr('id')+'&'+$('#play-list-inner ul').sortable('serialize');
                $.post(
                    '/do/songbank/playlist/setorder/', 
                    order, 
                    function(data){
                        reloadXml(playlistId);
                });
            },
            opacity: 0.8,
            revert: 100        
        });
    } catch(e) {}
    
    $('#playlist-lyrics a').click(function(){
        langId = $(this).attr('rel');
        $('#playlist-lyrics a.active').removeClass('active');
        $(this).addClass('active');
        $('.language').hide();
        $('#'+langId).show();
        return false;
    });


	//add to playlist overlay
	$('.add-to-playlist').click(function(){
	   
	   var trackId = $('#track-list-inner li.current a.audio-track').attr('rel');
	   if(!trackId){
	       trackId = $(this).attr('rel');
	   }
	   
	   $('#playlist-overlay').remove();
	   $('<div id="playlist-overlay" class="songs content-small scroll-wrap">'+
    	       '<h3 class="rounded">Add to existing playlist</h3>'+
    	       '<form action="/do/songbank/playlist/addtrack/" method="post">'+
    	       '<input type="hidden" name="track-id" value="'+trackId+'" />'+
    	       '<div style="height:28px" id="select-playlist"><img class="loading" alt="loading" src="/fileadmin/templates/images/global/loader-bar.gif" /></div>'+
    	       '<h3 class="rounded">Add to new playlist</h3>'+
    	       '<p>Playlist name: <input class="fieldrounded text-input" type="text" name="new-playlist" /></p>'+
    	       '<button class="submit-button rounded right" type="submit">Add Song</button>'+
    	       '</form>'+
    	       '</div>')
	       .css({
	           'top' : $(this).offset().top+$(this).height(),
	           'margin-left' : $(this).offset().left-($(this).width()/2)})
	       .appendTo('body');
        
        initPlaylistform();
        
        $(document).click(function(){
            $("#playlist-overlay").remove();
        });
        $("#playlist-overlay").click(function(){return false;});
        return false;
	});

});



function initPlaylistform(){
    //load dropdown
    $('#select-playlist').load('/do/songbank/playlist/getplaylists');

    $('#playlist-overlay form button').click(function(){
        $.ajax({
            url : $('#playlist-overlay form').attr('action'),
            type : 'POST',
            data : $('#playlist-overlay form').serialize(),
            success : function(data){
                $('#playlist-overlay').empty().html(data);
                closeTimer = setTimeout(function(){$('#playlist-overlay').remove()}, 3000);
            }
        });
        return false;
    });
}

function reloadXml(playlistId){
   var suPlayer = document.getElementById('thePlayer');
   if (suPlayer != null){
    suPlayer.loadAudioXML('/do/songbank/song/audioxml/playlist/'+playlistId);
   }
}

function editMyPlaylistTitle() {
    //change h1 into input
    var oldTitle = $('#playlist-title').text();
    $('#playlist-title').hide();
    $('<input id="new-playlist-title" type="text" name="new-playlist-title" value="'+oldTitle+'" />').appendTo('.title h1');
    
}

function saveMyPlaylistTitle() {
    var newTitle = $('#new-playlist-title').attr('value');
    var playlistId = $('#play-list-inner ul.track-list').attr('id');
    
    //send title
    $.ajax({
        url : '/do/songbank/playlist/updatetitle/',
        data : { newname : newTitle, id : playlistId },
        method : 'post'
    });
        
    //change input back into h1
    $('#playlist-title').text(newTitle).show();
    $('#new-playlist-title').remove();
       
    
}
