/** $Id: site.js 116 2006-07-12 19:51:35Z garrett $ **/

/**
 * simplelog site functions
 * hides appropriate divs on launch, shows divs, etc, etc
 * search box functionality as well
 */

$(document).ready(function()
{
	init(); // see below, this is to get around global vars not being ready
});
function init()
// 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');
	dashboard_div = document.getElementById('dashboard');
	loading_msg_span = document.getElementById('loading');
	results_span = document.getElementById('results');
	tag_block = document.getElementById('tags');
	about_blurb = document.getElementById('about');
	author_block = document.getElementById('authors');

	default_field_value = 'Search.';
	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
	
	//Make Dashboard draggable with jQuery interface plugin
	
	$('#dashboard').Draggable(
		{
			handle: '#dash_top',
			opacity:0.7
		}
	);
}
function showResults(bool)
{
	dashboard_div.style.visibility = 'visible';
	if (!bool)
	{
		loading_msg_span.innerHTML = message_when_searching;
		results_span.innerHTML = results_when_searching;
		results_span.style.display = 'inline';
		tag_block.style.display = 'none';
		about_blurb.style.display = 'none';
		results_wrapper_div.style.display = 'block';
		
	}
	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';
	dashboard_div.style.visibility = 'hidden';
	//results_wrapper_div.style.display = 'none';
	tag_block.style.display = 'none';
	about_blurb.style.display = 'none';
	search_field.style.color = passive_search_text_color;
	search_field.value = default_field_value;
}

function startSearch() {
	showResults(false);//Show the dashboard, pending
	$.post('/search', $('#q').serialize(), displayResults);//Do the search, set the callback
	return false;
}

function displayResults(data) {
	$('#results').html(data);
	showResults(true);
}

function embedFLVplayer(srcFile,vars,width,height) {
	height = height + 19;
	document.write(	'<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="'+width+'" height="'+height+'" id="myMovieName">' +
					'<param name="movie" value="'+srcFile+'">' +
					'<param name="quality" value=high>' +
					'<param name="bgcolor" value=#FFFFFF>' +
					'<param name="wmode" value="transparent">' +
					'<param name="flashvars" value="'+vars+'"/>' +
					'<embed src="'+srcFile+'?'+vars+'" quality="high" bgcolor="#FFFFFF" width="'+width+'" height="'+height+'" name="myMovieName" align="" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>' +
					'</object>');
}

/*

//onsubmit(form)	-> searchResults(bool)
			-> $.post '/search', serialize data(form) , searchComplete(data)

*/