/**
 * @fileoverview	Sepracor Common Javascript
 * @author			Michael Bester <michael@kimili.com>
 * @version			1.0
 */

/*
 *	Declare the global Sepracor object for namespacing.
 */

var Sepracor = window.Sepracor || (function() {
	
	var initSearchForm = function() {
		
		var form = $('search'),
			label,
			input;
		
		if (!form) {
			return;
		}
		
		label = form.getElement('label');
		input = form.getElement('input[type=text]');
		
		if (!label || !input) {
			return;
		}
		
		input.defaultValue = label.get('text');
		
		input.addEvent('focus', function(e){
			var val = this.get('value');
			this.set('value', ((val === this.defaultValue) ? '' : val));
		});
		
		input.addEvent('blur', function(e){
			var val = this.get('value');
			this.set('value', ((val === '') ? this.defaultValue : val));
		});
		
		// When initialized, fire blur to set up input.
		input.fireEvent('blur');
		
	};
	
	var patchIE = function() {
		
		// Fix lack of adjacent child selector
		
		var content = $$('#navSub + #content');
		
		if (content.length > 0) {
			content[0].addClass('col2');
		}
		
		// If we're dealing with IE6
		if (Browser.Engine.version === 4) {
			// Make up for lack of :first-child selector
			$$('ul#langSwap li:first-child', 'ul#navMain li:first-child').each(function(el){
				el.addClass('first-child');
			});
		}
		
	};
	
	return {
	
		initialize : function() {
			
			/*
			 *	For CSS purposes, right off the bat, let's add a 'js' class to the HTML tag.
			 *	While not technically valid, it'll prevent flash of non-js styles.
			 */
			$(document.html).addClass('js');
			
			/*
				Bootstrap IE
			*/
			if (Browser.Engine.trident) {
				patchIE();
			}
			
			/*
				Initialize the form
			*/
			initSearchForm();
			
			
		}
		
	};
	
}());

/*
 *	Check for MooTools Dependencies
 */
(function() {
	try {
		// Check for MooTools
		if (typeof MooTools === 'undefined' || Number(MooTools.version.match(/\d+\.\d+/)[0]) < 1.2) {
			var error = new Error();
			error.name = "Missing MooTools Javascript Dependency";
			error.message = "MooTools version 1.2 is required.\n Visit http://www.mootools.net and build the core with the following modules:\n \t - Core\n \t - Browser\n \t - Array\n \t - Function\n \t - Number\n \t - String\n \t - Hash\n \t - Event\n \t - Element\n \t - Element.Event\n \t - Selectors\n \t - DomReady";
			throw error;
		} else {
			// We're OK - set it up to run
			window.addEvent('domready', Sepracor.initialize.bind(Sepracor));
		}
	} catch(e) {
		if (typeof console !== 'undefined' && typeof console.error === 'function') {
			console.error(e.name);
			console.info(e.message);
		}
	}
}());