/////////////////////////////////////////// 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() { // Starts executing the initializing functions when the DOM structure of the page has been loaded.
		initExternalLinks();
		initRemoveLinkLines();
		loadYouTubePlayer();
		initTips();
//		initSponsors();
		initSpeakerLastItem();
		initSpeakerAccordion();
		loadRouteMap();
		alignF14PartnerLogos();
		initFooterLogoSwitch();

	},
	'load': function() { // Starts executing when the entire page has been loaded.
		infoBlockEqualHeights();
		alignFooterLogos();
	}
});


/**
 * initTips
 *
 * attaches customly styled tooltip to form fields
 *
 * @author Klaas Dieleman <klaas{AT}efocus.nl>
 * @return void
 */
function initTips() {
	if (!document.getElement('.formtip')) return;
		
	$$('.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
	});
}


/*
 * initExternalLinks
 * Used in order for certain links to open in a new window without the direct target attribute (so it validates properly).
 * 
 * @author CSD (clientsidedevelopers[AT]efocus.nl)
 * @uses <a href="http://www.efocus.nl/" rel="external">eFocus site</a>
 */
function initExternalLinks() {
	if (!document.getElementsByTagName) return; 
	var arrAnchors = document.getElementsByTagName("a"); 
	for (var i=0; i<arrAnchors.length; i++) { 
		var elAnchor = arrAnchors[i]; 
		if (elAnchor.getAttribute("href") && elAnchor.getAttribute("rel") == "external") {
			elAnchor.target = "_blank";
		}
	}
}

function initSponsors() {	
	setTimeout("getSponsors()", 5000);	
}

function getSponsors() {
	if(!$('footer')) return;	
	tx_csn_pi1getSponsors();
	setTimeout("getSponsors()", 30000);	
}

/*
 * initRemoveLinkLines
 * Removes the dotted lines around links, but breaks the Tab button on the keyboard: less accessability.
 * 
 * @author CSD (clientsidedevelopers[AT]efocus.nl)
 */
function initRemoveLinkLines() {
	$$('a').addEvent('focus', function(){this.blur()});
}



/////////////////////////////////////////// CLIENT-SIDE JAVASCRIPT FUNCTIONS ///////////////////////////////////////////

/*
 * loadYouTubePlayer
 * 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
 */
function loadYouTubePlayer() {
	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
			}
		);
	});
}



/*
 * infoBlockEqualHeights
 * Calculates the largest height of the three info blocks and adds it to all three.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @param 
 */
function infoBlockEqualHeights(){
	arrInfoBlocks = $$('.info_block');
	if (arrInfoBlocks.length == 0) return;
	
	var intInfoBlockMaxHeight = 0;
	var elInfoBlockResizeBox = null;
	
	function getElementToResize(elInfoBlock) {
		if (elInfoBlock.getChildren('.inner_block').length != 0) {
			elInfoBlockResizeBox = elInfoBlock.getChildren('.inner_block')[0];
		} else if (elInfoBlock.getChildren('.youtube_container').length != 0) {
			elInfoBlockResizeBox = elInfoBlock.getChildren('.youtube_container')[0];
		}
		return elInfoBlockResizeBox;
	}
	
	arrInfoBlocks.each(function(elInfoBlock, n){
		getElementToResize(elInfoBlock);
		
		if (elInfoBlockResizeBox.getSize().y > intInfoBlockMaxHeight) {
			intInfoBlockMaxHeight = elInfoBlockResizeBox.getSize().y;
		}
	});
	
	arrInfoBlocks.each(function(elInfoBlock, n){
		getElementToResize(elInfoBlock);
		
		var intInfoBlockHeightOffset = elInfoBlockResizeBox.getStyle('paddingTop').toInt() + elInfoBlockResizeBox.getStyle('paddingBottom').toInt();
		
		// Additional offset for IE, but not on the last column:
		if (Browser.Engine.trident && (n != (arrInfoBlocks.length - 1))) {
			intInfoBlockHeightOffset = intInfoBlockHeightOffset - 1;
		}
		
		elInfoBlockResizeBox.setStyle('height', intInfoBlockMaxHeight - intInfoBlockHeightOffset);	
	});
}



/*
 * alignFooterLogos
 * Aligns the footer logos vertically.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @param 
 */
