//var $$ = $.fn;
var $$ = jQuery.fn;

$$.extend({
  SplitID : function()
  {
    return this.attr('id').split('-').pop();
  },

  Slideshow : {
    Ready : function()
    {
	  jQuery('#rttr ul.items li:not(#slide-overlay)').hide();
      jQuery('#rttr ul.nav li')
        .hover(
          function() {
            jQuery(this).addClass('hover');
          },
          function() {
            jQuery(this).removeClass('hover');
          }
        )
        .click(
          function() {
            $$.Slideshow.Interrupted = true;

            jQuery('#rttr ul.items li').hide();
            jQuery('#rttr ul.nav li').removeClass('on');

            jQuery('#rttr li#slide-' + jQuery(this).SplitID()).show();
            jQuery(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 = jQuery('li.tmpSlide').length;
      }

      jQuery('li#slide-' + this.Last).fadeOut(
        'slow',
        function() {
        
          // show overlay
          jQuery('li#slide-overlay').show();
		  
		  // change items
          jQuery('li#nav-' + $$.Slideshow.Last).removeClass('on');
          jQuery('li#nav-' + $$.Slideshow.Counter).addClass('on');
          jQuery('li#slide-' + $$.Slideshow.Counter).show();
          
          // fade out overlay to reveal new item
          jQuery('li#slide-overlay').fadeOut("slow");

          $$.Slideshow.Counter++;

          if ($$.Slideshow.Counter > jQuery('li.tmpSlide').length) {
            $$.Slideshow.Counter = 1;
          }


          setTimeout('$$.Slideshow.Transition();', 5000);
        }
      );
    }
  }
});

jQuery(document).ready(function($) {
//$(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();

});