/////////////////////////////////////////// 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.1 JavaScript Library
 */
window.addEvents({
	'domready': function() {
		initExternalLinks();
		fixF3MainNavIE6Hover();
		initF3MainNavDropDown();
		initF8NewsOverview();
		initF11YouTubePlayer();
		initF12SlideShow();
		initF15Rijdersoverzicht();
		initF24FaqAccordion();
		initTips();
		initInvulhulp();
	},
	'load': function() {
		initF7PartnerSlideshows();
	}
});

function init(){
	fixErrorTooltips();
}


/**
 * 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;
	
	arrExternalLinks.each(function(elExternalLink) {
		elExternalLink.addEvent('click', function(event) {
			event.stop();
			window.open(this.get('href'));   
		});
	});
}



/////////////////////////////////////////// CLIENT-SIDE JAVASCRIPT FUNCTIONS ///////////////////////////////////////////

/**
 * fontReplacement
 * Replaces the webfont of specific elements with sIFR variants.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 */
function fontReplacement() {
	var fontCenturyGothic = { src: '/fileadmin/templates/flash/centurygothic.swf', ratios: [6, 1.41, 9, 1.35, 15, 1.29, 21, 1.25, 22, 1.22, 27, 1.24, 29, 1.21, 34, 1.22, 41, 1.21, 45, 1.2, 46, 1.21, 59, 1.2, 68, 1.19, 69, 1.2, 96, 1.19, 97, 1.18, 102, 1.19, 103, 1.18, 107, 1.19, 108, 1.18, 112, 1.19, 114, 1.18, 116, 1.19, 120, 1.18, 121, 1.19, 1.18] };
	
	sIFR.activate(fontCenturyGothic);
	
	sIFR.fitExactly = true;

	sIFR.replace(fontCenturyGothic, {
		selector: '.col_left h1, .col_left h2, div.f8_nieuwsoverzicht h2, div.f18_rijdersformulier h1, .col_home_left h1, .col_home_left h2',
		wmode: 'opaque',
		css: '.sIFR-root {background-color: #ffffff; color: #f7931e; }'
	});
	
	sIFR.replace(fontCenturyGothic, {
		selector: '.col_home_center h2, .col_right h2, .col_home_right h2, .col_full h1, .col_full h2',
		wmode: 'opaque',
		css: '.sIFR-root {background-color: #f4f2f2; color: #f7931e; }'
	});
	
	sIFR.replace(fontCenturyGothic, {
		selector: '.f3_hoofdnavigatie li span',
		wmode: 'opaque',
		css: {
			'.sIFR-root': {
				'background-color': '#f4f2f2'
			},
			'a': {
				'text-decoration': 'none',
				'color': '#333333'
			},
			'a:hover': {
				'text-decoration': 'underline',
				'color': '#f7931e'
			},
			'.active': {
				'text-decoration': 'underline',
				'color': '#f7931e'
			}
		}
	});
	
	sIFR.replace(fontCenturyGothic, {
		selector: '.f9_sponsorstand h3',
		wmode: 'opaque',
		css: {
			'.sIFR-root': {
				'background-color': '#666666',
				'color': '#ffffff'
			}
		}
	});
	
	sIFR.replace(fontCenturyGothic, {
		selector: 'p.amount',
		wmode: 'opaque',
		css: {
			'.sIFR-root': {
				'background-color': '#333333',
				'text-align': 'center',
				'color': '#ffffff'
			}
		}
	});
	
	sIFR.replace(fontCenturyGothic, {
		selector: 'ul.overige_rijders p.amount',
		wmode: 'opaque',
		css: {
			'.sIFR-root': {
				'background-color': '#333333',
				'text-align': 'center',
				'color': '#ffffff'
			}
		}
	});
	
}



/**
 * fixF3MainNavIE6Hover
 * Adds the "hover" CSS class faking the :hover pseudo-selector on li-elements.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @return void
 */
