﻿var Scrumpy = $(document);

Scrumpy.Edit = {

    CurrentProjectName: "",

    init: function() {
        $('div.taskboard div.postitholder div.tasks div span.date a').live('click', function() {
            taskId = $(this).attr('rel');
            $('div.actual' + taskId).toggle('fast');
            $('div.editable' + taskId).toggle('fast');
            // stop any hash appearing in the URL
            return false;
        });

        $('div.taskboard div.postitholder div.tasks div span.date a').each(function() {
            taskId = $(this).attr('rel');
            $('a.save' + taskId).bind('click', function() {
                Scrumpy.Edit.updateTaskText(taskId);
                // stop any hash appearing in the URL
                return false;
            });
        });
    },

    updateTaskText: function(taskId) {
        taskText = $('input.text' + taskId).val();

        $.ajax({
            type: 'POST',
            url: Scrumpy.Edit.CurrentProjectName + '/updatetaskaction.aspx',
            data: 'taskId=' + taskId + '&text=' + encodeURIComponent(taskText),
            cache: false,
            success: function(data) {
                taskData = JSON.parse(data)
                $('div.actual' + taskId + ' span.name span').html(taskData.DisplayText);
                $('div.actual' + taskId).toggle('fast');
                $('div.editable' + taskId).toggle('fast');
            }
        });
    }
}

Scrumpy.ProjectAdmin = {
    init: function() {
        $('div.userlist a.remove').bind('click', function() {
            user = $(this).parent().siblings().children('input[@type="hidden"]').val();
            Scrumpy.ProjectAdmin.removeUser(user);
            // stop any hash appearing in the URL
            return false;
        });

        $('input.find').bind('click', function() {
            pattern = $('#usernamesearch').val();
            Scrumpy.ProjectAdmin.findUsers(pattern);
            // stop any hash appearing in the URL
            return false;
        });
    },

    removeUser: function(username) {
        $.ajax({
            type: 'POST',
            url: 'ManageUserAction.aspx',
            data: 'u=' + username + '&a=d',
            cache: false,
            success: function(data) {
                if (data) {
                    // Remove user from list?!
                    // alert("Refresh to see the user removed");
                    window.location.reload(true);
                }
                else {
                    // Fail
                    alert("Sorry, but that didn't work!");
                }
            },
            error: function() {
                alert("Sorry, but that didn't work!");
            }
        });
    },

    findUsers: function(pattern) {
        $.ajax({
            type: 'POST',
            url: 'ManageUserAction.aspx',
            data: 'u=' + pattern + '&a=f',
            cache: false,
            success: function(data) {
                $('#usersFound').html(data);

                $('table.boardlist a.adduser').bind('click', function() {
                    user = $(this).parent().siblings().children('input[@type="hidden"]').val();
                    Scrumpy.ProjectAdmin.addUser(user);
                    // stop any hash appearing in the URL
                    return false;
                });
            }
        });
    },

    addUser: function(username) {
        $.ajax({
            type: 'POST',
            url: 'ManageUserAction.aspx',
            data: 'u=' + username + '&a=a',
            cache: false,
            success: function(data) {
                if (data) {
                    // Remove user from list?!
                    // alert("Refresh to see the user added");
                    window.location.reload(true);
                }
                else {
                    // Fail
                    alert("Sorry, but that didn't work - is that user perhaps already listed above?");
                }
            },
            error: function() {
                alert("Sorry, but that didn't work!");
            }
        });
    }
}

$(document).ready(function() {
    Scrumpy.Edit.init();
    Scrumpy.ProjectAdmin.init();
});
