jQuery(function($) {
	$('a.lightbox').lightBox({
		imageLoading: 'http://media.otherinbox.com/images/lightbox/lightbox-ico-loading.gif',
		imageBtnClose: 'http://media.otherinbox.com/images/lightbox/lightbox-btn-close.gif',
		imageBtnPrev: 'http://media.otherinbox.com/images/lightbox/lightbox-btn-prev.gif',
		imageBtnNext: 'http://media.otherinbox.com/images/lightbox/lightbox-btn-next.gif',
		imageBlank: 'http://media.otherinbox.com/images/blank.gif'
	});
	
	$('.carousel').jcarousel({
	  buttonNextHTML  : '<div class="jcarousel-next" >next <img class="icon" alt="Icon" src="http://media.otherinbox.com/images/blank.gif"/></div>',
	  buttonPrevHTML  : '<div class="jcarousel-back" ><img class="icon" alt="Icon" src="http://media.otherinbox.com/images/blank.gif"/> back</div>',
	  scroll          : 2
	});
	
	/*
	 *	Attempt to control the grid by adding margins to embeds that make up the difference in height between the
	 *	object and a divisor of lineHeight (the line-height)
	 */
	$('.embedded_content').each(function(i, obj) {
	  var $obj = $(obj);
	  var lineHeight = parseInt($obj.css('line-height').replace('px',""), 10);
	  var mod = $obj.height() % lineHeight;
		if ( mod != 0 ) {
	  	$obj.css('margin-bottom', lineHeight + (lineHeight -  mod) );
		}
	});
	
	/* 
	 * Twitter code from http://www.damnralph.com/2007/11/20/PullingTwitterUpdatesWithJSONAndJQuery.aspx
	 */	
    var twitterUser = "otherinbox";
    // var twitterUrl = "http://twitter.com/favorites/"+twitterUser+".json?page=1&callback=?"; 
    var twitterUrl = "http://twitter.com/statuses/user_timeline/"+twitterUser+".json?page=1&callback=?";
    
    $.getJSON(twitterUrl, 
         function(data){ 
            $("#tweets ul").empty();
             $.each(data, function(i, item) {
               if(i < 6 ) {     
                 twitterStatusUrl = "https://twitter.com/"+item.user.screen_name+"/status/"+item.id;   
                 $("#tweets ul").append("<li>"  
                       + item.text.linkify() 
                       + " <a href='"+twitterStatusUrl+"' class='created_at'>"  
                       + " said "+item.user.screen_name + " "
                       + relative_time(item.created_at)                         
                       + "</a></li>"); 
               }
             });
             $("#tweets ul li:first").addClass("first");
             $("#tweets ul li:last").addClass("last");
         });
});

String.prototype.linkify = function() {
  return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(m) {
   return m.link(m);
  }).atify();
};
String.prototype.atify = function() {
  return this.replace(/@[\w]+/g, function(m) {
   return "<a href='http://www.twitter.com/"+m.replace('@','')+"'>"+m+"</a>";
  });
};
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, 10);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  var r = '';
  if (delta < 60) {
    r = 'a minute ago';
  } else if(delta < 120) {
    r = 'a few minutes ago';
  } else if(delta < (45*60)) {
    r = (parseInt(delta / 60, 10)).toString() + ' minutes ago';
  } else if(delta < (90*60)) {
    r = 'an hour ago';
  } else if(delta < (24*60*60)) {
    r = 'about ' + (parseInt(delta / 3600, 10)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    r = '1 day ago';
  } else {
    r = (parseInt(delta / 86400, 10)).toString() + ' days ago';
  }

  return r;
}
function twitter_callback ()
{
 return true;
}


