/**
 * FlexibleAccordion - jQuery plugin for intuitively opening accordions and menus
 * http://wwww.joomlation.com
 * Copyright (c) 2009 Laurent BELLOEIL
 * Licensed under GPL license:
 * http://www.gnu.org/licenses/gpl.html
 * * You must not remove or change the copyright above !*
 * * If you make any change to the code, please indicated who tou are, when and where you did, like this :
 * LINE  |	changed on	|	By
 * 00       12/25/2009     nobody
 *
 * Requires jQuery 1.2.1 or higher
 * Version: 0.1.0
 *
 * Usage:
 * jQuery('#myUL').flexAccordion();
 * or
 * jQuery('.oneAccordin').flexAccordion({
 *  speed: 'fast', 			// see jQuery slideDown function for available options
 *  activeitem: 'none', 	// 'none' to close all sliders or specify the index of the slider you want to open (0,1,2...)
 *  showheaders: true, 		// Specify if you want to show a slide header (span)
 *  headerEl: 'span', 		// Specify the element to be the header of each slide
 *  contentEl: 'div,			// Specify the element to be the container of each slide
 *  targetdelimiter: ' ', 	// Specify the digit to separate the target slide index in custom buttons
 * });
 */
jQuery.fn.flexAccordion = function(options){
        // Setup options
        options = jQuery.extend({
            speed: 'fast',
            activeitem: 'none',
            showheaders: true,
            headerEl: 'span',
            contentEl: 'div',
            targetdelimiter: ' '
        }, options);

        // Current hover status
        var thisSlide = jQuery(this);
        var parts = thisSlide.children('li.accPart');

        // Switch to an specific slide based on its index (from 0) and hide the others
        function activate(item){
		jQuery(parts).each( function(i){
			if( item == i )
			{
				jQuery(this).children(options.contentEl+'.accContent').slideDown( options.speed );
			jQuery(this).addClass('active')
			}else{
				jQuery(this).children(options.contentEl+'.accContent').slideUp( options.speed );
			jQuery(this).removeClass('active')
			}
		});
	}
	// hide (slideUp) all sliders
	function hidesParts(){
		jQuery(thisSlide).find(options.contentEl+'.accContent').each( function(){
			jQuery(this).hide( );
		});
		jQuery(thisSlide).children('li.accPart').each( function(){
			jQuery(this).removeClass('active');
		});
	}
	
	// main section : trigger click event if headers are shown
	jQuery(parts).each( function(i){
		var header = jQuery(this).children(options.headerEl+'.accHeader');
		if( options.showheaders )
		{
			header.click( function(){
			activate(i);
			});
		}else{
			header.hide();
		}
	});

	// trigger click event for specific buttons
        jQuery(this).find("input[title='next']").click(function(){
            var content = jQuery(this).attr('name');
				var target = content.substring( content.lastIndexOf( options.targetdelimiter, content.length ) );
            activate(target);
        });
	if( options.activeitem == 'none' ){
		hidesParts();
	}else{
		activate(options.activeitem);
	}
        return this;
}

