$(document).ready(function() {
	// Fade effect on page load
	$('body').css('display', 'none');
	$('body').fadeIn(750);

	// Primary navigation drop down menu
	$(function()
	{
		$('#navigation-items').droppy();
	});

	// Add the .active class to current language
	if (language == 'en_EN')
	{
		$('a#en').addClass('active');
	}

	if (language == 'fr_FR')
	{
		$('a#fr').addClass('active');
	}

	// Toggle visibility of the extranet panel
	$('.show-panel').click(function() {
		$('#extranet-panel').slideToggle('normal');
		$(this).toggleClass('hide-panel'); return false;
	});

	// Display admin box
	$('a#admin').fancybox( {
		'padding': 0,
		'scrolling': 'no',
		'overlayOpacity': 0.85,
		'overlayColor': 'white',
		'titleShow': false,
		'autoDimensions': false,
		'width': 450,
		'height': 300,
		'transitionIn': 'elastic',
		'transitionOut': 'fade',
		'speedIn': 250,
		'speedOut': 150
	});

	// Display images and Google maps as lightboxes
	$('a.fancybox').fancybox( {
		'padding': 20,
		'scrolling': 'no',
		'overlayOpacity': 0.85,
		'overlayColor': 'white',
		'titleShow': false,
		'autoDimensions': true,
		'transitionIn': 'elastic',
		'transitionOut': 'fade',
		'speedIn': 250,
		'speedOut': 150
	});

	// Smooth scroll to the top of page
	function filterPath(string) {
		return string
		.replace(/^\//,'')
		.replace(/(index|default).[a-zA-Z] {3,4}$/,'')
		.replace(/\/$/,'');
	}
	var locationPath = filterPath(location.pathname);
	var scrollElem = scrollableElement('html', 'body');
	$('#scroll-to-top a').each(function() {
		var thisPath = filterPath(this.pathname) || locationPath;
		if (locationPath == thisPath
		&& (location.hostname == this.hostname || !this.hostname)
		&& this.hash.replace(/#/,'')) {
			var $target = $(this.hash), target = this.hash;
			if (target) {
				var targetOffset = $target.offset().top;
				$(this).click(function(event) {
					event.preventDefault();
					$(scrollElem).animate( {scrollTop: targetOffset}, 400, function() {
						location.hash = target;
					});
				});
			}
		}
	});
	function scrollableElement(els) {
		for (var i = 0, argLength = arguments.length; i <argLength; i++) {
			var el = arguments[i],
			$scrollElement = $(el);
			if ($scrollElement.scrollTop()> 0) {
				return el;
			}
			else {
				$scrollElement.scrollTop(1);
				var isScrollable = $scrollElement.scrollTop()> 0;
				$scrollElement.scrollTop(0);
				if (isScrollable) {
					return el;
				}
			}
		}
		return [];
	}

	// Homepage JavaScripts
	if (page_name == 'homepage')
	{
		$(function slider()
		{
			$('#homepage-slider').cycle(
			{
				// cleartype: false,
				cleartypeNoBg: true,
				fx: 'fade',
				timeout: 6000, // slide duration
				speed: 1000, // transition speed between 2 images
				pager: '#slider-navigation',
				before: beforeslider // transition callback (scope set to element to be shown): function(currSlideElement, nextSlideElement, options, forwardFlag
			});
		});
		function beforeslider(currSlideElement, nextSlideElement, options, forwardFlag)
		{
			$('#progress-bar div').stop(true, true).css('width', '0px').animate({width:'980px'}, 6000, 'linear');
		}
	}

	// Animate the home icon when the site logo is hovered
	$(function()
	{
		$('a#homepage').mouseenter(function()
		{
			var myObject = ('#home-icon');
			$(myObject).animate( { bottom : '0' }, 300);
		});
		$('a#homepage').mouseleave(function()
		{
			$('#home-icon').animate( { bottom: '-40px' }, 100);
		});
	});

	// Check required fields on contact form
	if (page_name == 'contact')
	{
		var userForm = document.getElementById('contact_form');
		userForm.onsubmit = function()
		{
			if (language == 'en_EN')
			{
				YY_checkform('contact_form', 'civility[0]', '#q', '2', 'Civility', 'lastname', '#q', '0', 'Last name', 'company', '#q', '0', 'Company or institution', 'city', '#q', '0', 'City', 'country', '#q', '1', 'Country', 'email', '#S', '2', 'eMail address', 'request', '#q', '1', 'Your request', 'message', '#q', '0', 'Message');return document.MM_returnValue;
			}
			else if (language == 'fr_FR')
			{
				YY_checkform('contact_form', 'civility[0]', '#q', '2', 'Civilité', 'lastname', '#q', '0', 'Nom', 'company', '#q', '0', 'Société ou institution', 'city', '#q', '0', 'Ville', 'country', '#q', '1', 'Pays', 'email', '#S', '2', 'eMail', 'request', '#q', '1', 'Objet de votre demande', 'message', '#q', '0', 'Message');return document.MM_returnValue;
			}
		}
	}

	// Check required fields on extranet registration request
	if (page_name == 'extranet_registration_request')
	{
		var userForm = document.getElementById('extranet_registration_request');
		userForm.onsubmit = function()
		{
			if (language == 'en_EN')
			{
				YY_checkform('extranet_registration_request', 'civility[0]', '#q', '2', 'Civility', 'lastname', '#q', '0', 'Last name', 'position', '#q', '0', 'Fonction', 'company', '#q', '0', 'Company or institution', 'phone', '#q', '0', 'Phone', 'city', '#q', '0', 'City', 'country', '#q', '1', 'Country', 'email', '#S', '2', 'eMail address');return document.MM_returnValue;
			}
			else if (language == 'fr_FR')
			{
				YY_checkform('extranet_registration_request', 'civility[0]', '#q', '2', 'Civilité', 'lastname', '#q', '0', 'Nom', 'position', '#q', '0', 'Fonction', 'company', '#q', '0', 'Société ou institution', 'phone', '#q', '0', 'Téléphone', 'city', '#q', '0', 'Ville', 'country', '#q', '1', 'Pays', 'email', '#S', '2', 'eMail');return document.MM_returnValue;
			}
		}
	}
	
	// Check required fields on Xpress-MP download form
	if (page_name == 'xpress_mp')
	{
		var userForm = document.getElementById('download_form');
		userForm.onsubmit = function()
		{
			if (language == 'en_EN')
			{
				YY_checkform('download_form', 'firstname', '#q', '0', 'First name', 'lastname', '#q', '0', 'Last name', 'email', '#S', '2', 'eMail address', 'institutionname', '#q', '0', 'Company or laboratory', 'address', '#q', '0', 'Address', 'city', '#q', '0', 'City', 'zipcode', '#q', '0', 'Code postal', 'country', '#q', '0', 'Country', 'phone', '#q', '0', 'Phone');return document.MM_returnValue;
			}
			else if (language == 'fr_FR')
			{
				YY_checkform('download_form', 'firstname', '#q', '0', 'Prénom', 'lastname', '#q', '0', 'Nom', 'email', '#S', '2', 'Adresse eMail', 'institutionname', '#q', '0', 'Société ou laboratoire', 'address', '#q', '0', 'Adresse', 'city', '#q', '0', 'Ville', 'zipcode', '#q', '0', 'Code postal', 'country', '#q', '0', 'Pays', 'phone', '#q', '0', 'Téléphone');return document.MM_returnValue;
			}
		}
	}

	// Check required fields on job application form
	if (page_name == 'job_application')
	{
		var userForm = document.getElementById('application_form');
		userForm.onsubmit = function()
		{
			if (language == 'en_EN')
			{
				YY_checkform('application_form', 'civility[0]', '#q', '2', 'Civility', 'firstname', '#q', '0', 'First name', 'lastname', '#q', '0', 'Last name', 'email', '#S', '2', 'eMail address');return document.MM_returnValue;
			}
			else if (language == 'fr_FR')
			{
				YY_checkform('application_form', 'civility[0]', '#q', '2', 'Civilité', 'firstname', '#q', '0', 'Prénom', 'lastname', '#q', '0', 'Nom', 'email', '#S', '2', 'eMail');return document.MM_returnValue;
			}
		}
	}

});
