/////////////////////////////////////////// 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 CSD (clientsidedevelopers[AT]efocus.nl)
 * @uses Mootools 1.2.4 JavaScript Library
 */
window.addEvents({
	'domready': function() {
		initAccordion('ul.agenda');
		findExternalLinks();
		defaultInputText();
		initSubmitButtons();
		removeTagsWhenEmpty("div.teasers-default");
	},
	'load': function() {
		initListItemHeight();
	}
});


/**
 * initExternalLinks
 * Opens external links valid in a new window without the target attribute.
 * 
 * @author CSD (clientsidedevelopers[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 false;
	
	arrExternalLinks.each(function(elExternalLink) {
		elExternalLink.addEvent('click', function(event) {
			event.stop();
			window.open(this.get('href'));
		});
	});
}


/**
 * findExternalLinks
 * add class external to external links
 * 
 * @author Mirjam (mirjam[AT]efocus.nl)
  */
function findExternalLinks () {
	
	var allExternalLinks = $$('a[href^="http://"]');
	var thisDomain = window.location.host;
	
	allExternalLinks.each(function(thisLink) {
		
		if (!thisLink.get('href').contains(thisDomain)) {
			thisLink.addClass('external');
		}
				
	});
	
	initExternalLinks();
		
}


/**
 * initListItemHeight
 * Vertically aligns all list items on each row of a list.
 *
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 * @author Lee Boonstra <lee.boonstra{AT}efocus.nl>
 * @return void
 */
function initListItemHeight() {
	arrLists = new Array(
		$(document.body).getElement('.member_list')
	);
	
	arrLists.each(function(elList){
		var arrListItems = getChildrenOfElement(elList);
		if (arrListItems.length > 0) {
			setListItemHeights(arrListItems);
		}
	});
}

function getChildrenOfElement(el) {
	var arrChildren = [];
	if (checkIfElementExists(el)){
		arrChildren = el.getChildren();
	} 
	
	return arrChildren;
}

function checkIfElementExists(el) {
	var blnElementExists = false;
	if (el) {
		blnElementExists = true;
	} 
	
	return blnElementExists;
}

/**
 * measureListItemHeights
 * Measures the height of all list items on a each row.
 *
 * @author Ralph Meeuws <ralph.meeuws{AT}efocus.nl>
 * @return void
 */
function setListItemHeights(arrListItems) {
	var intListItemMaxHeight = 0;
	var intListItemPosX = 0;
	var intListItemsPerRow = Math.floor(arrListItems[0].getParent().getWidth() / arrListItems[0].getWidth());
	
	getHeightWithoutStyling = function(elElement, intHeight) {
		var intHeightWithoutStyling = intHeight - (elElement.getStyle('padding-top').toInt() + elElement.getStyle('padding-bottom').toInt());
		
		return intHeightWithoutStyling;
	};
	
	setHeight = function(intIndex, intLoopCount, arrElements, elElement, intHeight) {
		for (var i = 0; i < intLoopCount; i++) {
			arrElements[intIndex - i].setStyle('height', getHeightWithoutStyling(elElement, intHeight));
		}
		intListItemMaxHeight = 0;
	}
	
	arrListItems.each(function(elListItem, intIndex) {
		if (elListItem.getHeight() > intListItemMaxHeight) {
			intListItemMaxHeight = elListItem.getHeight();
		}
		
		if ((intIndex + 1) % intListItemsPerRow == 0) {
			setHeight(intIndex, intListItemsPerRow, arrListItems, elListItem, intListItemMaxHeight);
		} else if ((intIndex + 1) == arrListItems.length) {
			var intLastRowItemsCount = arrListItems.length - Math.floor(arrListItems.length / intListItemsPerRow) * intListItemsPerRow;
			setHeight(intIndex, intLastRowItemsCount, arrListItems, elListItem, intListItemMaxHeight);
		}
	});
}

/**
* initAccordion
* Requires: MooTools 1.2.2
* @param: strElList - string class name element for accordion
* @author: lee.boonstra[AT]efocus.nl
*/
function initAccordion(strElList) {
	if (!$chk($$(strElList))) {
		return false;
	}

	var CLICK_ELEMENT = $$(strElList).getElements('h2');
	var FOLD_OUT_ELEMENT = $$(strElList).getElements('.fold-out-block');
	var accordion = null;
			
	accordion = new Accordion(CLICK_ELEMENT, FOLD_OUT_ELEMENT, {
		onActive: function(toggler){
			toggler.getParent().addClass('open');
		},
		onBackground: function(toggler){
			toggler.getParent().removeClass('open');
		},
		alwaysHide: true,
		show: 0
	});	
}

/**
 * removeTagsWhenEmpty
 * Check if element has no content or has a tag class="hidden", 
 * remove the tag from the DOM.
 * 
 * @param strClassNameEl - the class name of the element to check
 * @author: lee.boonstra[AT]efocus.nl
 */
function removeTagsWhenEmpty(strClassNameEl){
	var elToCheck = $$(strClassNameEl);
 	if(!$defined(elToCheck[0])) return  false;
	
	if(elToCheck[0].getChildren().length < 1 ||
	elToCheck[0].getChildren().get("class") == "hidden"){
		elToCheck[0].dispose();	
	}
}



/**
* defaultInputText
*
* toggles default text in text inputfields
*
* @author Rou-hun Fan <lowen{AT}efocus.nl>
* @return void
*/
 
function defaultInputText() {
	var arrInputfields = $$('.defaultText');
	if(!arrInputfields) return false;
	
	arrInputfields.each(function(elInputfields) {
		elInputfields.defaultText = elInputfields.value;
		
		elInputfields.addEvents({
			'focus': function() {
				if (elInputfields.value == elInputfields.defaultText) elInputfields.value = '';
			},
			'blur': function() {
				if (elInputfields.value == '') {
					elInputfields.value = elInputfields.defaultText;
				}
			}
		});
	});
}


/**
 * initSubmitButtons
 * Turns links with button_grey-class into submit buttons of parent form
 * 
 * @author DEV (gijs.oliemans[AT]efocus.nl)
 */
function initSubmitButtons() {
	var arrSubmitButtons = $$('a.button_grey');
	if (arrSubmitButtons.length == 0) return false;
	
	arrSubmitButtons.each(function(elSubmitButton) {
		elSubmitButton.addEvent('click', function(event) {
			var parentForm = elSubmitButton.getParent('form');
			parentForm.submit();
			event.preventDefault();
		});
	});
}
