function twitterCallback2(obj) {
	var twitters = obj;
	var statusHTML = "";
	var username = "";
	for (var i=0; i<twitters.length; i++){
		username = twitters[i].user.screen_name
		statusHTML += ('<p>'+twitters[i].text+' <a href="http://twitter.com/lhouserising">More updates</a>.</p>')
	}
	document.getElementById('twitter_update_list').innerHTML = statusHTML;
}

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 'One day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}

function activateCDInfoBubbles() {
  $('.info').each(function () {
    // options
    var distance = 10;
    var time = 250;
    var hideDelay = 100;

    var hideDelayTimer = null;

    // tracker
    var beingShown = false;
    var shown = false;
    
    var trigger = $('.trigger', this);
    var popup = $('.popup', this).css('opacity', 0);

    // set the mouseover and mouseout on both elements
    $([trigger.get(0), popup.get(0)]).hoverIntent(function () {
      // stops the hide event if we move from the trigger to the popup element
      if (hideDelayTimer) clearTimeout(hideDelayTimer);

      // don't trigger the animation again if we're being shown, or already visible
      if (beingShown || shown) {
        return;
      } else {
        beingShown = true;

        // reset position of popup box
        popup.css({
          top: -117,
          left: -58,
          display: 'block' // brings the popup back in to view
        })

        // (we're using chaining on the popup) now animate it's opacity and position
        .animate({
          top: '-=' + distance + 'px',
          opacity: 1
        }, time, 'swing', function() {
          // once the animation is complete, set the tracker variables
          beingShown = false;
          shown = true;
        });
      }
    }, function () {
      // reset the timer if we get fired again - avoids double animations
      if (hideDelayTimer) clearTimeout(hideDelayTimer);
      
      // store the timer so that it can be cleared in the mouseover if required
      hideDelayTimer = setTimeout(function () {
        hideDelayTimer = null;
        popup.animate({
          top: '-=' + distance + 'px',
          opacity: 0
        }, time, 'swing', function () {
          // once the animate is complete, set the tracker variables
          shown = false;
          // hide the popup entirely after the effect (opacity alone doesn't do the job)
          popup.css('display', 'none');
        });
      }, hideDelay);
    });
  });
  
}

function activateSongPreviews() {
  $(".track-preview").each(function(){
    var wrapper = $(this);
    
    var imgSrc = wrapper.find('img').attr('src');

    var songName = imgSrc.split(/\//).pop().split(/\./)[0];
    
    var songId = songName.split(/-/); songId.shift(); songId = songId.join('-');
    
    var themeDir = imgSrc.split(/\//); themeDir.pop(); themeDir.pop(); themeDir = themeDir.join('/')
    
    var previewsDir = themeDir + '/previews';
    
    var previewPath = previewsDir + '/' + songName + ".mp3";
    
    wrapper.prepend('<img src="'+themeDir+'/images/play-button.png" id="play-'+songId+'" class="preview-button"/><img src="'+themeDir+'/images/stop-button.png" id="stop-'+songId+'" class="preview-button"/>');
    
    var player = wrapper.before('<div id="'+songId+'">').prev();
    
    $(player).jPlayer({
  		ready: function () {
  			$(this).setFile(previewPath);
  		},
  		swfPath: themeDir+'/js',
  	})
  	.jPlayerId("play", 'play-'+songId)
  	.jPlayerId("pause", 'stop-'+songId);
  	
  	$('#'+songId).onSoundComplete( function() {
  	  alert('done!');
      // $(this).play(); // Auto-Repeat
    });
		
  	// .jPlayerId("stop", "player_stop");
  	
  })
}

var activateBioToggle = function() {
  var headerHeight = $('#header').height();
  $('a#bio-toggle').toggle(function() {
    $(this).find('img.less').toggle(); $(this).find('img.more').toggle();
    // $("#header").animate({ "height": "650" }, 500);
    $('#header').animate({'height': '+='+$('#bio').height()});
  }, function() {
    $('#header').animate({'height': headerHeight});
    $(this).find('img.less').toggle(); $(this).find('img.more').toggle();
  });
}

var $ = jQuery.noConflict();
$(document).ready(function () {
  activateCDInfoBubbles();
  activateSongPreviews();
  activateBioToggle();
});
