//! OxarnaJS.debug.js
//

(function($) {

Type.registerNamespace('OxarnaJS');

////////////////////////////////////////////////////////////////////////////////
// OxarnaJS.DiscussionReplyViewModel

OxarnaJS.DiscussionReplyViewModel = function OxarnaJS_DiscussionReplyViewModel(post) {
    /// <param name="post" type="Object">
    /// </param>
    /// <field name="id" type="Number" integer="true">
    /// </field>
    /// <field name="facebookId" type="Number" integer="true">
    /// </field>
    /// <field name="accountName" type="String">
    /// </field>
    /// <field name="postBody" type="String">
    /// </field>
    /// <field name="posted" type="String">
    /// </field>
    /// <field name="lastReply" type="String">
    /// </field>
    /// <field name="postedRelativeDate" type="String">
    /// </field>
    /// <field name="authorPosts" type="Number" integer="true">
    /// </field>
    /// <field name="hidden" type="Observable`1">
    /// </field>
    /// <field name="hiddenBy" type="Observable`1">
    /// </field>
    /// <field name="deleted" type="Observable`1">
    /// </field>
    /// <field name="detailsVisible" type="Observable`1">
    /// </field>
    /// <field name="initialHide" type="Observable`1">
    /// </field>
    /// <field name="highlightAsNew" type="Boolean">
    /// </field>
    this.deleted = ko.observable(false);
    this.detailsVisible = ko.observable(false);
    this.initialHide = ko.observable(false);
    this.id = post.Id;
    this.facebookId = post.FacebookId;
    this.accountName = post.AccountName;
    this.postBody = post.PostBody;
    this.posted = post.Posted;
    this.lastReply = post.LastReply;
    this.postedRelativeDate = post.PostedRelativeDate;
    this.authorPosts = post.AuthorPosts;
    this.deleted = ko.observable(false);
    this.hidden = ko.observable(post.Hidden);
    this.hiddenBy = ko.observable(post.HiddenBy);
}
OxarnaJS.DiscussionReplyViewModel.prototype = {
    id: 0,
    facebookId: 0,
    accountName: null,
    postBody: null,
    posted: null,
    lastReply: null,
    postedRelativeDate: null,
    authorPosts: 0,
    hidden: null,
    hiddenBy: null,
    highlightAsNew: false,
    
    remove: function OxarnaJS_DiscussionReplyViewModel$remove() {
        $.getJSON('/Discussion/Reply/' + this.id + '/Delete', ss.Delegate.create(this, function(data) {
            if (data) {
                this.deleted(true);
            }
        }));
    },
    
    undoRemove: function OxarnaJS_DiscussionReplyViewModel$undoRemove() {
        $.getJSON('/Discussion/Reply/' + this.id + '/UndoDelete', ss.Delegate.create(this, function(data) {
            if (data) {
                this.deleted(false);
            }
        }));
    },
    
    hide: function OxarnaJS_DiscussionReplyViewModel$hide() {
        $.getJSON('/Discussion/Reply/' + this.id + '/Hide', ss.Delegate.create(this, function(data) {
            if (data) {
                this.hidden(true);
                this.hiddenBy(window.accountName);
            }
        }));
    },
    
    undoHide: function OxarnaJS_DiscussionReplyViewModel$undoHide() {
        $.getJSON('/Discussion/Reply/' + this.id + '/UndoHide', ss.Delegate.create(this, function(data) {
            if (data) {
                this.hidden(false);
                this.hiddenBy('');
            }
        }));
    },
    
    showDetails: function OxarnaJS_DiscussionReplyViewModel$showDetails() {
        this.detailsVisible(true);
    },
    
    hideDetails: function OxarnaJS_DiscussionReplyViewModel$hideDetails() {
        this.detailsVisible(false);
    }
}


////////////////////////////////////////////////////////////////////////////////
// OxarnaJS.DiscussionPostViewModel

OxarnaJS.DiscussionPostViewModel = function OxarnaJS_DiscussionPostViewModel(indexViewModel, post) {
    /// <param name="indexViewModel" type="OxarnaJS.DiscussionIndexViewModel">
    /// </param>
    /// <param name="post" type="Object">
    /// </param>
    /// <field name="_indexViewModel" type="OxarnaJS.DiscussionIndexViewModel">
    /// </field>
    /// <field name="id" type="Number" integer="true">
    /// </field>
    /// <field name="facebookId" type="Number" integer="true">
    /// </field>
    /// <field name="accountName" type="String">
    /// </field>
    /// <field name="postBody" type="String">
    /// </field>
    /// <field name="posted" type="String">
    /// </field>
    /// <field name="lastReply" type="String">
    /// </field>
    /// <field name="postedRelativeDate" type="String">
    /// </field>
    /// <field name="authorPosts" type="Number" integer="true">
    /// </field>
    /// <field name="hidden" type="Observable`1">
    /// </field>
    /// <field name="hiddenBy" type="Observable`1">
    /// </field>
    /// <field name="deleted" type="Observable`1">
    /// </field>
    /// <field name="detailsVisible" type="Observable`1">
    /// </field>
    /// <field name="displayShowAllRepliesButton" type="Observable`1">
    /// </field>
    /// <field name="initialHiddenReplies" type="Observable`1">
    /// </field>
    /// <field name="replies" type="ObservableArray`1">
    /// </field>
    /// <field name="highlightAsNew" type="Boolean">
    /// </field>
    this.deleted = ko.observable(false);
    this.detailsVisible = ko.observable(false);
    this.displayShowAllRepliesButton = ko.observable(false);
    this.initialHiddenReplies = ko.observable(0);
    this._indexViewModel = indexViewModel;
    this.id = post.Id;
    this.facebookId = post.FacebookId;
    this.accountName = post.AccountName;
    this.postBody = post.PostBody;
    this.posted = post.Posted;
    this.lastReply = post.LastReply;
    this.postedRelativeDate = post.PostedRelativeDate;
    this.authorPosts = post.AuthorPosts;
    this.deleted = ko.observable(false);
    this.hidden = ko.observable(post.Hidden);
    this.hiddenBy = ko.observable(post.HiddenBy);
    var replies = post.Replies;
    if (replies != null) {
        this.replies = ko.observableArray(this._mapReplies(replies));
    }
    else {
        this.replies = ko.observableArray();
    }
    var total = this.replies().length;
    var i = 0;
    var $enum1 = ss.IEnumerator.getEnumerator(this.replies());
    while ($enum1.moveNext()) {
        var r = $enum1.current;
        if (total > 3 && i < total - 2) {
            r.initialHide(true);
            this.initialHiddenReplies(this.initialHiddenReplies() + 1);
            this.displayShowAllRepliesButton(true);
        }
        i++;
    }
}
OxarnaJS.DiscussionPostViewModel.prototype = {
    _indexViewModel: null,
    id: 0,
    facebookId: 0,
    accountName: null,
    postBody: null,
    posted: null,
    lastReply: null,
    postedRelativeDate: null,
    authorPosts: 0,
    hidden: null,
    hiddenBy: null,
    replies: null,
    highlightAsNew: false,
    
    _mapReplies: function OxarnaJS_DiscussionPostViewModel$_mapReplies(replies) {
        /// <param name="replies" type="System.Collections.Generic.IEnumerable">
        /// </param>
        /// <returns type="Array"></returns>
        var repliesViewModels = [];
        var $enum1 = ss.IEnumerator.getEnumerator(replies);
        while ($enum1.moveNext()) {
            var p = $enum1.current;
            repliesViewModels.add(new OxarnaJS.DiscussionReplyViewModel(p));
        }
        return repliesViewModels;
    },
    
    remove: function OxarnaJS_DiscussionPostViewModel$remove() {
        $.getJSON('/Discussion/Delete/' + this.id, ss.Delegate.create(this, function(data) {
            if (data) {
                this.deleted(true);
            }
        }));
    },
    
    undoRemove: function OxarnaJS_DiscussionPostViewModel$undoRemove() {
        $.getJSON('/Discussion/UndoDelete/' + this.id, ss.Delegate.create(this, function(data) {
            if (data) {
                this.deleted(false);
            }
        }));
    },
    
    hide: function OxarnaJS_DiscussionPostViewModel$hide() {
        $.getJSON('/Discussion/Hide/' + this.id, ss.Delegate.create(this, function(data) {
            if (data) {
                this.hidden(true);
                this.hiddenBy(window.accountName);
            }
        }));
    },
    
    undoHide: function OxarnaJS_DiscussionPostViewModel$undoHide() {
        $.getJSON('/Discussion/UndoHide/' + this.id, ss.Delegate.create(this, function(data) {
            if (data) {
                this.hidden(false);
                this.hiddenBy('');
            }
        }));
    },
    
    showDetails: function OxarnaJS_DiscussionPostViewModel$showDetails() {
        this.detailsVisible(true);
    },
    
    hideDetails: function OxarnaJS_DiscussionPostViewModel$hideDetails() {
        this.detailsVisible(false);
    },
    
    showAllReplies: function OxarnaJS_DiscussionPostViewModel$showAllReplies() {
        this.displayShowAllRepliesButton(false);
        var $enum1 = ss.IEnumerator.getEnumerator(this.replies());
        while ($enum1.moveNext()) {
            var r = $enum1.current;
            r.initialHide(false);
        }
    }
}


////////////////////////////////////////////////////////////////////////////////
// OxarnaJS.DiscussionIndexViewModel

OxarnaJS.DiscussionIndexViewModel = function OxarnaJS_DiscussionIndexViewModel(posts) {
    /// <param name="posts" type="System.Collections.Generic.IEnumerable">
    /// </param>
    /// <field name="postBody" type="Observable`1">
    /// </field>
    /// <field name="showForm" type="Observable`1">
    /// </field>
    /// <field name="loadingPage" type="Observable`1">
    /// </field>
    /// <field name="allPagesLoaded" type="Observable`1">
    /// </field>
    /// <field name="isSearching" type="Observable`1">
    /// </field>
    /// <field name="showingSearchResult" type="Observable`1">
    /// </field>
    /// <field name="nothingFound" type="Observable`1">
    /// </field>
    /// <field name="searchQuery" type="Observable`1">
    /// </field>
    /// <field name="newCount" type="Observable`1">
    /// </field>
    /// <field name="replyToPostId" type="Observable`1">
    /// </field>
    /// <field name="replyBody" type="Observable`1">
    /// </field>
    /// <field name="replyError" type="Observable`1">
    /// </field>
    /// <field name="posts" type="ObservableArray`1">
    /// </field>
    /// <field name="facebookId" type="String">
    /// </field>
    /// <field name="_lastLoadTime" type="Date">
    /// </field>
    /// <field name="_lastPageTime" type="Date">
    /// </field>
    this.postBody = ko.observable('');
    this.showForm = ko.observable('write');
    this.loadingPage = ko.observable(false);
    this.allPagesLoaded = ko.observable(false);
    this.isSearching = ko.observable(false);
    this.showingSearchResult = ko.observable(false);
    this.nothingFound = ko.observable(false);
    this.searchQuery = ko.observable('');
    this.newCount = ko.observable(0);
    this.replyToPostId = ko.observable(0);
    this.replyBody = ko.observable('');
    this.replyError = ko.observable('');
    this.posts = ko.observableArray(this._mapPosts(posts));
    this._lastLoadTime = Date.get_now();
    this._lastPageTime = Date.get_now();
    window.setInterval(ss.Delegate.create(this, function() {
        this.getNewPostsCount();
    }), 20000);
}
OxarnaJS.DiscussionIndexViewModel.prototype = {
    posts: null,
    facebookId: null,
    _lastLoadTime: null,
    _lastPageTime: null,
    
    _mapPosts: function OxarnaJS_DiscussionIndexViewModel$_mapPosts(posts) {
        /// <param name="posts" type="System.Collections.Generic.IEnumerable">
        /// </param>
        /// <returns type="Array"></returns>
        var postsViewModels = [];
        var $enum1 = ss.IEnumerator.getEnumerator(posts);
        while ($enum1.moveNext()) {
            var p = $enum1.current;
            postsViewModels.add(new OxarnaJS.DiscussionPostViewModel(this, p));
        }
        return postsViewModels;
    },
    
    restorePosts: function OxarnaJS_DiscussionIndexViewModel$restorePosts() {
        $.getJSON('/Discussion/' + window.listId + '/GetPosts', ss.Delegate.create(this, function(posts) {
            this.posts.removeAll();
            var $enum1 = ss.IEnumerator.getEnumerator(this._mapPosts(posts));
            while ($enum1.moveNext()) {
                var p = $enum1.current;
                this.posts.push(p);
            }
            this._lastLoadTime = Date.get_now();
        }));
    },
    
    getNewPostsCount: function OxarnaJS_DiscussionIndexViewModel$getNewPostsCount() {
        var url = '/Discussion/' + window.listId + '/GetNewCount/' + this._lastLoadTime.format('yyyyMMddHHmmss');
        $.getJSON(url, ss.Delegate.create(this, function(newCount) {
            this.newCount(newCount);
        }));
    },
    
    getNewPosts: function OxarnaJS_DiscussionIndexViewModel$getNewPosts() {
        var url = '/Discussion/' + window.listId + '/GetNewPosts/' + this._lastLoadTime.format('yyyyMMddHHmmss');
        this._lastLoadTime = Date.get_now();
        this.newCount(0);
        $.getJSON(url, ss.Delegate.create(this, function(posts) {
            var postViewModels = this._mapPosts(posts);
            postViewModels.reverse();
            var $enum1 = ss.IEnumerator.getEnumerator(postViewModels);
            while ($enum1.moveNext()) {
                var p = $enum1.current;
                this.posts.unshift(p);
            }
        }));
    },
    
    getNextPage: function OxarnaJS_DiscussionIndexViewModel$getNextPage() {
        if (this.showingSearchResult() || this.allPagesLoaded() || this.loadingPage() || this.posts().length >= 100) {
            return;
        }
        var lastReplyBefore = '0';
        if (this.posts().length > 0) {
            lastReplyBefore = this.posts()[this.posts().length - 1].lastReply;
        }
        this.loadingPage(true);
        var url = '/Discussion/' + window.listId + '/GetPage/' + lastReplyBefore + '/' + this._lastLoadTime.format('yyyyMMddHHmmss') + '/' + this._lastPageTime.format('yyyyMMddHHmmss');
        $.getJSON(url, ss.Delegate.create(this, function(posts) {
            this.loadingPage(false);
            var postViewModels = this._mapPosts(posts);
            if (!postViewModels.length) {
                this.allPagesLoaded(true);
                return;
            }
            var $enum1 = ss.IEnumerator.getEnumerator(this.posts());
            while ($enum1.moveNext()) {
                var existingPost = $enum1.current;
                var $enum2 = ss.IEnumerator.getEnumerator(postViewModels);
                while ($enum2.moveNext()) {
                    var newPost = $enum2.current;
                    if (existingPost.id === newPost.id) {
                        postViewModels.remove(newPost);
                        break;
                    }
                }
            }
            var $enum3 = ss.IEnumerator.getEnumerator(postViewModels);
            while ($enum3.moveNext()) {
                var p = $enum3.current;
                this.posts.push(p);
            }
            this._lastPageTime = Date.get_now();
        }));
    },
    
    post: function OxarnaJS_DiscussionIndexViewModel$post() {
        $('#discussion-write-submit').attr('disabled', 'disabled');
        var options = {};
        options.type = 'POST';
        options.data = 'body=' + this.postBody();
        options.success = ss.Delegate.create(this, function(post, textStatus, request) {
            var postViewModel = new OxarnaJS.DiscussionPostViewModel(this, post);
            postViewModel.highlightAsNew = true;
            this.posts.unshift(postViewModel);
            this.postBody('');
            $('#discussion-write-submit').removeAttr('disabled');
        });
        $.ajax('/Discussion/Write/' + window.listId, options);
    },
    
    clearSearch: function OxarnaJS_DiscussionIndexViewModel$clearSearch() {
        this.nothingFound(false);
        this.isSearching(false);
        if (this.searchQuery().length > 0) {
            this.searchQuery('');
            this.restorePosts();
        }
    },
    
    search: function OxarnaJS_DiscussionIndexViewModel$search() {
        this.nothingFound(false);
        this.posts.removeAll();
        if (!this.searchQuery().length) {
            this.restorePosts();
            return;
        }
        this.isSearching(true);
        var options = {};
        options.type = 'POST';
        options.data = 'query=' + this.searchQuery();
        options.success = ss.Delegate.create(this, function(data, textStatus, request) {
            this.isSearching(false);
            var posts = data;
            var i = 0;
            var $enum1 = ss.IEnumerator.getEnumerator(this._mapPosts(posts));
            while ($enum1.moveNext()) {
                var p = $enum1.current;
                this.posts.push(p);
                i++;
            }
            if (!i) {
                this.nothingFound(true);
            }
        });
        $.ajax('/Discussion/' + window.listId + '/SearchQuery', options);
    },
    
    showReplyForm: function OxarnaJS_DiscussionIndexViewModel$showReplyForm(id) {
        /// <param name="id" type="Number" integer="true">
        /// </param>
        this.replyBody('');
        this.replyToPostId(id);
        $('#replyBody-' + id).focus();
        eval('refreshAutoResize();');
    },
    
    postReply: function OxarnaJS_DiscussionIndexViewModel$postReply() {
        if (!this.replyBody().length) {
            this.replyError('Du m\u00e5ste ange ett meddelande.');
            return;
        }
        if (this.replyBody().length < 3 || this.replyBody().length > 1000) {
            this.replyError('Meddelandet m\u00e5ste vara mellan 3 och 1000 tecken.');
            return;
        }
        this.replyError('');
        var post = null;
        var $enum1 = ss.IEnumerator.getEnumerator(this.posts());
        while ($enum1.moveNext()) {
            var p = $enum1.current;
            if (p.id === this.replyToPostId()) {
                post = p;
                break;
            }
        }
        if (post == null) {
            this.replyError('Ett ok\u00e4nt fel intr\u00e4ffade.');
            return;
        }
        var options = {};
        options.type = 'POST';
        options.data = 'body=' + this.replyBody();
        options.success = ss.Delegate.create(this, function(reply, textStatus, request) {
            var replyViewModel = new OxarnaJS.DiscussionReplyViewModel(reply);
            replyViewModel.highlightAsNew = true;
            post.replies.push(replyViewModel);
            this.replyBody('');
            this.replyToPostId(0);
        });
        $.ajax('/Discussion/Post/' + this.replyToPostId() + '/Reply', options);
    }
}


OxarnaJS.DiscussionReplyViewModel.registerClass('OxarnaJS.DiscussionReplyViewModel');
OxarnaJS.DiscussionPostViewModel.registerClass('OxarnaJS.DiscussionPostViewModel');
OxarnaJS.DiscussionIndexViewModel.registerClass('OxarnaJS.DiscussionIndexViewModel');
})(jQuery);

//! This script was generated using Script# v0.7.3.0

