var NAV_HELP_TEXT = new Array(
	null,
	'Software developed for the Web Developer. Tools built for Mac OS X include Clover and ApatchMe are available for download here.',
	'Projects that mainly involve tinkering with Open Source software. Of interest might be the Amarok and KDE on Mac OS X under Qt/Mac projects.',
	'The archive of all posts made to this blog to date. This blog has ceased to be personal in nature, though it is still creative.',
	'Portfolio encompassing Web Design / Web Development work, Artwork and Photography. This is an external site and will open in a new window.'
);

//var g_effects = new Graphics();

if ( document.onDOMContentLoaded )
document.addEventListener('DOMContentLoaded', function()
{
//	if ( document.getElementById('navigation') ) setupNavHelp();

	if ( document.getElementById('comment_captcha') && document.getElementById('i_spam') ) setupSpamForm();
	
	if ( document.getElementById('comment_form') ) document.getElementById('comment_form').onsubmit = submitDefaultForm;

	setupDefaultFormText();
	
	setupAnalytics();
}, false );
else if ( document.addEventListener )
window.addEventListener('load', function()
{
//	if ( document.getElementById('navigation') ) setupNavHelp();

	if ( document.getElementById('comment_captcha') && document.getElementById('i_spam') ) setupSpamForm();
	
	if ( document.getElementById('comment_form') ) document.getElementById('comment_form').onsubmit = submitDefaultForm;

	setupDefaultFormText();
	
	setupAnalytics();
}, false );
else
window.attachEvent('onload', function()
{
//	if ( document.getElementById('navigation') ) setupNavHelp();

	if ( document.getElementById('comment_captcha') && document.getElementById('i_spam') ) setupSpamForm();
	
	if ( document.getElementById('comment_form') ) document.getElementById('comment_form').onsubmit = submitDefaultForm;
	
	setupDefaultFormText();
	
	setupAnalytics();
} );


function setupNavHelp()
{
	var parent = document.getElementById('navigation');
	var nav_items = parent.getElementsByTagName('a');
	
	for (var x = 1; x < nav_items.length; x++)
	{
		addEvent( nav_items[x], 'mouseover', showNavHelp, true );
		addEvent( nav_items[x], 'mouseout', hideNavHelp, true );
	}
}

function showNavHelp( e )
{
	var caller = (this.parentNode)? this : window.event.srcElement;

	// get the parent navigation list
	var parent_ul = caller.parentNode;
	while ( parent_ul.nodeName != 'UL' ) parent_ul = parent_ul.parentNode;
	
	// get the index of this list item
	var nav_index = 0;
	while ( parent_ul.getElementsByTagName('a')[nav_index] != caller ) nav_index++;
	
	// get the text to display
	var contents = NAV_HELP_TEXT[nav_index];
	
	// write in the help container if it doesn't exist
	if ( !document.getElementById( 'navigation_info' ) )
	{
		// create the help html
		var help_div = document.createElement( 'div' );
		help_div.setAttribute( 'id', 'navigation_info' );
		help_div.style.opacity = 0;
		help_title = document.createElement( 'h6' );
		help_div.appendChild( help_title );

		// write in help
		document.getElementById('header').appendChild( help_div );
	}
	else var help_div = document.getElementById( 'navigation_info' );
	
	// remove content from help div
	while( help_div.getElementsByTagName( 'p' ).length ) help_div.removeChild( help_div.getElementsByTagName( 'p' )[0] );
	while( help_div.getElementsByTagName( 'h6' )[0].childNodes.length ) help_div.getElementsByTagName( 'h6' )[0].removeChild( help_div.getElementsByTagName( 'h6' )[0].firstChild );
	
	// add in new content
//	help_div.getElementsByTagName( 'h6' )[0].appendChild( document.createTextNode( caller.getElementsByTagName( 'img' )[0].alt ) );
	var help_p = document.createElement( 'p' );
	help_p.appendChild( document.createTextNode( contents ) );
	help_div.appendChild( help_p );
	
	// get bubbles element
	var bubbles_div = document.getElementById( 'no_navigation_info' );
	
	// get help opacity
	var help_opacity = help_div.style.opacity;
	help_opacity = ( help_opacity )? help_opacity * 100 : 0;

	// get bubbles opacity
	var bubbles_opacity = bubbles_div.style.opacity;
	bubbles_opacity = ( bubbles_opacity )? bubbles_opacity * 100 : 100;
	
	// fade out bubbles
	g_effects.fadeOut( bubbles_div, bubbles_opacity );
	
	// fade in help
	g_effects.fadeIn( help_div, help_opacity );
}

