$(document).ready(function() {
  
  var isHome = $('.homepage').length;
  
  //if homepage, do fancy show!
  if(isHome){
    
    $('#skipshow').click(function() {
      //skip to last frame of animation
      $('#logobar').remove();
      $('#homeshow').remove();
      startShow();
      createCookie('animated','yes',5);
      $(this).remove();
      return false;
    });
    
    //if it hasn't animated yet, start animatinf
    if(readCookie('animated') != "yes"){
    
      $("#preload").one("load",function(){
        $('#homeshow').addClass("ready").fadeIn(2000, function(){homeAnimate();});
        $('#logobar').animate({paddingLeft:0}, 4200, 'swing');
      })
      .each(function(){
      if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) 
        $(this).trigger("load");
      });
      
      //default cookie for start slide (too high on purpose)
      cookieSlide = 10;
      
    }else{
      
      //if no animation is needed (because it's already fired)
      //show slides ASAP!!
      $('#logobar').remove();
      $('#homeshow').remove();
      $('#skipshow').remove();
      
      //is there a current slide needed???
      cookieSlide = readCookie('currentSlide');
      if(cookieSlide){
        cookieSlide = parseInt(cookieSlide);
      }else{
        cookieSlide = 10;
      }
      
      //start slideshow      
      startShow();
      
    }//endifelse animated
    
    //set up slide show
    //if there is a cookie, select correct slide
    //or choose one randomly otherwise
    
    //remove js free first slide
    $('.shown').removeClass('shown');
    whichSlide = Math.floor(Math.random() * 6);
    
    if(cookieSlide < 10){
      $("#slideshow .slide").eq(cookieSlide).addClass('shown');
      eraseCookie('currentSlide');
    }else{
      $('#slideshow .slide').eq(whichSlide).addClass('shown');
    }
    
    //find newly selected slide and sync with nav
    //var firstOne = $(".shown").index();
    var firstOne = $('.slide').index($('.shown'));
    
    //use firstOne to tag appropriate nav item
    $('#slidenav li').eq(firstOne).find('a').addClass('current');
    
    
    //add function to nav clicks to cycle through slides
    $('#slidenav a').click(function() {

  		if(! $(this).hasClass('current'))
  		{	
  		  stopShow();

  			var curindex = $(this).parent().index();

  			$("#slidenav .current").removeClass("current");
  			$(this).addClass("current");

  			$(".shown").fadeOut(800, function(){$(this).removeClass('shown');});
  			$(".slide:eq("+curindex+")").fadeIn(800).addClass("shown");

  			startShow();

  		}

  		return false;

    });//end nav clicks
    
  }//endif isHome
  
  function startShow(){
    //next slide fires every 10 sec
    slideTimer = window.setInterval(nextSlide, 10000);
  }

  function stopShow(){
    clearInterval(slideTimer);
  }
  
  function nextSlide(){
    totalSlides = 5;
    curSlide = $(".shown").index();

    if(curSlide == 5)
    {
      //select next nav item as current
      $("#slidenav .current").removeClass("current");
      $('#slidenav li:first a').addClass('current');

      //show next slide
      $(".shown").fadeOut(800, function(){$(this).removeClass('shown');});
      $(".slide:first").fadeIn(800).addClass("shown");
    }
    else
    {
      //select next nav item as current
      $("#slidenav .current").removeClass("current").parent().next().find('a').addClass('current');

      //show next slide
      $(".shown").fadeOut(800, function(){$(this).removeClass('shown');});
      $(".slide:eq("+(curSlide+1)+")").fadeIn(800).addClass("shown");
    }

  }//end next slide func
  
  //this function is called when the homepage needs to start animating
  function homeAnimate(){    
    $("#biglogo").delay(500).fadeIn(1500);
    $('#homeshow p').delay(1500).fadeIn(2000,function(){
        $('#homeshow').delay(1500).slideUp(2500);
        $('#logobar').delay(3500).fadeOut(2000, function(){startShow();});
        createCookie('animated','yes',5);
        $('#skipshow').remove();
      });
  }
  
  $('#tagline').click(function() {
    eraseCookie('animated');
    eraseCookie('currentSlide');
    
    $(this).slideUp("fast").slideDown("fast");
  });
  
  $('.sub #slidenav a').click(function() {
    
    clickedSlide = $(this).parent().index();
    createCookie('currentSlide',clickedSlide,1);
    
    var href = $(this).attr('href');
    location.href = href;
    
    return false;
  });
  
});
