// JavaScript Document
var myAccordion;
var folderDisplay = -1;
//Global Declorations 

//Add Event on load
window.addEvent('domready', function() {
    //var menu = new DropMenu('menu');

    $('navmenu').getChildren().each(function(e, i, a) {

        if (e.getChildren('ul').length > 0) {
            e.getFirst('ul').setStyle('z-index', '50');
        }

        e.addEvent('mouseenter', function() {
            if (e.getChildren().length > 1) {

                e.getFirst('ul').setStyle('display', 'block');
            }
        });
        
        e.addEvent('mouseleave', function() {
            if (e.getChildren().length > 1) {
                e.getFirst('ul').setStyle('display', 'none');
            }
        });
    });
});

/*
Script: dropMenu.js
Drop menu going Nth levels

License:
MIT-style license.

Author:
Copyright (c) 2008 Chris Esler, <http://www.chrisesler.com/mootools>

*/
// Also known as IE fix
var TridentFix = new Class({
    tridentFix: function(item) {
        item.addEvents({
            'mouseover': function() {
                this.addClass('iehover');
            },
            'mouseout': function() {
                this.removeClass('iehover');
            }
        });
    }
});


var DropMenu = new Class({
    Implements: [Options, TridentFix],
    /*
    don't know about options yet
    but set it up anyways just in case
    */
    options: {
        mode: 'horizontal'
    },
    menu: null,
    initialize: function(menu, options) {
        if (options) this.setOptions(options);

        this.menu = $(menu);

        // grab all of the menus children - LI's in this case
        if (this.menu.length > 0) {
            var children = this.menu.getChildren();

            // loop through children
            children.each(function(item, index) {
                // declare some variables
                var fChild, list;

                /*
                fChild = first child - which should be an A tag
                list = submenu UL
                */
                fChild = item.getFirst();
                list = fChild.getNext('ul');

                // check if IE, if so apply fix
                if (Browser.Engine.trident) this.tridentFix(item);

                // if there is a sub menu UL
                if (list) {
                    item.mel = list; // pel = parent element
                    list.pel = item; // mel = menu element
                    new SubMenu(list); // hook up the subMenu
                }
            }, this); // binding loop to this object for trident fix
        }
    }
});



var SubMenu = new Class({
    Implements: [Options, TridentFix],
    /*
    don't know about options yet
    but set it up anyways just in case
    */
    options: {
        mode: 'vertical'
    },
    menu: null, // storage for menu object
    depth: 0, // storage for current menu depth
    initialize: function(el, depth, options) {
        if (options) this.setOptions(options); // set options
        if (depth) this.depth = depth; // set depth

        this.menu = el; //attach menu to object

        if (this.depth == 0) this.menu.addClass('submenu'); // class for first level
        if (this.depth >= 1) this.menu.addClass('sub_submenu'); // class for deeper levels - in case :P

        this.menu.fade('hide'); // set menu to hid

        /*
        hook up menu's parent with event
        to trigger menu
        */
        this.menu.pel.addEvents(this.parentEvents);

        // get menu's child elements
        var children = this.menu.getChildren();

        // loop through children
        children.each(function(item, index) {
            // declare some variables
            var fChild, list;

            /*
            fChild = first child - which should be an A tag
            list = submenu UL
            */
            fChild = item.getFirst();
            list = fChild.getNext('ul');

            // check if IE, if so apply fix
            if (Browser.Engine.trident) this.tridentFix(item);

            // if the menu item has a sub_submenu
            if (list) {
                /*
                create marker for menu item
                that has a sub_submenu
                this is to show persistence and
                where you are in the menu tree
                */
                var count = new Element('span').set('html', '\&raquo;').addClass('counter');

                item.adopt(count); // stuff it inside li
                count.fade('hide'); // hide it

                item.mel = list; // mel = menu element
                item.count = count; // attach count accessor to menu item
                list.pel = item; // pel = parent element

                // create new subMenu with depth incremented
                new SubMenu(list, this.depth + 1);
            }
        }, this); //bound to this for trident fix
    },
    // menu parent mouse events
    parentEvents: {
        'mouseover': function() {
            /*
            if it has a count accesor
            then fade it in
            */
            if (this.count) this.count.fade('in');

            // fade in menu
            this.mel.fade('in');
        },
        'mouseout': function() {
            alert('dfgdfg');

            /*
            if it has a count accesor
            then fade it out
            */
            if (this.count) this.count.fade('out');

            // fade out menu
            this.mel.fade('out');
        }
    }
});


//Dougs Special non-Click Accordian

