﻿var newsfeed = new function($) {
    var options = {
        container: "#newsfeed_container",
        loadService: "/Newsfeed/GetNewsFeedItems",
        disableService: "/Newsfeed/DisableNewsFeed"
    };

    var items = [];
    var currentIndex = 0;
    var container = null;
    var newsfeedContent = null;

    function init(o) {
        if (o == undefined || o == null) o = {};
        options = $.extend(options, o);

        container = $(options.container);
        newsfeedContent = container.find(".x-newsfeedcontent");
        
        loadItems();
    };

    function next() {
        currentIndex++;
        if (currentIndex >= items.length) currentIndex = 0;
        setContent(items[currentIndex]);
    };

    function previous() {
        currentIndex--;
        if (currentIndex < 0) currentIndex = items.length - 1;
        setContent(items[currentIndex]);
    };

    function setContent(item) {
        if (item == undefined || item == null) return;

        newsfeedContent.hide();
        //container.fadeOut("normal", function() {
        newsfeedContent.find(".x-newsfeedtitle").html(item.Title);
        newsfeedContent.find(".x-newsfeeddescription").html(item.Description);

        newsfeedContent.fadeIn("normal");
        //})


    };

    function disable() {
        $.postJSON(options.disableService, null, function(data) {
            container.slideUp("slow");
            Viva.Notify.show(Viva.Notify.InformationType.Info, data.title, data.description);
        });
    }

    function loadItems() {
        $.getJSON(options.loadService, function(data) {
            items = data;
            setContent(items[currentIndex]);
        });
    };

    this.init = function() { init() };
    this.next = function() { next() };
    this.previous = function() { previous() };
    this.disable = function() { disable() };

} (jQuery);
