﻿function init_Blog(_moduleid, _iseditable) {

    var i, j;
    var html = "";
    var summary = "";
    var url;
    
    url = (_iseditable == 'True') ? "/desktopmodules/sitebuilder/components/WebService.asmx/FetchBlogs_All" : "/desktopmodules/sitebuilder/components/WebService.asmx/FetchBLogs_Current";

    $.ajax({
        type: "POST",
        url: url,
        // 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++) {
                    style = (msg[i].IsPublic) ? "" : "noshow";
                    html += (_iseditable == 'True') ? '<h3 class="' + style + '"><a class="shadowbox" rel=' + msg[i].EntryID + ' href="' + _privateurl + '?skin=' + _SiteSkin + '&mode=Blog&EntryID=' + msg[i].EntryID + '"><img src="/desktopmodules/sitebuilder/images/edit.gif" alt="edit" /></a>' : '<h3>';
                    summary = (msg[i].Summary == '' || msg[i].Summary == '<p>&#160;</p>') ? truncate(msg[i].Entry.replace(/<(.|\n)*?>/g, ''), 900, '', msg[i].EntryID) : msg[i].Summary.replace(/<(.|\n)*?>/g, '');
                    html += msg[i].Title + '</span></h3><h4></h4><div class="blog_' + msg[i].EntryID + '">' + summary;
                    html += '<p><a class="lnktoggle" rel="' + msg[i].EntryID + '" href="#">More...</a></p></div><div class="blog_' + msg[i].EntryID + '" style="display:none;"><p>' + msg[i].Entry + '</p>';
                    html += '<div id="comments_' + msg[i].EntryID + '"></div><p><a class="lnktoggle" rel="' + msg[i].EntryID + '" href="#">Less...</a></p>';
                    html += (msg[i].AllowComments) ? '<p class="addnew"><a rel=' + _moduleid + ' class="Button shadowbox" href="' + dnn.getVar('publicurl') + '?skin=' + _SiteSkin + '&moduleid=' + _moduleid + '&mode=blog_comments&entryid=' + msg[i].EntryID + '"><span class="btn"><span class="t">Add Comment</span> <span class="r"><span></span></span><span class="l"></span></span></a></p></div>' : '</div>';
                }
            }

            $('#modulebody').append(html);

            SetEditControls(_iseditable, _moduleid, 'blog');

            $('.lnktoggle').click(function() {

                var entryid = $(this).attr("rel");

                if ($('#comments_' + entryid).html() == "") {
                    html = '';

                    url = (_iseditable == 'True') ? "/desktopmodules/sitebuilder/components/WebService.asmx/FetchBlogComments_All" : "/desktopmodules/sitebuilder/components/WebService.asmx/FetchBlogComments_Current";

                    $.ajax({
                        type: "POST",
                        url: url,
                        // pass parameters to webmethod
                        data: "{EntryID: " + entryid + "}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(comments) {
                            html += "<hr /><h3>Comments (" + comments.length + ")</h3>"
                            if (comments.length > 0) {
                                for (j = 0; j < comments.length; j++) {
                                    style = (comments[j].Approved) ? "" : "noshow";
                                    html += (_iseditable == 'True') ? '<h3 class="' + style + '"><a class="shadowbox" rel=' + comments[j].CommentID + ' href="' + _privateurl + '?skin=' + _SiteSkin + '&mode=blog_comments&CommentID=' + comments[j].CommentID + '"><img src="/desktopmodules/sitebuilder/images/edit.gif" alt="edit" /></a>' : '<h3>';
                                    html += comments[j].Author + " - " + eval('new' + comments[j].CreatedOn.replace(/\//g, ' ')).toString('MM/dd/yyyy h:mm tt') + "</h3><p>" + comments[j].Comment + "</p>"
                                }
                            }
                            else {
                                html += "<h3>The posting has no comments.</h3>"
                            }

                            $('#comments_' + entryid).html(html);

                            Shadowbox.setup($('.shadowbox'), {
                                onClose: FetchModule
                            });
                        }
                    });
                }

                $('.blog_' + $(this).attr("rel")).toggle();
                return false;
            });


            Shadowbox.setup($('.shadowbox'), {
                onClose: FetchModule
            });

        }
    });
}

