// cybaea.js - JavaScript for cybaea.net
// Copyright © 2011 CYBAEA Limited (http://www.cybaea.net/)

//////
// Original from http://ejohn.org/files/pretty.js somewhat modified
// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(time){
	var date = new Date((time || "").replace(/[ ]/,"T")),
		diff = (((new Date()).getTime() - date.getTime()) / 1000),
		day_diff = Math.floor(diff / 86400);
			
	if ( isNaN(day_diff) )
		return time;

	if ( day_diff < 0 || day_diff >= 31 )
		return date.toLocaleDateString();

			
	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

// If jQuery is included in the page, adds a jQuery plugin to handle it as well
if ( typeof jQuery != "undefined" )
	jQuery.fn.prettyDate = function(){
		return this.each(function(){
			var orig = jQuery(this).text();
			var date = prettyDate(orig);
			if ( date ) {
				jQuery(this).attr("title", orig);
				jQuery(this).text( date );
			}
		});
	};


// jQuery
jQuery(document).ready(function(){
	jQuery("#deeplinkbig").jCarouselLite({
		mouseWheel: true,
		auto: 10000,
		speed: 2000,
		circular: true,
		visible: 1
	}).children("ul").css("visibility", "visible");
	jQuery("#deeplinksmall").jCarouselLite({
		mouseWheel: true,
		auto: 20000,
		speed: 2000,
		circular: true,
		visible: 1,
		vertical: true
	}).children("ul").css("visibility", "visible");
	jQuery("input:submit, input:reset, button").button();
	jQuery("#navigation li").hover(
		function () { jQuery(this).addClass("ui-state-hover"); },
		function () { jQuery(this).removeClass("ui-state-hover"); }
	)
	.focusin(
		function () { jQuery(this).addClass("ui-state-focus"); }
	)
	.focusout(
		function () { jQuery(this).removeClass("ui-state-focus"); }
	);
	jQuery(".blogEntries").accordion({
		autoHeight: false, 
		navigation: true, 
		collapsible: true
	});
	jQuery(".link a").button();
	jQuery(".accordion").accordion({
		navigation: true,
		collapsible: false
	});
    jQuery(".sortable").sortable({
		placeholder: "ui-state-highlight",
		distance: 5
	});
	jQuery(".sortable > div").addClass("ui-widget ui-helper-clearfix ui-corner-all");
	jQuery("span.date").prettyDate();
});

