function initialize_navigation () {
  var navigation = $('navigation');
  
  $$('#navigation a').each(function ( a ) {
    a.onmouseover = function () {
      navigation.className
        = navigation.className.replace(/\bnav_\w+/, "") + " " + a.id;
    }
    a.onmouseout = function () {
      navigation.removeClassName(a.id);
    }
  });
}

function validate_email () {
  if ( ! $('email_1').value || $('email_1').value == "Email Address" ) {
    alert("You must enter an email address.");
    return false;
  }
  
  if ( $('email_1').value != $('email_2').value ) {
    alert("Email addresses do not match.");
    $('email_2').focus();
    return false;
  }
  
  return true;
}

function validate_mailing_list_form () {
  if ( ! $('mailing_list_email').value
      || $('mailing_list_email').value == "enter email" ) {
    alert("You must enter an email address.");
    return false;
  }
  
  return true;
}

var balloonTimer = false;

/**
 * Set the position of the balloon
 */
function setBalloonPosition(balloon, event) {
  balloon.style.left = (Event.pointerX(event) - 45) + 'px';
  balloon.style.top = (Event.pointerY(event) - balloon.clientHeight - 5) + 'px';
}

/**
 * Callback for conditionally hiding the balloon
 */
function hideBalloonCallback() {
  var thumb = this;
  var balloon = $('info_balloon');

  if ((!thumb.isOver) && (!balloon.isOver)) {
    thumb.displayBalloon = false;
    balloon.style.display = 'none';
    balloonTimer = false;
  } else {
    balloonTimer = setTimeout(hideBalloonCallback.bind(thumb), 100);
  }
}

/**
 * Hide the balloon when the mouse mouse off of the case study thumbnail
 */
function hideBalloon(thumb) {
  if (!balloonTimer) {
    balloonTimer = setTimeout(hideBalloonCallback.bind(thumb), 100);
  }
}

/**
 * Determine if the mouse position associated with the event is inside
 * of the bounds of the provided element
 */
function isEventInsideOf(event, thumb) {
  var mX = Event.pointerX(event);
  var mY = Event.pointerY(event);
  var tPos = $(thumb).cumulativeOffset();
  var tW = thumb.clientWidth;
  var tH = thumb.clientHeight;
  
  return ((mX >= tPos.left) && (mX <= (tPos.left + tW)) && (mY >= tPos.top) && (mY <= (tPos.top + tH)));
}

/**
 * Display a balloon when a case study thumbnail is moused over
 */
function displayBalloon(event, thumb) {
  var caption = thumb.hiddenTitle;
  if (!caption) {
    thumb.displayBalloon = false;
    return;
  }
  
  thumb.isOver = true;
  if (balloonTimer) {
    clearTimeout(balloonTimer);
    balloonTimer = false;
  }
  
  var balloon = $('info_balloon');
  if (!balloon) {
    balloon = document.createElement('div');
    balloon.id = 'info_balloon';
    balloon.innerHTML = '<div class="top"></div><div id="info_balloon_text"></div><div class="bottom"></div>';
    $$('body').first().appendChild(balloon);
    Event.observe(balloon, 'mouseover', function (event) {
      if (isEventInsideOf(event, this.thumb)) {
        this.isOver = true;
        if (balloonTimer) {
          clearTimeout(balloonTimer);
          balloonTimer = false;
        }
        setBalloonPosition(this, event);
      } else {
        this.isOver = false;
        hideBalloon(this.thumb);
      }
    }.bind(balloon));
    Event.observe(balloon, 'mouseout', function (event) {
      this.isOver = false;
      hideBalloon(this.thumb);
    }.bind(balloon));
    Event.observe(balloon, 'mousemove', function (event) {
      if (isEventInsideOf(event, this.thumb)) {
        setBalloonPosition(this, event);
      } else {
        this.isOver = false;
        hideBalloon(this.thumb);
      }
    }.bind(balloon));
  }
  
  balloon.thumb = thumb;
  $('info_balloon_text').innerHTML = caption;
  
  balloon.style.display = 'block';
  
  setBalloonPosition(balloon, event);
  
  thumb.displayBalloon = true;
}

/**
 * Called when the mouse leaves the thumbnail
 */
function thumbnailOut(event, thumb) {
  thumb.isOver = false;
  hideBalloon(thumb);
}

/**
 * Move the balloon for a case study thumbnail along with the mouse
 */
function moveBalloon(event, thumb) {
  if (!thumb.displayBalloon)
    return;
  
  var balloon = $('info_balloon');
  if (!balloon)
    return;
    
  setBalloonPosition(balloon, event);
}

/**
 * Set up balloon mouseovers for con artists thumbnails
 */
Event.observe(window, 'load', function(event) {
  $$('.con_thumb').each(function(thumb) {
	  if (!thumb.title || thumb.title == '')
	    return;
	  
	  thumb.hiddenTitle = thumb.title;
	  thumb.title = '';

    Event.observe(thumb, 'mouseover', function (e) { displayBalloon(e, thumb); });
    Event.observe(thumb, 'mouseout',  function (e) { thumbnailOut(e, thumb); });
    Event.observe(thumb, 'mousemove', function (e) { moveBalloon(e, thumb); });
  });
});


/**
 * Audio player support.
 */
var ap_instances = new Array();

function ap_stopAll(playerID) {
	for(var i = 0;i<ap_instances.length;i++) {
		try {
			if(ap_instances[i] != playerID) document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 1);
			else document.getElementById("audioplayer" + ap_instances[i].toString()).SetVariable("closePlayer", 0);
		} catch( errorObject ) {
			// stop any errors
		}
	}
}

function ap_registerPlayers() {
	var objectID;
	var objectTags = document.getElementsByTagName("object");
	for(var i=0;i<objectTags.length;i++) {
		objectID = objectTags[i].id;
		if(objectID.indexOf("audioplayer") == 0) {
			ap_instances[i] = objectID.substring(11, objectID.length);
		}
	}
}

var ap_clearID = setInterval( ap_registerPlayers, 100 );

function embed_audio_player ( id, url ) {
  var div = document.createElement("DIV");
  div.id = "audioplayer" + ap_instances.length;
  
  $A($(id).childNodes).each(function ( e ) {
    div.appendChild(e);
  });
  
  $(id).appendChild(div);
  
  swfobject.embedSWF('/swf/audio_player.swf',
    'audioplayer' + ap_instances.length,
    290, 24, '9.0.115', '/swf/expressInstall.swf',
    { playerID : ap_instances.length, soundFile : url },
    { menu : 'false', quality : 'high', wmode : 'transparent' });
}