$(function () {
  if ($('#slideshow').length > 0) {
    function displaySlide (index) {
      $('.slides .current .info').hide()
      $('.slides .current .info').fadeIn(100)
      $('.slide-buttons button').removeClass('current')
      $('.slide-buttons button').eq(index).addClass('current')
    }

    function hideSlides () {
      if(api.cssTransitionsAvailable())
      {
        $('.slides .current .info').css({'opacity': 0})
      }
      else
      {
        $('.slides .current .info').fadeOut(100)
      }
    }

    function createButtons () {
      var slideCount = $('.slide').length
        , i
        , $button
        , $li
        , $buttonWrapper = $(document.createElement('ol'))
        , $listWrapper = $(document.createElement('div'))

      $buttonWrapper.addClass('slide-buttons')
      $listWrapper.addClass('slide-button-wrapper')

      for (i = 0; i < slideCount; i++) {
        $li = $(document.createElement('li'))
        $button = $(document.createElement('button'))
        $button.html(i)

        if (i == 0)
          $button.addClass('current')

        $li.append($button)
        $buttonWrapper.append($li)
      }

      $listWrapper.append($buttonWrapper)
      $('#slideshow').append($listWrapper)
    }

    createButtons()

    var api = $('#slideshow').centredScroller({duration:600, autoAdvance:6000, interactionDelay:500})

    api.onAutoAdvanceBefore(function () {
      hideSlides()
    })

    api.onAutoAdvanceAfter(function () {
      displaySlide(api.getIndex())
    })

    $('#slideshow').delegate('.info', 'mouseenter', function () {
      api.stopAutoAdvance()
    })

    $('#slideshow').delegate('.info', 'mouseleave', function () {
      api.startAutoAdvance()
    })

    $('.slide-buttons').delegate('button', 'click', function (e) {
      var i = parseInt($(this).html(), 10)

      api.display(i, function () {
        displaySlide(i)
      })
    })
  }

  if ($('.board-list').length > 0) {
    var boardList = $('.board-list')
    $('.entry-body', boardList).slideUp()
    $('.entry-title', boardList).click(function () {
      $(this).siblings('.entry-body').slideToggle()
    })
  }

  $('.foot form input').focus(function () {
    $(this).siblings('label').hide()
  })

  $('.foot form input').blur(function () {
    $(this).val($.trim($(this).val()))

    if ($(this).val() == '') {
      $(this).siblings('label').show()
    }
  })
})

