document.observe("dom:loaded", function() {
	searchText();
	dataCentreTabs();
	highlightForms();
	formError();
	changeTextSize();
	eventBreadcrumb();
	externalLink();
	servicesDirectory();
	closeFB();
	loadFB();
	updateYourDetails();
});

fbPageOptions = {
	outsideClickCloses: false,
	overlayOpacity: 80,
	shadowType: 'none',
	innerBorder: 0,
	infoPos: 'tl',
	controlPos: 'tr'
};
	
	//captionPos, caption2Pos, infoLinkPos, printLinkPos, newWindowLinkPos, itemNumberPos, indexLinksPos 

// When search input is clicked on, remove the word search
// and put back if value is blank
function searchText() {
	if ($("search-text")) {
		$("search-text").observe('focus',
			function(event) {			
				if ($F(this) == 'Search') {
					this.value = '';
				}
			}
		).observe('blur',
			function(event) {
				if ($F(this) == '') {
					this.value = 'Search';
				}
			}
		);
	}
}




//Start data centre tabs
function dataCentreTabs() {
	if ( $('browse-tabs') ) {
		tabs = new Fabtabs('browse-tabs');
    }
}


// Highlight form fields on focus
function highlightForms() {
	var highlightForm = $$('textarea, ol.form input[type=text], ol.form input[type=password], div#comments input[type=text]');
		highlightForm.each (
			function(highlightElement) {
				
				highlightElement.observe('focus',
					function(event) {
						event.element().addClassName("highlight");
					}
				).observe('blur',
					function(event) {
						event.element().removeClassName("highlight");
						event.element().removeClassName("highlight-error");
					}
				);
			}
		)
}

// Highlight form fields with errors
function formError() {
	var formError = $$("ol.form li .error, div#comments span.error");
		formError.each (
			function(formErrorHighlight) {
				var error = formErrorHighlight.innerHTML;
				var errorHTML = error.substring(0,4);		
				if (! (errorHTML == '<!--')) {
					var formErrorHighlightInput = formErrorHighlight.previous('input[type=text], input[type=password], textarea');
					if (typeof formErrorHighlightInput != 'undefined') {
						formErrorHighlightInput.addClassName("highlight-error");
					}
				}
			}
		);

}


// Increase/decrease text size in article
function setDefaultFontSize() {

	textToResize = $('bodyText');
	var fs = textToResize.getStyle('fontSize');

	if (fs.endsWith("px")) {
		myFontSize = parseFloat(fs);
		myFontSizeIncrement = 2;
		myFontSizeType = 'px';
	} else if (fs.endsWith("%")) {
		myFontSize = 100;
		myFontSizeIncrement = 10;
		myFontSizeType = '%';
	} else if (fs.endsWith("em")) {
		myFontSize = parseFloat(fs);
		myFontSizeIncrement = 0.1;
		myFontSizeType = 'em';
	} else {
		myFontSize = parseFloat(fs);
		myFontSizeIncrement = 2;
		myFontSizeType = 'px';
	}
}

function textSize(dir) {

	if (typeof myFontSize == 'undefined') {
		setDefaultFontSize();
		myNewFontSize = myFontSize;
	}

	if (dir == 'up') {
		myNewFontSize += myFontSizeIncrement;
	} else {
		myNewFontSize -= myFontSizeIncrement;
	}

	myNewFontSizeString = myNewFontSize + myFontSizeType;
	textToResize.setStyle({ fontSize: myNewFontSizeString });

	return;

}

function changeTextSize() {
	var textIncrease = $("text-increase");
	if (textIncrease) {
		textIncrease.observe('click',
			function(event) {
				textSize('up');
				return false;
			}
		);
	}
	
	var textDecrease = $("text-decrease");
	if (textDecrease) {
		textDecrease.observe('click',
			function(event) {
				textSize('down');
				return false;
			}
		);
	}
}

function eventBreadcrumb() {
	$$("body.events ul#breadcrumbs li:last-child a").each (
		function (lastCrumb) {
			lastCrumb.addClassName("last");
		}
	)
	
}


// Add target_blank to any external links
function externalLink() {	
	var currentHostname = window.location.hostname;
	
	$$("div#page a").each (
		function (anchor) {
			if (currentHostname !== anchor.hostname) {
				if (anchor.className !== 'print') {
					anchor.writeAttribute('target','_blank');
				}
			}
		}
	)
	$$('#bodyText a[href$=".pdf"]').each(
		function(pdfLink) {
			pdfLink.writeAttribute('target','_blank');
		}
	);
}


// Services directory page country filtering
function servicesDirectory() {
	if(!$('services-directory-country-filter')) {
		return false ;
	}

	var countrySelect = $('services-directory-country-filter').down('#country');
	if (countrySelect) {
		countrySelect.observe('change',
			function (event) {
				document.location = document.location.pathname + $F(this);
			}
		);
	
	}
}

function closeFB() {
	var closeFloatbox = $("continue");
	if (closeFloatbox) {
		closeFloatbox.observe('click',
			function(event) {				
				parent.fb.end();
			}
		);
	}
}


function loadFB() {
	var url = window.location.search.substring(1);
	var getQuery = url.split('?');
	
	if (getQuery == 'register') {
		$("header").insert ({
        	'bottom' : '<a href="/registration/" title="Captive Review Registration" rel="floatbox" rev="autoStart:true width:650 height:440" style="display:none;"></a>'
  		});
	}
}

function updateYourDetails() {
	var updateYourDetails = $$('#update-your-details form')[1];
	if (updateYourDetails) {
		updateYourDetails.observe('click',
			function(event) {
				var button = event.findElement('button');
				if (button) {
					button.hide().insert({before : '<div id="loading"><img src="/images/ajax-loader.gif" width="16" height="16" />Loading, please wait...</div>'});
				}
			}
		);
	}
}
