/////////////////////////////////////////// BASIC FUNCTIONS, DON'T EDIT ///////////////////////////////////////////

/*
 * domready
 * Starts executing the initializing functions when the DOM structure of the page has been loaded.
 * 
 * @author CSD (clientsidedevelopers[AT]efocus.nl)
 * @uses Mootools 1.2 JavaScript Library
 */
window.addEvent('domready', function() {
	initRemoveLinkLines();
	initExternalLinks();
	initFooterNewsletter();
	initQuickSearch();
	initDirectorySearch();
	initGoogleMaps();
});



/*
 * load
 * Starts executing the initializing functions when the entire page has been loaded.
 * 
 * @author CSD (clientsidedevelopers[AT]efocus.nl)
 * @uses Mootools 1.2 JavaScript Library
 */
window.addEvent('load', function() { // FOR USE AFTER EVERYTHING HAS LOADED: window.addEvent('load', function() {
	centerLogo();
	companyListCenterLogo();
});



/*
 * 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";
		}
	}
}

/*
 * 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()});
}







/////////////////////////////////////////// CUSTOM CLIENT-SIDE FUNCTIONS ///////////////////////////////////////////

/*
 * initFooterNewsletter
 * Removes the standard light grey text when focussing on the input field of the quicksearch form.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 */
function initFooterNewsletter() {
	if (!$('form_footer_newsletter')) return;
	
	var elFormFN = $('form_footer_newsletter');
	var elFormFNField = $('form_footer_newsletter_field');
	var strFormFNFieldValue = elFormFNField.value;
	
	elFormFNField.addEvents({
		'focus': function() {
			if (elFormFNField.value == strFormFNFieldValue) {
				elFormFNField.value = '';
				elFormFNField.removeClass('inactive');
			}
		},
		'blur': function() {
			if ((elFormFNField.value == strFormFNFieldValue) || (elFormFNField.value == '')) {
				elFormFNField.value = strFormFNFieldValue;
				elFormFNField.addClass('inactive');
			}
		}
	});
}



/*
 * initQuickSearch
 * Removes the standard light grey text when focussing on the input field of the quicksearch form.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 */
function initQuickSearch() {
	if (!$('form_quicksearch')) return;
	
	var elFormQS = $('form_quicksearch');
	var elFormQSField = $('form_quicksearch_field');
	var strFormQSFieldValue = elFormQSField.value;
	
	elFormQSField.addEvents({
		'focus': function() {
			if (elFormQSField.value == strFormQSFieldValue) {
				elFormQSField.value = '';
				elFormQSField.removeClass('inactive');
			}
		},
		'blur': function() {
			if ((elFormQSField.value == strFormQSFieldValue) || (elFormQSField.value == '')) {
				elFormQSField.value = strFormQSFieldValue;
				elFormQSField.addClass('inactive');
			}
		}
	});
}



/*
 * initDirectorySearch
 * Removes the standard light grey text when focussing on the input field of the quicksearch form.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 */
function initDirectorySearch() {
	if (!$('form_ict_directory_search')) return;
	
	var elFormDS = $('form_ict_directory_search');
	var elFormDSField = $('form_ict_directory_search_field');
	var strFormDSFieldValue = elFormDSField.value;
	
	elFormDSField.addEvents({
		'focus': function() {
			if (elFormDSField.value == strFormDSFieldValue) {
				elFormDSField.value = '';
				elFormDSField.removeClass('inactive');
			}
		},
		'blur': function() {
			if ((elFormDSField.value == strFormDSFieldValue) || (elFormDSField.value == '')) {
				elFormDSField.value = strFormDSFieldValue;
				elFormDSField.addClass('inactive');
			}
		}
	});
}


/*
 * initGoogleMaps
 * Loads Google Maps from the address within the div container.
 * 
 * @author Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 */
