/* $Id: jquery.flaps.js 545 2009-11-24 13:55:22Z christofh $ */

/* jQuery flaps plugin
 *
 * change history
 * 		version 1.5
 * 			added option flapHeadSelector  
 * 		version 1.4 
 * 			added closable setting
 * 			added callback after complete
 * 		version 1.3 
 * 			added option event
 * 		version 1.2 
 * 			added options "durationUp/Down" and "easingUp/Down" 
 *
 * HTML example::
 *
 *   <dl class="flaps">
 *     <dt class="flap-open">flapheader</dt>
 *     <dd>flapcontent</dd>
 *     <dt id="custom-ref">flapheader</dt>
 *     <dd>flapcontent</dd>
 *   </dl>
 *
 * JS example::
 *
 *   $('dl.flaps').flaps()
 *
 * An open flap dt gets a class="flap-open", which may be set beforehand too
 *
 * Each flap dt gets an ``id="flap-i-j"`` where ``i`` is the number
 * of the flap container (the <dl>) and ``j`` the position of a single flap (<dt>) in a flap containter.
 * If a flap already has a custom id attribute it is preserved instead.
 *
 * If the URL hash matches a flap's id this flap is opened on load
 *
 * options
 * -------
 * accordion = false
 *      if true any open flap will be closed before a new one is opened
 * closable = false
 * 		if true opened flap could be closed, even if accordion is true
 * durationUp = 300, durationDown = 300
 * 		duration of slide animation, int or "slow" | "normal" | "fast"
 * flapHeadSelector = 'dt'
 * 		selector of flaps, needs flap content to be the next element
 * easingUp = 'linear', easingDown = 'linear'
 * 		an easing type, see http://gsgd.co.uk/sandbox/jquery/easing/ which 
 * 		types are available
 * event = 'click'
 * 		defines the event for opening the flap
 *
 */
;
(function($){
    $.fn.flaps = function(options){
        var settings = jQuery.extend({
            accordion: false,
			closable: false,
			durationDown: 300,
			easingDown: 'linear',
			durationUp: 300,
			easingUp: 'linear',
			event: 'click',
			flapHeadSelector: 'dt',
			callback:function(){}
        }, options);

        return $(this).each(function(i){
            var openhash = document.location.hash
            var m = $(this);
            m.find(settings.flapHeadSelector).each(function(j){
                var dt = $(this)
				var dd = dt.next('dd')
                if (!dt.attr('id')) {
                    dt.attr('id', 'flap-' + i + '-' + j)
                }
                if (openhash == '#' + dt.attr('id')) {
                    dt.addClass('flap-open');
                    dd.show()
                }
                if (!dt.hasClass('flap-open')) {
                    dd.hide()
                }
                else {
                    dd.show()
                }
                dt[settings.event](function(){
                    if (!(settings.accordion && ($(this).hasClass('flap-open') || false )) || settings.closable) {
                        if (settings.accordion) {
                            $('dt.flap-open', m).removeClass('flap-open').next().slideUp()
                        }
						if (dd.css('display') != 'none') {
	                        dd.slideUp({
								duration: settings.durationUp,
								easing: settings.easingUp,
								complete: function(){ dt.removeClass('flap-open') }
							})
						}
						else {
							dt.addClass('flap-open')
	                        dd.slideDown({
								duration: settings.durationDown,
								easing: settings.easingDown,
								complete: function(){ settings.callback() }
							})
						}
                    }
                })
            })
        })
    }
	$.fn.flaps.VERSION = '1.5'
})(jQuery)

