// JavaScript Document
/*
2 * Accordion 1.3 - jQuery menu widget
3 *
4 * Copyright (c) 2006 Jörn Zaefferer, Frank Marcia
5 *
6 * Dual licensed under the MIT and GPL licenses:
7 * http://www.opensource.org/licenses/mit-license.php
8 * http://www.gnu.org/licenses/gpl.html
9 *
10 * Revision: $Id: jquery.accordion.js 1524 2007-03-13 20:09:19Z joern $
11 *
12 */

/**
15 * Make the selected elements Accordion widgets.
16 *
17 * Semantic requirements:
18 *
19 * If the structure of your container is flat with unique
20 * tags for header and content elements, eg. a definition list
21 * (dl > dt + dd), you don't have to specify any options at
22 * all.
23 *
24 * If your structure uses the same elements for header and
25 * content or uses some kind of nested structure, you have to
26 * specify the header elements, eg. via class, see the second example.
27 *
28 * Use activate(Number) to change the active content programmatically.
29 *
30 * A change event is triggered everytime the accordion changes. Apart from
31 * the event object, all arguments are jQuery objects.
32 * Arguments: event, newHeader, oldHeader, newContent, oldContent
33 *
34 * @example jQuery('#nav').Accordion();
35 * @before <dl id="nav">
36 * <dt>Header 1</dt>
37 * <dd>Content 1</dd>
38 * <dt>Header 2</dt>
39 * <dd>Content 2</dd>
40 * </dl>
41 * @desc Creates an Accordion from the given definition list
42 *
43 * @example jQuery('#nav').Accordion({
44 * header: 'div.title'
45 * });
46 * @before <div id="nav">
47 * <div>
48 * <div class="title">Header 1</div>
49 * <div>Content 1</div>
50 * </div>
51 * <div>
52 * <div class="title">Header 2</div>
53 * <div>Content 2</div>
54 * </div>
55 * </div>
56 * @desc Creates an Accordion from the given div structure
57 *
58 * @example jQuery('#nav').Accordion({
59 * header: 'a.head'
60 * });
61 * @before <ul id="nav">
62 * <li>
63 * <a class="head">Header 1</a>
64 * <ul>
65 * <li><a href="#">Link 1</a></li>
66 * <li><a href="#">Link 2></a></li>
67 * </ul>
68 * </li>
69 * <li>
70 * <a class="head">Header 2</a>
71 * <ul>
72 * <li><a href="#">Link 3</a></li>
73 * <li><a href="#">Link 4></a></li>
74 * </ul>
75 * </li>
76 * </ul>
77 * @desc Creates an Accordion from the given navigation list
78 *
79 * @example jQuery('#accordion').Accordion().change(function(event, newHeader, oldHeader, newContent, oldContent) {
80 * jQuery('#status').html(newHeader.text());
81 * });
82 * @desc Updates the element with id status with the text of the selected header every time the accordion changes
83 *
84 * @param Map options key/value pairs of optional settings.
85 * @option String|Element|jQuery|Boolean active Selector for the active element, default is the first child, set to false to display none at start
86 * @option String|Element|jQuery header Selector for the header element, eg. div.title, a.head, default is the first child's tagname
87 * @option String|Number showSpeed Speed for the slideIn, default is 'slow' (for numbers: smaller = faster)
88 * @option String|Number hideSpeed Speed for the slideOut, default is 'fast' (for numbers: smaller = faster)
89 * @option String selectedClass Class for active header elements, default is 'selected'
90 * @option Boolean alwaysOpen Whether there must be one content element open, default is true.
91 * @option Boolean animated Set to false to disable animations. Default: true
92 * @option String event The event on which to trigger the accordion, eg. "mouseover". Default: "click"
93 *
94 * @type jQuery
95 * @see activate(Number)
96 * @name Accordion
97 * @cat Plugins/Accordion
98 */

/**
101 * Activate a content part of the Accordion programmatically at the given zero-based index.
102 *
103 * If the index is not specified, it defaults to zero, if it is an invalid index, eg. a string,
104 * nothing happens.
105 *
106 * @example jQuery('#accordion').activate(1);
107 * @desc Activate the second content of the Accordion contained in <div id="accordion">.
108 *
109 * @example jQuery('#nav').activate();
110 * @desc Activate the first content of the Accordion contained in <ul id="nav">.
111 *
112 * @param Number index (optional) An Integer specifying the zero-based index of the content to be
113 * activated. Default: 0
114 *
115 * @type jQuery
116 * @name activate
117 * @cat Plugins/Accordion
118 */

