/*
	general.js

	Created by John Hunter on 2008-09-07.
	Licence CC-BSD <http://creativecommons.org/licenses/BSD/>
	www.johnhunter.info
	
*/


/**
 * Miscellaneous dom manipulation
 */
$(document).ready(function() {
	if ($.ifixpng) $('#branding h1').ifixpng();
	
	var contact = $('#contact, a.contact');
	contact.attr('href', contact.attr('href').
			replace(' (at) ','@').
			replace(/ \(dot\) /g, '.'));
});

/**
 * Gets a json list of photos and displays them as thumbnails.
 * Based on code from http://twitter.com/javascripts/blogger.js
 */
var twitterFeed = function ($) {
	var that = {},
		serviceUrl = 'http://twitter.com/statuses/user_timeline/johnhunter.json?count=',
		count = 5,
		container = null;
	
	
	function displayTweets (feedData) {
		var list = $('<ul></ul>'),
			ti;

		if (!feedData || !feedData.length) return;

		for (var i = 0; i < feedData.length; i++) {
			ti = feedData[i];
			list.append('<li><span>'+ createHyperlinks(ti.text) +'</span> &raquo; <a href="http://twitter.com/'+ 
				ti.user.screen_name +'/statuses/'+ti.id+'">'+ 
				relativeTime(ti.created_at) +'</a></li>');
		}
		container.empty().append(list).css('min-height', '');	
	}
	
	function createHyperlinks (text) {
		return text.replace(/(http:\/\/)(\S*)\b/gi, '<a href="$1$2">$1$2</a>');
	}

	function relativeTime(timeValue) {
		var values = timeValue.split(" ");
		timeValue = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
		var parsed_date = Date.parse(timeValue);
		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';
		if (delta < 120) return 'about a minute ago';
		if (delta < (60*60)) return (parseInt(delta / 60)).toString() + ' minutes ago';
		if (delta < (120*60)) return 'about an hour ago';
		if (delta < (24*60*60)) return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
		if (delta < (48*60*60)) return '1 day ago';
		else return (parseInt(delta / 86400)).toString() + ' days ago';
	}
	
	
	// init on dom load
	$(function () {
		container = $('#tweet');
		$.getJSON(serviceUrl + count +'&callback=?' , displayTweets);
	});
	
	return that;
}(jQuery);



/**
 * Gets a json list of photos and displays them as thumbnails.
 */
var photoList = function ($) {
	var that = {},
		serviceUrl = "http://api.flickr.com/services/feeds/photos_public.gne?id=20287332@N00&",
		photoList = [],
		count = 3,
		container = null;
	
	function displayPhotos (feedData) {
		photoList = feedData.items;
		
		if (!photoList || !photoList.length) return;
			/*
				photoList[n].
					media.m (image src)
					title
					tags
					date_taken
					link
			*/
		var p = null,
			len = Math.min(count, photoList.length);
			list = $('<span></span>');
		
		for (var i = 0; i < len; i++) {
			p = photoList[i];
			p.media.t = convertFlickrMediaSrc(p.media.m, 'thumbnail');
			list.append('<a href="'+ p.link +'"><img src="'+ p.media.t +'" alt="'+ p.title +' taken on '+ p.date_taken +'"></a>');
		}
		container.empty().append(list);
	}
	
	function convertFlickrMediaSrc (source, size) {
		var suffixes = { 
			thumbnail:'_t.jpg',small:'_m.jpg',medium:'.jpg',large:'_b.jpg',original:'_o.jpg'
		};
		return source.replace(/_[a-z].jpg|.jpg/, suffixes[size]);
	}
	

	// init on dom load
	$(function () {
		container = $('#flickr_badge_wrapper');
		$.getJSON(serviceUrl + 'lang=en-us&format=json&jsoncallback=?', displayPhotos);
	});


	return that;
}(jQuery);


/**
 * Gets a json list of your LibraryThing books and displays them as thumbnails.
 * Requires a user key
 */
var libraryList = function ($) {
	var that = {},
		serviceUrl = 'http://www.librarything.com/api/json_books.php?userid=johnhunter&key=1196411901&max=',
		linkUrl = 'http://www.librarything.com/work/book/',
		count = 2,
		container;
		
	function displayBooks (data) {
		var books = data.books,
			b,
			list = $('<span></span>');
		
		for (var i in books) {
			b = books[i];
			list.append('<a href="'+ linkUrl + b.book_id +'" class="LTitem"><img src="'+ b.cover +'" alt="'+ b.title +'"></a>');
		}
		$('span', container).remove();
		container.prepend(list);
	}
	
	// init on dom load
	$(function () {
		container = $('#LTwrapper');
		$.getJSON(serviceUrl + count +'&callback=?' , displayBooks);
	});	
		
	return that;
}(jQuery);
