﻿$.CFDefaultPageTabsResources = {
    globalLoadingText: 'Loading ...',
    globalNoActivitiesToShowText: 'There are no more activities to show right now.'
};
var defaultPageTabsResources = $.CFDefaultPageTabsResources;

///#region Set Resources


$.setDefaultPageTabsResources = function (options) {
    defaultPageTabsResources = $.extend({}, $.CFDefaultPageTabsResources, options || {});
};


$(function () {


    // Initialize this to 1, so that "Next" is disabled until
    //  GetItemCount returns and we know there's a second page.
    
    var whatsPageLength = 20;
    var whatsNewMoreRecentActivityDiv = $('#WhatsNewMoreRecentActivityDiv');

    whatsNewMoreRecentActivityDiv.click(function (e) {
        e.preventDefault();
        var instance = $(this);
        instance.addClass('loading');
        getRecentActivityList();

    });
    function getRecentActivityStartPage() {
        var i = 1;
        getRecentActivityStartPage = function () {
            return ++i;
        };
        return getRecentActivityStartPage();
    }
    function getRecentActivityTemplate() {
        var templateHtml;
        $.ajax({
            url: Communifire.buildAjaxTemplatesUrl('DefaultRecentActivityList.aspx'),
            success: function (data) {
                templateHtml = data;
            },
            async: false
        });
        getRecentActivityTemplate = function () {
            return templateHtml;
        };
        return getRecentActivityTemplate();
    }
    function getRecentActivityList() {
        var templateHtml = getRecentActivityTemplate();

        $.cfAjax({ url: Communifire.buildCommonWSUrl('GetRecentActivityList'),
            data: JSON.stringify({ 'showRecentlyJoinedMembers': false, 'startPage': getRecentActivityStartPage(), 'pageLength': whatsPageLength }),
            success: function (response) {
                var responseData = response.ResponseData;
                var initializeTemplate = parseTemplate(templateHtml, { newsfeeds: responseData });
                var s = initializeTemplate;
                $('#recentactivity').find('ul').append(s).each(function () {
                    if (responseData.length == 0) {
                        whatsNewMoreRecentActivityDiv.html(defaultPageTabsResources.globalNoActivitiesToShowText);
                    }
                    whatsNewMoreRecentActivityDiv.removeClass('loading');
                });
            }
        });
    }
    $("ul.tabs").tabs("div.panes > div", {
        effect: 'default',
        onBeforeClick: function (event, i) {

            // get the pane to be opened
            var pane = this.getPanes().eq(i);
            var targetDiv = pane;
            if (targetDiv.is(":empty")) {

                //Initialize variables
                var virtualDirectory = Communifire.virtualDirectory;
                var pageLength = 10;
                var dataPath = null;
                var templatePath = null;
                var initializeTemplate = function () { };

                //Set loading status
                targetDiv.html(Communifire.loadingImage);
                var hrefValue = this.getTabs().eq(i).attr("href");
                i = parseInt(hrefValue.substring(1), 10);
                //Set data + template paths
                switch (i) {
                    case 1:
                        dataPath = Communifire.buildCommonWSUrl('GetLatestBlogsListAjaxify');
                        templatePath = Communifire.buildAjaxTemplatesUrl('LatestBlogsList.aspx');
                        break;
                    case 2:
                        dataPath = Communifire.buildCommonWSUrl('GetLatestForumPostsListAjaxify');
                        templatePath = Communifire.buildAjaxTemplatesUrl('LatestForumPostsList.aspx');
                        break;
                    case 3:
                        dataPath = Communifire.buildCommonWSUrl('GetLatestArticlesListAjaxify');
                        templatePath = Communifire.buildAjaxTemplatesUrl('LatestArticlesList.aspx');
                        break;
                    case 4:
                        dataPath = Communifire.buildCommonWSUrl('GetLatestEventsListAjaxify');
                        templatePath = Communifire.buildAjaxTemplatesUrl('LatestEventsList.aspx');
                        break;
                    case 5:
                        dataPath = Communifire.buildCommonWSUrl('GetLatestJobsListAjaxify');
                        templatePath = Communifire.buildAjaxTemplatesUrl('LatestJobsList.aspx');
                        break;
                    case 6:
                        dataPath = Communifire.buildCommonWSUrl('GetWikiTopicsListAjaxify');
                        templatePath = Communifire.buildAjaxTemplatesUrl('LatestWikiTopicsList.aspx');
                        break;
                    case 7:
                        dataPath = Communifire.buildCommonWSUrl('GetLatestVideosListAjaxify');
                        templatePath = Communifire.buildAjaxTemplatesUrl('LatestVideosList.aspx');
                        pageLength = 20;
                        break;
                    case 8:
                        dataPath = Communifire.buildCommonWSUrl('GetLatestPhotosListAjaxify');
                        templatePath = Communifire.buildAjaxTemplatesUrl('LatestPhotosList.aspx');
                        pageLength = 20;
                        break;
                } //end switch

                //#region Make ajax call
                $.ajax({
                    type: 'POST',
                    url: dataPath,
                    data: '{"pageLength":' + pageLength + '}',
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    success: function (response) {
                        if (response != null && response.d != null && response.d) {

                            var templateHTML = null;
                            var output = response.d;

                            //Load template
                            $.get(templatePath, function (data) {
                                templateHTML = data;

                                //Apply template

                                //#region switch
                                switch (i) {
                                    case 1:
                                        initializeTemplate = parseTemplate(templateHTML, { blogList: output });
                                        break;
                                    case 2:
                                        initializeTemplate = parseTemplate(templateHTML, { postList: output });
                                        break;
                                    case 3:
                                        initializeTemplate = parseTemplate(templateHTML, { articleList: output });
                                        break;
                                    case 4:
                                        initializeTemplate = parseTemplate(templateHTML, { eventList: output });
                                        break;
                                    case 5:
                                        initializeTemplate = parseTemplate(templateHTML, { jobList: output });
                                        break;
                                    case 6:
                                        initializeTemplate = parseTemplate(templateHTML, { wikiTopicList: output });
                                        break;
                                    case 7:
                                        initializeTemplate = parseTemplate(templateHTML, { videosList: output });
                                        break;
                                    case 8:
                                        initializeTemplate = parseTemplate(templateHTML, { photosList: output });
                                        break;
                                }
                                //#endregion

                                var s = initializeTemplate;
                                targetDiv.html(s);
                            }); //end ajax
                        }
                        else {
                            targetDiv.html('');
                        }
                    }
                });
                //#endregion 
            }

        }
    });

});