function initGoogleMaps() {
	if (!$('map')) return;
	
	if (GBrowserIsCompatible()) {
		var strAddress = $('map').innerHTML;
		
		var map = new GMap2(document.getElementById("map"));
		var geocoder = new GClientGeocoder();
		
		function showAddress(address) {
			geocoder.getLatLng(
				address,
				function(point) {
					if (!point) {
						alert(address + " niet gevonden");
					} else {
						map.setCenter(point, 13);
						var marker = new GMarker(point);
						map.addOverlay(marker);
						map.addControl(new GLargeMapControl());
						map.addControl(new GMapTypeControl());
//						marker.openInfoWindowHtml(address);
						GEvent.addListener(marker, "click", function() {
							marker.openInfoWindowHtml(address);
						});
					}
				}
			);
		}
		
		showAddress(strAddress);
	}
}



/*
 * centerLogo
 * Centers and loops the participants logos.
 * 
 * @author Jasper Leferink (jasper.leferink{at}efocus.nl)
 * @editor Ralph Meeuws (ralph.meeuws{at}efocus.nl)
 */
function centerLogo(){
	var arrParticipantBoxes = $$('.participants');
	var elParticipantBox = arrParticipantBoxes[0];
	
	arrParticipantLogoLinks = elParticipantBox.getElements('a');
	arrParticipantLogoLinks.erase(elParticipantLink);
	
	if (arrParticipantLogoLinks.length == 0) return;
	
	var elParticipantLink = elParticipantBox.getElement('.participant_link');
	arrParticipantLogoLinks.erase(elParticipantLink);
	
	intParticipantLogoLinkSelected = 0;

	centeringParticipantLogoLink = function(logo) {
		var topMargin = (117 - logo.getSize().y) / 2;
		var leftMargin = (128 - logo.getSize().x) / 2;
		logo.setStyle('top', topMargin);
		logo.setStyle('left', leftMargin);
	}

	objParticipantsLoop = function() {
		if (intParticipantLogoLinkSelected >= arrParticipantLogoLinks.length) {
			intParticipantLogoLinkSelected = 0;
		}
		
		arrParticipantLogoLinks.each(function(elParticipantLogoLink){
			if (elParticipantLogoLink == arrParticipantLogoLinks[intParticipantLogoLinkSelected]) {
				elParticipantLogoLink.setStyle('visibility', 'visible');
				centeringParticipantLogoLink(elParticipantLogoLink);
			} else {
				elParticipantLogoLink.setStyle('visibility', 'hidden');
			}
		});
		
		intParticipantLogoLinkSelected++;
	}
	
	objParticipantsLoop();
	
	objParticipantsLoop.periodical(2000);
}



/*
 * companyListCenterLogo
 * Centers the logo in the companylist.
 * 
 * @editor Ralph Meeuws (ralph.meeuws[AT]efocus.nl)
 */
function companyListCenterLogo(){
	arrCompanyList = $$('.company_list');
	if (arrCompanyList.length == 0) return;
	
	arrCompanyList.each(function(elCompanyList){
		var arrCompanyListLinks = elCompanyList.getElements('a');
		arrCompanyListLinks.each(function(elCompanyListLink){
			var intCompanyListLinkWidth = elCompanyListLink.getStyle('width').toInt();
			var intCompanyListLinkHeight = elCompanyListLink.getStyle('height').toInt();
			var elCompanyListLinkSpanWidth = elCompanyListLink.getElement('span').getSize().x;
			var elCompanyListLinkImg = elCompanyListLink.getElement('img');
			if (elCompanyListLinkImg) {
				var elCompanyListLinkImgWidth = elCompanyListLinkImg.getSize().x;
				var elCompanyListLinkImgHeight = elCompanyListLinkImg.getSize().y;

				var leftMargin = ((intCompanyListLinkWidth - elCompanyListLinkSpanWidth) - elCompanyListLinkImgWidth) / 2;
				elCompanyListLinkImg.setStyle('margin-left', leftMargin);
				elCompanyListLinkImg.setStyle('margin-right', leftMargin - 2);
				var topMargin = (intCompanyListLinkHeight - elCompanyListLinkImgHeight) / 2;
				elCompanyListLinkImg.setStyle('margin-top', topMargin);
				elCompanyListLinkImg.setStyle('visibility', 'visible');			
			}
		});
	});
}





/////////////////////////////////////////// CUSTOM SERVER-SIDE FUNCTIONS ///////////////////////////////////////////