function alignFooterLogos(){
	if (!$('footer')) return;
	if (!$('footer').getElement('.logo_list')) return;
	
	var intLogoSubListImageMaxHeight = 0;
	
	elLogoMainList = $('footer').getElement('.logo_list');
	arrLogoSubLists = elLogoMainList.getElements('ul');
	
	arrLogoSubLists.each(function(elLogoSubList){
		arrLogoSubListImages = elLogoSubList.getElements('img');
	
		arrLogoSubListImages.each(function(elLogoSubListImage){
			intLogoSubListImageHeight = elLogoSubListImage.getStyle('height').toInt();

			intLogoSubListImageMaxHeight = (intLogoSubListImageHeight > intLogoSubListImageMaxHeight) ? intLogoSubListImageHeight : intLogoSubListImageMaxHeight;
		});
		
		arrLogoSubListImages.each(function(elLogoSubListImage){
			elLogoSubListImage.setStyle('paddingTop', ((intLogoSubListImageMaxHeight - elLogoSubListImage.getStyle('height').toInt()) / 2));
		});		
	});
}




/*
 * initSpeakerLastitem
 * remove border on last item of list
 * 
 * @author Phison Do (phison.do[AT]efocus.nl)
 */
function initSpeakerLastItem(){
	if (!$('sprekeroverzicht')) return;
	
	var elSpeakerList = $('sprekeroverzicht');	
	var arrSpeakerListItems = elSpeakerList.getElements('li');
	
	arrSpeakerListItems.each(function(elSpeakerListItem){
		arrSpeakerListItems[arrSpeakerListItems.length - 1].addClass('last');
	});
}



/**
 * initSpeakerAccordion
 *
 * puts Specifications list in an accordion
 *
 * @author Klaas Dieleman <klaas{AT}efocus.nl>
 * @editor Phi Son Do <phison.do{AT}efocus.nl>
 * @return void
 */

function initSpeakerAccordion() {
	if (!$('sprekeroverzicht')) return;
	
	var specsAccordion = new Accordion('li.speaker', 'ul.sprekeroverzicht div.speakerbio', {
		onActive: function(toggler){
			toggler.addClass('open');
		},
		onBackground: function(toggler){
			toggler.removeClass('open');
		},
		alwaysHide: true,
		show: -1
	});
}




/*
 * loadRouteMap
 * Loads a map with a custom icon using Google Maps.
 * 
 * @author Ralph Meeuws (ralph-meeuws[AT]efocus.nl)
 * @editor Phi Son Do <phison.do{AT}efocus.nl>
 */

function loadRouteMap() {
	if (!$('route_map')) return;
	
	var elRouteMap = $('route_map');
	var elRouteZip = $('route_zip');
	var elRouteBtn = $('route_button');
	var elRouteDir = $('route_directions');		
	var elRoutePrt = $('route_print');
	
	var strCompanyName = $('company_name').innerHTML;
	var strCompanyStreet = $('company_street').innerHTML;
	var strCompanyCity = $('company_city').innerHTML;
	var strBalloonContent = '<h4>' + strCompanyName + '</h4>' + '<p>' + strCompanyStreet + '<br />' + strCompanyCity + '</p>';
	var locale = 'nl_NL';

	if (GBrowserIsCompatible()) {
		var map = new GMap2(elRouteMap);
		var geocoder = new GClientGeocoder();
	
		var gdir = new GDirections(map, elRouteDir);
		
		var strAddress = strCompanyStreet + ', ' + strCompanyCity;

		function showAddress(address) {
			geocoder.getLatLng(
				address,
				function(point) {
					if (!point) {
						alert(address + " not found");
					} else {
						map.setCenter(point, 12);
												
						var marker = new GMarker(point);
						map.addOverlay(marker);

				        map.setUIToDefault();
						marker.openInfoWindowHtml(strBalloonContent);
						
						GEvent.addListener(marker, "click", function() {
							marker.openInfoWindowHtml(strBalloonContent);
						});			
						

					}
				}
			);
		}
		
		showAddress(strAddress);
	}
	
	
	elRouteBtn.addEvent('click', function(){
	  	strRouteZip = elRouteZip.value;
		gdir.load('from: ' + strRouteZip + ' to: ' + strAddress, { 'locale': locale });
		showPrintButton.delay(1000);
	});
	
	$('route_form').addEvent('submit', function(){
	  	strRouteZip = elRouteZip.value;
		gdir.load('from: ' + strRouteZip + ' to: ' + strAddress, { 'locale': locale });
		showPrintButton.delay(1000);
	});
	
	elRouteZip.addEvent('keydown', function(event){
		if (event.key == 'enter') {
	  		strRouteZip = elRouteZip.value;
			gdir.load('from: ' + strRouteZip + ' to: ' + strAddress, { 'locale': locale });	
			showPrintButton.delay(1000);		
		}
	});
			
	elRoutePrt.addEvent('click', function(event){
		window.open('http://maps.google.nl/maps?f=d&source=s_d&saddr='+strRouteZip+'&daddr='+strCompanyCity+'&hl=nl&geocode=&mra=pe&mrcr=0&ie=UTF8&z=10&layer=c&pw=2', '_blank');
	});

	function showPrintButton(){
		var elGoogleDir = elRouteDir.getElement('.googledir');
		if (elGoogleDir){
			elRoutePrt.removeClass('hidden');
		}		
	}

}



