﻿/// <reference path="../API/Agility.js" />

Agility.RegisterNamespace("BNN.Modules.BlogsListing");

(function(BlogsListing) {

    var properties = null;
    var container = null;
    var timeout = 500;
    var closetimer = 0;
    var menuItem = null;

    BlogsListing.OnInit = function(p) {

        properties = p;
        container = $('#' + properties.ContainerID);

        $('.Post:odd', container).addClass('AlternatePost');

        $('.MenuAuthors .Content ul', container).each(function() {
            $('li:last', $(this)).css('margin-bottom', '0px');
        });

        $('.MenuSectors .Content a:last', container).css('border-bottom', 'none');

        BlogsListing.OnMenuInit();
    };

    BlogsListing.LoadPosts = function() {

        var pageNum = BNN.Controls.Pager.CurrentPage;

        $.ajax({
            url: AjaxWebserviceSvcUrl + "/GetBlogPosts?pageNum=" + pageNum + "&pageSize=" + properties.PostsPerPage +
                "&year=" + 0 + "&month=" + 0 + "&day=" + 1 + "&category=" + properties.Sector + "&author=" + properties.Author,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(jsonResponse) {
                var res = { d: jsonResponse };
                if (res.d != undefined && res.d != null) {

                    var $container = container.find('.Posts').eq(0);
                    $container.html("");

                    if (!$container.hasTemplate()) {

                        var settings = { "f_escapeString": function(string) { return string; } };
                        $container.setTemplateURL(Agility.ResolveUrl("~/ClientTemplates/BlogsListing.htm"), null, settings);
                        $container.setParam('ContinueReadingLabel', properties.ContinueReadingLabel);
                    }

                    $container.processTemplate(res);
                    BNN.Controls.Pager.Render(properties.TotalCount);
                }
            }
        });
    };

    BlogsListing.OnMenuInit = function() {

        var config = {
            over: BNN.Modules.BlogsListing.OnMenuOpen,
            out: BNN.Modules.BlogsListing.OnMenuTimer,
            timeout: 10
        };

        $('.ViewBy > a', container).each(function(i, item) {
            var subMenuID = $(this).attr('menuclass');
            $(this).append($('.' + subMenuID, container));
        });

        var lis = $('.ViewBy > a', container);
        lis.hoverIntent(config);
    };

    BlogsListing.OnMenuOpen = function() {

        cancelTimer();
        BNN.Modules.BlogsListing.OnMenuClose();
        var subMenu = $(this).attr('menuclass');
        $('.' + subMenu, container).show();
    };

    BlogsListing.OnMenuClose = function() {

        $('.SubMenu', container).hide();
    };

    BlogsListing.OnMenuTimer = function() {

        closetimer = window.setTimeout("BNN.Modules.BlogsListing.OnMenuClose()", timeout);
    };

    BlogsListing.GetImageUrl = function(post) {

        var img = '';

        if (post.Author != null) {
            if (post.Author.BlogListingImage.URL != null && post.Author.BlogListingImage.URL != '') {
                img = post.Author.BlogListingImage.URL;
            } else {
                img = post.Author.Image.URL;
            }
        } else if (post.BylineImage.URL != '') {
            img = post.BylineImage.URL;
        }

        return (img != '' ? Agility.ResolveUrl('~/Handlers/ThumbS3.ashx') + '?img=' + img + ',180,94' : '');
    };

    BlogsListing.GetAuthorSection = function(post) {

        if (post.Author != null) {
            return "By <a href='" + Agility.ResolveUrl(post.Author.PageURL) + "' class='Name'>" + post.Author.Title + "</a>" +
				((typeof post.Author.Type != 'undefined' && post.Author.Type != '') ? ", " + post.Author.Type : "");
        } else if (post.Byline != null) {
            return post.Byline;
        }

        return "";
    };

    BlogsListing.GetTeaser = function(teaser, index) {

        if (teaser != null && teaser != '') {
            var length = (index == 0) ? properties.PostCharacterCount * 2 : properties.PostCharacterCount;
            length = Math.min(length, teaser.length);
            return teaser.substr(0, length) + "...";
        } else {
            return "";
        }
    };

    BlogsListing.GetDateTimeSection = function(post) {

        var st = '';

        if (post.Categories.length > 0) {
            st += 'Filed in: ';

            var links = new Array();
            for (var i = 0; i < post.Categories.length; i++) {
                var cat = post.Categories[i];
                var link = '<a href="' + Agility.ResolveUrl('~/Blogs.aspx') + '?sector=' + cat.FriendlyName.URL + '">' + cat.Title + '</a>';
                links.push(link);
            }
            st += links.join(', ');
        }

        return st;
    };

    BlogsListing.GetDate = function(dateStr) {
        var date = getDate(dateStr);
        return dateFormat(date, "mmm d yyyy");
    };

    function getDate(dateStr) {
        return eval("new " + dateStr.replace(/\//gi, ""));
    };

    function cancelTimer() {
        if (closetimer) {
            window.clearTimeout(closetimer);
            closetimer = null;
        }
    };

    BlogsListing.InitBlogRoll = function(p) {
    
        $.ajax(
            {url: AjaxWebserviceSvcUrl + "/GetBlogRoll",
            contentType: "application/json",
            dataType: "json",
            success: function(jsonResponse) {
                if (jsonResponse != undefined && jsonResponse != null) {
                    var html = "";
                    $.each(jsonResponse, function(i, roll) {
                        html += "<div class='Post " + (i == 0 ? "FirstPost" : "") + "'><div class='Thumbnail'>";
                        html += "<a href='" + roll.AuthorUrl + "'><img src='" + roll.AuthorImageUrl + "' alt='' /></a></div>";
                        html += "<div class='Headline'><a href='" + roll.PostUrl + "'>" + roll.Headline +"</a></div>";
					    html += "<div class='Details'>" + roll.PostDetails + "</div></div>";
                    });
                    var client = $("#" + p.ClientID);
                    client.find("div.Posts").html(html + "<div class='Clear'></div>");
                }
            }
        });
    };

})(BNN.Modules.BlogsListing);

