/** $Id: site.js 275 2007-01-11 21:06:20Z garrett $ **/

jQuery.noConflict(); // use jquery for the easteregg - anyone looking at the blog is going to have it cached from the main site

/**
 * simplelog site functions
 * hides appropriate divs on launch, shows divs, etc, etc
 * search box functionality as well
 */
 
document.observe("dom:loaded", function(){
	
	appInit(); // see below, this is to get around global vars not being ready

	// shadowbox the flickr pics, but first rewrite the links to point only at the images
	Shadowbox.init({skipSetup:true});
	$$('.flickr_badge_image>a').each(function(t){
		if(t.childElements()[0]) {
			t.href = t.childElements()[0].src.replace(/(.*)_s(.*)/, "$1$2");
		}
	})
	Shadowbox.setup($$('.flickr_badge_image>a'),{
		gallery: 'flickr'
	});
	
	// easter egg :)
	jQuery('<div id="worm"></div>').appendTo('#footer_sleeve').toggle(
		function(){
			jQuery(this).parents('#footer_sleeve').animate({height:'835px'},{queue:false,duration:2000});
			jQuery('html,body').animate({scrollTop:jQuery('#footer_sleeve').offset().top + 865},{queue:false,duration:2000});
		}, function(){
			jQuery(this).parents('#footer_sleeve').animate({height:'70px'},{queue:false,duration:2000});
		});
		
	
		jQuery(".fade_image").fadeTo("slow", 0.01); // This sets the opacity of the thumbs to fade down to 30% when the page loads
							   jQuery(".fade_image").hover(function(){
							   jQuery(this).fadeTo("slow", 1.0); // This should set the opacity to 100% on hover
							   },function(){
							   jQuery(this).fadeTo("slow", 0.01); // This should set the opacity back to 30% on mouseout
							   });
							 
	

});
function appInit()
// init vars, run some initial functions
{
	body_container = document.getElementById('wrapper');
	search_field = document.getElementById('q');
	search_div = document.getElementById('search');
	results_wrapper_div = document.getElementById('search_results');
	loading_msg_span = document.getElementById('loading');
	results_span = document.getElementById('results');
	tag_block = document.getElementById('tags');
	author_block = document.getElementById('authors');

	default_field_value = 'Search posts...';
	message_when_searching = 'Searching...';
	message_when_done = '';
	results_when_searching = '';

	passive_search_text_color = '#777';
	active_search_text_color = '#000';

	isIE = false;

	searchInit(search_field); // capture key events
	clearSearch(); // set everything right with search field / areas
}
function showResults(bool)
{
	results_wrapper_div.style.display = 'inline';
	if (!bool)
	{
		loading_msg_span.innerHTML = message_when_searching;
		results_span.innerHTML = results_when_searching;
		results_span.style.display = 'inline';
	}
	else
	{
		loading_msg_span.innerHTML = message_when_done;
		results_span.style.display = 'block'; // for some reason, you have to go to block then back
		results_span.style.display = 'inline'; // to avoid it from dropping down a line in ff
	}
}
function searchInit()
{
	if (navigator.userAgent.indexOf('Safari') > 0)
	{
		search_field.addEventListener('keydown',searchKeyPress,false);
	}
	else if (navigator.product == 'Gecko')
	{
		search_field.addEventListener('keypress',searchKeyPress,false);
	}
	else
	{
		search_field.attachEvent('onkeydown',searchKeyPress);
		search_field.blur();
		isIE = true;
	}
}
function searchKeyPress(event)
{
	if (event.keyCode == 27)
	// escape key pressed
	{
		clearSearch();
	}
}
function clearSearch()
// clears the search field, sets its default color and value, hides appropriate areas
{
	//search_div.style.display = 'none'; //edited by paul to stop the search box being hidden
	search_field.style.color = passive_search_text_color;
	search_field.value = default_field_value;
	if(results_wrapper_div) results_wrapper_div.style.display = 'none';
}
function searchFocus()
{
	search_field.style.color = active_search_text_color;
	if (search_field.value == default_field_value)
	{
		search_field.value = '';
	}
}
function showSearch()
{
	if (search_div.style.display == 'block')
	{
		clearSearch();
	}
	else
	{
		search_div.style.display = 'block';
	}
}
function showTags()
{
	if (tag_block.style.display == 'block')
	{
		tag_block.style.display = 'none';
	}
	else
	{
		tag_block.style.display = 'block';
	}
}
function showAuthors()
{
	if (author_block.style.display == 'block')
	{
		author_block.style.display = 'none';
	}
	else
	{
		author_block.style.display = 'block';
	}
}