if (!window.Luminosity)
    window.Luminosity = {};

Luminosity.NavItem = function() {
    this.sequence = 0;
    this.isActive = false;
    this.isExpanded = false;
    this.isHidden = false;
    this.isHiddenByParent = false;
    this.senderItem = null;
    this.hasChildren = false;
    this.children = null;
    this.parent = null;
    this.completeOnVisit = true;
    this.nestingLevel = 1;
    this.title = "";
    this.guid = "";
    this.uri = "";
    this.isMediaPage = false;
    this.isMediaOnly = false;

    this.htmlButton = {
        Root: null,
        Expander: null,
        Main: null,
        Holder: null
    };

    this.state = 0;

    // States:
    //   -1 = NotVisited,
    //    0 = Unlocked,
    //    1 = Visited,
    //    2 = Completed,
    //    3 = Locked,    
}

Luminosity.NavItem.prototype =
{

    GetState: function() {
        return this.state;
    },

    SetState: function(newstate) {
        this.state = newstate;
        this.UpdateVisualCue();
    },

    UpdateVisualCue: function() {
        $(this.htmlButton.Main).removeClass('lu-chrome-navtree-locked')
                               .removeClass('lu-chrome-navtree-unlocked')
                               .removeClass('lu-chrome-navtree-visited')
                               .removeClass('lu-chrome-navtree-completed');

        switch (this.state) {
            case 0:
                $(this.htmlButton.Main).addClass('lu-chrome-navtree-unlocked');
                break;

            case 1:
                $(this.htmlButton.Main).addClass('lu-chrome-navtree-visited');
                break;

            case 2:
                $(this.htmlButton.Main).addClass('lu-chrome-navtree-completed');
                break;

            case 3:
                $(this.htmlButton.Main).addClass('lu-chrome-navtree-locked');
                break;
        }
    },

    AddChild: function(child) {
        if (null != child) {
            if (null == this.children) {
                this.children = new Array();
            }

            this.children.push(child);
            this.hasChildren = true;
            this.isExpanded = true;

            if (this.isHidden || this.isHiddenByParent) {
                if (!child.isHidden) {
                    child.isHiddenByParent = true;
                }
            }
        }
    },

    GetParent: function() {
        return this.parent;
    },

    GetChildren: function() {
        return this.children;
    },

    GetRoot: function() {
        var root = null;

        if (null != this.parent) {
            root = parent.GetRoot();
        }
        else {
            root = this;
        }

        return root;
    },

    LinkToButton: function(button) {
        var id = button.id;

        // Use ID of main as prefix to locate
        // other required items, such as expander
        // and holder
        this.htmlButton.Main = button;
        this.htmlButton.Holder = document.getElementById(id + "h");

        if (this.hasChildren === false) {
            $(this.htmlButton.Main).addClass("lu-chrome-expander-hidden");
        } else {
            $(this.htmlButton.Main).addClass("lu-chrome-expander-expanded");
        }
    },

    ToggleActive: function() {
        $(this.htmlButton.Main).toggleClass("ui-state-active");

        if (!this.isActive) {
            this.SetActive();

            // Expand branch
            // TODO: Need a better way to handle activating items for Linear vs. Tree navigation.
            if ("tree" === Config.Navigation.Type) {
                this.ExpandBranch();
            }

            //            if (this.isHidden || this.isHiddenByParent)
            //            {
            //                if (null != this.senderItem)
            //                {
            //                    this.senderItem.AppearActive();
            //                }
            //            }
        }
        else {
            this.isActive = false;

            $(this.htmlButton.Main).removeClass("ui-state-active");

            //            if (null != this.senderItem)
            //            {
            //                this.senderItem.AppearInactive();
            //                this.senderItem = null;
            //            }
        }
    },

    SetActive: function() {
        this.isActive = true;

        // Update state to visited
        if (this.state != 2) {
            this.SetState(1);
        }

        // Trigger change of page content
        ChangeContent(this.uri);
    },

    ToggleExpanded: function() {
        if (this.htmlButton.Holder) {
            if (this.isExpanded) {
                this.Collapse();
            }
            else {
                this.Expand();
            }
        }
    },

    ExpandBranch: function() {
        if (null != this.parent) {
            this.parent.ExpandBranch();
        }

        if (!this.isExpanded) {
            this.Expand();
        }
    },

    Expand: function() {
        $(this.htmlButton.Holder).show();
        $(this.htmlButton.Main).removeClass("lu-chrome-expander-closed")
                               .addClass("lu-chrome-expander-expanded");

        this.isExpanded = true;
    },

    Collapse: function() {
        $(this.htmlButton.Holder).hide();
        $(this.htmlButton.Main).addClass("lu-chrome-expander-closed")
                               .removeClass("lu-chrome-expander-expanded");

        var hasActiveChild = this.CheckActiveChildOnCollapse();
        if (hasActiveChild) {
            SetActiveAuto(this.sequence);
        }

        this.isExpanded = false;
    },

    CheckActiveChildOnCollapse: function() {
        var activeChild = false;

        if (null != activeNavItem) {
            if (activeNavItem.sequence != this.sequence) {
                if (null != this.children && this.children.length > 0) {
                    for (var i = 0; i < this.children.length; i++) {
                        var child = this.children[i];
                        if (activeNavItem.sequence == child.sequence) {
                            activeChild = true;
                            break;
                        }
                        else {
                            var result = child.CheckActiveChildOnCollapse();
                            if (result) {
                                activeChild = true;
                                break;
                            }
                        }
                    }
                }
            }
        }

        return activeChild;
    }
}