function fixF3MainNavIE6Hover(){
	if (!Browser.Engine.trident4 && !document.getElement('.f3_hoofdnavigatie')) return;
	
	var arrF3MainNavChildren = document.getElement('.f3_hoofdnavigatie').getChildren();
	
	arrF3MainNavChildren.each(function(elF3MainNavChild){
		elF3MainNavChild.addEvents({
			'mouseenter': function(){
				this.addClass('hover');
			},
			'mouseleave': function(){
				this.removeClass('hover');
			}
		});
	});
	
}



/**
 * initF3MainNavDropDown
 * Calculates the width of the dropdown lists of the main nav.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @return void
 */
function initF3MainNavDropDown(){
	var elMainNav = document.getElement('.f3_hoofdnavigatie');
	if (!elMainNav) return;
	
	elMainNav.getChildren('li').each(function(elMainNavChild){
		elMainNavChild.addClass('hover');
		elMainNavDropdownList = elMainNavChild.getElement('ul');
		if (elMainNavDropdownList) {
			var intMainNavDropdownListWidth = 0; 
			elMainNavDropdownList.getElements('li').each(function(elMainNavDropdownListItem){
				intMainNavDropdownListWidth = intMainNavDropdownListWidth + elMainNavDropdownListItem.getWidth();
			});
			elMainNavDropdownList.setStyle('width', intMainNavDropdownListWidth);
		}
		elMainNavChild.removeClass('hover');
	});
}

	

/**
 * initF7PartnerSlideshows
 * Adds slideshows to the bronze sponsors and partners logo's.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @return void
 */
function initF7PartnerSlideshows() {
	var elF7PartnerOverview = document.getElement('.f7_partner_overzicht');
	if (!elF7PartnerOverview) return;
	
	var arrPartnerOverviewSlideshowLists = new Array();
	
	if (elF7PartnerOverview.getElement('.bronze_sponsors_slideshow')) arrPartnerOverviewSlideshowLists.include(elF7PartnerOverview.getElement('.bronze_sponsors_slideshow'));
	if (elF7PartnerOverview.getElement('.partners_slideshow')) arrPartnerOverviewSlideshowLists.include(elF7PartnerOverview.getElement('.partners_slideshow'));
	
	arrPartnerOverviewSlideshowLists.each(function(elPartnerOverviewSlideshowList){
		elPartnerOverviewSlideshowListWidth = elPartnerOverviewSlideshowList.getParent().getWidth() - (elPartnerOverviewSlideshowList.getStyle('padding-left').toInt() + elPartnerOverviewSlideshowList.getStyle('padding-right').toInt());
		
		var arrPartnerOverviewListItems = elPartnerOverviewSlideshowList.getElements('li');
		var intPartnerOverviewImageMaxHeight = 0;
		
		arrPartnerOverviewListItems.each(function(elPartnerOverviewListItem){
			elPartnerOverviewListItem.fade('hide');
			elPartnerOverviewListItem.setStyles('width', elPartnerOverviewSlideshowListWidth);
			var intPartnerOverviewImageHeight = elPartnerOverviewListItem.getElement('img').getHeight();
			if (intPartnerOverviewImageHeight > intPartnerOverviewImageMaxHeight) {
				intPartnerOverviewImageMaxHeight = intPartnerOverviewImageHeight;
			}
		});
		elPartnerOverviewSlideshowList.setStyle('height', intPartnerOverviewImageMaxHeight);
		
		var objPartnerOverviewSlideShow = new EfxNavSlideShow({
			arrSlides: arrPartnerOverviewListItems,
			blnAutoPlay: true,
			intInterval: 3000
		});
	});
}



/**
 * initF8NewsOverview
 * Creates a slideshow from all news messages.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @return void
 */
