window.addEvents({
	'domready': function() {
	
		if (document.id('brandbox')) {
			var bbox = new Brandbox('brandbox', {
				'slides': 'a.bg',
				'initwidth': 'auto',
				'maxwidth': 480,			
				'minwidth': 'auto'
			});
		};

		if (document.id('calendar')) {
			initCalendar = function() {
				var cal = new Calendar('calendar', {
					'days': 'span',
					'tooltip': {
						'el': 'div.popup',
						'pointer': 'div.arrow',
						'pos': 'lock_y',
						'displace': 15
					}
				});
			}
			initCalendar();
		};
		
		if (document.id(document.body).getElement('div.calendar-big')) {
			initBigCalendar = function() {
				var container = document.id(document.body).getElement('div.calendar-big');
				var table = container.getElement('table');
				var rows = table.getElements('tr');
	
				rows.each(function(row) {
					var height = 0;
					var eventlists = row.getElements('td').getElements('div.events_holder');
					eventlists.each(function(list) {
						var listheight = list.getElement('span').getHeight()[0];
						if (listheight > height) {
							height = listheight;
						}
					});
					eventlists.each(function(list) {
						list.getElement('span').setStyle('height', height);
					});
				});
			}
			initBigCalendar();
		}
		
		initAccordionFAQ();
		openFAQItem();
		positionLeftColBlock();
		initSitemap();
		if ($("catselect")) submitFormOnChangeSelect($("catselect"));
		if ($("monthselect")) submitFormOnChangeSelect($("monthselect"));
	},
	'load': function() {

		if ($('slideshow')) {
			window.pb = new Pushbox_Slide(document.id('slideshow'), {
				viewport: document.id('slideshow').getElement('div.viewport'),
				slides: document.id('slideshow').getElements('li.slide'),
				direction: 'right'
			});
		};
		
		findExternalLinks();
		
	}
});

/**
 * positionLeftColBlock
 *
 * set height of leftcol 
 *
 * @editor Phi Son Do <phison.do{AT}efocus.nl>
 * @return void
 */
function positionLeftColBlock(){
	var elLeftColBlock = document.getElement('div.did_u_know');
	if(!elLeftColBlock) return;
	
	var elLeftCol = document.getElement('div.leftcol');
	var elSubNavHeight = document.getElement('div.subnavigation').getHeight();
	var elLeftColBlockHeight = elLeftColBlock.getHeight();
	
	elLeftCol.setStyle('height', elSubNavHeight+elLeftColBlockHeight);
}


/**
 * initAccordionFAQ
 *
 * puts Specifications list in an accordion
 *
 * @editor Phi Son Do <phison.do{AT}efocus.nl>
 * @return void
 */
function initAccordionFAQ(){
	var elFaq = $$('div.faq')[0];
	if(!elFaq) return;
	
	var togglerName = '.accordion_toggler_';
	var contentName = '.accordion_content_';
	
	var counter = 1;	
	var toggler = $$(togglerName+counter);
	var content = $$(contentName+counter);

	while(toggler.length > 0){
		new Accordion(toggler, content, {
			opacity: false,
			display: -1,
			alwaysHide: true,
			onActive: function(toggler, content) {
				toggler.addClass('open');
			},
			onBackground: function(toggler, content) {
				toggler.removeClass('open');
			}
		});
		counter++;
		toggler = $$(togglerName+counter);
		content = $$(contentName+counter);
	}
	
};


function openFAQItem(){
	var elFaq = $$('div.faq')[0];
	if(!elFaq) return;
	
	var elFirstCategory = $$('.accordion_toggler_1')[0];
	var arrTogglerQuestion = elFaq.getElements('h4 a');
	var strUrl = self.document.location.hash.substring(1).split(' ').join('_');
	
	if (strUrl.length > 0) {
		//id in url dus er doorheen lopen
		arrTogglerQuestion.each(function (el){
			var strId = el.get('href');
			var questionText = el.get('text').split(' ');
			
			if (Browser.Engine.trident) {
				var questionTextID = questionText.join('%20');
			} else{
				var questionTextID = questionText.join('_');	
			}
			
			el.getParent().set('id', questionTextID);
	
			if(el.getParent().get('id') == strUrl){
				el.getParent('li').getParent('li').getElement('h3').fireEvent('click');
				el.getParent('li').getElement('h4').fireEvent('click');
			};
			
		});	
	
	} else {
		//geen id in url dus eerste openen
		elFirstCategory.fireEvent('click').addClass('open');
	}	
	
}

/**
 * replaces sitemap with 3 column sitemap
 * @author Rocco Janse, <rocco@efocus.nl>
 * @return void(0)
 */
function initSitemap() {
	
	if ($('sitemap')) {
		
		var items = $('sitemap').getChildren('li');
		var height = 555;
		var colheight = Math.ceil(height/3);
		var currheight = 0;
		var lists = [];
		var currlist = 0;
				
		items.each(function(item) {
			
			var elementheight = item.getSize().y;
			if (elementheight+currheight >= colheight) {
				currheight = 0;
				currlist++;
			}

			if (!lists[currlist]) {
				lists[currlist] = [];
			}
			
			lists[currlist].include(item);
			currheight += elementheight;
			
		});
		
		lists.each(function(list) {
			
			var newlist = new Element('ul', {
				'class': 'sitemap col',
				'styles': {
					'float': 'left'
				}
			});
			
			list.each(function(row) {
				row.inject(newlist);
			});
			
			newlist.inject($('sitemap'));
			
		});
	}
}

/*
 * Submits form when changing select
 * 
 * @author Ralph Meeuws <ralph.meeuws[AT]efocus.nl>
 * @author Lowen Fan <lowen[AT]efocus.nl>
 * @author Gijs Oliemans <gijs.oliemans[AT]efocus.nl>
 * @since 1.0, 18 may, 2010
 */
function submitFormOnChangeSelect(elForm) {
	if (!$defined(elForm)) return false;
	elForm.getElements('select').addEvent('change', function(){
		elForm.submit();
	});
}


/**

 * Find all external links, and add class External

 * 

 * @author Mirjam Verloop, <mirjam.verloop@efocus.nl>

 * @version 1.0, 12 feb, 2010

 * @requires MooTools 1.2.4 Core, <http://www.mootools.net>

 * @requires <a href="http://www.efocus.nl/" class="external">eFocus</a>

 * @return void

 */

function findExternalLinks() {

	

	var allExternalLinks = $$('a[href^="http://"]');

	var thisDomain = window.location.host;


	

	allExternalLinks.each(function(thisLink) {		

		if (!thisLink.get('href').contains(thisDomain) && thisLink.hasClass("linkBlocker") == false) {

			thisLink.addClass('external');

		} 		

	});

	

	initExternalLinks();	

}


/**

 * Opens external links valid in a new window without the target attribute.

 * 

 * @author Mirjam Verloop, <mirjam.verloop@efocus.nl>

 * @version 1.0, 12 feb, 2010

 * @requires MooTools 1.2.4 Core, <http://www.mootools.net>

 * @requires <a href="http://www.efocus.nl/" class="external">eFocus</a>

 * @return void

 */

function initExternalLinks() {

	var arrExternalLinks = $$('a.external');

	if (arrExternalLinks.length > 0) {

		arrExternalLinks.each(function(elExternalLink) {

			elExternalLink.addEvent('click', function(event) {

				event.stop();

				window.open(this.get('href'));

			});

		});	

	}

}

