
function Carousel(named_args) {
  var wrapper = $(named_args.wrapper);
  var contents = $(named_args.contents);
  var tabs = $(named_args.tabs);
  var activeTabClass = named_args.activeTabClass || 'activeTab';
  var fade_speed = named_args.fade_speed || 400;
  var switch_tab_events = named_args.switch_tab_events || ['click'];
  var slideshow_frame_duration = named_args.slideshow_frame_duration || 10000;
  var use_slideshow = named_args.use_slideshow || false;

  var open_tab = named_args.open_tab || 0;
  var slideshow_paused = false;
  var slideshow_timeout = null;
  
  tabs.each(function(index){
    this.tab_index = index;
  });
  
  function switch_tab(index){
    if (typeof(index) != 'number') { index = this.tab_index; }
    var tab = tabs[index];
    var content = contents[index];
    $(tabs).removeClass(activeTabClass);
    contents.filter(function(){return this != content}).fadeOut(fade_speed);
    $(tab).addClass(activeTabClass);
    $(content).fadeIn(fade_speed);
    open_tab = index;
    return false;
  };
  
  function start_slide_show(is_first){
    if (!slideshow_paused && is_first !== true) {
      if (open_tab+1 >= tabs.length) { open_tab = 0;}
      else {open_tab ++;}
      switch_tab(open_tab);
    }
    slideshow_timeout = window.setTimeout(start_slide_show, slideshow_frame_duration);
  };
  
  function stop_slide_show(){
    window.clearTimeout(slideshow_timeout);
  };
  
  switch_tab(open_tab);
  
  for (var i=0; i<switch_tab_events.length; i++){
    tabs.bind(switch_tab_events[i], switch_tab);
  }
  wrapper.bind('mouseenter', function(){
    slideshow_paused = true;
  });
  wrapper.bind('mouseleave', function(){
    slideshow_paused = false;
  });    
  
  if (use_slideshow) {
    start_slide_show(true);
  }

  this.start_slide_show = start_slide_show;
  this.stop_slide_show = stop_slide_show;
  this.switch_tab = switch_tab;
}