// ukrywa/pokazuje odpowiednie zakladki
function redrawTabs() {
  var elements = $('.tabs_selectors a');
  var foundActive = new Array();
 
  tab0Index = 0;

  for (var i = 0; i < elements.length; i++) {
    var element = elements[i];

    var elementArray = element.id.split('_');
    if (foundActive[elementArray[1]] == undefined) {
      foundActive[elementArray[1]] = element;
    }
    
    element.parentNode.className = element.parentNode.className.replace('active', '');
    
    if (window.location.hash.indexOf('t_' + elementArray[1] + '_' + elementArray[2]) != -1) {
      element.parentNode.className += ' active';
      foundActive[elementArray[1]] = true;
      if (typeof onTabSelect != 'undefined')
        onTabSelect(elementArray[1], elementArray[2]);

      tab0Index = elementArray[2];
    }
  }
  
  for (var i = 0; i < foundActive.length; i++) {
    if (foundActive[i] !== true && foundActive[i] !== undefined) {
      var element = foundActive[i];
      element.parentNode.className += ' active';
      if (typeof onTabSelect != 'undefined')
        onTabSelect(i, 0);
    }
  }

  elements = $('.tab_content'); 
  var foundActive = new Array();
  
  for (var i = 0; i < elements.length; i++) {
    var element = elements[i];
    
    var elementArray = element.id.split('_');
    if (foundActive[elementArray[1]] == undefined) {
      foundActive[elementArray[1]] = element;
    }

    $(element).css('display', 'none');
//$(element).fadeOut(100);
//Cufon.replace('#flash .tabs_selectors li');

    
    if (window.location.hash.indexOf('t_' + elementArray[1] + '_' + elementArray[2]) != -1) {
      //$(element).css('display', '');

$(element).fadeIn();
//Cufon.replace('#flash .tabs_selectors li');

      foundActive[elementArray[1]] = true;
    }
  }

  for (var i = 0; i < foundActive.length; i++) {
    if (foundActive[i] !== true && foundActive[i] !== undefined) {
      var element = foundActive[i];
      $(element).css('display', '');
    }
  }
}

// rysuje scrolla
function redrawScroll() {
  $('.tab_scroll').jScrollPane({showArrows: true}); 
}

// ustawia hasha
function setHash(hash, omitExisting) {
  // zmieniamy nazwe hasha aby byla inna niz istniejace id
  hash = hash.replace('tab', 't');

  var currentHash = window.location.hash.slice(1);
  
  var currentHashSplitted = currentHash.split(';');
  
  var hashFound = false;
  for (var i = 0; i < currentHashSplitted.length; i++) {
    if (currentHashSplitted[i].indexOf(hash.slice(0, hash.lastIndexOf('_'))) != -1) {
      if (omitExisting != true)
        currentHashSplitted[i] = hash;
      
      hashFound = true;
      break;
    }
  }
  
  if (!hashFound)
    currentHashSplitted.push(hash);
    
  while (currentHashSplitted[0] == '') {
    currentHashSplitted.shift();
  }

  var newHash = '#' + currentHashSplitted.join(';');
    
  window.location.hash = newHash;
  
  $('.tabs_content a').each(function(index, element) {
    var hrefSplitted = element.href.split('#');

    element.href = hrefSplitted[0] + newHash;
  });
}

function generateHideStyle() {
  if (typeof isHideStyleGenerated == 'undefined') {
    document.write('<style type="text/css" media="screen">.js_display_none { display: none; }</style>');
    isHideStyleGenerated = true;
  }
}

function checkHashChange() {
  if (currentHashValue != window.location.hash) {
    currentHashValue = window.location.hash;
    redrawTabs();
  }
}

$(window).load(function() {
  var elements = $('.tabs_paragraph');

  // for (var i = 0; i < elements.length; i++)
    // setHash('tab_' + i + '_0', true);

  // zakladamy handler na click
  $('.tabs_selectors a').each(function(index, element) {
    $(element).click(function(event) {
      var target = event.target;
      
      while (target.tagName.toLowerCase() != 'a')
        target = target.parentNode;
      
      setHash(target.id);
      redrawTabs();

      stopSlideshow();
    });
  });

  $('.tab_content').each(function(index, element) {
    element.className = 'tab_content';
  });

  redrawScroll();

  redrawTabs(); 

  currentHashValue = window.location.hash;
  if (typeof checkHashChangeInterval == 'undefined') {
    checkHashChangeInterval = setInterval(checkHashChange, 50);
  }

  startSlideshow();
});

function startSlideshow() {
  timer = setInterval(showSlide, 5000);
  var counter = tab0Index;

  function showSlide() {
    counter = ++counter % 5;

    setHash('tab_0_' + counter);
    redrawTabs();
  }
}

function stopSlideshow() {
  clearInterval(timer);
}


