// domready event global
window.addEvent('domready', function(){
	modalHandler = new ModalHandler();
	
	
	// have search label copy automatically added and removed when the user clicks in to the search box
	var searchInput = $('search-input-wrap').getFirst();
	var searchInputCopy = searchInput.get('value');
	searchInput.addEvents({
		focus: function(event){
			var value = this.get('value').trim();
			if(value == searchInputCopy)
				this.set('value', '');
		},
		blur: function(event){
			var value = this.get('value').trim();
			if(value.length == 0)
				this.set('value', searchInputCopy);
		}
	});
	
	/*
	 * link helper - assits with opening new windows
	 *
	 * Add a class of new window to a link and it will open in a new window.
	 * The defautl dimensions are 800 x 600 but can also be set in the rel
	 * attribute of the link using rel="width=1024;height=768;"
	 *
	 * The interactive map class will open a link in a new window the size
	 * of the users screen. If desired you can place a # at the beginning of
	 * the href to disable the link for non java script users. The window
	 * will also open with all of the options disabled.
	 */
	$(document.body).addEvent('click', function(e){
		var target = $(e.target);
		// look for a clicked link
		if(target.get('tag') == 'a'){
			if(target.hasClass('new-window')){
				e.preventDefault();
				// default dimensions 800x600
				var width = 800;
				var height = 600;
				// parse user configured window dimensions
				var rel = target.get('rel');
				if(rel != null){
					width = rel.rel.split('width=')[1].split(';')[0].toInt();
					height = rel.rel.split('height=')[1].split(';')[0].toInt();
				}
				window.open(target.get('href'), '', 'toolbar=1,location=1,menubar=1,resizable=1,scrollbars=1,height=' + height + ',width=' + width);
			}
			else if(target.hasClass('interactive-map')){
				e.preventDefault();
				var href = target.getProperty('href').replace('#', '');
				window.open(href,null,'toolbar=0,scrollbars=1,location=0,status=0,menubar=0,resizable=0,width='+screen.availWidth+',height='+screen.availHeight+',left=0,top=0');
			}
		}
	});
});


/*
 * This is a patch for Mootools Element.toQueryString()
 *
 * toQueryString() was not including empty form fields in
 * the generated string. This is an official patch pulled
 * from their source control and will appear in a later
 * version of mootools.
 */
Element.implement({
	toQueryString: function(){
		var queryString = [];
		this.getElements('input, select, textarea').each(function(el){
			if (!el.name || el.disabled) return;
			var value = (el.tagName.toLowerCase() == 'select') ? Element.getSelected(el).map(function(opt){
				return opt.value;
			}) : ((el.type == 'radio' || el.type == 'checkbox') && !el.checked) ? null : el.value;
			$splat(value).each(function(val){
				if (typeof val != 'undefined') queryString.push(el.name + '=' + encodeURIComponent(val));
			});
		});
		return queryString.join('&');
	}
});


/*
 * This is a patch for Mootools Request.HTML
 *
 * If you use Request.HTML to load some data containing any HTML special chars
 * like &nbsp; or &laquo; it won't work with Webkit based browsers.
 */
Request.HTML.implement({
	processHTML: function(text){
		var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
		text = (match) ? match[1] : text;
		
		var container = new Element('div');
		
		return $try(function(){
			var root = '<root>' + text + '</root>', doc;
			if(Browser.Engine.trident){
				doc = new ActiveXObject('Microsoft.XMLDOM');
				doc.async = false;
				doc.loadXML(root);
			}else{
				doc = new DOMParser().parseFromString(root, 'text/html');
			}
			root = doc.getElementsByTagName('root')[0];
			for(var i = 0, k = root.childNodes.length; i < k; i++){
				var child = Element.clone(root.childNodes[i], true, true);
				if(child) container.grab(child);
			}
			return container;
		}) || container.set('html', text);
	}
});