/*my teaching tools management
====================================================================*/
$(function() {
    
    //edit title
    $('#edit-myteachingtool a').click(function() {
        if($(this).hasClass('editing')){
            //show normal controls
            $(this).removeClass('editing').text('Edit Set');
            $('.edit-controls').slideUp();
            saveMyTeachingToolTitle();
        } else {
            //show edit controls
            $(this).addClass('editing').text('Done');
            $('.edit-controls').slideDown();
            editMyTeachingToolTitle();
        }
        return false;
    });
    
    //add to my teaching tools overlay
	$('.add-to-myteachingtools').click(function(){
	   
	   var toolId = $('#track-list-inner li.current a.audio-track').attr('rel');
	   if(!toolId){
	       toolId = $(this).attr('rel');
	   }
	   
	   $('#playlist-overlay').remove();
	   $('<div id="playlist-overlay" class="songs content-small scroll-wrap">'+
    	       '<h3 class="rounded">Add to existing teaching tool set</h3>'+
    	       '<form action="/do/songbank/myteachingtools/addteachingtool/" method="post">'+
    	       '<input type="hidden" name="tool-id" value="'+toolId+'" />'+
    	       '<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 teaching tool set</h3>'+
    	       '<p>Set name: <input class="fieldrounded text-input" type="text" name="new-myteachingtool" /></p>'+
    	       '<button class="submit-button rounded right" type="submit">Add Teaching Tool</button>'+
    	       '</form>'+
    	       '</div>')
	       .css({
	           'top' : $(this).offset().top+$(this).height(),
	           'margin-left' : $(this).offset().left-($(this).width()/2)})
	       .appendTo('body');
        
        initMyTeachingToolsform();
        
        $(document).click(function(){
            $("#playlist-overlay").remove();
        });
        $("#playlist-overlay").click(function(){return false;});
        return false;
	});
    
    //delete whole set
    $('#delete_myteachingtools button').click(function(){
        $(this).text('Deleting...');
        $.post(
            $('#delete_myteachingtools').attr('action'),
            $('#delete_myteachingtools').serialize(),
            function(data){
                $('#delete_myteachingtools input:checked').parent().slideUp(function(){$(this).remove()});
                $('#delete_myteachingtools button').text('Delete selected playlists');
            });
        return false;
    });
    
    //delete button
    $('.edit-controls .delete').click(function(){
        var toolId = '#' + $(this).attr('rel');        
        var myteachingtoolId = $(this).parent().attr('id');
        
        //console.log(toolId + ' ... ' +  myteachingtoolId);
        
        $.get($(this).attr('href'), function(){
            $(toolId).slideUp(function(){
                $(this).remove();
                
                if($('#my-teaching-tool-' + myteachingtoolId + ' li').size() == 0){
                    var deleted = confirm("This set is empty. Shall we delete it?");
                    if(deleted){
                        $.post('/do/songbank/myteachingtools/deletemyteachingtools/', { 'deleted-myteachingtools[]' : myteachingtoolId }, function() {
                            window.location = "/songbank/my-teaching-tools/";
                        });
                    }
                } else {
                    reloadXml(myteachingtoolId);                
                }
            });
        });
        return false;
    });
    
    //sortable playlist
    try {
        $('.results-list').sortable({
            handle : '.move',
            update : function(){
                var order = 'existing-set='+$('.results-list').attr('id')+'&'+$('.results-list').sortable('serialize');
                $.post(
                    '/do/songbank/myteachingtools/setorder/', 
                    order
                );
            },
            opacity: 0.8,
            revert: 100        
        });
    } catch(e) {}
});


function initMyTeachingToolsform(){
    //load dropdown
    $('#select-playlist').load('/do/songbank/myteachingtools/getmyteachingtools');

    $('#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 editMyTeachingToolTitle() {
    //change h1 into input
    var oldTitle = $('#myteachingtool-title').text();
    $('#myteachingtool-title').hide();
    $('<input id="new-playlist-title" type="text" name="new-myteachingtool-title" value="'+oldTitle+'" />').appendTo('.title h1');
}

function saveMyTeachingToolTitle() {
    var newTitle = $('#new-playlist-title').attr('value');
    var toolId = $('.mainbox ul.results-list').attr('id');
    
    //send title
    $.ajax({
        url : '/do/songbank/myteachingtools/updatetitle/',
        data : { newname : newTitle, id : toolId },
        method : 'post'
    });
        
    //change input back into h1
    $('#myteachingtool-title').text(newTitle).show();
    $('#new-playlist-title').remove();
}
