//keep track of the element
var dropped = false;

//store of markup for the playlist
var _html;

$(document).ready(function(){
    $('.MusicResultSummary, .MusicResultDetailsAlternativeEdits ul li').css('cursor', 'move');
    //$('.MusicResultDetailsAlternativeEdits ul li:hover').css('background', '#ebebeb');
    //check to see if _playlist is defined (if its not then we are not viewing a playlist)
    if(typeof _playlist !== 'undefined') {
        setSortable();
    } else {
        $(".MusicResultSummary, .MusicResultDetailsAlternativeEdits ul li").draggable({
            revert: true, 
            opacity: 0.75, 
            cursor: 'move',
            zIndex: 99999,
            helper: 'clone'
        });
    }
    
    //make the playlists catch dropped tracks
    $(".droppable").droppable({
        drop: function(event, ui) { 
            var data = 'playlist_to_add_to=' + $(this).children('span').text() + '&track_to_add=' + ui.draggable.find('.tid:first').text(); 
            ajax(config.thisURL, data, $(this));
            $(this).css('background', '#E8E8E8');
            dropped = true;
        },
        over: function(event, ui) { 
            $(this).css('background', '#F00');
        },
        out: function(event, ui) { 
            $(this).css('background', '#E8E8E8');
        },
        tolerance: 'pointer'
    });
    
	/*
    $('.PlaylistAdminDelete').bind('click', function() {
        var link_array = $(this).attr('href').split('=');
        var id = link_array[link_array.length-1]
        link_array = null;
        
        console.log(id);
        
        $.get(config.thisURL + '?ajax=1&update_playlist=' + _playlist + '&update_track=' + id + '&delete=1&rand=' + Math.random(), 
             function(data){
                 console.log(data);
             }, 
             'JSON');
        
        return false;
    });
	*/
});

function ajax(url, data, e) {
    //pass e into a new var to get around JS scoping issues (e is not visable inside $.ajax.success)
    var el = e;
    
    $.ajax({
            type: "GET",
            async: true,
            url: url,
            data: data + "&ajax=1&rand=" + Math.random(),           
            success: function(data){   
                obj = $.parseJSON(data);
                
                if(obj.success === true) {
                    el.find('.Count').text('(' + obj.total + ')');
                } else {
                    alert(obj.result);
                }
            }
    });
}

function setSortable() {
    //allow the track order to be changed by drag n drop
    $("#MusicResultsAll").sortable({
        update: function(event, ui) {
            if(!dropped) {
                var order = $(this).sortable("serialize");
                $.post(config.thisURL + '?ajax=1&update_order=1&playlist=' + _playlist + '&rand=' + Math.random(), order, function(theResponse){
                    //  
                }); 
            }
        },
        stop: function(event, ui) {
            if(dropped){
                //destory the list
                $("#MusicResultsAll").sortable('destroy').html(_html);
                dropped = false;
                //recreate it
                setSortable();
            }
        },
        opacity: '0.75'
    });
    $("#MusicResultsAll").disableSelection();
    $(".MusicResultDetailsAlternativeEdits ul li").draggable({
            revert: true, 
            opacity: 0.75, 
            cursor: 'move',
            zIndex: 99999,
            helper: 'clone'
        });
    
    $("#MusicResultsAll li").bind('mousedown',function(){
        _html = $("#MusicResultsAll").html();
    });
}


function bind_player_pop() {  
    //IF WE'RE NOT IN A POPOUT WINDOW, DISPLAY A "pop out" LINK, WHICH WILL OPEN A POPOUT WINDOW AND PUT THE PLAYER INTO
    //IT INSTEAD OF WHERE IT CURRENTLY IS
    $('#PlayerPopOutButton').bind('click', function() {
        updatePlayerPopStatus('out');
        popout_player();
        return false;
    });
}       
    
function updatePlayerPopStatus(direction) {          
    $.get(config.thisURL + '?ajax=1&player_pop=1&direction='+direction);
}
