	
	function hpSlideshow(){
		var elts = $('#hp-slideshow .stages li');
		var btns = $('#hp-slideshow .indicator li');
		var current = 0;
		
		btns.click(function(event){
			event.preventDefault();
			var indx = btns.index($(this));
			if(indx != current){
				elts.parent().animate({
					left : -(72.2*indx)+'em'
				}, 500);

				$(this).parent().find('.on').removeClass('on');
				$(this).addClass('on');
				current = indx;
			}
		});

		timer = window.setInterval('hpnext()',7000);
	}

function hpnext() {
	var lis = $('#hp-slideshow .indicator li');
	var lion = $('#hp-slideshow .indicator li.on');
	var i = lis.index(lion);
	i++;
	if (i == lis.length) {
		i = 0;
	}
	lis[i].click();
}

function coverflow(){
	var ctnr = $('#coverflow');
	var e = ctnr.find('li');
	var t = e.length;
	var c = 2;
	var speed = 250;
	var w = 130;
	var draggable = false;
	var ctnrPos = ctnr.offset();
	var u = Math.ceil(ctnr.width()/t);

	e.css({
		'position': 'absolute',
		'opacity' : 0,
		width : 28,
		top : 50
	});
	
	function update(){
		e.hide();
		if(c>=4){
			e.eq(c-4).show().animate({
				'z-index': 0,
				width : 28,
				left : 0,
				top : 50,
				'margin-left' : -28,
				'opacity' : 0
			}, speed);
		}
		if(c>=3){
			e.eq(c-3).show().animate({
				'z-index': 0,
				width : 28,
				left : 0,
				top : 50,
				'margin-left' : -28,
				'opacity' : 0
			}, speed);
		}
		
		if(c>=2){
			e.eq(c-2).show().animate({
				'z-index': 0,
				width : (w/2.5),
				left : 0,
				top : 40,
				'margin-left' : 0,
				'opacity' : 1
			}, speed);
		}
		if(c>=1){
			e.eq(c-1).show().animate({
				'z-index': 5,
				width : (w/1.5),
				left : '20%',
				top : 20,
				'margin-left' : -(w/3),
				'opacity' : 1
			}, speed);
		}
		e.eq(c).show().animate({
			'z-index': 10,
			width : w,
			top : 0,
			left : '50%',
			'margin-left' : -(w/2),
			'opacity' : 1
		}, speed);
		e.eq(c+1).show().animate({
			'z-index': 5,
			width : (w/1.5),
			left : '80%',
			top : 20,
			'margin-left' : -(w/3),
			'opacity' : 1
		}, speed);
		e.eq(c+2).show().animate({
			'z-index': 0,
			width : (w/2.5),
			left : '100%',
			top : 40,
			'margin-left' : -(w/2.5),
			'opacity' : 1
		}, speed);
		e.eq(c+3).show().animate({
			'z-index': 0,
			width : 28,
			left : '100%',
			top : 50,
			'margin-left' : 0,
			'opacity' : 0
		}, speed);
		e.eq(c+4).show().animate({
			'z-index': 0,
			width : 28,
			left : '100%',
			top : 50,
			'margin-left' : 0,
			'opacity' : 0
		}, speed);
		
		ctnr.find('.drag')
		.animate({
			'left' : (ctnr.width()/t)*c
		}, 250);
	}

	update();
	
	e.click(function(event){
		c = $(this).index();
		update();
	});
	ctnr.find('.drag').mousedown(function(event){
		event.preventDefault();
		draggable = true;
	}).mousemove(function(event){
		if(draggable==true){
			var parentOffset = $(this).parent().offset();
			var w2 = $(this).width()/2;
			var mX = event.pageX;
			ctnr.find('.drag').css('left', mX-parentOffset.left-w2);
		}
	})
	
	$(document).mouseup(function(){
		draggable = false;
		var dragPos = ctnr.find('.drag').position();
		var dragPosX = dragPos.left;
		c = Math.round(dragPosX/u);
		update();
	});

}

