ProspectDirector.Modules.Explorer.ExplorerContentTabstrip = Class.extend({

    //---------------------------
    //--- CONSTRUCTOR
    //---------------------------

    init: function (options) {

        this.options = $.extend(true, {
            selector: null,
            data: {
                tabData: null,
                entityId: null,
                folderId: null
            }
        }, options);

        this.init_tabStrip();

        ProspectDirector.RUNTIME.loadModule(this, "ExplorerTabStrip");

    },

    //---------------------------
    //--- PROPERTIES
    //---------------------------

    get_tabStrip: function () {
        return $(this.options.selector);
    },

    //---------------------------
    //--- INITIALIZERS
    //---------------------------

    init_tabStrip: function () {

        var data = this.options.data.tabData.slice(0),
            item = null,
            label = null,
            image = null,
            index = 0,
            that = this;

        for (index; index < data.length; index++) {

            item = data[index];

            item.encoded = false;

            if (item.icon) {
                item.image = ProspectDirector.RUNTIME.resolveUrl("~/application/icons/16x16/" + item.icon);
            }

            data[index] = item;

        }

        this.get_tabStrip().kendoTabStrip({
            dataSource: data,
            dataTextField: "name",
            dataImageUrlField: "image",
            select: $.proxy(this.tabStrip_onSelect, this)
        });

        this.highlightSelectedTab(this.options.data.folderId);

        this.tabStripPostInit();

        /* TODO: HACK ALERT! Should find a better way to get a handle on an ACTIVATED viewport splitter rather than relying on a timeout. */
        setTimeout(function () {

            var splitter = $(".viewport-splitter").data("kendoSplitter");

            if (splitter) {
                splitter.bind("resize", $.proxy(that.containingPane_onResize, that));
            }
             
        }, 50);

        this.get_tabStrip().data("tabScroller", new ProspectDirector.Types.KendoExtensions.TabStripScroller({
            tabStrip: this.get_tabStrip()
        }));

        this.get_tabStrip().data("tabScroller").scrollIntoView(this.get_tabStrip().data("kendoTabStrip").select());

    },

    containingPane_onResize: function () {

        var scroller = this.get_tabStrip().data("tabScroller");

        if (scroller) {
            scroller.update();
        }

    },

    tabStrip_onSelect: function (e) {

        var tabStrip = this.get_tabStrip().data("kendoTabStrip");
        var index = $.inArray(e.item, tabStrip.items());
        var data = null;
        var that = this;
        var entityId = that.options.data.entityId;
        var folderId = that.options.data.folderId;

        if (index > -1) {
            data = tabStrip.dataSource.data()[index];
        }

        if (data !== null) {

            e.preventDefault();

            ProspectDirector.RUNTIME.MODULES.Navigator.go(function () {

                ProspectDirector.RUNTIME.MODULES.Explorer.loadContent(data.id, that.options.data.entityId);
                that.get_tabStrip().select(index)
                window.history.replaceState({}, null, Jade.Path.resolve("~/explore/" + entityId + "/folder/" + data.id));

            });

        }

    },

    highlightSelectedTab: function (id) {

        var tabStrip = this.get_tabStrip().data("kendoTabStrip"),
            index = 0,
            data = tabStrip.dataSource.data(),
            items = tabStrip.items();

        for (index; index < items.length; index++) {
            if (data[index].id === id) {
                $(items[index]).addClass("k-state-active");
                index = items.length;
            }
        }

    },

    tabStripPostInit: function () {

        var tabStrip = this.get_tabStrip().data("kendoTabStrip"),
            index = 0,
            tabs = tabStrip.items();

        for (index; index < tabs.length; index++) {

            this.applyTabTemplate($(tabs[index]), tabStrip.dataSource.data()[index]);

        }

    },

    applyTabTemplate: function (tab, data) {

        var image = $(tab).find("IMG"),
            html = null;

        if (data.isHidden) {

            tab.find(".k-link").css({
                color: "#BBBBBB"
            });

            if (image.length > 0) {

                image.css({
                    opacity: .5
                });

            }

        }


        image = $(tab).find("IMG");

        html = ""

        if (image.length > 0) {
            html += $(tab).find("IMG")[0].outerHTML;
        }

        html += data.name


        if (data.fileCount > 0) {
            html += "&nbsp;<b>(" + data.fileCount + ")";
        }

        tab.find(".k-link").empty();
        tab.find(".k-link").html(html);


    },

    reloadTab: function (data) {

        var tabStrip = null,
            tab = null;

        if (this.get_tabStrip().length === 0) {
            return;
        }

        tabStrip = this.get_tabStrip().data("kendoTabStrip");

        if (!tabStrip) {
            return;
        }

        tab = this.getTabById(data.id);

        if (tab) {
            this.applyTabTemplate(tab, data);
        }

    },

    getTabById: function (id) {

        var tabStrip = this.get_tabStrip().data("kendoTabStrip"),
            item = tabStrip.dataSource.get(id),
            index = -1,
            result = null;

        if (item) {
            index = tabStrip.dataSource.data().indexOf(item);
        }

        result = this.get_tabStrip().find("LI.k-item:eq(" + index + ")");

        if (result.length !== 1) {
            result = null;
        }

        return result;

    }

});