/**
 * @fileoverview frozAccordionRec plugin
 * @author Lawrence Natividad
 * @copyright Copyright ( c ) 2010 Frozynart Designs
 * @license Licensed under MIT ( http://www.frozynart.com/license/mit )
 * @version: 1.0 2010-04-28
 */
 
( function( $ ) {
    /**
     * all parameters
     * <ul> passes must have ID
     * all content banners must have the same dimensions
     * When thumbnails are clicked, contents slide over accordingly
     * @namespace frozAccordionRec Plugin Namespace
     * @requires jQuery 1.2.6 and above / jQuery Easing / jquery.color
     * @example jQuery( "#id-of-list-that-contains-contents" ).frozAccordionRec
     * @return {jQuery Object}
     */
    jQuery.fn.frozAccordionRec = function( options ) {
        var opts                            = jQuery.extend( {}, jQuery.fn.frozAccordionRec.defaults, options );
        jQuery.fn.frozAccordionRec.opts      = opts;
        var $this                           = jQuery( this );
        
        console.log($this);
        
        var init = function(){
            jQuery('ul', $this).hide();
            jQuery('li', $this).each( function(){
                if ( jQuery(this).parent().attr('id') !== $this.attr('id') ){
                    jQuery(this).addClass('sub');
                }
            })
        }
        
        var makeSelected = function(link){
            link.addClass(opts.selectedClassName);
            link.parent().addClass(opts.selectedClassName);
            if ( link.parent().parent().attr('id') !== $this.attr('id') ){
                makeSelected(link.parent().parent().prev('a'));
            }
        }
        
        var unSelectOthers = function(link){
            link.parent().siblings('li').each( function(){
                jQuery(this).removeClass(opts.selectedClassName);
                jQuery(this).children('a').removeClass(opts.selectedClassName);
                if ( jQuery(this).children('ul').length > 0 ){
                    unSelectOthers(jQuery(this).children('ul').children('li').children('a'));
                }
            })
        }
        
        var closeOthers = function(link){
        
        }
        
        var showContent = function(link){
        
            if ( opts.autoClose === true){
                unSelectOthers(link);
            }
        
            makeSelected(link);
            link.next('ul').slideDown({duration:opts.downDuration, easing:opts.easing}); 
              
            if ( opts.autoClose === true){
                closeOthers(link);
            }     
            
        }
        
        jQuery('li > a', $this).click( function(){
            if ( jQuery(this).parent().children('ul').length > 0){
                if ( jQuery(this).hasClass(opts.selectedClassName) ){
//                    hideContent(jQuery(this));
                } else {
                    showContent(jQuery(this));
                }
            }
        })
        
        init();
        
        return $this;
    };

    /** 
     * Sends a message to the console
     * @private
     */
    var _debug = function( message ) {
        if ( true === jQuery.fn.frozAccordionRec.opts.debug ) {
            console.log( message );
        }
    };
    
    jQuery.fn.frozAccordionRec.defaults =   {
        autoClose: true,
        selectedClassName: 'selected',
        downDuration: 500,
        easing: 'swing'
    };
} )(jQuery);

