$(document).ready(function() {
	
	//apply hasJs class to body to target js centric styling eg: body{color:blue;} body.hasJs{color:red;}
	$(document.body).addClass('hasJs');
	
	// init search input focus and blur fns
	initSearchInput();

	// jump menus init
	initJumpMenus();

	// printLink init
	printLink();

	// apply event handlers to search filters
	initFilters();

});

// search input focus and blur on search input box
function initSearchInput() {
	searchInput = document.getElementById('searchterm');
	if (searchInput != null) {
		defaultTerm = searchInput.defaultValue;
		searchInput.defaultValue = defaultTerm; /* add default text to input*/
		searchInput.onfocus = function() { /*add event handler*/
			if (this.value==defaultTerm) this.value='';
			else this.select()
		}
		searchInput.onblur = function() { /*add event handler*/
			if (this.value=='') {this.value=defaultTerm}
		}
	}
}

// jump menus from home page
function initJumpMenus() {
	// Turns all <select> elements with the 'head_select' class into jump menus
	var selectElements = document.getElementsByTagName("select");
	for( i = 0; i < selectElements.length; i++ ) {
		// Check for the class and make sure the element has an ID
		if( selectElements[i].className == "head_select" && document.getElementById(selectElements[i].id) != "" ) {
			jumpmenu = document.getElementById(selectElements[i].id);
			jumpmenu.onchange = function() {
				if( this.options[this.selectedIndex].value != '' ) {
					location.href=this.options[this.selectedIndex].value;
				}
			}
		}
	}
}

// create the print link if js available
function printLink() {
	if (window.print && document.createElement){
		var target = document.getElementById('header_tools');
		if (target != null){
			var listItem = document.createElement('li');
			target.appendChild(listItem);
			listItem.innerHTML = '<a href="javascript:void(0);" onclick="window.print();" title="Print page">Print page</a>';
		}
	}
}

// add event handlers to filter drop-downs and checkboxes
function initFilters() {
	$('.drop_down select').change(function() {
		document.getElementById('filters').submit()
	});
	
	$('.checkbox input').click(function() {
		document.getElementById('filters').submit()
	});
}