function initMouseEvents() {
  var movement = document.getElementById('movement-mouse-event');
  var truth = document.getElementById('truth-mouse-event');
  var tools = document.getElementById('tools-mouse-event');
  xb.addEvent(movement, 'mouseenter', enterMovement, false);
  xb.addEvent(truth, 'mouseenter', enterTruth, false);
  xb.addEvent(tools, 'mouseenter', enterTools, false)    
}
function enterMovement(e)
{
  $('movement-effect').hide();
  Effect.Appear('movement-effect', { duration: 0.3 });
}

function enterTruth(e)
{
  $('truth-effect').hide();
  Effect.Appear('truth-effect', { duration: 0.3 });
}

function enterTools(e)
{
  $('tools-effect').hide();
  Effect.Appear('tools-effect', { duration: 0.3 });
}

function doTweets(obj) {
  var twitters = obj;
  var statusHTML = "";
  var username = "";
  for (var i=0; i<twitters.length; i++)
  {
    username = twitters[i].user.screen_name
    statusHTML += ("<li class='tweet-"+ (i+1) +"'><span class='body'>"+htmlifyTweet(twitters[i].text)+"</span> <a class='tweet-link' href='http://twitter.com/"+username+"/statuses/"+twitters[i].id+"'>"+relative_time(twitters[i].created_at)+"</a></li>")
  }
  document.getElementById('b4l-twitter-list').innerHTML = statusHTML;
}

function htmlifyTweet(tweet) {
  var txt = tweet.replace(
    /(https?:\/\/[-a-z0-9._~:\/?#@!$&\'()*+,;=%]+)/ig,
    '<a href="$1">$1</a>'
  ).replace(
    /@+([_A-Za-z0-9-]+)/ig, 
    '<a href="http://twitter.com/$1">@$1</a>'
  ).replace(
    /#+([_A-Za-z0-9-]+)/ig,
    '<a href="http://search.twitter.com/search?q=%23$1">'
        +'#$1</a>'
  );
  return txt
}

function relative_time(time_value) {    
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];

  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}

$j(function() {

  $j(".aside:first").height($j(".houston-main:first").height());
  
  $j("#play-houston-video").click(function() {
    var videoLeft = ($j(window).width() / 2) - ($j("#video-container").width() / 2);
    var videoTop = ($j(window).height() / 2) - ($j("#video-container").height() / 2);
    var videoPositioning = "fixed";
    
    if (videoLeft < 0) { videoLeft = 0}
    if (videoTop < 0) { videoTop = 0; videoPositioning = "absolute";}

    $j("#overlay").fadeIn();
    $j("#video-container").css({left:videoLeft, top:videoTop, position:videoPositioning});
    $j("#video-container").fadeIn();
    $j("#close-video").click(function() {
      $j("#video-container").fadeOut();
      $j("#overlay").fadeOut();
      return false;
    });
    $j("#overlay").click(function() {
      $j("#video-container").fadeOut();
      $j("#overlay").fadeOut();
    });
    return false;
  });
  
  $j("#play-houston").click(function () {
    var videoLeft = ($j(window).width() / 2) - ($j("#video-container").width() / 2);
    var videoTop = ($j(window).height() / 2) - ($j("#video-container").height() / 2);
    var videoPositioning = "fixed";
    
    if (videoLeft < 0) { videoLeft = 0}
    if (videoTop < 0) { videoTop = 0; videoPositioning = "absolute";}
    
    hideSIFR();
    
    $j("#overlay").fadeIn();
    $j("#video-container").css({left:videoLeft, top:videoTop, position:videoPositioning});
    $j("#video-container").fadeIn();
    $j("#houston-video").fadeIn();
    $j("#close-houston").click(function() {
      showSIFR();
      $j("#video-container").fadeOut();
      $j("#overlay").fadeOut();
      $j("#houston-video").fadeOut();
      return false;
    });
    $j("#overlay").click(function() {
      showSIFR();
      $j("#video-container").fadeOut();
      $j("#overlay").fadeOut();
      $j("#houston-video").fadeOut();
    });
  });
  
  $j("#play-numbers").click(function () {
    var videoLeft = ($j(window).width() / 2) - ($j("#video-container").width() / 2);
    var videoTop = ($j(window).height() / 2) - ($j("#video-container").height() / 2);
    var videoPositioning = "fixed";
    
    if (videoLeft < 0) { videoLeft = 0}
    if (videoTop < 0) { videoTop = 0; videoPositioning = "absolute";}
    
    hideSIFR();
    
    $j("#overlay").fadeIn();
    $j("#video-container").css({left:videoLeft, top:videoTop, position:videoPositioning});
    $j("#video-container").fadeIn();
    $j("#numbers-video").fadeIn();
    $j("#close-numbers").click(function() {
      showSIFR();
      $j("#video-container").fadeOut();
      $j("#overlay").fadeOut();
      $j("#numbers-video").fadeOut();
      return false;
    });
    $j("#overlay").click(function() {
      showSIFR();
      $j("#video-container").fadeOut();
      $j("#overlay").fadeOut();
      $j("#numbers-video").fadeOut();
    });
  });

  $j(".watch-the-view").click(function () {
    var videoLeft = ($j(window).width() / 2) - ($j("#video-container").width() / 2);
    var videoTop = ($j(window).height() / 2) - ($j("#video-container").height() / 2);
    var videoPositioning = "fixed";
    
    if (videoLeft < 0) { videoLeft = 0}
    if (videoTop < 0) { videoTop = 0; videoPositioning = "absolute";}
    
    hideSIFR();
    
    $j("#overlay").fadeIn();
    $j("#video-container").css({left:videoLeft, top:videoTop, position:videoPositioning});
    $j("#video-container").fadeIn();
    $j("#view-video").fadeIn();
    $j("#close-view").click(function() {
      showSIFR();
      $j("#video-container").fadeOut();
      $j("#overlay").fadeOut();
      $j("#view-video").fadeOut();
      return false;
    });
    $j("#overlay").click(function() {
      showSIFR();
      $j("#video-container").fadeOut();
      $j("#overlay").fadeOut();
      $j("#view-video").fadeOut();
    });
    return false;
  });
  
  $j("a.lite-me").lightBox();
  
})

function hideSIFR() {
  $j(".sIFR-replaced").each(function() {
    $j(this).hide();
  });
}

function showSIFR() {
  $j(".sIFR-replaced").each(function() {
    $j(this).show();
  });
}