/**
 * Gets all the ancestors of an element.
 *
 * Accepts an element object or id and walks the DOM
 * to retrieve all the element's ancestors.
 *
 * @method getAncestors
 * @static
 * @param {Mixed} An element or the id of an element
 * @return {Mixed} Returns an array of elements or false.
 */

YAHOO.lang.augmentObject(YAHOO.util.Dom, {

    getAncestors: function(element) {
        var element = $D.get(element);
        var elements = [];

        if (!element) return false;

        while (element = element.parentNode) {
            if (element.nodeType === 1) {
                elements.push(element);
            }
        }

        return elements;
    }
});

/**
 * Utility for toggling events
 *
 * Takes a trigger element, affected element(s),
 * affecting event, and an object with three methods
 * that control the load, activated, and deactivated
 * states.
 *
 * @method toggle
 * @static
 * @param {Mixed} An element or the id of an element
 * @param {Mixed} An array of elements or element id's
 * @param {String} An event handler i.e. (click, change, etc.)
 * @param {Bool} A Boolean value
 * @param {Object} An object with three methods load, onActivate, onDeactivate
 * @return {Bool}
 */
YAHOO.lang.augmentObject(YAHOO.util.Event, {
    toggle: function(toggler, toggledElements, toggleEvent, preventDefaultBehavior, toggleBehavior)
    {       
        // Toggler element - accepts the id of the toggler element or the element itself
        var toggler = $D.get(toggler);

        // Toggled elements - will accept an element's id, array of ids, single element, or collection of elements
        var toggledElementsCollection = new Array();
        $D.batch(toggledElements, function(el) {
            if($D.get(el)) {
                toggledElementsCollection.push($D.get(el));
            }
        });

        // The default toggling behavior - adding or removing a class of
        // 'active' from toggled elements
        if(!toggleBehavior) {
            var toggleBehavior = {
                load: function(toggler, togglee) {                
                    $D.removeClass(toggler, 'active');
                    $D.setStyle(togglee, 'display', 'none');
                },
                onActivate: function(toggler, togglee) {
                    $D.addClass(toggler, 'activated');
                    $D.setStyle(togglee, 'display', 'block');
                },
                onDeactivate: function(toggler, togglee) {
                    $D.removeClass(toggler, 'activated');
                    $D.setStyle(togglee, 'display', 'none');            
                }
            }           
        }
        
        // Attach loading events
        $E.onDOMReady(function() {
            $D.batch(toggledElementsCollection, function(el){
                toggleBehavior.load(toggler, el);
            });
        });

        // Attach activate and deactivate events
        $E.on(toggler, toggleEvent, function(e) {
            // Stop the event
            if(preventDefaultBehavior) {
                $E.preventDefault(e);
            } 
            
            var target = $E.getTarget(e);

            // Check the toggle state and determine what method to run and change state
            if(!$D.hasClass(target, 'activated')) {
                $D.batch(toggledElementsCollection, function(el){ toggleBehavior.onActivate(target, el); });                
            } else {
                $D.batch(toggledElementsCollection, function(el){ toggleBehavior.onDeactivate(target, el); });
            }
        });
    }
});