var slideInterval = 8000
var timer = null
var links = ['healthier_lives/cold_flu', 
		     'products', 
		     'healthier_lives/new_moms', 
             
             'tools/cleaning_advisor']

function currentSlide() {
  return $('#controls li a.on')
}

function selectSlide(next) {
  if (currentSlide().length == 0) {
    $('#controls li a:eq('+ next + ')').addClass('on')
    $('#slides li:eq(' + next + ')').addClass('on')

    return true;
  }

  var current = $.inArray($('#controls li a.on')[0], $('#controls li a'))
  $('#learnmore').attr('href', links[current])
  if (next == current) return true

  $('#controls li a:eq('+ current + ')').removeClass('on')
  $('#controls li a:eq('+ next + ')').addClass('on')
  transitionSlide('slides', current, next)
}

function transitionSlide(div, current, next) {
  $('#' + div + ' li:eq(' + next + ')').addClass('next')
  $('#' + div + ' li:eq(' + current + ')').fadeOut('slow', function() {
    $(this).removeClass('on')
    $(this).removeAttr('style')
    $('#' + div + ' li:eq(' + next + ')').removeClass('next').addClass('on')
  })
  $('#learnmore').attr('href', links[next])
}

function resetSlides(start) {
  timer = setInterval(function() {
    selectSlide(start++)
    if (start >= $('#slides li').length) start = 0
  }, slideInterval)
}

$(document).ready(function() {
  $("#controls li").each(function(k, v) {
    $(this).children('a').click(function() {
      selectSlide(k)
      clearInterval(timer)
      resetSlides(k)
    })
  })

  selectSlide(0)
  resetSlides(1)
})
