
$(document).ready(function()
{
  // Custom tabs function
  // @DANIEL: Only checked in Firefox 7, should be ok in other browsers, but check.
  if ($('.tabs').length >= 1)
  {
    var timerOffset = 0;
    var timerOffsetAdjustment = 1000; // 1 second
    
    $('.tabs').each(function(){
      var numOfTabs = $(this).children('div').length;
      
      $(this).children('div').addClass('inactiveTab');
      $(this).children('div').eq(0).css('display', 'block').addClass('firstTab').addClass('activeTab').removeClass('inactiveTab');
      $(this).children('div').eq(numOfTabs - 1).addClass('lastTab');
        
      var callingTab = $(this);
      
      setTimeout(function(){
        startCustomTabsRotation(callingTab);
      }, timerOffset);
      
      timerOffset = timerOffset + timerOffsetAdjustment;  
    });
  }
  
  // jQuery tabs function
  if ($('.tabs').length >= 1)
  {
    //$('.tabs').tabs({
    //  fx: { opacity: 'toggle', duration: 500 }
    //}).tabs("rotate", 5000, false);
    
    //startTabRotation();
  }
});

function startCustomTabsRotation(callingTab)
{
    var tabDuration = 5000;
    var tabFadeSpeed = 1000;
    
      var timer = setInterval(function() {
          callingTab.children('.activeTab').fadeTo(tabFadeSpeed, 0.0, function(){
            $(this).removeClass('activeTab').addClass('inactiveTab');
          
          });
          
          if (callingTab.children('.activeTab').hasClass('lastTab'))
          {
            callingTab.children('.inactiveTab').eq(0).fadeTo(tabFadeSpeed, 1.0, function(){
              $(this).removeClass('inactiveTab').addClass('activeTab');
            })
          }
          else
          {
            callingTab.children('.activeTab').next('div.inactiveTab').fadeTo(tabFadeSpeed, 1.0, function(){
              $(this).removeClass('inactiveTab').addClass('activeTab');
            })
          }
        
        }, tabDuration);
}

  
function startTabRotation()
{
    var timeout = 0; // First starts half a second after the function is called
    
    // start tabs, one after the other. Change $('.tabs') to your required selector
    $('.tabs').each(function() {
        var callingTab = $(this);
        
        var timer = setTimeout(function() {
            callingTab.tabs("rotate", 1000, false);
        }, timeout);
        
        timeout = timeout + 1000; // Plus one second (1000). Delays the rotate by 1 second, therefore each one will rotate 1 second later than the last.
    });
}
