
// avoid javascript errors on browsers that aren't using FireBug.
//
if ( ! window.console || ! console.firebug )
{
    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() {}
}


$(document).ready(function()
{
    // activate current 'area' button
    var area = document.location.pathname.match(/^\/([\w\-]+)/);
    if ( area ) {
        area = area[1];
    }
    else {
        area = 'about';
        if ( document.location.hostname.match(/(projects|trac).martini.nu/) ) area = 'projects';
    }
    $("#nav-" + area).addClass('nav-active');

    // automatic drop cap for 1st paragraph
    if ( area != 'projects' && area != 'blog' ) drop_caps();
	
    // inlined "pretty" quotes
    $(".quote").each( pretty_quote );	

    harvest_prevention();

    // auto-update current jabber status
    $("#xmpp").JabberPresence({
        parsexml: true,
        handler : 'jabber-presence',
        host    : location.hostname,
        port    : 80,
        id      : 'mahlon@chat.martini.nu'
    });

    // don't be annoying
    if ( $.cookie('martini-visual') ) return;

    // slide open the title and nav
    if ( area != 'blog' ) {
        $("#titlearea").hide(); // safari workaround
        $("#inner-content").hide();
        $("#titlearea:hidden").slideDown('slow', function(){
            $("#inner-content:hidden").fadeIn('slow');
        });
    }

    // set cookie so the visual effects aren't annoying
    var now = new Date();
    var expire = new Date( now.getTime() + ( 1000 * 60 * 10 ) );  // 10 minutes
    $.cookie('martini-visual', '1', { expires: expire });
});


// Fix various link types to go to the right spot.
//
function harvest_prevention()
{
    $('a.email').click( function() {
        var email = $(this).html().split('').reverse().join('');
        $(this).attr( 'href', 'mailto:' + email );
    });

    $('a.xmpp').click( function() {
        var roster = '?roster;name=Mahlon%20E.%20Smith';
        var xmpp = $(this).html().split('').reverse().join('');
        $(this).attr( 'href', 'xmpp:' + xmpp + roster );
    });

    $('a.finger').click( function() {
        var finger = $(this).html().split('').reverse().join('');
        $(this).attr( 'href', 'finger:' + finger );
    });
}


// Based on code from http://www.learningjquery.com/2006/12/multiple-fancy-drop-caps,
// dynamically replace the first letter on the page with a nice drop-letter graphic,
// which is generated from a truetype font in mason-land.
//
function drop_caps( font, size )
{
	if ( ! font ) font = 'doves';
	if ( ! size ) size = 36;
	
	var first_paragraph = $('#inner-content p')[0];
	if ( ! first_paragraph ) return false;

	// find the first text node
	var node = first_paragraph;
	while ( node.childNodes.length ) {
		node = node.firstChild;
	}

	var text = node.nodeValue;
	if ( ! text ) return false;

	var first_letter = text.substr(0,1);
	var match = /[a-zA-Z]/.test( first_letter );
		
	if ( match ) {
		node.nodeValue = text.slice(1); // trim letter
		$('<img />')
			.attr('src','/ttf/' + font + '/' + size + '/' + '0,0,0' + '/' + first_letter.toUpperCase() + '.png')
			.attr('alt',first_letter)
			.addClass('drop-cap')
			.prependTo( first_paragraph );
	}
	
	return true;
}


// Replace quoted text with an inlined image.
//
function pretty_quote()
{
	text = $(this).html();
	image = $('<img />')
		.attr('src', '/ttf/greyscale_i/16/0,0,0/' + text + '.png')
		.attr('alt', text);
	$(this).html( image )
}

