/*
 * DevEiate.org Script Library
 * $Id$
 *
 * Author: Michael Granger <devEiant{at}devEiate{dot}org>
 *
 * Depends on jQuery 1.4.2
 */

const DeliciousFeedUrl = 'http://feeds.delicious.com/v2/json/rubymage?callback=?';
const FlowplayerSWF = '../swf/flowplayer-3.1.1.swf';

/* Provide console simulation for firebug-less environments */
if (!("console" in window) || !("groupEnd" in console)) {
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {};
}


function showDeliciousItems( items, status ) {
	console.group( "Showing delicious items" );
	console.debug( "Got items: %o", items );
	var list = $('#delicious-bookmarks');
	var item_template = list.children('li').remove();
	console.debug( "Using template %o for items in %o", list );

	for ( var i in items ) {
		var item = items[i];
		var content = item_template.clone();

		content.children('a').attr( 'href', item['u'] );
		content.children('a').html( item['d'] );

		console.debug( "Appending %o to %o", content, list );
		content.appendTo( list );
	}
	console.groupEnd();
};

function load_delicious_feed() {
	$.getJSON( DeliciousFeedUrl, {count: 5}, showDeliciousItems );
};

const Schemes = {
	mail: 'ma' + 'il' + 'to:%s',
	chat: 'xm' + 'pp:' + '%s?roster;name=%s'
};
function make_addr_uri( selector, scheme ) {
	var target = $(selector);
	var em = target.text().replace( /^\s+|\s+$/g, '' );
	var name = target.attr( 'title' );
	var tmpl = Schemes[ scheme ];

	console.debug( "Constructing a %s uri: content is: %o", scheme, em );
	return sprintf( tmpl, em.split('').reverse().join(''), name );
};

function redirect_mailclick( ev ) {
	var loc = make_addr_uri( ev.target, 'mail' );
	console.debug( "Redirecting mail link to: %s.", loc );
	document.location = loc;
	return false;
};

function redirect_chatclick( ev ) {
	var loc = make_addr_uri( ev.target, 'chat' );
	console.debug( "Redirecting chat link to: %s.", loc );
	document.location = loc;
	return false;
};

function hook_obfuscated_links() {
	$('a.email').click( redirect_mailclick );
	$('a.chat').click( redirect_chatclick );
};

function load_fullsize_image( overlay ) {
	var wrap = this.getContent().find("div.wrap");
	var imgurl = this.getTrigger().attr("href");
	this.getTrigger().css({ opacity: 0.2 });

	console.debug( "Overlaying fullsize image %s", imgurl );

	wrap.html( '<img src="' + imgurl + '" alt="" />' );
};

function reveal_thumbnail( overlay ) {
	this.getTrigger().css({ opacity: 1.0 });
};

function hook_thumbnails() {
	console.debug( "Hooking thumbnails." );
	var sel = $('a:has(img.thumbnail)').overlay({
		onBeforeLoad: load_fullsize_image,
		onClose: reveal_thumbnail
	});
	console.debug( "  hooked %d.", sel.length );
};

/* Start doing stuff as soon as the DOM is ready */
$(document).ready( function() {
	load_delicious_feed();
	hook_obfuscated_links();
	hook_thumbnails();

	console.debug( "Syntax highlighting example sections..." );
	SyntaxHighlighter.highlight();
});


