(function($){
  $.fn.advertRotator = function(settings) {
    var config = { IsNavigable : true, IsRotating : true, RotateSpeed : 8 }
    if (settings) $.extend(config,settings);
    this.append( $('<div>',{id:'bottomNav'}) );
    var $adverts = this.find('a'),
        $bottomNav = this.find('#bottomNav'),
	 rotateInterval;
    
    if (config.IsNavigable) {
      $adverts.each(function(idx){
        $bottomNav.append(
          $('<a>',{href:'javascript:void(0)',rel:idx,'class':(idx==0)?'selected':''}).html(idx+1)
        );
      });
    }
    
    if (config.IsRotating) {
      function AdvertRotator(){
        var current = $adverts.filter(':visible'),
            index   = $adverts.index(current),
            next    = (index+1==$adverts.length)?$adverts.get(0):$adverts.get(index+1);
        current.hide();
        $(next).fadeIn('fast');
	 $bottomNav.find('a').removeAttr('class').filter(':eq('+($adverts.index(next))+')').addClass('selected');
      }
      rotateInterval = setInterval(AdvertRotator,config.RotateSpeed*1000);
    }
    
    $bottomNav.find('a').live('click',function(){
      $adverts.filter(':visible').hide();
      $( $adverts.get(this.rel) ).fadeIn('fast');
      $bottomNav.find('a').removeAttr('class');
      $(this).addClass('selected');
      clearInterval(rotateInterval);
      return false;
    });
    
    $adverts.first().show();
    return this;
  }
})(jQuery)