var DougsAccordion = new Class({

    Extends: Fx.Elements,

    options: {
        /*
        onActive: $empty,
        onBackground: $empty,
        onNone: $empty
        */
        display: 0,
        show: false,
        height: true,
        width: false,
        opacity: true,
        fixedHeight: false,
        fixedWidth: false,
        wait: false,
        alwaysHide: false,
        selectedIndex: -1,
        containerHeight: false
    },

    initialize: function() {
        var params = Array.link(arguments, { 'container': Element.type, 'options': Object.type, 'togglers': $defined, 'elements': $defined });
        this.parent(params.elements, params.options);
        this.togglers = $$(params.togglers);
        this.container = $(params.container);
        this.previous = -2;
        if (this.options.alwaysHide) this.options.wait = true;

        if ($chk(this.options.show)) {
            this.options.display = false;
            this.previous = this.options.show;
        }
        if (this.options.start) {
            this.options.display = false;
            this.options.show = false;
        }

        this.effects = {};
        this.container.addEvent('mouseleave', function() {
            if ($chk(myAccordion.options.display)) {
                myAccordion.display(myAccordion.options.display);
            }
            else {
                myAccordion.display(-1);
            }
        });

        if (this.options.opacity) this.effects.opacity = 'fullOpacity';
        if (this.options.width) this.effects.width = this.options.fixedWidth ? 'fullWidth' : 'offsetWidth';
        if (this.options.height) this.effects.height = this.options.fixedHeight ? 'fullHeight' : 'scrollHeight';
        //if (this.options.nonSelectedHeight) this.effects.height = this.options.nonSelectedHeight ? 'fullHeight' : 'Height';
        for (var i = 0, l = this.togglers.length; i < l; i++) {
            this.addSection(this.togglers[i], this.elements[i]);
        }
        this.elements.each(function(el, i) {
            if (this.options.show === i) {
                this.fireEvent('active', [this.togglers[i], el]);
            } else {
                for (var fx in this.effects) el.setStyle(fx, 0);
            }
        }, this);
        //if ($chk(this.options.display)) 
        this.display(this.options.display);
    },

    addSection: function(toggler, element, pos) {
        toggler = $(toggler);
        element = $(element);
        var test = this.togglers.contains(toggler);
        var len = this.togglers.length;
        this.togglers.include(toggler);
        this.elements.include(element);

        if (len && (!test || pos)) {
            pos = $pick(pos, len - 1);
            toggler.inject(this.togglers[pos], 'before');
            element.inject(toggler, 'after');
        } else if (this.container && !test) {
            toggler.inject(this.container);
            element.inject(this.container);
        }

        var idx = this.togglers.indexOf(toggler);
        toggler.addEvent('mouseover', this.display.bind(this, idx));
        if (this.options.height) element.setStyles({ 'padding-top': 0, 'border-top': 'none', 'padding-bottom': 0, 'border-bottom': 'none' });
        if (this.options.width) element.setStyles({ 'padding-left': 0, 'border-left': 'none', 'padding-right': 0, 'border-right': 'none' });
        element.fullOpacity = 1;
        if (this.options.fixedWidth) element.fullWidth = this.options.fixedWidth;
        if (this.options.fixedHeight) element.fullHeight = this.options.fixedHeight;
        element.setStyle('overflow', 'hidden');
        if (!test) {
            for (var fx in this.effects) element.setStyle(fx, 0);
        }
        return this;
    },

    display: function(index) {
        //alert(index);
        this.selectedIndex = index;
        index = ($type(index) == 'element') ? this.elements.indexOf(index) : index;
        if ((this.timer && this.options.wait) || (index === this.previous && !this.options.alwaysHide)) return this;
        this.previous = index;
        var obj = {};
        //alert(index);

        if (this.selectedIndex == -1) {
            //Animate to non selecte state
            //alert(this.options.containerHeight + " / " + this.togglers.length + " == " + height);
            this.togglers.each(function(element, index, array) {
                //var TogglerHeight = parseInt(this.options.containerHeight / array.length);
                //alert(this.containerHeight);
                var e = element.getFirst("a");
                e.morph('.noMooBoxElementSelected');
            });
        }
        else {
            this.togglers.each(function(element, index, array) {
                //var TogglerHeight = parseInt(this.options.containerHeight / array.length);
                //alert(this.containerHeight);
                var e = element.getFirst("a");
                e.morph('.MooBoxElementSelected');

            });
        }


        this.elements.each(function(el, i) {
            obj[i] = {};
            var hide = (i != index) || (this.options.alwaysHide && (el.offsetHeight > 0));
            for (var fx in this.effects) obj[i][fx] = hide ? 0 : el[this.effects[fx]];
        }, this);

        return this.start(obj);
    }
});