/**
 * @author Wingweb Communications
 * Configuration is in this class
 */

var SearchBox = {

	'boxIdentifier': 'mainSearch',						// The identifier (id) of the search input.
	'defaultValue': 'Snel zoeken binnen de website...',	// The default value of the search input.
	'autoFocus': false,									// Autofocus (true / false)
	'autoClear': true,									// Adds the clear button
	
	'initSearchBox': function(){
	
		var searchBoxField = document.getElementById(this.boxIdentifier);
		if( searchBoxField.value.length == 0)
			searchBoxField.value = this.defaultValue;
		
		searchBoxField.onfocus = this.checkField;
		
		if( this.autoFocus )
			this.focusField();
			
		if( this.autoClear ) {
			var clearBtn = document.getElementById( this.boxIdentifier + 'Clear' );
				clearBtn.onclick = this.autoClear;
				searchBoxField.onkeyup = this.clearSwitcher;
		}
	},
	
	'checkField': function() {
		var searchBoxField = document.getElementById( SearchBox.boxIdentifier );
		searchBoxField.value = searchBoxField.value == SearchBox.defaultValue ? '' : searchBoxField.value;
		
	},
	
	'focusField': function() {
		var searchBoxField = document.getElementById( SearchBox.boxIdentifier );
		searchBoxField.focus();
	},
	
	'autoClear': function() {
		var clearBtn = document.getElementById( SearchBox.boxIdentifier + 'Clear' );
		var searchBoxField = document.getElementById( SearchBox.boxIdentifier );
		
		if (searchBoxField.value.length > 0) {
			searchBoxField.value = '';
			clearBtn.style.display = 'none'; 
			searchBoxField.focus();
		}	
	},
	
	'clearSwitcher': function() {
		var clearBtn = document.getElementById( SearchBox.boxIdentifier + 'Clear' );
			clearBtn.style.display = clearBtn.style.display == 'none' ? 'block' : '';
	}
}

window.onload = function() {
	SearchBox.initSearchBox();
}
