﻿var url;
var filter = "current";

function init_Announcements(_moduleid, _iseditable) {

    if (_iseditable == 'True') {
       $('#options').show();
    }

    FetchAnnouncements(_moduleid, _iseditable);

    $('#options a').click(function() {
        filter = $(this).attr('rel');
        $('#options a').removeClass('active');
        $(this).addClass('active');
        FetchAnnouncements(_moduleid, _iseditable);
    });

}

function FetchAnnouncements(_moduleid, _iseditable) {
    var i;
    var html = "";
    var summary = "";

    switch (filter) {
        case 'current':
            url = "/desktopmodules/sitebuilder/components/WebService.asmx/FetchAnnouncements_Current";
            break;
        case 'expired':
            url = "/desktopmodules/sitebuilder/components/WebService.asmx/FetchAnnouncements_Expired";
            break;
        case 'future':
            url = "/desktopmodules/sitebuilder/components/WebService.asmx/FetchAnnouncements_Future";
            break;
        case 'all':
            url = "/desktopmodules/sitebuilder/components/WebService.asmx/FetchAnnouncements_All";
            break;
    }

    $('#modulebody').empty();

    $.ajax({
        type: "POST",
        url: url,
        // pass parameters to webmethod
        data: "{ModuleID: " + _moduleid + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        dataFilter: function (data) {
            // This boils the response string down 
            //  into a proper JavaScript Object().
            var msg = eval('(' + data + ')');

            // If the response has a ".d" top-level property,
            //  return what's below that instead.
            if (msg.hasOwnProperty('d'))
                return msg.d;
            else
                return msg;
        },
        success: function(msg) {
            // clear all current modules
            $('#placeholder').empty();
            // if data is returned, iterate through response
            if (msg != '') {
                for (i = 0; i < msg.length; i++) {
                    html += '<div class="item">';
                    if (_iseditable == 'True') { html += '<h2><a class="lnkedit shadowbox" rel=' + msg[i].ItemID + ' href="' + _privateurl + '?skin=' + _SiteSkin + '&mode=announcements&siteid=' + _SiteID + '&pageid=' + _PageID + '&ModuleID=' + _moduleid + '&ItemID=' + msg[i].ItemID + '"><img visible="false" class="imgedit" src="/desktopmodules/sitebuilder/images/edit.gif" alt="edit" /></a>' } else { html += '<h2>' }

                    //summary = (msg[i].Summary != '') ? msg[i].Summary.replace(/<(.|\n)*?>/g, '') : msg[i].Description.replace(/<(.|\n)*?>/g, '');
                    //if (summary.length > 300) { summary = truncate(summary, 300, '', msg[i].ItemID); }
                    //html += msg[i].Title + '</h2><div class="announcement_' + msg[i].ItemID + '">' + summary.replace(/<(.|\n)*?>/g, '');
                    //html += ((msg[i].Summary != '') || (summary.length > 300)) ? '<a class="lnktoggle" rel="' + msg[i].ItemID + '" href="#">More</a></div><div class="announcement_' + msg[i].ItemID + '" style="display:none;"><p>' + msg[i].Description + '</p><p><a class="lnktoggle" rel="' + msg[i].ItemID + '" href="#">Less...</a></p></div></div>' : '</div></div>';

                    html += msg[i].Title + '</h2><div class="announcement_' + msg[i].ItemID + '">' + msg[i].Summary;
                    html += (msg[i].Description != '') ? '<a class="lnktoggle" rel="' + msg[i].ItemID + '" href="#">More</a></div><div class="announcement_' + msg[i].ItemID + '" style="display:none;"><p>' + msg[i].Description + '</p><p><a class="lnktoggle" rel="' + msg[i].ItemID + '" href="#">Less...</a></p></div></div>' : '</div></div>';
                }
            }
            else {
                if (filter != 'all') {
                    html = "<h3>There are no " + filter + " announcements.</h3>"
                }
                else {
                    html = "<h3>There are no announcements.</h3>"
                }
            }

            $('#modulebody').append(html);

            $('.lnktoggle').click(function() {
                $('.announcement_' + $(this).attr("rel")).toggle();
                return false;
            });

            SetEditControls(_iseditable, _moduleid, 'announcements');
        }
    });
}
