﻿///<reference path="../../../Element.js"/>

var njs;
if (!njs) njs = {};
if (!njs.ui) njs.ui = {};
if (!njs.ui.widgets) njs.ui.widgets = {};

njs.ui.widgets.FileCabinet = function(container, defaultOpen, tabsOnBottom, alreadyActiveFunction) {
    njs.ui.widgets.FileCabinet.Instance = this;
    var _container = container;
    this.base = njs.ui.Element;
    this.base(container);
    this.childTabs = getChildTabs(this);
    this.childContents = getChildContents(this);
    this.Drawer = [];
    this.AddClass("njs-FileCabinet");

    this.fileTabs = new njs.ui.Div();
    this.fileTabs.SetClass("njs-FileTabs");
    this.fileContents = new njs.ui.Div();
    this.fileContents.SetClass("njs-FileContents");

    function getChildTabs(that) {
        var childTabs = [];
        var tabs = that.Container.getElementsByTagName('dt');
        for (var i = 0; tabs[i]; i++) {
            if (tabs[i].parentNode.getAttribute("id") == _container)
                childTabs.push(tabs[i])
        }
        return childTabs;
    }

    function getChildContents(that) {
        var childContents = [];
        var contents = that.Container.getElementsByTagName('dd');
        for (var i = 0; contents[i]; i++) {
            if (contents[i].parentNode.getAttribute("id") == _container)
                childContents.push(contents[i])
        }
        return childContents;
    }

    for (var i = 0; this.childTabs[i] && this.childContents[i]; i++) {
        var file = new njs.ui.widgets.File(this.childTabs[i], this.childContents[i], i, alreadyActiveFunction);
        file.SetDrawer(this.Drawer);
        this.Drawer.push(file);
    }

    for (var i = 0; this.Drawer[i]; i++) {
        this.fileTabs.Add(this.Drawer[i].tab);
        this.fileContents.Add(this.Drawer[i].content);
    }

    if (typeof defaultOpen == "number" && this.Drawer[defaultOpen - 1] != "undefiened")
        this.SetActiveTab(defaultOpen);
    else
        this.SetActiveTab(1);

    if (tabsOnBottom) { this.Add(this.fileContents); this.Add(this.fileTabs); }
    else { this.Add(this.fileTabs); this.Add(this.fileContents); }

}
njs.ui.widgets.FileCabinet.prototype = new njs.ui.Element;

njs.ui.widgets.FileCabinet.prototype.SetActiveTab = function(aNumber) {
    var file = aNumber-1;
    if(typeof this.Drawer[file]!="undefined"){
        this.Drawer[file].Activate();
    }
}