(function($) {
  // Add .js to the html of the page immediately
  $('html').addClass("js");
  
  function animate_bg(ele, from, to) {
    ele.css("background-color", "rgba(0, 0, 0, " + (from += from > to ? -1 : 1) / 10 + ")"); 
    if(from != to) { 
        setTimeout(function() { animate_bg(ele, from, to) }, 80);
    }
  }
  
  // Wait for the window to load for this
  function mainMenuAnimate() {
    // Bring in the menu links
    $('#main-menu li:not(.processed)').each(function(i, el) {
      var wait = 25 * ((i + 1) * (i + 1));
      $(el).addClass("processed");
      setTimeout(function(){
        $(el).animate({opacity: 1}, wait);
      }, wait);
    });
  }
  
  $(window).load(function(){    
    // Cheeky fade for background
    $('#background').fadeOut(1500);
    
    setTimeout(function(){
      animate_bg($('#logo'),10,0);
    },1200);
    
    // Wait until the window loads if its quick
    mainMenuAnimate();
  });
  
  $(document).ready(function() {
    
    // Dont wait forever for window load for the main menu
    setTimeout(function() {
      mainMenuAnimate();
    }, 1500);
    
    // Map
    (function(){
      $('#map-tabs a').click(function(event){
        event.preventDefault();
        
        var mapid = $(this).attr("href");
        $('#map-maps .map-map:not('+mapid+')').stop(true).fadeOut(function(){
          $(this).removeClass("active");
        });
        $(mapid).stop(true).fadeIn(function(){
          $(this).addClass("active");
          $(this).stop(true).animate({opacity: 1},'fast');
        });
        // Remove old active class
        $('#map-tabs a[href!="'+mapid+'"]').removeClass("active");
        // Give our link the active class
        $('#map-tabs a[href="'+mapid+'"]').addClass("active");
        // Adjust height of map-maps
        $('#map-maps').stop(true).animate({height: $(mapid).css("height")});
      });
      
      $('#map-maps').css({height: $(".map-map.active").css("height")});
      
    })();
    
    // Slideshow
    // Copy title and subtitle into each slide
    (function(){
      var slideshow = $('.node-slide-show');
      var title = $('h1:first', slideshow).clone();
      var subtitle = $('.field-name-field-subtitle', slideshow).clone();

      var slides = $(".field-name-field-slide .field-item", slideshow).addClass("slide");
      
      var active_slide;
      var slide_links;
      
      var timer;
      
      var use_timer = true;
      
      // Loop through slides to include our titles
      $(slides).prepend(subtitle).prepend(title);
      
      function setup(slideshow) {
        var slide_controls = '<div class="slideshow-controls">'; 
        
        slide_controls += '<div class="slide-position">1 of ' + slides.length + '</div>';
        
        slide_controls += '<ul>';
        for (var i = 0, j = slides.length; i < j; i++) {
          slide_controls += '<li><a href="" class="'+ (i === 0 ? "active" : "") + '" rel="'+ i + '">Slide ' + (i+1) +'</a></li>';
        }
        slide_controls += '</ul>';
        
        slide_controls += '<a href="" class="show-status"></a>';
        
        slide_controls += '</div>';
        $(slideshow).append(slide_controls);
        
        // Attach slide action to controls
        slide_links = $('.slideshow-controls li a').click(function(event){
          event.preventDefault();
          
          if ($(this).attr("rel") == (slides.length - 1)) {
            clearInterval(timer);
            $('.show-status', slideshow).removeClass("play pause").text("replay").addClass("replay");
          }
          else {
            pause();
          }
          
          toSlide($(this).attr("rel"));
        });
      }
      
      function startTimer() {
        timer = setInterval()
      }
      
      function start() {
        // Hide the intro and bring in slide 1
        $("h1:first", slideshow).fadeOut();
        $(".field-name-field-subtitle:first", slideshow).fadeOut();
        $(".field-name-body", slideshow).fadeOut();
        $(".slideshow-controls", slideshow).fadeIn();
        active_slide = $(slides).first().addClass("active").fadeIn();
        play();
      }
      
      function pause() {
        clearInterval(timer);
        $('.show-status', slideshow).removeClass("pause replay").text("play").addClass("play");
      }
      
      function play() {
        clearInterval(timer);
        $('.show-status', slideshow).removeClass("play replay").text("pause").addClass("pause");
        timer = setInterval(function() {
          nextSlide();
        }, 5000);
      }
      
      function replay() {
        clearInterval(timer);
        toSlide(0);
        play();
      }
      
      function nextSlide() {
        var next = $(active_slide).next(".slide");
        
        if (next.length > 0) {
          toSlide(slides.index(next));
        }
        
        if (next.next().length === 0) {
          clearInterval(timer);
          $('.show-status', slideshow).removeClass("play pause").text("replay").addClass("replay");
        }
      }
      
      function toSlide(slide_num) {
        if(slides[slide_num] !== active_slide) {
          $(active_slide).removeClass("active").fadeOut();
          active_slide = $(slides[slide_num]).addClass("active").fadeIn();
          $(slide_links).removeClass("active");
          $(slide_links[slide_num]).addClass("active");
          $('.slide-position', slideshow).text((slide_num * 1 + 1) + " of " + slides.length);
        }
      }
      
      // Setup the slideshow
      setup(slideshow);
      
      $('.show-status.play').live("click",function(event) {
        event.preventDefault();
        play();
        nextSlide();
      });
      
      $('.show-status.pause').live("click",function(event) {
        event.preventDefault();
        pause();
      });
      
      $('.show-status.replay').live("click",function(event) {
        event.preventDefault();
        replay();
      });
      
      $('.start_slide_show').click(function(event){
        event.preventDefault();
        start();
      });
      
    })();
  
  });
    
})(jQuery);
;

