/**
 * JQuery Plugin: "frozAccordion"
 * by: Jerome Coloma (http://www.frozynart.com/)
 *
 * Copyright (c) 2009 Frozynart Designs
 * Licensed under MIT (http://www.frozynart.com/license/mit)
 *
 * Description: Accordion
 * Dependencies: jQuery 1.2.6 and above
 * Usage Example: jQuery('#menu1').frozAccordion();
 *
 * Version: 1.3, 2010-04-14 Bug fixes on Level 4 folding
 *          1.2, 2010-03-31 Added support for level 4 and fixed some bugs on folding
 *          1.1, 2009-06-26 Added support for level 3 and fixed some bugs on folding
 *          1.0, 2009-04-02
 */

(function($) {

    $.fn.frozAccordion = function(options) {

        var opts = $.extend({}, $.fn.frozAccordion.defaults, options);
        
        var rootNode = jQuery(this);
        
        rootNode.data('isRoot', true);
        
        jQuery( '> li > a', rootNode ).find( '+ ul' ).hide();//slideUp( {duration:opts.closeDuration, easing:opts.closeEasing} );
        
        // 2nd level
        jQuery( '> li > a + ul > li > a', rootNode ).find( '+ ul' ).hide();
        
        // 3rd level
        jQuery( '> li li > a + ul > li > a', rootNode ).find( '+ ul' ).hide();
        
        jQuery( 'li > a', rootNode ).filter(
            function(index) {

                var currentPage   = unescape(location.pathname);
                var href          = jQuery( this ).attr('href');

                if( href == currentPage ) {
                    
                    jQuery( this ).addClass( opts.selectedClassName ).parent('li').addClass(opts.selectedClassName);                    
                    
                    
                    if(jQuery( this ).parent().parent().parent().parent().parent().parent().parent().parent().data('isRoot')) {
//                      console.log('Root level 4');
                        
                        jQuery( this ).parent().parent().parent().parent().slideToggle( {duration:opts.closeDuration, easing:opts.closeEasing} );
                        jQuery( this ).parent().parent().parent().parent().parent().parent().slideToggle( {duration:(opts.closeDuration/2), easing:opts.closeEasing} );
                        

                        // works but under revision
                        jQuery( this ).parent().parent().parent().parent().siblings('a').addClass(opts.selectedClassName);
                        jQuery( this ).parent().parent().parent().parent().parent().parent().siblings('a').addClass(opts.selectedClassName);
                        
                        
                        // jQuery( this ).parent().parent().slideToggle( {duration:opts.closeDuration, easing:opts.closeEasing} );
                        // jQuery( this ).parent().parent().parent().parent().slideToggle( {duration:(opts.closeDuration/2), easing:opts.closeEasing} );
                        
                        // jQuery( this ).parent().parent().siblings('a').addClass(opts.selectedClassName);
                        // jQuery( this ).parent().parent().parent().parent().siblings('a').addClass(opts.selectedClassName);
                        
                    } else if(jQuery( this ).parent().parent().parent().parent().parent().parent().data('isRoot')) {
//                      console.log('Root level 3');
                        
                        jQuery( this ).parent().parent().slideToggle( {duration:opts.closeDuration, easing:opts.closeEasing} );
                        jQuery( this ).parent().parent().parent().parent().slideToggle( {duration:(opts.closeDuration/2), easing:opts.closeEasing} );
                        
                        // works but under revision
                        jQuery( this ).parent().parent().siblings('a').addClass(opts.selectedClassName);
                        jQuery( this ).parent().parent().parent().parent().siblings('a').addClass(opts.selectedClassName);
                        
                        
                        
                    } else if(jQuery( this ).parent().parent().parent().parent().data('isRoot')) {
//                      console.log('Root level 2');
                        
                        jQuery( this ).parent().parent().siblings('a').addClass(opts.selectedClassName);
                        jQuery( this ).parent().parent().slideToggle( {duration:opts.closeDuration, easing:opts.closeEasing} );
                        
                    } else if(jQuery( this ).parent().parent().data('isRoot')) {
                        //console.log('Root level 1');
                        
                    }
    
                    
                    
                    
    
                }
            }
        ); 

        var closeAllMenu = function( ) {
            jQuery( '> li > a', rootNode ).find( '+ ul' ).slideUp( {duration:opts.closeDuration, easing:opts.closeEasing} );
        };
        
        
        var firstLevel = jQuery( '> li > a', rootNode ).click(
            function() {
                 // console.log('1st Level');
                 // console.log(jQuery(this));
                 if(jQuery(this).hasClass(opts.selectedClassName)) {
                     jQuery(this).removeClass(opts.selectedClassName);
                 } else {
                     jQuery(this).addClass(opts.selectedClassName);
                 }
                 
                
                if(true === opts.autoClose) {
                    closeAllMenu( );
                }
                jQuery( this ).find( '+ ul' ).slideToggle( {duration:opts.openDuration, easing:opts.openEasing} );
                
            }
        );
        
        
        var secondLevel = jQuery( '+ ul', firstLevel ).find('> li > a').click(
            function() {
                
                // console.log('2nd Level');
                // console.log(jQuery(this));
                if(jQuery(this).hasClass(opts.selectedClassName)) {
                    jQuery(this).removeClass(opts.selectedClassName);
                } else {
                    jQuery(this).addClass(opts.selectedClassName);
                }
                
                
                if(true === opts.autoClose) {
                    // closeAllMenu( );
                }
                jQuery( this ).find( '+ ul' ).slideToggle( {duration:opts.openDuration, easing:opts.openEasing} );
                
            }
        );
        
        
        var thirdLevel = jQuery( '+ ul', secondLevel ).find('> li > a').click(
            function() {
                
                // console.log('3rd Level');
                // console.log(jQuery(this));
                
                if(jQuery(this).hasClass(opts.selectedClassName)) {
                    jQuery(this).removeClass(opts.selectedClassName);
                } else {
                    jQuery(this).addClass(opts.selectedClassName);
                }
                
                
                if(true === opts.autoClose) {
                    // closeAllMenu( );
                }
                jQuery( this ).find( '+ ul' ).slideToggle( {duration:opts.openDuration, easing:opts.openEasing} );
            }
        );
        
    
        
        return jQuery(this);
    };

    $.fn.frozAccordion.defaults = {
        autoClose: true,
        selectedClassName: 'selected',
        collapsedClassName: 'collapsed',
        openDuration: 300,
        closeDuration: 800,
        openEasing: 'swing',
        closeEasing: 'swing'
    };

})(jQuery);


