/****************************************************************************/
/* Browser information object												*/
/****************************************************************************/

/**
 * Provides information about the browser.
 *
 * @since	4.2.2
 * @access	public
 */
function BrowserInfo() {

	this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
	this.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
	this.isSafari = navigator.userAgent.indexOf('Safari') != -1;
	this.isMac = navigator.userAgent.indexOf('Mac') != -1;
	this.isOpera = navigator.userAgent.indexOf('Opera') != -1;

	// Version of MSIE or -1 for other browsers.
	this.version = (navigator.appName=='Microsoft Internet Explorer')
		? parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1])
		: -1;

} // end constructor BrowserInfo.

/**
 * Browser information.
 *
 * @type	BrowserInfo
 * @access	public
 * @since	4.2.2
 */
document.browserInfo = new BrowserInfo();

/****************************************************************************/
/* Event information object													*/
/****************************************************************************/

/**
 * Provides standardised information about an event.
 *
 * @access	public
 * @since	4.2.2
 */
function EventInfo(e) {

	if (!e) {
		e = window.event;
	}

	// Store original event object.
	this.event = e;

	// Event type.
	this.type = e.type;

	// Event target.
	if (e.target != null || e.srcElement != null) {
		this.target = (e.target != null ? e.target : e.srcElement);
		if (this.target.nodeType == 3) {
			// Safari bug.
			this.target = this.target.parentNode;
		}
	}

	// Key press.
	if (e.which != null) {
		this.keyCode = e.which;
	} else {
		this.keyCode = e.keyCode;
	}
	this.keyCharacter = String.fromCharCode(this.keyCode);
	this.shiftKey = e.shiftKey;

	// Button type.
	this.rightClick = (e.button == 2);

	// Mouse position

	this.mouseX = 0;
	this.mouseY = 0;
	if (e.pageX || e.pageY) {
		this.mouseX = e.pageX;
		this.mouseY = e.pageY;
	} else if (e.clientX || e.clientY) {
		this.mouseX = e.clientX + document.body.scrollLeft;
		this.mouseY = e.clientY + document.body.scrollTop;
	}

} // end constructor EventInfo.

/**
 * Cancels the event.
 *
 * @access	public
 * @since	4.2.2
 */
EventInfo.prototype.cancel = function() {

	document.cancelEvent(this.event);

} // end function cancel.

/****************************************************************************/
/* Document element extensions												*/
/****************************************************************************/

/**
 * Cancels an event.
 *
 * @param	Event	e	The event to cancel.
 * @access	public
 * @since	4.2.2
 */
document.cancelEvent = function(e) {

	// Check if an EventInfo object was provided.
	if (e.event != null) {
		e = e.event;
	}

	if (document.browserInfo.isMSIE) {
		e.returnValue = false;
		e.cancelBubble = true;
	}
	if (e.stopPropagation) {
		e.stopPropagation();
	}
	if (e.preventDefault) {
		e.preventDefault();
	}

} // end function cancelEvent.

/**
 * Registers an event handler for an object.
 *
 * @param	Object		obj			The object.
 * @param	String		eventName	The event that will be handled.
 * @param	Function	handler		The function that will handle the event.
 * @access	public
 * @since	4.2.2
 */
document.registerEvent = function(obj, eventName, handler) {

	if (this.browserInfo.isMSIE) {
		obj.attachEvent('on' + eventName, handler);
	} else {
		obj.addEventListener(eventName, handler, true);
	}

} // end function registerEvent.

/****************************************************************************/
/* Template functions														*/
/****************************************************************************/

/**
 * Ensures the user has selected at least one search option before submitting
 * a search.
 *
 * @access	public
 * @since	1.0
 */
function checkSearchOptions() {

	if (document.getElementById('search_marketSector').value == '_' &&
		document.getElementById('search_location').value == '_' &&
		document.getElementById('search_contractType').value == '_' &&
		document.getElementById('search_specialisation').value == '_' &&
		document.getElementById('search_salary').value == '_') {

		alert('Please select at least one search option.');
		return false;

	}

	return true;

} // end function checkSearchOptions.

/**
 * Checks required values are entered on administration login screen.
 *
 * @access	public
 * @since	1.0
 */
function checkLogin() {

	with (document.login) {

		if (app_username.value == '') {
			alert('Please enter your username');
			app_username.focus();
			return false;
		}

		if (app_password.value == '') {
			alert('Please enter your password');
			app_password.focus();
			return false;
		}

	}

	return true;

} // end function checkLogin.

/**
 * Adds additional functionality to the drop menus.
 *
 * @access	public
 */
