var $$ = $.fn;

$$.extend({
  SplitID : function()
  {
    return this.attr('id').split('-').pop();
  },

  Slideshow : {
    Ready : function()
    {
	  $('#rttr ul.items li:not(#slide-overlay)').hide();
      $('#rttr ul.nav li')
        .hover(
          function() {
            $(this).addClass('hover');
          },
          function() {
            $(this).removeClass('hover');
          }
        )
        .click(
          function() {
            $$.Slideshow.Interrupted = true;

            $('#rttr ul.items li').hide();
            $('#rttr ul.nav li').removeClass('on');

            $('#rttr li#slide-' + $(this).SplitID()).show();
            $(this).addClass('on');
          }
        );

      this.Counter = 1;
      this.Interrupted = false;

      this.Transition();
    },

    Transition : function()
    {
      if (this.Interrupted) {
        return;
      }

      this.Last = this.Counter - 1;

      if (this.Last < 1) {
        this.Last = 5;
      }

      $('li#slide-' + this.Last).fadeOut(
        'slow',
        function() {
        
          // show overlay
          $('li#slide-overlay').show();
		  
		  // change items
          $('li#nav-' + $$.Slideshow.Last).removeClass('on');
          $('li#nav-' + $$.Slideshow.Counter).addClass('on');
          $('li#slide-' + $$.Slideshow.Counter).show();
          
          // fade out overlay to reveal new item
          $('li#slide-overlay').fadeOut("slow");

          $$.Slideshow.Counter++;

          if ($$.Slideshow.Counter > 5) {
            $$.Slideshow.Counter = 1;
          }


          setTimeout('$$.Slideshow.Transition();', 5000);
        }
      );
    }
  }
});

$(document).ready(function(){
  
	// genterate tabs
	function tabsFn(parent, head){
		//create a ul container for the tabs
		$(parent).prepend("<ul class=\"nav\"><\/ul>");
		//create an array for the ids
		tabsArray=new Array();
		$(parent + " " + head).each(function(){
			thisIndex = $(parent + " " + head).index(this) + 1;
			$(parent + " ul.nav").append('<li id="nav-' + thisIndex + '"><a href="#' + tabsArray[thisIndex] + '">' + $(this).text() + '<\/a><\/li>');
		});

	}
	tabsFn("#rttr","h2");
	
	// run slideshow
    $$.Slideshow.Ready();

});