/* Author:

*/
(function($) {
  $.fn.tooltip = function(options){
	  var opts = $.extend({}, $.fn.tooltip.defaults, options);

	  return this.each(function(){
	    var me = $(this);
	    var originalTitle = "";
    	originalTitle = me.attr('title');
    	me.removeAttr("title");
        me.hover(function(e) {
	        var titleDisplayed = opts.content == null? originalTitle : opts.content;
	        if(titleDisplayed.length>0){
		  	  	  $("body").append("<div id='tooltip'>"+titleDisplayed+"</div>");
		  	  	  var tip=$("#tooltip");
		  	  	  tip.css("max-width",opts.maxWidth);
		  	  	  var offset=me.offset();
			      var targetLeft=offset.left+Math.round(me.width())/2-Math.round(tip.width()/2);
				  var targetTop=offset.top-tip.outerHeight()+opts.yOffset;
				  tip
		  	  	    .animate({opacity: 1.0}, opts.delay)
		  	  	  	.css("top", (targetTop) + "px")
		  	  	  	.css("left",(targetLeft)+ "px")
		  	  	  	.fadeIn(60);
	  	  	}
	      },function(){
	        $("#tooltip").remove();
	      });
	    });

	};

  $.fn.tooltip.defaults = {
    content: null,
    xOffset: 0,
    yOffset: -10,
    /* maxWidth is needed, IE7 creates full with cells */
    maxWidth: 120,
    delay: 0
  };
})(jQuery);

Date.prototype.toISO8601 = function(date) {
          var pad = function (amount, width) {
              var padding = "";
              while (padding.length < width - 1 && amount < Math.pow(10, width - padding.length - 1))
                  padding += "0";
              return padding + amount.toString();
          };
          date = date ? date : new Date();
          var offset = date.getTimezoneOffset();
          return pad(date.getFullYear(), 4)
              + "-" + pad(date.getMonth() + 1, 2)
              + "-" + pad(date.getDate(), 2)
              + "T" + pad(date.getHours(), 2)
              + ":" + pad(date.getMinutes(), 2)
              + ":" + pad(date.getUTCSeconds(), 2)
              + (offset > 0 ? "-" : "+")
              + pad(Math.floor(Math.abs(offset) / 60), 2)
              + ":" + pad(Math.abs(offset) % 60, 2);
      };


var count=0;
var list=[
                                  {
                                      service: "github",
                                      user : "thomasf"
                                  },
                                  {
                                      service: "lastfm",
                                      user: "googli"
                                  },
                                  {
                                      service: "twitter",
                                      user: "maximega"
                                  },
                                  {
                                      service: "youtube",
                                      user: "pleggli"
                                  }
                              ];

$(document).ready(function() {
                      $("header nav div").tooltip();
                      $("abbr.timeago").timeago();
                      $("#lifestream").lifestream(
                          {
                              limit: 100,
                              list:list,
                              feedloaded: function(){
                                  count++;
                                  // Check if all the feeds have been loaded
                                  if( count === list.length )
                                  {
                                      $("#lifestream li").each(function()
                                                               {
                                                                   var element = $(this),
                                                                   date = new Date(element.data("time"));
                                                                   element.append(' <abbr class="timeago" title="' + date.toISO8601(date) + '">' + date + "</abbr>");
                                                               });
                                      $("#lifestream .timeago").timeago();
                                  }
                              }

                          });

                  });


