// dom ready
$(function() {
  
  // freaking IE 6
  if (typeof homepage_promo_movie != 'undefined') {
    $('#homepage-promo').append(homepage_promo_movie);
  }

  if (typeof countdown_movie != 'undefined') {
    $('#countdown').append(countdown_movie);
  }
  
  // nav links should blur to keep from having crazy outlines
  if ($('#nav').length == 1) {
    
    $('#nav li a').focus(function () { $(this).blur() });
    
  }// if
  
  // agenda toggles
  if ($('body.agenda ul#subnav.toggle').length == 1) {
    
    $('body.agenda ul#subnav.toggle a').click(function() {
      var $this = $(this);
      var li = $this.parents('li');
      var cn = li.attr('class')
      if (cn == "") { cn = "nothing" };
      
      // toggle
      if ($this.hasClass('on')) { // turn off
        
        $this.removeClass('on');
        
        $('#agenda-table td.' + cn).each(function(i) {
          $(this).parents('tr').removeClass('on');
        });
      
      } else { // turn on
        
        $this.addClass('on');
        
        $('#agenda-table td.' + cn).each(function(i) {
          $(this).parents('tr').addClass('on');
        });
        
      }// if
      
      return false;
    });// click
    
    $('body.agenda ul#subnav.toggle a').each(function() {
      $(this).click();
    })
    
  }// if
  
  // past photos fader
  if ($('#sidebar ul.past').length == 1) {
    
    fader = function() {
      
      $('#sidebar ul.past li:first-child').fadeOut(700, function() {
        $(this).insertAfter('#sidebar ul.past li:last-child');
        $(this).show();
      });
      
      setTimeout(fader, 5000); // set it to go off again later
      
    }// function
    
    setTimeout(fader, 5000); // start it up for the first time
    
  }// if
  
  // photo gallery lightbox
  if ($('ul#photo-gallery').length == 1) {
    
    $('ul#photo-gallery li a').lightBox();
    
  }// if

  // contact form lightbox
  if ($('li.contact-us a[rel="ajax"]').length == 1) {
    
    $('li.contact-us a[rel="ajax"]').lightBox();
    
  }// if
  
  // share lightbox
  if ($('li.share a[rel="ajax"]').length == 1) {
    
    $('li.share a[rel="ajax"]').lightBox();
    
  }// if
  
  // FAQ toggles
  if ($('dl#faq').length == 1) {
    
    $('dl#faq dd').hide();
    
    $('dl#faq dt').addClass('on').click(function(event) {
      $(this).next('dd').toggle();
    });
    
  }// if
  
  // Sponsors fade
  if ($('ul[rel="cycle"]').length == 1) {
    
    $('ul[rel="cycle"]').addClass('on');
    
    var lis = $('ul[rel="cycle"] li');
    var amount_of_groups = parseInt(lis.length / 6);
    
    if (lis.length % 6 != 0) {
      amount_of_groups++;
    }
    
    if (amount_of_groups > 1) {
      
      var groups = [];

      var group = -1;
      var item = 0;

      lis.hide().each(function(i) {
        if (i % 6 == 0) { group ++; item = 0; }
        if (!groups[group]) { groups[group] = [] }
        groups[group][item] = this;
        item++;
      });

      var current_group = 0;

      $(groups[0]).show();

      var next_cycle = function() {
        groups.push(groups.shift());

        $(groups[0]).fadeIn(700);

        setTimeout(cycler, 10000);
      }

      var cycler = function() {
        $(groups[0]).fadeOut(700);

        setTimeout(next_cycle, 900);
      }

      setTimeout(cycler, 10000);
      
    }
    
  }// if
  
});
