/*
# $Id$
*/

$(document).ready(function() {

	// Add menu hovering mechanics to main menu
	$('#main-menu li div').each(function(index, el) {
		var a = $(el).parent('li');
		a.hover(function() {
			$(this).css('position', 'relative').find('div').css('visibility', 'visible');
		}, function() {
			$(this).find('div').css('visibility', 'hidden');
			$(this).css('position', 'static');
		});
	});

	// Convert all hyperlinks with rel="external" so they open in new windows
	$('a[rel="external"]').click(function() {
		window.open(this.href);
		return false;
	});

	// Replace all ".button" buttons with styled hyperlink. The block containing
	// these links must be given the "float-wrapper" class.
	// Only add the "button" class to "submit" buttons or hyperlinks.
	$('input.button').each(function(i, el) {

		// Gather some info and attributes from the button element first
		el = $(el);
		var button = {
			parent: el.parent(),
			type: el.attr('type')
		};

		// Replace the button with a hyperlink and ensure the container block
		// has the "float-wrapper" class. The original "submit" button is just
		// hidden rather than removed so that pressing ENTER within the form
		// will still submit it.
		var a = $('<a class="button" href="#"><span>'+el.val()+'</span></a>').click(function() {
			var form = $(this).parents('form');
			form.submit();
			return false;
		});
		a.insertAfter(el);
		el.css('display', 'none');
		button.parent.addClass('float-wrapper');
	});

	// Add clear/fill handler to quicksearch keywords input
	var qsKw = $('#quicksearch-kw');
	qsKw.data('originalValue', qsKw.val());
	qsKw.focus(function() {
		var el = $(this);
		if(el.val()==el.data('originalValue')) {
			el.val('');
		}
	});
	qsKw.blur(function() {
		var el = $(this);
		if(el.val()=='') {
			el.val(el.data('originalValue'));
		}
	});
});