﻿/* Homepage Variables */
var panels = [];
var panel_coords = [];
var bob_count = [];
var bob_max = 3;
var panel_vis = false;
var bob_timer;

/* End Homepage */


var nav = {
  frameLimit: 3,
  topPadding: 10,
  stepBackgroundDistance: -70,
  stepTimeout: 25,
  items: []
};


$(document).ready(function () {
  if (window.location.hash.length == 0) {
  // Auto scroll to content
      try {
        $('html, body').animate({
          scrollTop: $('#spw-outer-wrapper').offset().top
        }, 300);
      }
       catch (ex) {  }
  }


  // Function to handle the animation
  nav.animate = function (id, step) {
    nav.items[id].index += step;

    // Ensure index is within threshold
    if (nav.items[id].index < 0) {
      nav.items[id].index = 0;
    }

    // Ensure index is within threshold
    if (nav.items[id].index > nav.frameLimit) {
      nav.items[id].index = 3;
    }

    var index = nav.items[id].index;

    var obj = $('#' + id + ' a');
    var offset = (index * nav.stepBackgroundDistance) + nav.topPadding;

    obj.css({ 'background-position': '0px ' + offset + 'px' });

    if (index < nav.frameLimit && index > 0) {
      nav.items[id].timer = setTimeout('nav.animate("' + id + '", ' + step + ')', nav.stepTimeout);
    }
  }

  // Populate the nav items array
  $("#spw-navigation div").each(function () {
    nav.items[$(this).attr('id')] = { index: 0, timer: 0 };
  });

  // Bind the navigation hover
  $("#spw-navigation a").hover(
    function () {
      var id = $(this).closest('div').attr('id');
      nav.animate(id, 1);
    },
    function () {
      var id = $(this).closest('div').attr('id');
      clearTimeout(nav.items[id].timer);
      nav.animate(id, -1);
    }
  );



  // Home page panels
  $(".spw-home-panel").hover(
    function () { showOverlay($(this)); },
    function () { hideOverlay($(this)); }
  );

  // Start the homepage animation
  panels = $(".spw-home-panel");
  start_bob();

  $('#spw-disclaimer-link').bind('click', function () {
    var content = $(this).next();
    if (content.hasClass('spw-closed')) {
      content.slideUp(200);
    } else {
      content.show(200);
    }

    content.toggleClass('spw-closed');

    $('.spw-tape-south').css("bottom", "-30px");
    $('.spw-tape-south').animate({
      bottom: "-37px"
    }, 200);

    return false;
  });

  $('.spw-disclaimer-content').hide();
  $('.spw-tape-south').css("bottom", "-37px");

});

var start_bob = function () {
  for (var i = 0; i < panels.length; i++) {

    // Delay the start of each panel for wave effect
    var id = $(panels[i]).attr('id');
    panel_coords[id] = $(panels[i]).position();
    bob_count[id] = 0;

    setTimeout("bob('" + id + "')", 270 * i);
  }
}

// Bobbing animation
var bob = function (id) {

  // Content shown? No animation for you!
  if (panel_vis) {
    return;
  }

  // Only bob a preset number of times
  if (bob_count[id]++ >= bob_max) {
    clearTimeout(bob_timer);
    bob_timer = setTimeout('start_bob()', 5000);
    return;
  }

  var start_pos = panel_coords[id];
  var obj = $('#' + id);
  // Ease out
  obj.animate({ top: start_pos.top - 10 }, 800, "easeInOutQuad", function () {
    // Ease in
    obj.animate({ top: start_pos.top }, 800, "easeInOutQuad", function () {

      // Do it all again
      bob(id);
    });
  });
}

// Show a homepage panel overlay
var showOverlay = function (obj) {
  panel_vis = true;
  var shiftDistance = 80;
  var left = panel_coords['spw-home-panel-center'].left;

  // Clear any queued animation
  clearTimeout(bob_timer);

  // Show the overlay
  obj.children('.spw-overlay').stop(true).fadeTo(300, 0.7);
  obj.children('.spw-home-panel-content').stop(true).show();
  $(".spw-home-panel").stop(true);


  // Calculate where to move the center panel
  switch (obj.attr('id')) {
    case 'spw-home-panel-right': left -= shiftDistance; break;
    case 'spw-home-panel-left': left += shiftDistance; break;
    default: left = panel_coords['spw-home-panel-center'].left;
  }

  // Reset the panels to their original location
  $(".spw-home-panel").each(function () {
    var id = $(this).attr('id');
    if (id != 'spw-home-panel-center') {
      // Reset side panels
      $(this).animate({ top: panel_coords[id].top }, 300);
    } else {
      // Move the center panel out of the way
      $(this).animate({ left: left, top: panel_coords['spw-home-panel-center'].top }, 300);
    }
  });
}

// Hide a homepage panel overlay
var hideOverlay = function (obj) {
  panel_vis = false;

  obj.children('.spw-overlay').stop(true).fadeTo(300, 0);
  obj.children('.spw-home-panel-content').stop(true).hide();

  // Reset the center panel
  $('#spw-home-panel-center').animate({ left: panel_coords['spw-home-panel-center'].left }, 300);

  // Queue the animation
  bob_timer = setTimeout('start_bob()', 2000);
}

var resetTape = function () {
    // Set it wrong then reset it to force recalculation of page height in ie7
    $('.spw-tape-south').css("bottom", "-35px");
    $('.spw-tape-south').css("bottom", "-37px");
}