function hideNavHelp( e )
{
	var caller = (this)? this : window.event.srcElement;

	// get bubbles element
	var bubbles_div = document.getElementById( 'no_navigation_info' );

	// get help element
	var help_div = document.getElementById( 'navigation_info' );

	// get help opacity
	var help_opacity = help_div.style.opacity;
	help_opacity = ( help_opacity )? help_opacity * 100 : 100;

	// get bubbles opacity
	var bubbles_opacity = bubbles_div.style.opacity;
	bubbles_opacity = ( bubbles_opacity )? bubbles_opacity * 100 : 0;

	// fade out help
	g_effects.fadeIn( bubbles_div, bubbles_opacity );
	
	// fade in bubbles
	g_effects.fadeOut( help_div, help_opacity );
}


function setupAnalytics()
{
	if ( !window.location.toString().match( /admin/ ) )
	{
		var script = document.createElement( 'script' );
		script.setAttribute( 'type', 'text/javascript' );
		script.setAttribute( 'src', 'http://www.google-analytics.com/urchin.js' );
		script.onload = function() { _uacct = "UA-402220-2"; urchinTracker() }
		document.body.appendChild( script );
	}
}


function setupDefaultFormText()
{
	var inputs = document.getElementsByTagName( 'input' );
	for ( var x = 0; x < inputs.length; x++ )
	{
		if ( inputs[x].type == 'text' && inputs[x].parentNode.getElementsByTagName( 'label' ).length )
		{
			// write in default text if empty
			if ( inputs[x].value == '' ) inputs[x].value = inputs[x].parentNode.getElementsByTagName( 'label' )[0].firstChild.nodeValue.replace(':','');

			// set events
			inputs[x].onfocus = handleDefaultFormText;
			inputs[x].onblur = unhandleDefaultFormText;
		}
	}
}

function handleDefaultFormText()
{
	var input = ( this.parentNode )? this : window.event.srcElement;
	var label = input.parentNode.getElementsByTagName( 'label' )[0];

	if ( input.value + ':' == label.firstChild.nodeValue ) input.value = '';
}

function unhandleDefaultFormText()
{
	var input = ( this.parentNode )? this : window.event.srcElement;
	var label = input.parentNode.getElementsByTagName( 'label' )[0];

	if ( input.value == '' ) input.value = label.firstChild.nodeValue.replace(/:/,'');
}

function submitDefaultForm()
{
	var caller = (this.parentNode)? this : window.event.srcElement;

	// set empty fields if they are at default value still
	var inputs = caller.getElementsByTagName( 'input' );
	for (var x = 0; x < inputs.length; x++ )
	{
		if ( inputs[x].parentNode.getElementsByTagName( 'label' ).length )
		{
			var label = inputs[x].parentNode.getElementsByTagName( 'label' )[0];
			if ( inputs[x].value + ':' == label.firstChild.nodeValue ) inputs[x].value = '';
		}
	}

	caller.submit();
}

function setupSpamForm()
{
	var spam_input = document.getElementById( 'comment_captcha' );
	var spam_image = document.getElementById( 'i_spam' );

	// set up the event listener for the form submit
	var parent_form = spam_input.parentNode;
	while ( parent_form.nodeName != 'FORM' ) parent_form = parent_form.parentNode;
	parent_form.onsubmit = displaySpamForm;
	
	// create popup layer
	var popup = document.createElement( 'div' );
	popup.setAttribute ( 'id', 'spam_popup' );

	// create heading and text content
	var popup_header = document.createElement( 'h5' );
	popup_header.appendChild( document.createTextNode( 'Please enter the code in the image to post your comment.' ) );
	var popup_text = document.createElement( 'p' );
	popup_text.appendChild( document.createTextNode( 'Apologies to non-spammer humans.' ) );

	// create refresh link
	var popup_refresh = document.createElement( 'a' );
	popup_refresh.setAttribute( 'href', '#' );
	popup_refresh.className = 'button';
	popup_refresh.onclick = function() { spam_image.src = spam_image.src + '?' + parseInt( 1000 * Math.random() ); return false };
	popup_refresh.appendChild( document.createTextNode( 'CHANGE IMAGE' ) );
	
	// create post link
	var popup_post = document.createElement( 'a' );
	popup_post.setAttribute( 'href', '#' );
	popup_post.className = 'button';
	popup_post.onclick = submitSpamForm;
	popup_post.appendChild( document.createTextNode( 'POST' ) );
	
	// create close link
	var popup_close = document.createElement( 'a' );
	popup_close.setAttribute( 'href', '#' );
	popup_close.className = 'button';
	popup_close.onclick = closeSpamForm;
	popup_close.appendChild( document.createTextNode( 'CLOSE' ) );
	
	// populate it
	popup.appendChild( popup_header );
	popup.appendChild( popup_text );
	popup.appendChild( spam_image.parentNode );
	popup.appendChild( spam_input.parentNode );
	spam_input.parentNode.appendChild( document.createTextNode(' ') );
	spam_input.parentNode.appendChild( popup_refresh );
	popup.appendChild( popup_post );
	popup.appendChild( document.createTextNode(' ') );
	popup.appendChild( popup_close );
	
	// write it in
	document.body.appendChild( popup );
}

