$(function() {
  function set_home_nav_right() {
  $('nav#home-nav').css('right', ($(window).width() - $('nav#home-nav').width())/2);
  if($(window).height() < 1055) {
    $('img#footer-img').addClass('non-fixed');
    $('nav#home-nav').addClass('non-fixed');
    if($(document).height() > $(window).height()) {
      $('img#footer-img').css({top: 0, display: 'none'});
      $('nav#home-nav').css({top: 0, display: 'none'});
      var top = $(document).height();
      $('img#footer-img').css({top: top-22, display: 'block'});
      $('nav#home-nav').css({top: top-58, display: 'block'});
    }
  } else if($(window).height() >= 1055) {
    $('img#footer-img').removeClass('non-fixed');
    $('nav#home-nav').removeClass('non-fixed');
    $('img#footer-img').css('top', '');
    $('nav#home-nav').css('top', '');
  }
}
set_home_nav_right();
$('iframe').load(set_home_nav_right);
$('img').load(set_home_nav_right);
$(window).resize(set_home_nav_right);
$('nav#home-nav li').hover(
  function() {
    $(this).animate({'margin-top': '-10px'}, 100);
  },
  function() {
    $(this).animate({'margin-top': '0'}, 100);
  }
);


// Sermon Series Thumbnails
$('div.sermonseries').hover(
  function() {
    $(this).data('original-left', $(this).children('ul').css('left'));
    $(this).children('ul.set-other').css('display', 'none');
    $(this).children('ul').animate({'left': '0', 'opacity': '1.0'}, 300);
  },
  function() {
    var original_left = $(this).data('original-left');
    $(this).children('ul').animate({'left': original_left, 'opacity': '0.0'}, 300);
  }
);
$('div.sermonseries a.list-swap').click(function(event) {
  $(this).parents("div.sermonseries").children('ul.set-current').hide();
  $(this).parents("div.sermonseries").children('ul.set-other').show();
  $(this).parents("div.sermonseries").children('ul.set-other').addClass('set-current2').removeClass('set-other');
  $(this).parents("div.sermonseries").children('ul.set-current').addClass('set-other').removeClass('set-current');
  $(this).parents("div.sermonseries").children('ul.set-current2').addClass('set-current').removeClass('set-current2');
  event.preventDefault();
});


// Event Expansion
function expandEventDescription(event) {
  $(this).parents('.event').children('.event-description').slideToggle(400, set_home_nav_right);
  event.preventDefault();
}
$('#event-list .event img').click(expandEventDescription);
$('#event-list .event h4.title').click(expandEventDescription);


// Staff Page
try {
$('#staff-tabs').tabs({
  show: set_home_nav_right
});
} catch(e) {};


// Homepage Ticket
nextHomeTickerItem = function() {
  var current = $('#home-ticker span.event-title:visible');
  var next = current.next();
  if(next.length == 0) { next = $('#home-ticker span.event-title:first'); }
  current.hide(400, function() { next.show(400) });
}
if($('#home-ticker span.event-title').length > 0) {
  $('#home-ticker span.event-title:first').delay(700).show(500);
  setInterval('nextHomeTickerItem()', 8000);
}


// Show Map Buttons
/*$('a.show-map').click(function(event) {
  $('#google-map').slideToggle();
  event.preventDefault();
});*/


// Twitter
setupTwitterFeed = function(data) {
  $('#home-twitter #tweet-1 span.tweet-text').text(data[0].text);
  $('#home-twitter #tweet-2 span.tweet-text').text(data[1].text);
  $('#home-twitter').slideDown(500);
}
$.ajax({
  url: 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=bpbc&count=2&trim_user=1&callback=setupTwitterFeed',
  dataType: 'jsonp'
});

// Expansion
$('h3.has-expandable-content').each(function(i, e) {
  $(e).prepend('<span class="display-toggle">[+]</span> ');
  return $(this);
}).click(function(event) {
  $(this).nextAll('div:first').slideToggle(400, set_home_nav_right);
  var toggleIcon = $(this).children('span.display-toggle');
  if(toggleIcon.text() == '[+]') {
    toggleIcon.text('[-]');
  } else {
    toggleIcon.text('[+]');
  }
  event.preventDefault();
  return $(this);
});

// Drop-down Nav
$('header nav ul.top-level li').mouseenter(function(event) {
  var toShow = $(this).children('ul');
  $(this).parent().find('ul:visible').each(function(i,e) {
    if(e != toShow[0]) {
      $(e).hide(200);
    }
  });
  toShow.show(200);
});
$('header nav').mouseleave(function(event) {
  $(this).find('ul.top-level ul').hide(200);
});

// Home Slider
$('#home-video .slider-thumbs a').mouseenter(function(event) {
  if($('.slide.animating').length >= 1)
    return;

  new_active = $('#'+event.currentTarget.rel);
  if(new_active != $('.slide.active')) {
    new_active.toggleClass('animating');
    new_active.animate({left: '0'},
                       {duration: 300,
                        queue: true,
                        complete: function() {
                          $('.slide.active').css('left', '1000px');
                          $('.slide.active').toggleClass('active');
                          $(this).toggleClass('active');
                          $(this).toggleClass('animating');
                        }
                       });
  }
});
});
