/**
 * eFocus Default Scripts
 * default Javascript Ecabo
 *
 * @author Lee Boonstra, <lee.boonstra@efocus.nl>
 * @author Phi Son Do, <phison.do@efocus.nl>
 * @since 2.0, 1 jan, 2010
 * @copyright eFocus
 * @uses MooTools 1.2.3 More, <http://www.mootools.net>
 */
window.addEvents({
	'domready': function() {
		findExternalLinks();
		setDefaultInputText();
		pushboxSlideShow();
		initNewsFix();
		initContentOverviewFix();
	},
	'load': function() {
		setEqualHeight($$('div.box'));
		removeTagsWhenEmpty("div.tx-pagebrowse-pi1");
	}
});

/**
 * initExternalLinks
 * Opens external links valid in a new window without the target attribute.
 * 
 * @author CSD <clientsidedevelopers@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 Verloop <mirjam.verloop@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();
}

/**
* setEqualHeight
* makes multiple different elements all the same height,
* assuming the height of the highest element
*
* @param arrElements, Array of DOM elements to compare fix height
* @author Klaas Dieleman <klaas.dieleman@efocus.nl>
* @author Lee Boonstra <lee.boonstra@efocus.nl>
* @return void
*/

function setEqualHeight(arrElements) {
	var intElHeight = 0;
	var intStyleHeight = 0;
			
	for(var i = 0 ; i < arrElements.length ; i++) {
		if(arrElements[i].getSize().y > intElHeight) intElHeight = arrElements[i].getSize().y;
	}
	
	arrElements.each(function(item) {
		intStyleHeight = item.getStyle('padding-top').toInt();
		intStyleHeight += item.getStyle('padding-bottom').toInt();
		intStyleHeight += item.getStyle('border-top-width').toInt();
		intStyleHeight += item.getStyle('border-bottom-width').toInt();
		item.setStyle('height', intElHeight-intStyleHeight);
	});
}

/**
 * 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 <lee.boonstra@efocus.nl>
 * @return void
 */
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();	
	}
}

/**
* getDefaultInputText
* toggles default text in text inputfields
*
* @author Lee Boonstra <lee.boonstra@efocus.nl>
* @return void
*/
 
function setDefaultInputText() {
	var arrInputElements = $$('input');
	arrInputElements.each(function(el){
		if(el.get("alt")){
			new OverText(el);
		}
	});
}


/**
 * pushboxSlideShow
 * Animates the header slideshow on the homepage and defines the interaction.
 *
 * @author Ralph Meeuws <ralph.meeuws@efocus.nl>
 */
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,
		intInterval: 15000
	});
}


/**
 * initialize the Accordion
 * 
 * @author Phi Son Do <phison.do@efocus.nl>
 * @param strClassToClick - the item to click on
 * @param strClassToOpen - the item to open
 * @return void
 */
function initAccordion(strClassToClick, strClassToOpen){
	var specsAccordion = new Accordion($$(strClassToClick), $$(strClassToOpen), {
	      onActive: function(toggler){
	            toggler.getParent().addClass('open');
	      },
	      onBackground: function(toggler){
	            toggler.getParent().removeClass('open');
	      },
	      alwaysHide: true,
	      show: 0
	});	
}


/**
 * initNewsFix
 * removes marginRight of every third element
 * 
 * @author Phi Son Do <phison.do@efocus.nl>
 * @return void
 */

function initNewsFix() {
	if(!document.getElement('div.home-content')) return;
	
	var arrNewsList = $$('div.nieuws ul');
	var arrNewsListThirdItems = arrNewsList.getElements('li:nth-child(3n+3)'); 
	
	arrNewsListThirdItems.each(function(elNewsListThirdItem){
		elNewsListThirdItem.setStyle('margin-right', 0);	
	});
	
}


/**
 * initContentOverviewFix
 * removes marginRight of every even element
 * 
 * @author Phi Son Do <phison.do@efocus.nl>
 * @return void
 */

function initContentOverviewFix() {
	if(!document.getElement('div.contentoverview ul li')) return;
	
	var arrContentOverviewList = $$('div.contentoverview ul');
	var arrContentOverviewEvenListItems = arrContentOverviewList.getElements('li::nth-child(2n)'); 
	
	arrContentOverviewEvenListItems.each(function(elContentOverviewEvenListItem){
		elContentOverviewEvenListItem.setStyle('margin-right', 0);	
	});
}