$(document).ready(function () {
  $(window).scroll(function() {
    if ($(this).scrollTop() > 1330) {
      $('#back_to_top').fadeIn(300);

      if (!$('body').hasClass('night')) {
        Header.start_night();
      }
    } else {
      $('#back_to_top').fadeOut(300);
    }
  });

 $("img.logo").bind("click", function() {
      var src = ($(this).attr("src") === "images/logo-top.png")
                    ? "images/logo-top-vikings.png" 
                    : "images/logo-top.png";
      $(this).attr("src", src);
  });

 $("#disguises").bind("click", function() {
      var src = ($(".josh_williams").attr("src") === "images/speakers/josh_williams.jpg")
                    ? "images/speakers/d_josh_williams.jpg" 
                    : "images/speakers/josh_williams.jpg";
      $(".josh_williams").attr("src", src);
      
      var src = ($(".jeff_lawson").attr("src") === "images/speakers/jeff_lawson.jpg")
                    ? "images/speakers/d_jeff_lawson.jpg" 
                    : "images/speakers/jeff_lawson.jpg";
      $(".jeff_lawson").attr("src", src);
      
      var src = ($(".amy_hoy").attr("src") === "images/speakers/amy_hoy.jpg")
                    ? "images/speakers/d_amy_hoy.jpg" 
                    : "images/speakers/amy_hoy.jpg";
      $(".amy_hoy").attr("src", src);
      
      var src = ($(".micah_baldwin").attr("src") === "images/speakers/micah_baldwin.jpg")
                    ? "images/speakers/d_micah_baldwin.jpg" 
                    : "images/speakers/micah_baldwin.jpg";
      $(".micah_baldwin").attr("src", src);

      var src = ($(".hiten_shah").attr("src") === "images/speakers/hiten_shah.jpg")
                    ? "images/speakers/d_hiten_shah.jpg" 
                    : "images/speakers/hiten_shah.jpg";
      $(".hiten_shah").attr("src", src);
 
      var src = ($(".rhonda_kallman").attr("src") === "images/speakers/rhonda_kallman.jpg")
                    ? "images/speakers/d_rhonda_kallman.jpg" 
                    : "images/speakers/rhonda_kallman.jpg";
      $(".rhonda_kallman").attr("src", src);
      
      var src = ($(".tom_rossi").attr("src") === "images/speakers/tom_rossi.jpg")
                    ? "images/speakers/d_tom_rossi.jpg" 
                    : "images/speakers/tom_rossi.jpg";
      $(".tom_rossi").attr("src", src);
      
      var src = ($(".steven_walker").attr("src") === "images/speakers/steven_walker.jpg")
                    ? "images/speakers/d_steven_walker.jpg" 
                    : "images/speakers/steven_walker.jpg";
      $(".steven_walker").attr("src", src);

      var src = ($(".jason_beaird").attr("src") === "images/speakers/jason_beaird.jpg")
                    ? "images/speakers/d_jason_beaird.jpg" 
                    : "images/speakers/jason_beaird.jpg";
      $(".jason_beaird").attr("src", src);

      var src = ($(".sarah_hatter").attr("src") === "images/speakers/sarah_hatter.jpg")
                    ? "images/speakers/d_sarah_hatter.jpg" 
                    : "images/speakers/sarah_hatter.jpg";
      $(".sarah_hatter").attr("src", src);
      
  });

  $('#top-popup').slideDown("slow");
  $('.hidepop').click(function(){
      $('.hidepop').fadeOut("fast");
      $('#top-popup').slideUp("slow");
  });

  $(function () {
      $('.bubbleInfo').each(function () {
          var distance = 10;
          var time = 250;
          var hideDelay = 500;

          var hideDelayTimer = null;

          var beingShown = false;
          var shown = false;
          var trigger = $('.trigger', this);
          var info = $('.popup', this).css('opacity', 0);


          $([trigger.get(0), info.get(0)]).mouseover(function () {
              if (hideDelayTimer) clearTimeout(hideDelayTimer);
              if (beingShown || shown) {
                  // don't trigger the animation again
                  return;
              } else {
                  // reset position of info box
                  beingShown = true;

                  info.css({
                      top: 60,
                      left: -110,
                      display: 'block'
                  }).animate({
                      top: '-=' + distance + 'px',
                      opacity: 1
                  }, time, 'swing', function() {
                      beingShown = false;
                      shown = true;
                  });
              }

              return false;
          }).mouseout(function () {
              if (hideDelayTimer) clearTimeout(hideDelayTimer);
              hideDelayTimer = setTimeout(function () {
                  hideDelayTimer = null;
                  info.animate({
                      top: '-=' + distance + 'px',
                      opacity: 0
                  }, time, 'swing', function () {
                      shown = false;
                      info.css('display', 'none');
                  });

              }, hideDelay);

              return false;
          });
      });
  });

  $('img#rex').mouseover(function(){
    $('embed').remove();
    $('body').append('<embed src="audio/raptor-sound.mp3" autostart="true" hidden="true" loop="false">');
  });  

  $('img#back-to-the-future').click(function(){
    $('embed').remove();
    $('body').append('<embed src="audio/back-to-the-future.mp3" autostart="true" hidden="true" loop="false">');
  });  

});

oldTextAry = new Array();
	function changeText (fieldObj, newTexStr) {
		if (newTexStr == fieldObj.innerHTML) {
			fieldObj.innerHTML = oldTextAry[fieldObj.id];
		} else {
			oldTextAry[fieldObj.id] = fieldObj.innerHTML;
			fieldObj.innerHTML = newTexStr;
		}
	}

var scroll_speed = function(from, to) {
  var divisor = 4;
  var foffset = $(from).offset();
  var toffset = $(to).offset();
  var speed = 0;

  if (foffset.top > toffset.top) {
    // We are below the target, go up
    speed = (foffset.top - toffset.top) / divisor;
  } else {
    // We are above the target, go down
    speed = (toffset.top - foffset.top) / divisor;
  }

  // 300 milliseconds min
  if (speed < 300) {
    speed = 300;
  }

  // 1 second max
  if (speed > 1000) {
    speed = 1000;
  }

  return speed;
}

Header = {
  back_to_top: function() {
    $.scrollTo('#header', scroll_speed('#back_to_top', '#header'));
  },
  opposite_direction: function(direction) {
    return (direction.substring(0, 1) == '+') ? '-' + direction.substring(1) : '+' + direction.substring(1);
  },
}

