// global


// variables 
var msg_noquery = 'Please enter something to find.';


window.addEvent('domready', function() {
	
	// search behavior
	makeSearch();
	
	// initialize browser-dependent styles
	$$('input').addClass('dormant');
	$$('textarea').addClass('dormant');
	
	if (window.ie) {
		$$('input').setStyles({
			'margin': '-1px 0',
			'width': '97%'
		});
		$$('textarea').setStyles({
			'margin': '-1px, 0',
			'width': '98%'
		});
		
		$$('fieldset').setStyles({
			'position': 'relative',
			'padding-top': '2em'
		});
		$$('legend').setStyles({
			'position': 'absolute',
			'top': '-.6em',
			'left': '1em'
		});
			
	} else if (window.gecko) {
		$$('input').setStyle('width', '97%');		
		$$('textarea').setStyle('width', '98%');
		
	} else if (window.webkit) {
	
	}
	
	// email links
	$$('a.email').addEvent('click', function() {
		window.location = 'mailto:'+this.innerHTML.split(' [at] ').join('@');
	});
	
});


//
function makeSearch() {
	$$('div#site_search input').addEvent('focus', function() {
		if (this.getValue() == this.defaultValue) {
			this.value = '';
		}
		this.removeClass('dormant');
	});
	$$('div#site_search input').addEvent('blur', function() {
		if (!this.getValue()) {
			this.value = this.defaultValue;
			this.addClass('dormant');
		}
	});
	//element.onkeyup = function(e) { // ie doesn't like this
	//	var unicode = e.charCode ? e.charCode : e.keyCode;
	//	if (unicode == 13) {
	//		submitSearch(this.getValue());
	//	}
	//}
	$$('div#site_search img').addEvent('click', function() {
		submitSearch($('query').getValue());
	});
}


// submit search
function submitSearch(query) {
	if (query == $('query').defaultValue || query == '' || query == msg_noquery) { // valid
		//alert(msg_nosearch);
		$('query_wrap').addClass('invalid');
		$('query').value = msg_noquery;
		$('query').focus();
		$('query').select();
		
	} else { // invalid
		query = query.split(' ').join(',');
		window.location = '/search='+query;
	}
}


// fixing flash
function fixFlash() {
	var objects = $$('.flash');
	objects.each(function(object) {
		object.innerHTML = object.innerHTML;
	});
}