/**
121 * Override the default settings of the Accordion. Affects only following plugin calls.
122 *
123 * @example jQuery.Accordion.setDefaults({
124 * showSpeed: 1000,
125 * hideSpeed: 150
126 * });
127 *
128 * @param Map options key/value pairs of optional settings, see Accordion() for details
129 *
130 * @type jQuery
131 * @name setDefaults
132 * @cat Plugins/Accordion
133 */

jQuery.fn.extend({
 // nextUntil is necessary, would be nice to have this in jQuery core
 nextUntil: function(expr) {
 var match = [];

 // We need to figure out which elements to push onto the array
 this.each(function(){
 // Traverse through the sibling nodes
 for( var i = this.nextSibling; i; i = i.nextSibling ) {
 // Make sure that we're only dealing with elements
 if ( i.nodeType != 1 ) continue;

 // If we find a match then we need to stop
 if ( jQuery.filter( expr, [i] ).r.length ) break;

 // Otherwise, add it on to the stack
 match.push( i );
 }
 });

 return this.pushStack( match );
 },
 // the plugin method itself
 Accordion: function(settings) {
 // setup configuration
 settings = jQuery.extend({}, jQuery.Accordion.defaults, {
 // define context defaults
 header: jQuery(':first-child', this)[0].tagName // take first childs tagName as header
 }, settings);

 // calculate active if not specified, using the first header
 var container = this,
 active = settings.active
 ? jQuery(settings.active, this)
 : settings.active === false
 ? jQuery("<div>")
 : jQuery(settings.header, this).eq(0),
 running = 0;

 container.find(settings.header)
 .not(active || "")
 .nextUntil(settings.header)
 .hide();
 active.addClass(settings.selectedClass);

 function clickHandler(event) {
 // get the click target
 var clicked = jQuery(event.target);

 // due to the event delegation model, we have to check if one
 // of the parent elements is our actual header, and find that
 if ( clicked.parents(settings.header).length ) {
 while ( !clicked.is(settings.header) ) {
 clicked = clicked.parent();
}
 }

 var clickedActive = clicked[0] == active[0];

 // if animations are still active, or the active header is the target, ignore click
 if(running || (settings.alwaysOpen && clickedActive) || !clicked.is(settings.header))
 return;

 // switch classes
 active.toggleClass(settings.selectedClass);
 if ( !clickedActive ) {
 clicked.addClass(settings.selectedClass);
 }

 // find elements to show and hide
var toShow = clicked.nextUntil(settings.header),
 toHide = active.nextUntil(settings.header),
 data = [clicked, active, toShow, toHide];
 active = clickedActive ? jQuery([]) : clicked;
 // count elements to animate
 running = toHide.size() + toShow.size();
 var finished = function(cancel) {
 running = cancel ? 0 : --running;
 if ( running )
 return;

 // trigger custom change event
 container.trigger("change", data);
 };
 // TODO if hideSpeed is set to zero, animations are crappy
 // workaround: use hide instead
 // solution: animate should check for speed of 0 and do something about it
 if ( settings.animated ) {
 if ( !settings.alwaysOpen && clickedActive ) {
 toShow.slideToggle(settings.showSpeed);
 finished(true);
 } else {
 toHide.filter(":hidden").each(finished).end().filter(":visible").slideUp(settings.hideSpeed, finished);
 toShow.slideDown(settings.showSpeed, finished);
 }
 } else {
 if ( !settings.alwaysOpen && clickedActive ) {
 toShow.toggle();
 } else {
 toHide.hide();
 toShow.show();
 }
 finished(true);
 }

 return false;
 };
 function activateHandlder(event, index) {
 // call clickHandler with custom event
 clickHandler({
 target: jQuery(settings.header, this)[index]
 });
 };

 return container
 .bind(settings.event, clickHandler)
 .bind("activate", activateHandlder);
 },
 // programmatic triggering
 activate: function(index) {
 return this.trigger('activate', [index || 0]);
 }
});

jQuery.Accordion = {};
jQuery.extend(jQuery.Accordion, {
 defaults: {
 selectedClass: "selected",
 showSpeed: 'slow',
 hideSpeed: 'fast',
 alwaysOpen: true,
 animated: true,
 event: "click"
 },
 setDefaults: function(settings) {
 jQuery.extend(jQuery.Accordion.defaults, settings);
 }
});