function displaySpamForm()
{
//	if ( document.getElementById( 'comment_captcha' ).value == '' )
	{
		var popup_layer = document.getElementById( 'spam_popup' );

		// calculate the middle of the screen
		var dimensions = ( self.innerWidth )? [self.innerWidth,self.innerHeight] :
						 ( document.documentElement && document.documentElement.clientWidth)?  [document.documentElement.clientWidth,document.documentElement.clientHeight] :
						 (document.body)? [document.body.clientWidth,document.body.clientHeight] : [0,0];
		var scrolled   = (self.pageYOffset)? [self.pageXOffset,self.pageYOffset] : 
						 (document.documentElement && document.documentElement.scrollTop)? [document.documentElement.scrollLeft,document.documentElement.scrollTop] :
						 (document.body)? [document.body.scrollLeft,document.body.scrollTop] : [0,0];
		var content	   = [( document.body.scrollHeight > document.body.offsetHeight )? document.body.scrollHeight : document.body.offsetHeight,
						 ( document.body.scrollHeight > document.body.offsetHeight )? document.body.scrollHeight : document.body.offsetHeight]

		popup_layer.style.visibility = 'hidden';
		popup_layer.style.display = 'block'

		var position_left = parseInt( dimensions[0] / 2 ) - ( popup_layer.offsetWidth / 2 );
		var position_top = parseInt( dimensions[1] / 2 ) - ( popup_layer.offsetHeight / 2 ) + scrolled[1];
		
		if ( !document.getElementById( 'js_dim_background' ) )
		{
			// dim background
			var dimmer = document.createElement( 'div' );
			dimmer.setAttribute( 'id', 'js_dim_background' )
			dimmer.style.width = dimensions[0] + ( ( scrolled[1] > 0 )? -15 : 0 ) + 'px';
			dimmer.style.height = content[1] + 'px';
			document.body.appendChild( dimmer );
		}
		else document.getElementById( 'js_dim_background' ).style.display = 'block';

		// display it
		popup_layer.style.top = position_top + 'px';
		popup_layer.style.left = position_left + 'px';
		popup_layer.style.visibility = 'visible';
		
		document.getElementById( 'comment_captcha' ).focus();
		
		return false;
	}
//	else return true;

}

function submitSpamForm()
{
	if ( document.getElementById( 'comment_captcha' ).value != 'Anti-spam code' )
	{
		var popup_layer = document.getElementById( 'spam_popup' );
		
		// hide popup
		popup_layer.style.display = 'none';
		document.getElementById( 'js_dim_background' ).style.display = 'none';
		
		// move input back into form
		document.getElementById('frm_comment').getElementsByTagName('fieldset')[0].appendChild( document.getElementById( 'comment_captcha' ) );
		document.getElementById( 'comment_captcha' ).style.display = 'none';

		// set empty fields if they are at default value still
		var inputs = document.getElementById( 'frm_comment' ).getElementsByTagName( 'input' );
		for (var x = 0; x < inputs.length; x++ )
		{
			if ( inputs[x].parentNode.getElementsByTagName( 'label' ).length )
			{
				var label = inputs[x].parentNode.getElementsByTagName( 'label' )[0];
				if ( inputs[x].value + ':' == label.firstChild.nodeValue ) inputs[x].value = '';
			}
		}
	
		document.getElementById('frm_comment').submit();
		
		return false;
	}
	else return false;
}

function closeSpamForm()
{
	document.getElementById( 'spam_popup' ).style.display = 'none';
	document.getElementById( 'js_dim_background' ).style.display = 'none';
	return false;
}

function addEvent ( caller, type, fn, capture )
{
	if ( document.addEventListener ) caller.addEventListener ( type, fn, capture );
	else if (document.attachEvent ) caller.attachEvent ( 'on'+type, fn );
	else return false;
}

function removeEvent ( caller, type, fn, capture )
{
	if ( document.removeEventListener ) caller.removeEventListener ( type, fn, capture );
	else if (document.detachEvent ) caller.detachEvent ( 'on'+type, fn );
	else return false;
}