window.addEvents({
	'load': function() {
		toggleDefValues();
		showBox();
		initCalendar();
		findExternalLinks();
		
		if ($(document).getElement($('archiver'))) {
			submitFormOnChangeSelect($(document).getElement($('archiver'))); 
		}
	}
});

/**
 * 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.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'));
			});
		});
	}
} 

/**
 * Loads the monkey physics calendar
 *
 * @author Rou-hun Fan, <lowen@efocus.nl>
 * @version 1.0, 21/10/2010
 * @return void
 */

function initCalendar() {
	if(!document.getElement('input.calendar')) return false;	
	
	var currentDate = new Date();
	
	var calendar = new DatePicker('input.calendar', {
		allowEmpty: true,
		maxDate: currentDate,
		toggleElements: '.cal_toggler'
	});
}


/**
 * shows a popup box
 *
 * @author Rou-hun Fan, <lowen@efocus.nl>
 * @version 1.0, 21/10/2010
 * @return void
 */
function showBox() {
	if(!document.getElement('.show_message')) return false;
	
	document.getElement('.show_message').addEvent('click', function(event){
		event.stop();
		document.getElement('p.call_to_action_message').removeClass('hidden');
	});
}

/**
 * toggle default values in form inputs
 * handles empty submits
 * 
 * NOTE: IE does not like type changes of inputs, so we insert a new element on the fly
 * this does NOT work in Sitecore projects
 *
 * @author Rocco Janse, <rocco@efocus.nl>
 * @version 1.0, 20/10/2010
 * @return void
 */
function toggleDefValues() {
	
	// get forms
	var arrForms = $(document.body).getElements('form');
	arrForms.each(function(form) {
		
		// get inputs per form
		var arrInputs = form.getElements('input');
		arrInputs.each(function(input) {
			
			// get default value (from DOM)
			var defValue = input.get('defaultValue');
			
			// if default value set, remove default value
			if(defValue) {
				input.removeEvents();
				input.addEvents({
					'focus': function() {
						if (input.get('value') == defValue) {
							input.set('value', '');
						}
				
						// new input for IE, if input is password
						if(input.get('id') == 'pass') {
							newInput = new Element('input', {
								'type': 'password',
								'name': input.get('name'),
								'id': input.get('id'),
								'value': input.get('value'),
								'class': input.get('class')
							});
							
							// replace and add event to toggle value
							newInput.removeEvents();
							newInput.replaces(input);
							newInput.addEvents({
								'blur': function() {
									if (newInput.get('value') == '') {
										input.set('value', defValue);
										input.replaces(newInput);									
									}
								},
								'keydown': function(event) {
									if (event.key == 'enter') {
										form.submit();
									}
								}
							});
							
							// set focus to new input with a timeout otherwise
							// focus does not work
							window.setTimeout(function() {
								newInput.focus();
							}, 10);
						}
					}.bind(this),
					'blur': function() {
						if (input.get('value') == '') {
							input.set('value', defValue);
						}
					},
					'keydown': function(event) {
						if (event.key == 'enter') {
							form.submit();
						}
					}
				});				
			} else {
				
				if (input.hasClass('submitonenter')) {
					
					input.addEvents({
						'keydown': function(event) {
							if (event.key == 'enter') {
								form.submit();
							}
						}
					});					
				}

			};
		});
	});
}

/**
 * compares default values and current values of inputs
 * and empties inputs if the same before sending current form
 * so error trapping is possible
 * 
 * @author Rocco Janse, <rocco@efocus.nl>
 * @version 1.0, 20/10/2010
 * @return void
 */
function submitForm(form) {
	form.submit();
}

/*
 * Submits form when changing select
 *
 * @author Ralph Meeuws <ralph.meeuws[AT]efocus.nl>
 * @author Phi Son Do <phison.do[AT]efocus.nl>
 * @since 1.0, 18 may, 2010
 */

function submitFormOnChangeSelect(elForm) {
 if (!$defined(elForm)) return false;
 elForm.getElements('select').addEvent('change', function(){
	 this.getElements('option').each(function(elOption){
		 if (elOption.get('selected')) window.location.replace(elOption.get('value'));
	 });
 });
} 
