// This file assumes that the jQuery library has been loaded prior to this invocation
// Load logic when document finishes page load
$(document).ready( function(){
	// Set observer on onChange for the order by drop-down
	$('select#sort_options').change( function(){
		// Localize current element as jQuery object
		var jQElement = $(this);
		// Check the value
		if(		jQElement.val().length > 0 )
			jQElement.parent().submit();
	});// End sort_option over-ride
	$('input#retry_search_input,input#front_search_input').focus( function(){
		// Check our value
		if(		this.value == this.defaultValue ){
			// Clear value
			this.value = '';
			// Update text color
			$(this).css('color','#202020');
		}// End check for the default value
	}).blur( function(){
		// Check our value
		if(		this.value.length == 0 ){
			// Reset value
			this.value = this.defaultValue;
			// Update text color
			$(this).css('color','#808080');
		}// End back-fill of the default value
	});// End retry search input over-ride
	// Set=up our search form handlers
	$('form.search_form').submit( function(){
		// Check to see if we have our search value around
		if(		typeof(this.searchval) != 'undefined' ){
			// Pass the ball over to the url fixing function
			searchTerm( this.searchval.value );
			// Return false
			return false;
		}// End the url fix-up
		else
			return true;
	});// End search form over-ride
	// Set-up our non-blank search form hanldes
	$('form.noblank_search').submit( function(){
		// First indicate that our return result will be false
		var blnSubmitForm = true;
		// Loop over each match inside our form object
		$.each( $(this).find('input.noblank_field'), function(index,curField){
			// First see if we already have decided to leave the loop
			if(		blnSubmitForm == true
				&&	(	curField.value.length == 0
					||	curField.value.match(/\w/) == null
					)
				)
				blnSubmitForm = false;
		});// End for loop for blank fields
		// Return status to caller
		return blnSubmitForm;
	});// End non-blank search over-ride
});// End document onReady over-ride

// Function: searchTerm; Takes in term and forwards current window to the target location
window.searchTerm = function( term ){
	// Really simple, we just need to encode our current string and redirect user to the target page
	var term = encodeURIComponent(term.replace(/^(\ ){1,}|(\ ){1,}$|\n|\r|\t|\<|\>/g,''));
	if( term.length > 0 ){
		window.location = '/search/'+term+'.html';
	}
	else{
		window.location = '/search.html';
	}
	return false;
}// End searchTerm

// Function: homepageSearch( form ); Takes the form object and will post the submission based on the value
window.homepageSearch = function( form ){
	// First validate our input
	if( form.searchval.value == form.searchval.defaultValue ){
		// Redirect to search page
		window.location = '/search.html';
		return false;
	}
	else if( form.searchval.value.length == 0 )
		return false;
	// Return good status
	return true;
}// End homepageSearch

// Function: clearSearch( DOMObject, Click ); Takes reference (this ptr) to the DOM object and will determine if we should reset the value
window.clearSearch = function ( DOMObject, Click ){
	// Validate that we have our state supplied
	if(		arguments.length < 2 )
		var Click = false;
	// Next determine what action we are doing
	if(		Click==false
	   	&&	(		DOMObject.value == null || DOMObject.value.length == 0 ) )
		DOMObject.value = DOMObject.defaultValue;
	else if(	Click==true
			&&	DOMObject.value== DOMObject.defaultValue )
		DOMObject.value='';
}// End clearSearch