if (typeof GRAFS === 'undefined') {
    GRAFS = {};
}

if (typeof GRAFS.widget === 'undefined') {
    GRAFS.widget = {};
}

GRAFS.widget.Navigation = function(selectedCategoryId, selectedBranchId, baseUrl) {
    var topNav = YAHOO.util.Selector.query('#grafs_navigation > li');
    var topLen = topNav.length;

    activeBranch = null;

    for (var i = 0; i < topLen; i++) {
        topNav[i].categoryId = topNav[i].innerHTML.match(/\/(\d+)">/)[1];

        topNav[i].processing = false;

        topNav[i].selectNav = function() {
            // Get the category ID out of the link URL
            var categoryId = this.innerHTML.match(/\/(\d+)">/)[1];

            if (activeBranch && activeBranch !== this) {
                YAHOO.util.Dom.removeClass(activeBranch, 'active');

                if (activeBranch.subNav) {
                    activeBranch.subNav.style.display = 'none';
                }
            }

            activeBranch = this;

            this.processing = true;

            if (this.subNav) {
                YAHOO.util.Dom.addClass(this, 'active');
                this.subNav.style.display = "block";
                this.processing = false;
                return;
            }

            var throbber = document.createElement('img');
            throbber.src = 'packages/swat/images/swat-button-throbber.gif';
            throbber.alt = 'Loading';
            this.appendChild(throbber);

            var that = this;

            var prefix = 'retail';

            if (YAHOO.util.Dom.hasClass(document.body, 'dealer')) {
                prefix = 'dealer';
            }

            YAHOO.util.Connect.asyncRequest(
                'GET',
                baseUrl + '/' +  prefix + '/catalog/subcategories/parentId/' + categoryId,
                {
                    success: function(o) {
                        YAHOO.util.Dom.addClass(that, 'active');

                        var subCat = YAHOO.lang.JSON.parse(o.responseText);
                        var subLen = subCat.length;

                        var ul = document.createElement('ul');

                        for (var i = 0; i < subLen; i++) {
                            var li    = document.createElement('li');
                            var a     = document.createElement('a');
                            var title = document.createTextNode(subCat[i].name);

                            a.href          = prefix + '/catalog/category/categoryId/' + subCat[i].category_id;
                            a.subcategoryId = subCat[i].category_id;
                            a.categoryId    = that.categoryId;

                            if (selectedCategoryId === a.subcategoryId) {
                                YAHOO.util.Dom.addClass(a, 'active');
                            }

                            a.appendChild(title);
                            li.appendChild(a);
                            ul.appendChild(li);

                            YAHOO.util.Event.on(a, 'click', function(e) {
                                YAHOO.util.Event.stopPropagation(e);
                            });
                        }

                        that.appendChild(ul);

                        that.subNav = ul;

                        throbber.style.display = 'none';

                        that.processing = false;
                    },
                    failure: function(o) {
                    
                    }
                }
            );
        };

        YAHOO.util.Event.on(topNav[i], 'click', function(e) {
            YAHOO.util.Event.stopEvent(e);
            
            if (! this.processing) {
                this.selectNav();
            }
        });

        if (null !== selectedBranchId && selectedBranchId == topNav[i].categoryId) {
            topNav[i].selectNav();
        }
    }
};


