﻿if (!Networker) {
    var Networker = function() { };
}

Networker.Feed = {
};

Networker.Feed.hideFeedItems = function(items) {
    items.slideUp('fast', function() {
    
        // Remove the seperators
        $(this).prev(".FeedItemSeperator").remove();

        // Clean up empty days
        var emptyParent = $(this).parents(".FeedDay").filter(
			                function(index) {
			                    return $(this).find(".FeedItem:visible").length == 0;
			                }
		                ).slideUp('fast');

        $(this).remove();
    });
};

Networker.Feed.bindFeedActionsToElement = function(element, targetBlank) {
    if (targetBlank) {
        $(element).find("a").attr("target", "_blank");
    }
    $(element).find("a[href*=.ashx]").each(function(index, asyncLink) {
        if (targetBlank) {
            $(asyncLink).attr("target", "");
        }
        $(asyncLink).click(function() {
            $.get(this.href + "&Async=true");

            Networker.Feed.hideFeedItems($(this).parents(".FeedItem"));
            return false;
        });
    });

    // Dialogs
    $(element).find(".MessageReply").each(function(index, messageElement) {
        $(messageElement).attr("href", $(messageElement).attr("href").replace("#Compose", ""));
        $(messageElement).click(function() {
            var parent = $(this).parents(".FeedItem");
            Networker.Dialog.openDialog("/Messaging/Dialogs/Reply.aspx?MessageId=" + $(this).attr("messageId") + "&Modal=true&Transparent=true", 500, 400, function() {
                Networker.Feed.hideFeedItems(parent);
            });
            return false;
        });
    });

    if (!targetBlank) {
        $(element).find(".OpenDialog").click(function() {
            var parent = $(this).parents(".FeedItem");
            Networker.Dialog.openDialog($(this).attr("href") + "&Modal=true&Transparent=true", 500, 300, function() {
                Networker.Feed.hideFeedItems(parent);
            });
            return false;
        });
    }
};

Networker.Feed.bindFeedActions = function(targetBlank) {
    $(".FeedItem").each(function(index, element) {
        Networker.Feed.bindFeedActionsToElement(element, targetBlank);
    });
};

Networker.Feed.initialize = function() {
    $(document).ready(function() {
        Networker.Feed.bindFeedActions(false);
    });
} ();