function loadDropMenu() {

	// Parse menu tree to add event handlers.
	var menu = document.getElementById('menu');
	for (var i = 0; i < menu.childNodes.length; i++) {
		if (menu.childNodes[i].tagName == 'LI') {

			rootMenu = menu.childNodes[i];

			// Remember the active root menu.
			if (rootMenu.childNodes[0].className == 'active') {
				menu['activeRootMenu'] = rootMenu;
			}

			// Check if this root menu has a drop menu attached.
			var dropMenu = rootMenu.getElementsByTagName('UL');
			if (dropMenu.length == 0) {
				continue;
			}

			// Add ie 6 menu handlers.
			if (document.browserInfo.isMSIE && document.browserInfo.version == 6) {
				document.registerEvent(rootMenu, 'mouseover', function (e) {
					e = new EventInfo(e);
					getRootMenu(e.target).getElementsByTagName('UL')[0].style.display = 'block';
				});
				document.registerEvent(rootMenu, 'mouseout', function (e) {
					e = new EventInfo(e);
					getRootMenu(e.target).getElementsByTagName('UL')[0].style.display = 'none';
				});
			}

			// Add event handler to drop menu.
			if (!document.browserInfo.isMSIE || document.browserInfo.version > 6) {
				dropMenu = dropMenu[0];
				document.registerEvent(dropMenu, 'mouseover', function (e) {
					e = new EventInfo(e);
					getRootMenu(e.target).childNodes[0].className = 'selected';
				});
				document.registerEvent(dropMenu, 'mouseout', function (e) {
					e = new EventInfo(e);
					var rootMenu = getRootMenu(e.target)
					rootMenu.childNodes[0].className = (rootMenu == document.getElementById('menu')['activeRootMenu'] ? 'active' : '');
					if (document.browserInfo.isMSIE && document.browserInfo.version == 6) {
						rootMenu.getElementsByTagName('UL')[0].style.display = 'none';
					}
				});
			}

		}
	}

} // end function loadDropMenu.

/**
 * Returns the parent root menu element from any given element.
 *
 * @param	Object	element	The element.
 * @return	Object
 * @access	public
 */
function getRootMenu(element) {

	while (element.className != 'root') {
		element = element.parentNode;
	}

	return element;

} // end function getRootMenu.

/**
 * Loads links to show google maps on the contact us page.
 *
 * @return	void
 * @access	public
 */
function loadGoogleMaps() {

	// Check if this is a contact us page.
	if (document.getElementById('blue_map_london')) {
		loadGoogleMap('london', '<iframe width="350" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.uk/maps?f=q&amp;hl=en&amp;geocode=&amp;q=107+-+111+Fleet+Street,+London,+EC4A+2AB&amp;sll=51.51427,-0.104893&amp;sspn=0.009801,0.019956&amp;ie=UTF8&amp;s=AARTsJozo0B4JmOg3qVJkkWrhtGENb5xuw&amp;ll=51.519372,-0.106773&amp;spn=0.021363,0.030041&amp;z=14&amp;iwloc=cent&amp;output=embed"></iframe>');
		loadGoogleMap('manchester', '<iframe width="350" height="400" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.uk/maps?f=q&amp;hl=en&amp;geocode=&amp;q=Pall+Mall+61+-+67+King+Street,+Manchester,+M2+4PD&amp;ie=UTF8&amp;s=AARTsJqFxoECeutbbPVtkmaVTDdfOo58Ew&amp;ll=53.481457,-2.243614&amp;spn=0.010215,0.01502&amp;z=15&amp;output=embed"></iframe>');
	}

} // end function loadGoogleMaps.

/**
 * Loads a Google map for a particular location.
 *
 * @param	String	location		The location to load.
 * @param	String	googleMapCode	The HTML embed code to display the google map.
 * @return	void
 * @access	public
 */
function loadGoogleMap(location, googleMapCode) {

	var map = document.getElementById('blue_map_' + location);

	var linkStatic = document.createElement('A');
	linkStatic.id = 'blue_map_' + location + '_link';
	linkStatic.className = 'selected';
	linkStatic.href = '#' + location;
	linkStatic.innerHTML = 'Static Map';
	document.registerEvent(linkStatic, 'click', function() {
		document.getElementById('blue_map_' + location).style.display = '';
		document.getElementById('blue_map_' + location + '_google').style.display = 'none';
		document.getElementById('blue_map_' + location + '_link').className = 'selected';
		document.getElementById('blue_map_' + location + '_google_link').className = '';
	});
	var linkGoogle = document.createElement('A');
	linkGoogle.id = 'blue_map_' + location + '_google_link';
	linkGoogle.href = '#' + location;
	linkGoogle.innerHTML = 'Google Map';
	document.registerEvent(linkGoogle, 'click', function() {
		document.getElementById('blue_map_' + location).style.display = 'none';
		document.getElementById('blue_map_' + location + '_google').style.display = '';
		document.getElementById('blue_map_' + location + '_link').className = '';
		document.getElementById('blue_map_' + location + '_google_link').className = 'selected';
	});

	var links = document.createElement('DIV');
	links.className = 'mapLinks';
	links.appendChild(linkStatic);
	links.appendChild(linkGoogle);

	map.parentNode.insertBefore(links, map);

	var googleMap = document.createElement('DIV');
	googleMap.id = 'blue_map_' + location + '_google';
	googleMap.style.display = 'none';
	googleMap.innerHTML = googleMapCode;

	map.parentNode.insertBefore(googleMap, map);

} // end function loadGoogleMap.

document.registerEvent(window, 'load', loadDropMenu);
//document.registerEvent(window, 'load', loadGoogleMaps);