$(document).ready(function()
{
    $("#contact-form").validate();
	$("#mailinglist_form").validate();
	$("#comment_form").validate();
	$('#live-search').liveUpdate($('#archive')).focus();
	
	// flash header
	if ($('body').hasClass('home'))
	{
		$('#flash').flash(
		{
			src:'/themes/site_themes/default/flash/home.swf',
			width:577,
			height:268,
			wmode:'transparent'
		},
		{version:9}
		);
	}
});

//Clears the input on focus if there is a particular value
$.fn.clearInput = function(inputVal)
{
	return this.focus(function()
	{
		obj = $(this);
		if(obj.val() == inputVal)
		{
			obj.val('');
		}
	})
	.blur(function()
	{
		obj = $(this);
		if(obj.val() == '')
		{
			obj.val(inputVal);
		}
	});
};
$('#search-input').clearInput('Search');
$('#mailinglist_form-input').clearInput('Your email address');

// safari search field
if($.browser.safari)
{
	$('#search-input').replaceWith('<input id="search-input" placeholder="Search" results="10" style="width:195px;" type="search" title="Type keywords and press Enter"/>'); 
}

// Print function
$('.print').click(function()
{
	window.print();
});

// setup external links
$('a[rel=external]').click(function() {
	$(this).attr('target', 'blank');
});

// FAQ list
$('.faq-list h4').click(function()
{
	$(this).parent().toggleClass('open');
	return false;
});

// Homepage news ticker
$('#news_ticker_items').newsticker(6000);

// add sub nav arrow image
$('#nav-02 .here a').prepend('<img src="/themes/site_themes/default/images/nav-02-current-arrow.png" width="18" height="18" alt=""/> ');

jQuery.fn.liveUpdate = function(list)
{
	list = jQuery(list);

	if(list.length)
	{
		var rows = list.children('li'),
		cache = rows.map(function()
		{
			return $('a', this)[0].innerHTML.toLowerCase();
		});
		
		this
			.keyup(filter).keyup()
			.parents('form').submit(function()
			{
				return false;
			});
	}
   
	return this;
   
	function filter()
	{
		var term = jQuery.trim(jQuery(this).val().toLowerCase()), scores = [];

		if(!term)
		{
			rows.show();
		}
		else
		{
			rows.hide();

			cache.each(function(i)
			{
				var score = this.score(term);
				if(score > 0)
				{
					scores.push([score, i]);
				}
			});

			jQuery.each(scores.sort(function(a, b)
			{
				return b[0] - a[0];
			}),
			function()
			{
				jQuery(rows[this[1]]).show();
			});
		}
	}
};