$(document).ready(function() 
{
	$('body').delegate('a[href="#"]', 'click', function(event)
	{
		event.preventDefault();
		return false;
	});
	
	$('.feature').cycle(
	{ 
		timeout		: 5000,
		speed		: 2000,
		sync		: 1,
		pause		: false,
		slideExpr	: 'a'
	});
	
	$('#slideshow ul').cycle(
	{ 
		timeout		: 5000,
		speed		: 2000,
		sync		: 1,
		pause		: true,
		pager		: '#slideshow_nav'
	});
	
	$('#search input[type="text"], #newsletter input[type="text"]').css({outline: 'none'}).clearOnFocus();
	
	
	
	function menuOver()
	{
		var columns = Number($(this).find('.column').length);
		
		$(this).find('.menu')
			.css({'width':columns * 220}) // all.css - #main_nav .menu .column - width + padding
			.stop()
			.fadeTo('fast', 1)
			.show();
	}
	
	function menuOut()
	{
		$(this).find('.menu').stop().fadeTo('fast', 0, function() 
		{
			$(this).hide(); 
		});
	}

	$('ul#main_nav li')
		.each(function()
		{
			var item = $(this);
			var menu = $(this).find('.menu');
			var offset = 30 + Number(item.width()/2) - 9; // menu left/right offset + nav item width/2 - arrow width/2
			
			if(menu.hasClass('left'))
			{
				item.find('.arrow').css({'left': offset+'px'});
			}
			else
			{
				item.find('.arrow').css({'right': offset+'px'});
			}
			
			menu.css({'opacity':0});
		})
		.hoverIntent(
		{
			sensitivity	: 200,
			interval	: 100,
			timeout		: 200,
			over		: menuOver,
			out			: menuOut
		});
	
});

(function($) 
{
	$.fn.clearOnFocus = function() 
	{	
		function clearOnFocusFocus(event)
		{
			$(this).addClass('focused');
			
			if($(this).val() == $(this).data('clearOnFocus'))
			{
				$(this).val('');
			}
		}
		
		function clearOnFocusBlur(event)
		{
			$(this).removeClass('focused');
			
			if($.trim($(this).val()) == '')
			{
				$(this).val($(this).data('clearOnFocus'));
			}
		}
		
		return this.each(function()
		{
			$(this).data('clearOnFocus', $(this).attr('value'));
			
			//	unbind any previous listeners
			$(this).unbind('focus', clearOnFocusFocus);
			$(this).unbind('blur', clearOnFocusBlur);
			
			//	bind listeners to the functions
			$(this).bind('focus', clearOnFocusFocus);
			$(this).bind('blur', clearOnFocusBlur);
		});
	};
	
})(jQuery);
