if(!window['cole']) {
	window['cole'] = {};
	var cole = window['cole'];
}

(function($){
	
	$(document).ready(function() {
		cole.timestamps();
		cole.lightbox();
	});
	
	var protos = {
		'div':	$('<div />'),
		'a':	$('<a />')
	};
	
	var body = $('body');
	
	cole.timestamps = function() {
		var now = Date.parse(new Date());

	    $('abbr.datetime').each(function() {
	      var abbr = $(this);
	      var timestamp = Date.parse(abbr.attr('title'));
	      var diff = now - timestamp;
	      var nicetext = "";
	      time = {};
	      time.year =   Math.floor( diff / 31536000000 ); // 365 time.day. Fuck leap year.;
	      time.month =  Math.floor( ( diff - ( time.year * 31536000000 ) ) / 2592000000 ); // Assuming a month is 30 time.day. Close enough.;
	      time.week =   Math.floor( ( diff - ( time.year * 31536000000 ) - ( time.month * 2592000000 ) ) / 604800000 );
	      time.day =    Math.floor( ( diff - ( time.year * 31536000000 ) - ( time.month * 2592000000 ) - ( time.week * 604800000 ) ) / 86400000 );
	      time.hour =   Math.floor( ( diff - ( time.year * 31536000000 ) - ( time.month * 2592000000 ) - ( time.week * 604800000 ) - ( time.day * 86400000 ) ) / 3600000);
	      time.minute = Math.round( ( diff - ( time.year * 31536000000 ) - ( time.month * 2592000000 ) - ( time.week * 604800000 ) - ( time.day * 86400000 ) - ( time.hour * 3600000 ) ) / 60000 );

	      for(var key in time) {
	        if(time[key] > 0) {
	          switch(key) {
	            case 'minute':
	              if ( time.month > 0 || time.year > 0  || time.hour > 12 || time.week > 0 || time.day > 0 ) { continue; }
	            case 'hour':
	              if( time.week > 0 || time.month > 0 || time.year > 0 ) { continue; }
	            case 'day':
	              if( time.year > 0 || ( time.week > 0 && time.month > 0 ) ) { continue; }
	            default:
	              // keep going
	          }
	          nicetext = nicetext + " " + time[key] + " " + key + (time[key] > 1 ? "s," : ",");
	        }
	      }
	      if(diff <= 30000) {
	        nicetext = "now"
	      } else if(nicetext == "") {
	        nicetext = abbr.text();
	      } else {
	        nicetext = nicetext.substr(0,nicetext.length - 1) + " ago";
	      }

	      abbr.text(nicetext);
	    });
	}
	
	cole.lightbox = function() {
		var links = $('a[rel=lightbox]');
		var overlay = $('#overlay').size() ? $('#overlay') : protos.div.clone().attr({'id': 'overlay'}).appendTo(body);
		var lightbox = $('#lightbox').size() ? $('#lightbox') : protos.div.clone().attr({'id': 'lightbox'}).appendTo(body);
		var target = $('#lb-target').size() ? $('#lb-target') : protos.div.clone().attr({'id': 'lb-target'}).appendTo(lightbox);
		var controls = $('#lb-controls').size() ? $('#lb-controls') : protos.div.clone().attr({'id': 'lb-controls'}).appendTo(lightbox);
		var title = $('#lb-title').size() ? $('#lb-title') : protos.div.clone().attr({'id': 'lb-title'}).prependTo(lightbox);
		var close = $('#lb-close').size() ? $('#lb-close') : protos.a.clone().attr({'id': 'lb-close', 'href': '#close', 'title': 'Close'}).prependTo(lightbox).html('&times;');
		var prev = $('#lb-prev').size() ? $('#lb-prev') : protos.a.clone().attr({'id': 'lb-prev', 'href': '#previous', 'title': 'Previous'}).prependTo(controls).html('&laquo;');
		var next = $('#lb-next').size() ? $('#lb-next') : protos.a.clone().attr({'id': 'lb-next', 'href': '#next', 'title': 'Next'}).appendTo(controls).html('&raquo;');
		
		close.add(overlay).click(function(c) {
			c.preventDefault();
			overlay.fadeOut(1000);
			lightbox.fadeOut(1000, function(){
				target.empty();
			});
		});
		
		overlay.height($(window).height());
		
		prev.add(next).click(function(c) {
			c.preventDefault();
			var current = links.filter('[href=' + target.find('img').attr('src') + ']');
			var lbIndex = parseInt(current.attr('lb-index'), 10);
			if($(this).is('#lb-next')) {
				var to = links.size() - 1 > lbIndex ? links.eq(lbIndex + 1) : links.eq(0);
			} else {
				var to = lbIndex > 0 ? links.eq(lbIndex - 1) : links.eq(links.size() - 1);
			}
			to.click();
		});
		
		lightbox.click(function(c) {
			c.stopPropagation();
		});
		
		lightbox.css({
			'margin-left': 0,
			'left': ((body.width() - lightbox.width()) / 2) + 'px'
		});
		
		var loadImage = function(source) {
			var img = new Image();
			img.onload = function() {
				var maxHeight = $(window).height() - 288;
				var minWidth = parseInt(lightbox.css('min-width'), 10);
				if( img.height >=  maxHeight ) {
					img.width = (maxHeight / img.height) * img.width;
					img.height = maxHeight;
				}
				target.animate({
					'height': img.height
				}, 500);
				lightbox.animate({
					'width': (img.width > minWidth ? img.width : minWidth) + 50,
					'left': ((body.width() - ((img.width > minWidth ? img.width : minWidth) + 50)) / 2) + 'px'
				});
				$(img).hide().appendTo(target).fadeIn(500);
			};
			img.src = source;
		}
		
		links.each(function(index) {
			var link = $(this);
			link.attr({'lb-index': index});
			link.click(function(c) {
				c.preventDefault();
				title.text(link.attr('title'));
				if(!overlay.is(':visible')) {
					target.empty();
					overlay.fadeIn(1000);
					lightbox.fadeIn(1000, function() {
						loadImage(link.attr('href'));
					});
				} else {
					target.children().fadeOut(1000, function(){
						target.children().remove();
						loadImage(link.attr('href'));
					});
				}
			});
		});
	}
})(jQuery);