
(function($) {

            $.fn.fader = function(options) {
                  var defaults = {  
                        duration: 3000,  
                        fade: 'slow',  
                        pause: 8000,  
                        menu_id: '#section_menu',
                        select_class: 'section_selected',
                        unique_id_text: 'section'
                  };  

                  var options = $.extend(defaults, options);
                                   
                  return this.each(function() {   
                        var $obj = $(this);
                        var $sections = $obj.children();
                        var $ul = jQuery('<ul/>');
                        var _timer = null;
                        var _uniqueCounter = 0;

                        $sections.each(function (i) {
                              var $section= $(this);

                              var uniqueID = generateUniqueID();
                              $section.attr('id',uniqueID);

                              var $li = jQuery('<li/>', {
                                    text: $section.find('h1').text(),
                                    ref: uniqueID,
                                    click: function(){
                                          if ($obj.children().length > 1) {
                                                clearTimeout(_timer);
                                                _timer = setTimeout(function() {next()},options.pause);
                                                var id = $(this).attr('ref');
                                                var select = '#'+id+' ~ div';
                                                $(select).prependTo($obj).show();
                                                next_menu(id);
                                          }
                                    }
                              }); 
                              $ul.prepend($li);
                        });
                        $ul.appendTo(options.menu_id);

                        if ($obj.children().length > 1) {
                              next(); 
                              next_menu($obj.children().last().attr("id"));
                        }

                        function next() {  
                              clearTimeout(_timer);
                              _timer = setTimeout(function() {
                                    $sections = $obj.children();
                                    $sections.last().fadeOut(options.fade, function() {
                                          // Animation complete.
                                          $(this).prependTo($obj).show();
                                          next();
                                          next_menu($obj.children().last().attr("id"));                             
                                    });
                              },options.duration);
                        }

                        function next_menu(ref) {
                              var $menu_li = $(options.menu_id+ " ul li");
                              $menu_li.each(function() {
                                          $(this).removeClass(options.select_class);
                              });
                              $(options.menu_id+" li[ref="+ ref +"]").addClass(options.select_class);
                        }

                        function generateUniqueID() { 
                              _uniqueCounter++;
                              return options.unique_id_text + '_' + _uniqueCounter.toString(16); 
                        }

                  }); //return $ object end
            }; // fn end

})(jQuery);
