jQuery(document).ready(function($) {
    
    //Rotates all quotes on the homepage
    function startQuoteRotation(elem) {
      $(elem).delay(5000).fadeOut(500,function() {
        var next = $(this).next();
        if ($(this).next().length == 0)
          next = $(this).siblings().first();
        $(next).fadeIn(500,function() {
          startQuoteRotation(this);
        });
      });
    };
    
    startQuoteRotation($('#home-page-quotes table').first());


    //Rotates all objects inside the container passed
    $('#promo-images img').first().fadeIn('fast');
    
    $('#promo-images img').each(function(i) {
      $('#promo-menu').append('<a href="#" rel="'+(i+1)+'">'+(i+1)+'</a>');
    });
    
    $('#promo-menu a').first().addClass('activeSlide');
    $('#promo-images img').first().addClass('visible').css('z-index','3');
    
    $('#promo-menu a').click(function() {
      if ($('#promo-images img:animated').length != 0)
        return false;
      
      var num = $(this).attr('rel');
      
      $('#promo-images img.visible').fadeOut(1000,function() {
        $(this).removeClass('visible');
      });
      
      $('#promo-images img').eq(num - 1).css('z-index','2').fadeIn(1000,function() {
        $(this).css('z-index','2');
        $(this).addClass('visible');
      });
    
      $(this).addClass('activeSlide');
      $(this).siblings().removeClass('activeSlide');
    
      return false;
    });

  
  //open all external links in a new window
    $('a[href^="http"], a[href^="https"]').not('a[href~="'+document.domain+'"]').attr('target','_blank');

    
  //Handles the accordion funcationality for the faqs
  $('.faq').accordion({
      create:function() {
        $('.faq').first().show();
      },
      active: false,
      autoHeight:false,
      animation:'slide'
    });
    
    $('#faq_menu li').click(function() {
      var i = $(this).index();
      $('#faq_answers .faq').hide();
      $('#faq_answers .faq').eq(i).show();
      
      $(this).siblings().removeClass('current');
      $(this).addClass('current');

    });

    $('.slideshow').cycle({
        fx: 'fade',
    	speed: 0,
    	timeout: 250,
    	autostop:20
    });



    $('.snow').cycle({
    	fx: 'fade',
    	speed: 0,
    	timeout: 250
    });

});
