﻿function init_Announcements(_moduleid, _iseditable) {

    var i;
    var html = "";
    var summary = "";

    $.ajax({
        type: "POST",
        url: "/desktopmodules/sitebuilder/components/WebService.asmx/FetchAnnouncements_Current",
        // pass parameters to webmethod
        data: "{ModuleID: " + _moduleid + "}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // clear all current modules
            $('#placeholder').empty();
            // if data is returned, iterate through response
            if (null != msg) {
                for (i = 0; i < msg.length; i++) {
                    if (_iseditable == 'True') { html += '<h2><a class="lnkedit shadowbox" rel=' + msg[i].ItemID + ' href="' + _privateurl + '?skin=' + _SiteSkin + '&mode=announcements&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)) ? '<p><a class="lnktoggle" rel="' + msg[i].ItemID + '" href="#">More...</a></p></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>';
                }
            }

            $('#modulebody').append(html);

            $('.lnktoggle').click(function() {
                $('.announcement_' + $(this).attr("rel")).toggle();
                return false;
            });

            SetEditControls(_iseditable, _moduleid, 'announcements');
        }
    });
}
