Thread = {
    
    id : "",
    forums : {},
    selectBox : "",
    forumId : 0,
    selectedForum : 0,


    removeApply : function(id)
    {
        $.ajax({
            url: '/?page=moderation.DeleteThread&id=' + id,
            success: function(data) {
                if(Thread.forumId == 0)
                {
                    document.location = '/';
                }
                else
                {
                    document.location = '/?page=ForumView&forum_id=' + Thread.forumId;
                }

            }
        });
    },

    remove : function(id, forumId)
    {
        this.id = id;
        this.forumId = forumId;
        
        buttons = {
            "delete" : function(){
                $dialogWindow.container.dialog("close");
                Thread.removeApply(Thread.id);
            }
        };
        
        $dialogWindow.show('Do you really want to delete this thread? All included posts will be deleted.', 'Thread delete', buttons);
    },

    move : function(id, forumId)
    {
        this.id = id;
        this.forumId = forumId;
        
        this.initForumsList();
        
        buttons = {
            "move" : function(){
                Thread.selectedForum = $("#threadMoveTo").val()
                if(Thread.selectedForum != 0)
                {
                    $dialogWindow.container.dialog("close");
                    Thread.moveApply();
                }
            }
        }

        $dialogWindow.show('Move to: ' + this.selectBox, 'Move thread', buttons);
    },

    initForumsList : function()
    {
        var count = 0;
        for(obj in this.forums)
        {
            if(count>0)
            {
                break;
            }
            count++;
        }
        if(count == 0)
        {
            $.ajax({
                url: '/?page=moderation.GetForums',
                success: function(data) {
                    Thread.forums = data;
                    Thread.generateSelectBox();
                },
                dataType: 'json',
                async:false
            });
        }
    },

    generateSelectBox : function()
    {
        if(this.selectBox.length == 0)
        {
            $.each(this.forums, function(key, category){
                Thread.selectBox += '<optgroup label="' + category.title + '">';

                $.each(category.forums, function(key, forum){
                    if(Thread.forumId != forum.id)
                    {
                        Thread.selectBox += '<option value="' + forum.id + '">' + forum.title + '</option>';
                    }
                });

                Thread.selectBox += '</optgroup>';
            });

            this.selectBox = '<select name="moveTo" id="threadMoveTo"><option value="0">&nbsp;</option>' + this.selectBox + '</select>';
        }
    },

    moveApply : function()
    {
        $.ajax({
            url: '/?page=moderation.MoveThread&id=' + this.id + '&moveTo=' + this.selectedForum,
            success: function(data) {
                document.location = '/?page=ForumView&forum_id=' + Thread.selectedForum;
            }
        });
    },

    togglePin : function(id)
    {
        $.ajax({
            url: '/?page=moderation.ToggleThreadPin&id=' + id,
            success: function(data) {
                location.reload(true);
            }
        });
    }
}

