var defaultSearchFieldValue = '';

/**
* code that must be executed when the
* page is loaded
*/
$(document).ready(function() {
	writePrintLink();
	writeSendLink();
	initializePrefilling();
});

/** 
* This function sets the default value 
* to the search site wide field. When no value
* is filled in, or the default text is still present
* the search will not be executed when the
* form is submitted
*/
function initializePrefilling() {
	$('.sitewidesearchfield').each(function() {
		defaultSearchFieldValue = $(this).val();
		
		$(this).focus(function() {
			if ($(this).val() == defaultSearchFieldValue) {
				$(this).val('');
			}
		});
		$(this).blur(function() {
			if ($(this).val() == '') {
				$(this).val(defaultSearchFieldValue);
			}
		});
	});
	
	$('.sitewidesearch').each(function() {
		$(this).submit(function() {
			var $inputField = $('#' + $(this).attr('id') + ' :input[.sitewidesearchfield]');
			if ($inputField.val() == defaultSearchFieldValue) {
				return false;
			} else {
				return true;
			}
		});
	});
}

/**
* This function appends the print link
* to the DIV tag with ID 'printlink-text'
*/
function writePrintLink() {
	var printLinkText = $("#printlink-text").html();
	var printLinkContainer = $("#printlink-container");
	if (printLinkContainer != null) {
		printLinkContainer.append('<a href="#" onclick="window.print(); return false;">' + printLinkText + '</a>');
	}
}

/**
* This function appends the send link to the 
* DIV tag with ID 'sendlink-container'
*/
function writeSendLink() {
	var sendLinkText = $("#sendlink-text").html();
	var sendLinkContainer = $("#sendlink-container");
	if (sendLinkContainer != null) {
		sendLinkContainer.append('<a href="#">' + sendLinkText + '</a>');
	}
}
