/////////////////////////////////////////// BASIC FUNCTIONS, DON'T EDIT ///////////////////////////////////////////

/**
 * Starts executing the initializing functions when either the DOM structure of the page has been loaded ('domready'), or the entire page ('load').
 * 
 * @author Ralph.Meeuws (ralph.meeuws{AT}efocus.nl)
 * @uses Mootools 1.2.2 JavaScript Library
 */
window.addEvents({
	'domready': function() {
		initExternalLinks();
		// Commenting this out as form submits are handled by ATG
		//initFormSubmitButtons();
		initPageBrowserCentering();
		initAZIndexColumns();
		initEqualHeightFix();
		pushboxSlideShow();
	},
	'load': function() {
		
	}
});


/**
 * initExternalLinks
 * Opens external links valid in a new window without the target attribute.
 * 
 * @author Ralph.Meeuws (ralph.meeuws{AT}efocus.nl)
 * @uses <a href="http://www.efocus.nl/" class="external">eFocus</a>
 */
function initExternalLinks() {
	var arrExternalLinks = $$('a.external');
	if (arrExternalLinks.length == 0) return;
	
	arrExternalLinks.each(function(elExternalLink) {
		elExternalLink.addEvent('click', function(event) {
			event.stop();
			window.open(this.get('href'));   
		});
	});
}



/**
 * initFormSubmitButtons
 * Centers the page browser.
 * 
 * @author Ralph.Meeuws (ralph.meeuws{AT}efocus.nl)
 * @return void
 */
function initFormSubmitButtons() {
	var arrForms = $$('form');
	if (arrForms.length == 0) return;
	
	arrForms.each(function(elForm){
		if(elForm.getElement('.button') != null ) {
			elForm.getElement('.button').addEvent('click', function(event){
				event.stop();
				elForm.submit();
			});
		}
	});
}



/**
 * initPageBrowserCentering
 * Centers the page browser.
 * 
 * @author Ralph.Meeuws (ralph.meeuws{AT}efocus.nl)
 * @return void
 */
function initPageBrowserCentering() {
	arrPageBrowsers = $$('.page_browser');
	if (arrPageBrowsers.length == 0) return;
	
	arrPageBrowsers.each(function(elPageBrowser){
		intPageBrowserWidth = elPageBrowser.getWidth();
		
		elPageBrowser.setStyles({
			'position': 'relative',
			'left': '50%',
			'width': intPageBrowserWidth,
			'margin-left': -(intPageBrowserWidth / 2)
		});
	});
}


/**
 * pushboxSlideShow
 * Animates the header slideshow on the homepage and defines the interaction.
 *
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @editor Phi Son do <phison.do{AT}efocus.nl>
 * @param arrSlides, intStartSlide, blnLoop, intInterval, blnAutoPlay, blnPlaying
 * @param strOutEffectProperty, intOutEffectStartValue, intOutEffectEndValue, intOutEffectDuration
 * @param strInEffectProperty, intInEffectStartValue, intInEffectEndValue, intInEffectDuration
 * @param elNav, strLabelPrev, strLabelNext, strLabelFirst, strLabelLast, strLabelPlay, strLabelPause
 * @return EfxNavSlideShow Class instance
 */
function pushboxSlideShow(){
	if (!$('pushbox')) return;
	
	var elMySs = $('pushbox');
	var arrMySlides = elMySs.getElement('.slides').getElements('li');
	
	var elMyNav = elMySs.getElement('.pushbox_nav');

	var objSlideShow = new EfxNavSlideShow({
		arrSlides: arrMySlides,
		elNav: elMyNav,
		strOutEffectProperty: 'left',
		intOutEffectStartValue: 0,
		intOutEffectEndValue: -714,
		strInEffectProperty: 'left',
		intInEffectStartValue: 714,
		intInEffectEndValue: 0,		
		intInterval: 5000
	});
}


/**
 * initAZIndexColumns
 * Divides the A-Z Index list into three separate lists acting as columns.
 * 
 * @author Ralph.Meeuws (ralph.meeuws{AT}efocus.nl)
 * @return void
 */
function initAZIndexColumns() {
	if (!$(document).getElement('.index_subjects')) return;
	
	var elIndexSubjects = $(document).getElement('.index_subjects');
	var arrIndexSubjects = elIndexSubjects.getChildren('li');
	var intIndexColumnAverageHeight = Math.ceil(elIndexSubjects.getHeight() / 3);
	var intIndexColumnHeight = 0;
	var intIndexFrom = 0;
	
	arrIndexSubjects.each(function(elIndexSubject, intSubjectIndex){
		intIndexColumnHeight = intIndexColumnHeight + (elIndexSubject.getHeight() + elIndexSubject.getStyle('margin-bottom').toInt());
		if ((intIndexColumnHeight > intIndexColumnAverageHeight) || (intSubjectIndex == arrIndexSubjects.length - 1)) {
			elIndexSubjectsCol = elIndexSubjects.clone();
			elIndexSubjectsCol.getChildren('li').each(function(elChild, intChildIndex){
				if ((intChildIndex < intIndexFrom) || (intChildIndex > intSubjectIndex)) {
					elChild.dispose();
				}
			});
			elIndexSubjectsCol.injectBefore(elIndexSubjects);
			
			intIndexFrom = intSubjectIndex + 1;
			intIndexColumnHeight = 0;
		}
	});
	
	elIndexSubjects.dispose();
}


/**
 * initEqualHeightFix
 * Fix equal height for small content block
 * 
 * @author CSD (phison.do{AT}efocus.nl)
 */
function initEqualHeightFix() {
	if (!document.getElement('.small_content_block')) return;
	
	var arrSmallContentBlocks = $$('.small_content_block div');
	
	var intSmallContentBlockHeightMax = 0;
	
	arrSmallContentBlocks.each(function(elSmallContentBlock){	
		if (elSmallContentBlock.getHeight() > intSmallContentBlockHeightMax) {
			intSmallContentBlockHeightMax = elSmallContentBlock.getHeight();
		}
	});	
	
	arrSmallContentBlocks.each(function(elSmallContentBlock){
		elSmallContentBlock.setStyle('height', intSmallContentBlockHeightMax);
	});		

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * removeTextOnFocus
 * Remove the default text that appears within a field
 *
 * @author NS
 */
function removeTextOnFocus(fieldId, defaultText) {
		if (!$(fieldId)) return;
		var field = $(fieldId).value;
		if(field == defaultText) {
			$(fieldId).value = "";
		}
}
