var DEF_VAL = "Search this site..."; // Default Value
var isWebkitBrowser = (navigator.userAgent.indexOf("AppleWebKit") !=-1); // Detect all WebKit-based browsers

// Apply getElementById patch
if (!document.getElementById) {
    if (document.all) {
        document.getElementById = function(x) {
            return this.all[x];
        }
    }
    else if (document.layers) {
        document.getElementById = function(x) {
            return this[x];
        }
    }
}

function detectWideScreen() {
	var rawClassname = document.body.className;
	if ( rawClassname.indexOf('wide') != -1 ) {
		rawClassname = rawClassname.substring(0,rawClassname.indexOf('wide')) + rawClassname.substring(rawClassname.indexOf('wide') + 4);
	}
	if (document.body.clientWidth > 1030) {
		document.body.className = rawClassname + " wide";
	} else {
		document.body.className = rawClassname;
	}
}
$(document).ready(changeSearchField);
$(document).ready(detectWideScreen);
$(window).resize(detectWideScreen);

function changeSearchField() {
	//if (!document.getElementById) return;
	var theSearchField = document.getElementById('searchfield');
	var theGoButton = document.getElementById('go');
	if (isWebkitBrowser) {
        // Change input type from 'text' to 'search' and insert 'autosave', 'results', 'placeholder' values
		theSearchField.setAttribute('type', 'search');
		theSearchField.setAttribute('autosave', 'saved.data');
		theSearchField.setAttribute('results', '10');
		theSearchField.setAttribute('placeholder', DEF_VAL);
		theGoButton.style.display = 'none';
	} else {
		// Set behaviours for other browsers
		theSearchField.value = DEF_VAL;
		if (theSearchField.style) {
			theSearchField.style.color = '#999';
		}
		theGoButton.style.display = '';
		theSearchField.onfocus = function() {
			if (this.value == DEF_VAL) {
				this.value = '';
			}
			if (this.style) {
				this.style.color = '#333';
			}
		}
		theSearchField.onblur = function() {
			if (this.value == '') {
				this.value = DEF_VAL;
				if (this.style) {
					this.style.color = '#999';
				}
			}
		}
	}
}