/*
    Top navigation - the subNavItems are the items that show up in the sub list - name is the 
    text that becomes the link and href is the link's URL
*/
var TopNav = {
    subNavItems : [
        [ {'name':'Patient Information','href':'/patient-information.asp'}, 
            {'name':'Find a Physician','href':'/physicians.asp?showallprofiles=yes'}, 
            {'name':'Departments &amp; Services','href':'/departments.asp'},
            {'name':'Conditions &amp; Treatments','href':'/conditions.asp'},
            {'name':'Classes &amp; Programs','href':'/public-patient-education.asp'},
            {'name':'International Center','href':'/international-center.asp'}],
        [ {'name':'Specialties','href':'/professional.asp'}, 
            {'name':'Professional Education','href':'/professional-education.asp'}, 
            {'name':'Articles &amp; Videos','href':'/professional-conditions.asp'},
            {'name':'Academic Training','href':'/academic-training.asp'}, 
            {'name':'HSS Alumni','href':'/alumni.asp'}],
        [ {'name':'Programs','href':'/research-programs.asp'}, 
            {'name':'Professional Staff','href':'/research-professional-staff.asp'},
            {'name':'Core Facilities','href':'/research-core-facilities.asp'},
            {'name':'Clinical Trials','href':'/clinical-trials.asp'}],
        [ {'name':'Hospital Information','href':'/about.asp'}, 
            {'name':'Career Opportunities','href':'/careers.asp'},
            {'name':'Volunteer at HSS','href':'/volunteer-in-new-york-city.asp'}, 
            {'name':'Publications','href':'/publications.asp'},
            {'name':'News','href':'/newsroom.asp'}, 
            {'name':'Giving to HSS','href':'/giving.asp'}, 
            {'name':'International','href':'/international.asp'}],
        [ {'name':'Phone Numbers','href':'/contact.asp'},
            {'name':'Maps &amp; Directions','href':'/maps-directions.asp'}] ], 
    defaultItem : null,
    activeItem : null,
    initialize : function () {
        var itemsPerCol = 5;
        var listColWidth = 165;
        var listColOffset = 10
        var main = $('mainNav');
        this.navItems = main.getElements('.navItem');

        var subNavContainer = new Element('div');
        main.adopt(subNavContainer);
        subNavContainer.setStyles({'position' : 'relative', 'top': 0, 'left': 0, 'clear': 'both', 'padding-top': '0', 'width': '100%', 'overflow': 'hidden'});
        
        var listContainer, listWidth, listOffset;
        for (var i=0, l = this.navItems.length; i<l; i++) {
            var hand = this.navItems[i];
            var subItems = this.subNavItems[i];
            var listCount = 0;
            if (hand.getElements('.subNav').length > 0) {
                this.defaultItem = hand;
                this.activeItem = hand;
                listContainer = hand.getElement('.subNav');
                var uls = hand.getElements('ul')
                listCount = uls.length;
                listOffset = uls[0].getStyle('padding-right').toInt();
                
            } else {
                listContainer = new Element('div', {'class': 'subNav'});
                hand.adopt(listContainer);
                var ulNode;
                for (var j=0, l2 = subItems.length; j<l2; j++) {
                    var si = subItems[j];
                    if (j%itemsPerCol == 0) {
                         ulNode = new Element('ul');
                         listContainer.adopt(ulNode);
                         listCount++;
                    }
                    liNode = new Element('li');
                    liNode.setHTML('<span><a href="' + si.href + '">' + si.name + '</a></span>');
                    ulNode.adopt(liNode);
                    /*if (j%itemsPerCol == (itemsPerCol-1) || j+1 == l2) {
                        liNode.addClass('lastSub');
                    }*/
                }
            }
            listContainer.setStyle('position', 'static');
            subNavContainer.adopt(listContainer);
        
// Positioning Strangeness - if the items will go beyond the borders of the page, then ir aligns to the right

            var mainCoords = main.getCoordinates();
            var adjustedLeft = hand.getCoordinates().left - mainCoords.left;
            var combinedColumnWidth = (listColWidth * listCount);
            
            if ( (adjustedLeft + combinedColumnWidth) > mainCoords.width) {
                listContainer.setStyle('padding-left', (mainCoords.width - combinedColumnWidth) + 'px');
            } else {
                listContainer.setStyle('padding-left', adjustedLeft + 'px');
            }
            hand.slideTrans = new Fx.Slide(listContainer, {duration: 500, wait: false, 'transition': Fx.Transitions.Cubic.easeOut});
            hand.addEvent('mouseenter', this.showItem.pass([hand], this));
            main.addEvent('mouseleave', this.reset.bind(this));
        }
        this.reset();
    },
    reset : function () {
// If there is no preselected item, then slide out, otherwise slide the presel in.
        if (this.defaultItem == null && this.activeItem != null) {
            this.activeItem.slideTrans.slideOut();
        } else {
            this.showItem(this.defaultItem);
        }
    },
    showItem : function (item) {
        for (var i=0, l=this.navItems.length; i<l; i++) {
            var ni = this.navItems[i];
            ni.slideTrans.stop();
            if (ni == item) {
                ni.slideTrans.slideIn();
                ni.addClass('selected');
                this.activeItem = ni;
            } else {
                ni.slideTrans.hide();
                ni.removeClass('selected');
            }
        }    
    }
};