function initF8NewsOverview() {
	elF8NewsOverview = document.getElement('.f8_nieuwsoverzicht');
	if (!elF8NewsOverview) return;
	
	var elNewsOverviewViewport = elF8NewsOverview.getElement('.slideshow_viewport');
	var elNewsOverviewNav = elF8NewsOverview.getElement('.slideshow_nav');
	
	var objF8SlideShow = new EfxNavSlideShow({
		arrSlides: elF8NewsOverview.getElements('.slide'),
		elNav: elNewsOverviewNav,
		blnAutoPlay: false,
		
		strOutEffectProperty: 'left',
		intOutEffectStartValue: 0,
		intOutEffectEndValue: -465,
		intOutEffectDuration: 'long',
		
		strInEffectProperty: 'left',
		intInEffectStartValue: 465,
		intInEffectEndValue: 0,
		intInEffectDuration: 'long',

		intInterval: 4000
	});
	
	if(elNewsOverviewNav) {
		elNewsOverviewNav.setStyle('paddingLeft', (elNewsOverviewViewport.getWidth() - elNewsOverviewNav.getWidth()) / 2);
	}
}



/**
 * initF11YouTubePlayer
 * Embeds the YouTube player based on the video id in the HTML.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @param string 11-digit YouTube video ID, e.g. "HWOejAcTXDs"
 * @return void
 */
function initF11YouTubePlayer() {
	arrYouTubeContainers = $$('.youtube_container');
	if (arrYouTubeContainers.length == 0) return;
	
	arrYouTubeContainers.each(function(elYouTubeContainer, n){
		var strYouTubeVideoId = elYouTubeContainer.get('html');
		
		var YoutubePlayer = new Swiff(
			'http://www.youtube.com/v/'+ strYouTubeVideoId + '&enablejsapi=1&playerapiid=ytplayer', {
				id: elYouTubeContainer.className + '_' + n,
				width: elYouTubeContainer.getStyle('width'),
				height: elYouTubeContainer.getStyle('height'),
				params: {
					wmode: 'transparent'
				},
				container: elYouTubeContainer
			}
		);
	});
}



/**
 * initF12SlideShow
 * Creates a slideshow from all car images.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @return void
 */
function initF12SlideShow() {
	elF12Slideshow = document.getElement('.f12_slideshow');
	if (!elF12Slideshow) return;
	
	var objF12SlideShow = new EfxNavSlideShow({
		arrSlides: elF12Slideshow.getElements('.slide'),
		elNav: elF12Slideshow.getElement('.slideshow_nav'),
		intInterval: 4000
	});
}



/**
 * initF15Rijdersoverzicht
 * Update selectboxes of F15.
 * 
 * @author Richard van Yperen (richardvy[AT]efocus.nl)
 * @return void
 */
function initF15Rijdersoverzicht() {
	if(!$('rijder') || !$('voertuig')) return;

	$('rijder').addEvent('change', function() {
		var newLocation = 'http://'+location.hostname+location.pathname+'?tx_maartenmemorial_pi8[vehicleId]='+this.value;
		window.location = newLocation;
	});
	
	$('voertuig').addEvent('change', function() {
		var newLocation = 'http://'+location.hostname+location.pathname+'?tx_maartenmemorial_pi8[vehicleId]='+this.value;
		window.location = newLocation;
	});
}



/**
 * initF24FaqAccordion
 * Creates an accordion from the F24 FAQ list.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @return void
 */
function initF24FaqAccordion(){
	elF24Faq = document.getElement('.f24_faq');
	if (!elF24Faq) return;
	
	var objF24FaqAccordion = new Accordion(elF24Faq.getElements('div.toggler'), elF24Faq.getElements('div.panel'), {
		show: 0,
		alwaysHide: true,
		onActive: function(el){
			el.getParent().addClass('open');
			el.getElement('.indicator').set('html', '-');
		},
		onBackground: function(el){
			el.getParent().removeClass('open');
			el.getElement('.indicator').set('html', '+');
		}
	});
}



/**
 * initInvulhulp
 *
 * 
 *
 * @author Klaas Dieleman <klaas{AT}efocus.nl>
 * @return void
 */