/*
 * alignF14PartnerLogos
 * Aligns the logos vertically.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @param 
 */
function alignF14PartnerLogos(){
	if (!document.getElement('.f14_partneroverzicht')) return;
	
	var intLogoListImageMaxHeight = 0;
	
	arrLogoLists = document.getElement('.f14_partneroverzicht').getElements('ul');
		
	arrLogoLists.each(function(elLogoList){
		arrLogoListImages = elLogoList.getElements('img');
		arrLogoListImages.each(function(elLogoListImage){
			intLogoListImageHeight = elLogoListImage.getStyle('height').toInt();
			intLogoListImageMaxHeight = (intLogoListImageHeight > intLogoListImageMaxHeight) ? intLogoListImageHeight : intLogoListImageMaxHeight;
		});
		
		arrLogoListImages.each(function(elLogoListImage){
			elLogoListImage.setStyle('paddingTop', ((intLogoListImageMaxHeight - elLogoListImage.getStyle('height').toInt()) / 2));
		});		
	});
}




/*
 * initFooterLogoSwitch
 * switches the logos dynamicly.
 * 
 * @author Phison Do (phison.do[AT]efocus.nl)
 * @editor zRalph Meeuws (ralph.meeuws[AT]efocus.nl)
 * @param 
 */
function initFooterLogoSwitch(){
	if (!$('footer') || !$('footer').getElement('.logo_list')) return;
	
	var elFooterLogoList = $('footer').getElement('.logo_list');
	var arrFooterLogoSublists = elFooterLogoList.getElements('ul');	
	
	var intFooterLogoOddSelected = 1;
				
	arrFooterLogoSublists.each(function(elFooterLogoSublist){
		var arrFooterLogoSublistItems = elFooterLogoSublist.getElements('li');
			
		var arrFooterLogoPairs = new Array();
		var intLogoPairNumber = 1;
		
		arrFooterLogoSublistItems.each(function(elFooterLogoSublistItem, i){			
			if (intFooterLogoOddSelected <= arrFooterLogoSublistItems.length) {
		
				var arrLogoPair = new Array();
		
				arrLogoPair.include(arrFooterLogoSublistItems[intFooterLogoOddSelected - 1]);
				if (arrFooterLogoSublistItems[intFooterLogoOddSelected]) arrLogoPair.include(arrFooterLogoSublistItems[intFooterLogoOddSelected]);
		
				arrFooterLogoPairs.include(arrLogoPair);				
				intFooterLogoOddSelected = intFooterLogoOddSelected + 2;			
			}			
 		});
		
		intFooterLogoOddSelected = 1;
	
		function showLogoPair(){
			arrFooterLogoPairs.each(function(arrFooterLogoPair, i){
				if (i == intLogoPairNumber -1) {
					arrFooterLogoPair.each(function(elFooterLogo){
						elFooterLogo.removeClass('hide_logo');
					});
				}else {
					arrFooterLogoPair.each(function(elFooterLogo){
						elFooterLogo.addClass('hide_logo');
					});
				}
			});
			
			if (intLogoPairNumber >= arrFooterLogoPairs.length){
				intLogoPairNumber = 1;
			}else {
				intLogoPairNumber++;
			}
		}		
		showLogoPair();
		var intervalLogoTimer = showLogoPair.periodical(5000);
		
	});
}




/////////////////////////////////////////// SERVER-SIDE JAVASCRIPT FUNCTIONS ///////////////////////////////////////////

