// Version 1.0.2.
(function($)
{
	$.fn.site_search_box = function(options)
	{
		var defaults = $.fn.site_search_box.defaults;
		
		// Set the options.
		var o = $.extend({}, defaults, options);

		// Go through each form.
		return this.each(function()
		{
			o.submit_link_value = $(this).find('input[type=submit]').attr('value') || $(this).find('input[type=image]').attr('alt') || $(this).find('button[type=submit]').text() || o.submit_link_value;
			o.submit_link_html = '<a class="' + o.submit_link_class + '" href="http://search.tcd.ie/search"><span>' + o.submit_link_value + '</span></a>';
			
			if (o.submit_button_to_link_swap)
			{
				// Swap out the old submit button for a link.
				if ($(this).find('input[type=submit]').length)
				{
					$(this).find('input[type=submit]').replaceWith(o.submit_link_html);
				}
				else if ($(this).find('input[type=image]').length)
				{
					$(this).find('input[type=image]').replaceWith(o.submit_link_html);
				}
				else if ($(this).find('button[type=submit]').length)
				{
					$(this).find('button[type=submit]').replaceWith(o.submit_link_html);
				}
				else
				{
					o.submit_button_found = false; // No submit button found.
				}
				var self = this;
				if (o.submit_button_found)
				{
					$('.' + o.submit_link_class).click(function()
					{
						$(self).submit();
						return false;
					});
					$('.' + o.submit_link_class).keyup(function(e)
					{
						if (e.keyCode == 13 || e.keyCode == 32) // Return/enter or space.
						{
							$(self).submit();
						}
						return false;
					});
					$('.' + o.submit_link_class).keydown(function(e)
					{
						if (e.keyCode == 13 || e.keyCode == 32) // Return/enter or space.
						{
							return false;
						}
					});
				}
			}
			$(this).submit(function()
			{
				if ($('.' + o.query_class).attr('value') == o.query_message)
				{
					$('.' + o.query_class).attr('value', '');
				}
				return true;
			});
			if ($('.' + o.query_class).attr('value') != o.query_message)
			{
				if (o.query_posted != '')
				{
					$('.' + o.query_class).attr('value', o.query_posted);
				}
				else
				{
					$('.' + o.query_class).attr('value', o.query_message);
				}
			}
			$('.' + o.query_class).focus(function()
			{
				if ($(this).attr('value') == o.query_message)
				{
					$(this).attr('value', '');
					$(this).parent().addClass(o.query_focus_class);
				}
			});
			$('.' + o.query_class).blur(function()
			{
				if (!$(this).attr('value') || $(this).attr('value').replace(/^\s*/, "").replace(/\s*$/, "") == '')
				{
					$(this).attr('value', o.query_message);
					$(this).parent().removeClass(o.query_focus_class);
				}
			});

		});
	};
	// Public defaults
	$.fn.site_search_box.defaults = {			
		query_message: 'Search this site',
		query_class: 'site-search-query',
		query_focus_class: 'site-search-focus',
		query_posted: '',
		submit_button_to_link_swap: true, // Toggle whether the submit button should get swapped for a stylable link.
		submit_link_class: 'site-search-submit-link',
		submit_button_found: true, // Flag seeded to indicate that a submit button was found.
		submit_link_value: 'Go'
	};
})(jQuery);