function initInvulhulp() {
	
	
	$$('span.invulhulpcontainer').each(function(item) {

		item.getElement('select').addEvent('change', function() {
			if (item.getElement('select').getSelected().getProperty('value') != '_emptyvalue_') {
				item.getElement('input').setProperty('value', item.getElement('select').getSelected().get('text'));
			}
			item.getElement('input').focus();
		});
	});
}



/**
 * fixErrorTooltips
 * fixes 'left-overs' of tooltips
 *
 * @author Rocco Janse, <rocco[at]efocus.nl>
 * @return void()
 */
function fixErrorTooltips() {
	var errortips = $$(document.body).getElements('div.errortipcontainer');
	if (errortips == 0) return;
	errortips.each(function(tip) {
		tip.setStyle('visibility', 'hidden');
	});
}



/**
 * initTips
 *
 * attaches customly styled tooltip to form fields
 *
 * @author Klaas Dieleman <klaas{AT}efocus.nl>
 * @return void
 */
function initTips() {
	$$('.formtip').each(function(el) {
		
		if(el.get('title')){
			var content = el.get('title').split('::');   
			el.store('tip:title', content[0]);   
			el.store('tip:text', content[1]);
		}
	});
	
	var formtips = new eFocusTips('.formtip', {
		className: 'formtipcontainer',
		offsets: {'x': 260, 'y': -17},
		fixed: true,
		triggerEvent: 'focus',
		fitInWindow: false
	});
	var errortips = new eFocusTips('.errortip', {
		className: 'errortipcontainer',
		offsets: {'x': -195, 'y': 25},
		fixed: true,
		triggerEvent: 'hover',
		fitInWindow: false
	});
}

/////////////////////////////////////////// DEVELOPMENT JAVASCRIPT FUNCTIONS ///////////////////////////////////////////

/**
 * Prevent copy paste
 * 
 * @author Aart de Bruijn
 * @created 05-15-2009
 */
function disableCtrlModifer(evt)
{
	var disabled = {a:0, c:0, x:0, v:0};
	var ctrlMod = (window.event)? window.event.ctrlKey : evt.ctrlKey;
	var key = (window.event)? window.event.keyCode : evt.which;
	key = String.fromCharCode(key).toLowerCase();

	return (ctrlMod && (key in disabled))? false : true;
}

/**
 * Prevent rightclick
 * 
 * @author Aart de Bruijn
 * @created 05-15-2009
 */
function disabelRightClick(evt)
{
	return false;	
}


/**
 * displayErrors
 * Displays errors.
 * 
 * @author Gijs Epping (gijs.epping[AT]efocus.nl)
 * @return void
 */
function displayErrors(error){
	
	/*
	 * Nog een keer aanroepen, omdat deze functie altijd met parameters aangeroepen wordt en initialiseren dus misgaat
	 * @author Aart de Bruijn (aart.de.bruijn[AT]efocus.nl)
	 * gewijzigd op 27-04-09
	 */
	
	if(!error) return;
	
	var myObject = JSON.decode(error);

	$$('.error').each(function(el){
		if(el.get('tag')!='li'){
			el.removeClass('error');
		}
	});
	
	$$('.errortip').each(function(el){
		el.setStyle('display','none');
	});

	myObject.each(function(obj){

		if($(obj.id)){		
			$(obj.id).addClass('error');
			$('error_'+obj.id).setStyle('display','block');

			var content = $('error_'+obj.id).get('title').split('::');
			$('error_'+obj.id).store('tip:title', content[0]);   
			$('error_'+obj.id).store('tip:text', content[1]);				
		}
	});	
}

/**
 * showExample
 * Shows example of mail to be sent in mail a friend
 * 
 * @author Aart de Bruijn(aart.de.bruijn[AT]efocus.nl)
 * @return void
 */
function showExample(){
	var exampleDiv = $$(document.body).getElements('div.example');
	var message = ($('form_message').value);
	var recipientname = ($('form_recipientname').value);
	var link = ($('form_linkToPage').value);
	
	$$('.example').setStyle('display', 'block');
	$('recipienttext').set('text', recipientname);
	$('mailtext').set('text', message);
	$('maillink').set('text', link);
}