﻿var Landor = Landor || {};

Landor.LeftNav = function (container, canvas)
{
    this._container = container;
    this._canvas = canvas;
    this._filterIds = new Array();
    this._values = new Array();

    if (null != this._container)
    {
        this._Init();
    }
}

Landor.LeftNav.prototype =
{
    ToggleFilters: function (e)
    {
        var label = e.data.head;
        var panel = e.data.container;

        if (null == label || null == panel)
        {
            return;
        }

        panel.animate({ 'height': 'toggle' }, { 'duration': 200, 'complete': function ()
        {
            if ("hidden" == panel.css("visibility"))
            {
                panel.css("visibility", "visible");
                panel.css("display", "block");

                var divHeight = 0;
                $('.leftNav').children('div').each(function () { divHeight += $(this).height() + 28; });
                var hrHeight = $(".leftNav").children('hr').length * 2;
                var totalHeight = divHeight + hrHeight;
                var roundTo = 147;
                var newHeight = totalHeight;
                var newRoundedHeight = roundTo * Math.ceil(newHeight / roundTo);
                $(".leftNav").css("height", newRoundedHeight);


                var bgImage = label.css("background-image");
                label.css("background-image", bgImage.replace("Up", "Down"));
            }
            else
            {
                panel.css("visibility", "hidden");

                var divHeight = 0;
                $('.leftNav').children('div').each(function () { divHeight += $(this).height() + 28; });
                var hrHeight = $(".leftNav").children('hr').length * 2;
                var totalHeight = divHeight + hrHeight;
                var roundTo = 147;
                var newHeight = totalHeight;
                var newRoundedHeight = roundTo * Math.ceil(newHeight / roundTo);
                $(".leftNav").css("height", newRoundedHeight);

                var bgImage = label.css("background-image");
                label.css("background-image", bgImage.replace("Down", "Up"));
            }
        }
        });
    },

    ApplyFilter: function (e)
    {
        var filterLink = $(e.target);
        filterLink.css('font-weight', '700');

        for (var i = 0; i < filterLink.parent().parent().children().length; ++i)
        {
            var sibling = $($(filterLink.parent().parent().children()[i]).children()[0]);

            if (sibling[0] != filterLink[0] || null == e.data.value)
            {
                sibling.css('font-weight', '400');
            }
        }

        if (null != e.data.filterId)
        {
            if (null != e.data.value)
            {
                var existingIndex = $.inArray(e.data.filterId, this._filterIds);

                if (-1 == existingIndex)
                {
                    this._filterIds.push(e.data.filterId);
                    this._values.push(e.data.value);
                }
                else
                {
                    this._values[existingIndex] = e.data.value;
                }
            }
            else
            {
                var index = $.inArray(e.data.filterId, this._filterIds);

                if (-1 != index)
                {
                    this._filterIds.splice(index, 1);
                    this._values.splice(index, 1);
                }
            }
        }

        var data = { "url": window.location.hash };

        if (0 != this._filterIds.length)
        {
            data["filters"] = this._filterIds;
            data["values"] = this._values;
        }

        $.ajax({
            'url': '/data-services/clientservice.svc/getdocumentcontent',
            'context': this,
            'type': 'POST',
            'contentType': 'application/json',
            'dataType': 'json',
            'data': JSON.stringify(data),
            'success': function (content)
            {
                if (null == content || null == content.d)
                {
                    return;
                }

                this._canvas.ClearTemplateContent();

                if(null != content.d.HtmlContent && 0 != content.d.HtmlContent.length)
                {
                    this._canvas.DisplayContent(content.d.HtmlContent);
                }
                else if(null != content.d.ContentViews && 0 != content.d.ContentViews.length)
                {
                    this._canvas.LayoutTiles(content.d.ContentViews);
                }
            },
            'error': function (jqXHR, textStatus, errorThrown)
            {
                alert(textStatus);
                alert(errorThrown);
            }
        });
    },

    _Init: function ()
    {
        var filterSections = $("div.leftNavHead", this._container);

        for (var i = 0; i < filterSections.length; ++i)
        {
            var head = $(filterSections[i]);
            var container = $("div[id = " + head[0].id + "Container]", this._container);
            
            head.unbind("click");
            head.bind("click", { 'head': head, 'container': container }, this.ToggleFilters);

            var filterLinks = $("a.subMenuItem", container);

            for (var j = 0; j < filterLinks.length; ++j)
            {
                var filterLink = $(filterLinks[j]);
                filterLink.unbind("click");
                filterLink.bind("click", { 'filterId': head[0].id, 'value': 0 != j ? filterLink.text() : null }, $.proxy(this.ApplyFilter, this));
            }
        }
    }
}










