var $$ = $.fn;

$$.extend({
  SplitID : function()
  {
    return this.attr('id').split('-').pop();
  },

  Slideshow : {
    Ready : function()
    {
	  $('#rttr ul.items li:not(#slide-1)').hide();
	  $('#rttr ul.items li')
        .hover(
          function() {
            $$.Slideshow.Interrupted = true;
          },
          function() {
            $$.Slideshow.Interrupted = false;
         	 //setTimeout('$$.Slideshow.Transition();', 5000);
          }
        );
      $('#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()).fadeIn();
            $(this).addClass('on');
			return false;
          }
        );

      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 = 4;
      }

      $('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 > 4) {
            $$.Slideshow.Counter = 1;
          }


          setTimeout('$$.Slideshow.Transition();', 7000);
        }
      );
    }
  }
});

$(document).ready(function(){
  
	/// genterate tabs
	function tabsFn(parent, head){
		//create a ul container for the tabs
		$(parent).append("<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(".home #rttr","h3");
	//tabsFn(".landingTop #rttr","h2.title");
	
	// run slideshow
    $$.Slideshow.Ready();

});
