/*!
 * jQuery JavaScript accordion Plugin v1.0
 *
 * Copyright 2011, Stephan Schröter
 *
 */

(function( $ ){

    var methods = {
        
        init : function( options ) {
            
            var settings = {
                showFirstItem: true,
                animationSpeed: 600
            }
            
            return this.each(function(){
                
                if ( options ) {
                    $.extend( settings, options );
                }
                
                var obj = $(this);
                
                obj.accordion('hideAccordionContent', settings);
                obj.accordion('showFirstItem', settings);
                obj.accordion('handleAccordionHead', settings);
                
                
            });
            
        },
        
        
        // showFirstItem
        showFirstItem : function( settings ) {
            
            return this.each(function(){
                
                if (settings.showFirstItem) {
                
                    var obj = $(this);
                    
                    obj
                        .children('.accordion-head:first')
                        .toggleClass('active')
                        .next('.accordion-content')
                        .show()
                    ;
                    
                }
                
            });
            
        },
        
        // handleAccordionHead
        handleAccordionHead : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                obj
                    .children('.accordion-head')
                    .click(function(){
                        
                        if ( !$(this).next('.accordion-content').is(':animated') ) {
                        
                            $(this)
                                .toggleClass('active')
                            ;
                            
                            $(this)
                                .next('.accordion-content')
                                .slideToggle(settings.animationSpeed)
                            ;
                        
                        }
                        
                        return false;
                            
                    })
                ;
                
            });
            
        },
        
        // hideAccordionContent
        hideAccordionContent : function( settings ) {
            
            return this.each(function(){
                
                var obj = $(this);
                
                obj
                    .children('.accordion-head')
                    .next('.accordion-content')
                    .hide()
                ;
                
            });
            
        }
        
    };

    $.fn.accordion = function( method ) {
    
        if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.accordion' );
        }
    
    };

})( jQuery );
