$(function()
{
    var boardID  = '';
    var onlybody = config.requestType == 'GET' ? "&onlybody=yes" : "?onlybody=yes";
    $.cookie('selfClose', 0, {expires:config.cookieLife, path:config.webRoot});
    var $kanban = $('#kanban');

    var statusMap =
    {
        task:
        {
            wait   : {doing: 'start', done: 'finish', cancel: 'cancel'},
            doing  : {done: 'finish', pause: 'pause'},
            pause  : {doing: 'activate', done: 'finish', cancel: 'cancel'},
            done   : {doing: 'activate', closed: 'close'},
            cancel : {doing: 'activate', closed: 'close'},
            closed : {doing: 'activate'}
        },
        bug:
        {
            wait   : {done: 'resolve', cancel: 'resolve'},
            doing  : {},
            done   : {wait: 'activate', closed: 'close'},
            cancel : {wait: 'activate', closed: 'close'},
            closed : {wait: 'activate'}
        }
    };

    var kanbanModalTrigger = new $.zui.ModalTrigger({type: 'iframe', width:800});
    var lastOperation;
    var dropTo = function(id, from, to, type)
    {
        if(statusMap[type][from] && statusMap[type][from][to])
        {
            lastOperation = {id: id, from: from, to: to};
            kanbanModalTrigger.show(
            {
                url: $.createLink(type, statusMap[type][from][to], 'id=' + id) + onlybody,
                shown:  function(){$('.modal-iframe').addClass('with-titlebar').data('cancel-reload', true)},
                hidden: function()
                {
                    var selfClose = $.cookie('selfClose');
                    $.cookie('selfClose', 0, {expires: config.cookieLife, path: config.webRoot});
                    $item = $('#' + type + '-' + id);
                    $item.removeClass('board-' + type + '-' + lastOperation.to).removeClass('drop-in');
                    if(selfClose != 1 && lastOperation)
                    {
                        $item.appendTo($item.closest('.boards').find('.board[data-type="'+ lastOperation.from + '"]'));
                    }
                    else
                    {
                        $.get($.createLink(type, 'ajaxGetByID', 'id=' + id), function(data)
                        {
                            $('#' + type + '-' + id).find('.' + type + '-assignedTo .text').html(data.assignedTo);
                            if(type == 'task')
                            {
                                $('#task-' + id).find('.task-left').html(data.left + 'h');
                                if(data.story) $('div.board-story[data-id="' + data.story + '"]').find('.story-stage').html(data.storyStage);
                            }
                        }, 'json');
                    }
                }
            });
            return true;
        }
        return false;
    };

    $kanban.droppable(
    {
        selector: '.board-item:not(.disabled)',
        target: function($ele)
        {
            var itemType = $ele.data('type');
            var $board = $ele.closest('.board');
            var type = $board.data('type');
            return $board.siblings('.board').filter(function()
            {
                var typeMap = statusMap[itemType];
                var actionMap = typeMap && typeMap[type];
                return !!actionMap && actionMap[$(this).data('type')];
            });
        },
        start: function(e)
        {
            $kanban.addClass('dragging');
            e.targets.addClass('can-drop-in');
            var $item = $(e.element).addClass('dragging');
            $item.closest('.boards').addClass('dragging');
        },
        drag: function(e)
        {
            var $item = $(e.element);
            var $target = $(e.target);
            var $holder = $target.find('.board-drag-holder');
            if (!$holder.length) $holder = $('<div class="board-drag-holder"></div>').appendTo($target);
            $kanban.find('.c-board.dragging').removeClass('dragging');
            $kanban.find('.c-board.s-' + $target.data('type')).addClass('dragging');
            $holder.height($item.outerHeight());
        },
        drop: function(e)
        {
            var result = dropTo(e.element.data('id'), e.element.closest('.board').data('type'), e.target.data('type'), e.element.data('type'));
            if(result !== false)
            {
                e.element.insertBefore(e.target.find('.board-drag-holder'));
            }
        },
        finish: function()
        {
            $kanban.removeClass('dragging').find('.can-drop-in').removeClass('can-drop-in');
            $kanban.find('.dragging').removeClass('dragging');
        }
    });

    var refresh = function()
    {
        var selfClose = $.cookie('selfClose');
        $.cookie('selfClose', 0, {expires:config.cookieLife, path:config.webRoot});
        if(selfClose == 1) $kanban.load(location.href + ' #kanban');
    }

    $kanban.on('click', '.kanbaniframe', function(e)
    {
        var $link = $(this);
        new $.zui.ModalTrigger($.extend(
        {
            type: 'iframe',
            url: $link.attr('href'),
            width: '80%',
        }, $link.data())).show(
        {
            shown:  function(){$('.modal-iframe').addClass('with-titlebar').data('cancel-reload', true)},
            hidden: function(){refresh();}
        });
				return false;
    });

    $.extend({'closeModal':function(callback, location)
    {
        kanbanModalTrigger.close();
        if(callback && $.isFunction(callback)) callback();
    }});
});