/*
function popinMsg(c){

	var popin = '<div id="overlay"><div class="bg"></div><div class="msg">';
	popin +=	'<p class="success">Votre candidature a bien été envoyée !</p>';
	popin +=	'<p>Merci d’avoir postulé à cette annonce.</p>';
	popin +=	'<a href="#" class="closer button">Fermer</a>';
	popin +=	'</div></div>';

	function msg(){
		$('body').append(popin);
		$('#overlay').fadeIn(500, function(){
			$(this).find('.closer, .bg').click(function(event){
				event.preventDefault();
				$('#overlay').fadeOut(500, function(){
					$(this).remove();
				})
			})
		});
	}

	$(c).submit(function(event){
		$.ajax({
			url: this.action,
			type: this.method,
			data: $(this).serialize(),
			success: function(msg) {
				if (msg == 'Enregistré') {
					msg();
				}
				else {
					$('.formerror').remove();
					$('#application h2').after($('.formerror', msg));
				}
			},
			error: function(){
				//console.log('error');
			}
		});
		return false;
	})
	
}*/

function msgform(){
	var popin = '<div id="overlay"><div class="bg"></div><div class="msg">';
	if(ln != '' && ln == 'en')
	{
	  popin +=	'<p class="success">Your application has been successfully submitted ! </p>';
	  popin +=	'<p>Thank you for your confidence in ABERDYN. One of our consultants will study your profile and come back to you shortly to discuss your application. </p>';
	  popin +=	'<a href="#" class="closer button">Close</a>';
  }
  else
  {
	  popin +=	'<p class="success"><?php Votre candidature a bien été envoyée !</p>';
	  popin +=	'<p>Merci de nous avoir transmis vos informations, nous vous recontacterons au plus vite.</p>';
  	popin +=	'<a href="#" class="closer button">Fermer</a>';
  }
	popin +=	'</div></div>';
	$('body').append(popin);
	$('#overlay').fadeIn(500, function(){
		$(this).find('.closer, .bg').click(function(event){
			event.preventDefault();
			$('#overlay').fadeOut(500, function(){
				$(this).remove();
			})
		})
	});
}

function selectReplace(c){
	var sel = $(c).find('select');

	sel.each(function(){
		var currentSelect = $(this);
		var seloptions = currentSelect.find('option');
		var inFocus = false;
		currentSelect.css('display', 'none');
		if(currentSelect.find('option:selected') && currentSelect.find('option:selected').text() != ''){
			var initiaLabel = currentSelect.find('option:selected').text();
		} else if(seloptions.eq(0).text() != ''){
			var initiaLabel = seloptions.eq(0).text();
		}else {
			var initiaLabel = 'Choisissez';
		}

		var substitute = '<div class="list-substitute"><a href="#" class="btn-open-list"><span class="arrow"></span><span class="text">'+initiaLabel+'</span></a><ul>';
		for (var i=0; i<seloptions.length; i++ ){
			if(seloptions.eq(i).text() != ''){
				substitute += '<li><a href="#" id="s'+i+'">'+seloptions.eq(i).text()+'</a></li>';
			}
		}
		substitute += '</ul></div>';
		currentSelect.after(substitute);
		var container = currentSelect.next();
		var liste = container.find('ul');
		if(currentSelect.is(':disabled')){
			container.addClass('disabled');
		}
		container.find('.btn-open-list').click(function(event){
			event.preventDefault();
			if(!$(this).parent().hasClass('disabled')){
				sel.next().find('ul').not(liste).slideUp(125);
				liste.slideToggle(250);
			}
		}).blur(function(){
				if (liste.is(':visible') && !inFocus) {
					liste.slideUp(250);
				}
		});
		var elts = liste.find('a');
		elts.mouseenter(function(){
			 inFocus=true;
			 $(this).parent().addClass('hover');
		 }).mouseleave(function(){
			 inFocus = false;
			 $(this).parent().removeAttr('class');
		 }).click(function(event){
				event.preventDefault();
				var value = $(this).text();
				var n = $(this).attr('id').replace("s", "");
				seloptions.eq(n).attr('selected', 'selected');
				currentSelect.trigger('change');
				container.find('.btn-open-list').html('<span class="arrow"></span><span class="text">'+value+'</span>');
				liste.slideUp(250);
				inFocus = false;
		});
	});
}

