$(function() {

  // DROPDOWN MENU

  $('#nav-top ul.nav li').hover(function () {
    $(this).find('ul.subnav').stop('true', 'true').animate({ height: 'show', opacity: 'show' }, { duration:400 });
  }, function () {
    $(this).find('ul.subnav').animate({ height: 'hide', opacity: 'hide' }, { duration:400 });
  });

  // SLIDER

  // On page load
  $('#slider-panels .slider-panel').hide(); // Hide slider panels
  $('#slider-images img').hide(); // Hide slider images
  $('#slider-nav ul li:first').addClass('active'); // Activate first tab
  $('#slider-panel-1').show(); // Show first slider panel
  $('#slider-image-1').show(); // Show first image

  // Automatic rotation function
  interval_function = function () {
    // Find current rel counter
    var activeRel = $('#slider-nav ul li.active a').attr('rel'); //Find the href attribute value to identify the active tab + content

    // Remove current active class and hide stuff
    $('#slider-nav ul li.active').removeClass('active').next().addClass('active');
    $('#slider-panels .slider-panel').fadeOut(1000);
    $('#slider-images img').fadeOut(1000);
    if ( activeRel == 9) {
      activeRel = 1;
      // Add active class and show stuff
      $('#slider-nav-'+activeRel).parent().addClass('active');
      $('#slider-panel-'+activeRel).fadeIn(1000);
      $('#slider-image-'+activeRel).fadeIn(1000);
    } else {
      // Add active class and show stuff
      $('#slider-nav-'+activeRel).next().addClass('active');
      $('#slider-panel-'+activeRel).next().fadeIn(1000);
      $('#slider-image-'+activeRel).next().fadeIn(1000);
    }
  };

  // Set automatic rotation timer
  interval = setInterval(interval_function, 5000);

  // On hover event
  $('#slider').hover(function () {
      clearInterval(interval);
  }, function () {
      interval = setInterval(interval_function, 5000);
  });

  // On click event
  $('#slider-nav ul li a').click(function() {

    clearInterval(interval); // restart automatic rotation timer

    // If active item is clicked
    if ($(this).hasClass('active')) {
      return false;
    }

    // If non-active item is clicked
    else {

      $('#slider-nav ul li').removeClass('active'); // Remove any "active" class
      $(this).parent().addClass('active'); // Add "active" class to selected tab
      var activeRel = $(this).attr('rel'); //Find the href attribute value to identify the active tab + content
      $('#slider-panels .slider-panel').fadeOut(1000); // Hide all tab content
      $('#slider-images img').fadeOut(1000); // Hide slider images
      $('#slider-panel-'+activeRel).fadeIn(1000); //Fade in the active ID content
      $('#slider-image-'+activeRel).fadeIn(1000); //Fade in the active ID content
      return false;
    }

  });

  // LAYOUT FIXES
  // Sticky footer & full height

  fixLayout();

  function fixLayout() {

    $page = $('#page');
    $header = $('#header');
    $content = $('#content');
    $footer = $('#footer');
    $push = $(window).height() - $header.outerHeight(true) - $content.outerHeight(true) - $footer.outerHeight(true);

    if (( $header.outerHeight(true) + $content.outerHeight(true) + $footer.outerHeight(true) ) < $(window).height() ) {

      $footer.css({ position: 'relative' }).stop('true', 'true').animate({ top: $push }, { duration:300 });
      $page.css({ height: $(window).height() });

    } else {

      $footer.css({ position: 'static', top: '0' });
      $page.css({ height: '100%' });
      
    }

  }

  $(window).load(fixLayout);
  $(window).resize(fixLayout);

});