function fakeFile(c){
	$(c).find('input:file').each(function(){
		$(this).change(function(){
				var cur_value = $(this).val();
				var valArray = cur_value.split('\\');
				var newVal = valArray[valArray.length-1];
	
				$(this).prev().find('.text').text(newVal);
			}
		).hover(function(){
				$(this).prev().addClass('hover');
			},function(){
				$(this).prev().removeClass('hover');
			}
		);
	})
}

function clearInput(c){
	var champs = $(c).find('input.exVal');
	champs.each(function(){
		var initValue = $(this).val();
		$(this).focus(function(){
			if($(this).val()==initValue){
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val()==''){
				$(this).val(initValue);
			}
		})
	})
	
}

function inrLabels(c){
	var labels = $(c).find('.inrLabel label');
	var inputs = $(c).find('.inrLabel input:text');
	
	function isEmpty(elem){
		if (elem.not(':empty') && elem.val() != '') {
			elem.prev().addClass('hastext');
		}else {
			elem.prev().removeClass('hastext');
		}
	}
	
	inputs.each(function(){
		$(this).focus(function(){
			$(this).prev().addClass('focus');  
		}).blur(function(){
			if (!$(this).val()){
				$(this).prev().removeClass('focus').removeClass('hastext');          
			}
		}).keypress(function() {
		 $(this).prev().addClass('hastext');
		}).change(function(){
			isEmpty($(this));
		});
		
		isEmpty($(this));
	});
}

/**
 * Complete le formulaire vert de la partie offre
 * Gère aussi le "tri" et le nombre d'éléments par page
 */
function fixoffer() {
	if (($('#views-exposed-form-offer-page').length > 0) && ($('#vform').length > 0)) {
		$('#vfield_offer_categorie_tid').html($('#edit-field-offer-categorie-tid').html());
		$('#vfield_offer_lieu_tid').html($('#edit-field-offer-lieu-tid').html());
		$('#vfield_offer_remuneration_tid').html($('#edit-field-offer-remuneration-tid').html());
		$('#vform').submit(function(event){
			event.preventDefault();
			$('#edit-field-offer-categorie-tid').val($('#vfield_offer_categorie_tid').val());
			$('#edit-field-offer-lieu-tid').val($('#vfield_offer_lieu_tid').val());
			$('#edit-field-offer-remuneration-tid').val($('#vfield_offer_remuneration_tid').val());
			$('#views-exposed-form-offer-page').submit();
		});
		$('#date').val($('#created').val());
		$('#date').change(function(){ $('#created').val($('#date').val());$('#views-exposed-form-offer-page').submit();});
		$('#pages').val($('#items_per_page').val());
		$('#pages').change(function(){ $('#items_per_page').val($('#pages').val());$('#views-exposed-form-offer-page').submit();});
	}
}

/**
 * Gère le défilement des logos de la partie "Ils nous ont fait confiance"
 */
function coverfliplaunch() {
	if ($('#flip').length > 0) {
		// Lance le coverflip
		$('#flip').jcoverflip({
			current: 0,
			change: function(event, ui) {
				var unitCoverflip = getCoverflipUnit();
				$('#scrollbar').slider('value', (ui.to * unitCoverflip));
			}
		});
		// Prépare la barre
		$('#scrollbar').slider({
			value: 0,
			stop: function(event, ui) {
				if (event.originalEvent) {
					var unitCoverflip = getCoverflipUnit();
					var newIndex = Math.round(ui.value / unitCoverflip);
					$('#flip').jcoverflip('current', newIndex);
					$('#scrollbar').slider('value', (newIndex * unitCoverflip));
				}
			}
		});
	}
}

// Calcul de la longueur d'un cran
function getCoverflipUnit() {
	return (100 / $('#flip').jcoverflip('length'));
}

function initForms(){
	fixoffer();
	clearInput('#page');
	inrLabels('#page')
	selectReplace('#page');
	fakeFile('#page');
}

function toggleSitemap(){
	$('footer .toggleSitemap, #sitemap .closer').click(function(event){
		event.preventDefault();
		$('#sitemap').slideToggle(500);
	})
}

$(document).ready(function(){
	initForms();
	toggleSitemap();
	coverfliplaunch();
})

