// JavaScript Document é
var timeOutlstLangues = null;
var timeOutlstSites = null;

function ieSpec()
  {
	//$('.sousmenu li').each(function() {$(this).css('width',$(this).parents('.soumenu').width());});
	$('.sousmenu .haut').each(function() {$(this).css('width',$(this).parents('div').width());});
	$('.sousmenu li').each(function() {$(this).css('width',$(this).parents('div').width());});
	$('#lstSites li').each(function() {$(this).css('width',$(this).parents('div').width());});
	$('#lstLangues li').each(function() {$(this).css('width',$(this).parents('div').width());});
	if($.browser.version.indexOf('7')>-1)
	  {
		$('#lstSites').each(function() {$('#lstSites').css('margin-left', - $('#triggerlstSites').width()).css('margin-top','17px');});
		$('#lstLangues').each(function() {$('#lstLangues').css('margin-left', - $('#triggerlstLangues').width()).css('margin-top','17px');});
	  }
  }

function init()
  {
	setupAccroche();
	$('ul li:last-child').addClass('last');
	alignerHauteurPush();
	$('.scrUp').hover(
		function() {tmp = $(this).parent().attr('id').split('p');num = tmp[1];startScroll(num,1)},
		function() {tmp = $(this).parent().attr('id').split('p');num = tmp[1];stopScroll(num)}
	);
	$('.scrDown').hover(
		function() {tmp = $(this).parent().attr('id').split('p');num = tmp[1];startScroll(num,-1)},
		function() {tmp = $(this).parent().attr('id').split('p');num = tmp[1];stopScroll(num)}
	);
	$('#menu li').each(function() 
		{
		  $(this).hover(function() {$(this).find('.sousmenu').css('display','block');},function() {$(this).find('.sousmenu').css('display','none');});
		  if($(this).find('.sousmenu').outerWidth(true)<$(this).outerWidth(true))
		    {
			  $(this).find('.sousmenu li a').css('width',$(this).outerWidth(true)-20);
			}
		});
	//$('#lstLangues').css('width',$('#triggerlstLangues').innerWidth()-10);
	if($('#lstLangues').length>0)
	  {$('#triggerlstLangues').hover(function() {ouvrir('lstLangues');},function() {retarderFermeture('lstLangues');});}
	//$('#lstSites').css('width',$('#triggerlstSites').innerWidth()-15);
	$('#triggerlstSites').hover(function() {ouvrir('lstSites');},function() {retarderFermeture('lstSites');});
	$('#conteneurLienPlan a').click(function() {$('#planSite').toggle('blind');});
	if($.browser.msie)
	  {ieSpec();}
	$('.lstOffres').each(function() {$(this).css('height',$(this).find('div').first().height()-1);});
	$('.zoneUpload').each(function()
		{
		  var nid = 'zcv'+$('.zoneUpload').index(this);
		  $(this).attr('id',nid);
		  $('#'+nid).flash({swf: pathRoot+'images/uploadFichiers.swf',height: 25, width:200,flashvars: {fileTarget: pathRoot+'library/ajax/upload.php', idField:$(this).attr('data-champ'), callBack: 'changerValeur', texte1:'Parcourir', texte2:'Fichier chargé'}});
		});
	checkFormsDataTypes();
  }
function changerValeur(chp,valeur)
  {$('#'+chp).val(valeur);}
function ouvrir(cible)
  {
	clearTimeout(window['timeOut'+cible]);
	if($('#'+cible).css('display')=="none")
	  {$('#'+cible).show('blind');}
  }
function fermer(cible)
  {
	clearTimeout(window['timeOut'+cible]);
	if($('#'+cible).css('display')=="block")
	  {$('#'+cible).hide('blind');}
  }
function retarderFermeture(cible)
  {
	window['timeOut'+cible] = setTimeout("fermer('"+cible+"')",500);
  }
function alignerHauteurPush()
  {
	$('.liste4pushs .push .scrolling .contenuInterne,.liste2pushs .push .scrolling .contenuInterne').each(
		function() 
		  {
			//alert($(this).attr('id')+" - "+$(this).height()+" - "+$(this).parent().height());
			tmp = $(this).attr('id').split('get');num = tmp[1];
			if($(this).height() - $(this).parent().height()>0)
			  {$('#scroll'+num).addClass('showMe');}
			else
			  {$('#scroll'+num).removeClass('showMe');}
/*
			  {$('#scroll'+num).css('display','block');}
			else
			  {$('#scroll'+num).css('display','none');}
*/
		  }
	);
  }
function startScroll(numBloc,sens)
  {
	timeScroll = setInterval ( "scrollMe('"+numBloc+"',"+sens+")", 15 );
  }
function stopScroll()
  {
	clearInterval(timeScroll);
  }
function scrollMe(numBloc,sens)
  {
	maxScroll = $('#scrollTarget'+numBloc).parent().height() - $('#scrollTarget'+numBloc).height();
	if(maxScroll<0)
	  {
		pAct = parseInt($('#scrollTarget'+numBloc).css('margin-top'));
		nPos = pAct + (sens*2);
		if(sens>0)
		  {
			if(nPos>0)
			 {$('#scrollTarget'+numBloc).css('margin-top','0px');}
			else
			  {$('#scrollTarget'+numBloc).css('margin-top',nPos+'px');}
		  }
		else
		  {
			if(nPos<maxScroll)
			  {$('#scrollTarget'+numBloc).css('margin-top',maxScroll+'px');}
			else
			  {$('#scrollTarget'+numBloc).css('margin-top',nPos+'px');}
		  }
	  }
  }


var accroche;
var current_accroche=0;
var first = true;

function setupAccroche(){
	accroche=$("[id^=accroche_]");
	if(accroche.length) {
		$('#slogan').html((accroche.eq(current_accroche).html()));
		loadAccroche();
		window.setInterval('loadAccroche()', 5000);
	}
}

function loadAccroche(){
	if (first) {
		first = false;
	}
	else {
		$('.slogan').fadeOut(400, function () {
			$('#slogan').html((accroche.eq(current_accroche).html()));
			$('.slogan').css ('top', '-400px');
			$('.slogan').css ('display', 'block');
			$('.slogan').animate({top: '14px'}, 600);
		});
		current_accroche=current_accroche+1;
	}
	if(current_accroche==5) {current_accroche=0;}
}
function increaseFont()
  {
	var currentFontSize = $('#contenu').css('font-size');
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum+2;
    $('#contenu').css('font-size', newFontSize);
	alignerHauteurPush();
  }
  // Decrease Font Size
function decreaseFont()
  {
    var currentFontSize = $('#contenu').css('font-size');
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum-2;
    $('#contenu').css('font-size', newFontSize);
	alignerHauteurPush();
  }
function movePageActive(id,n)
  {
	var cumul = 0;
	$('#'+id+' > div:lt('+n+')').each(
	  function() 
		{cumul+= $(this).height();});
	$('#'+id).animate({scrollTop: cumul, height: $('#'+id+' > div:eq('+n+')').height()-1}, 600);
  }
function offPrec(id)
  {
	if($('#'+id).attr('data-pageActive')>0)
	  {
		$('#'+id).attr('data-pageActive',parseInt($('#'+id).attr('data-pageActive'))-1);
		movePageActive(id,$('#'+id).attr('data-pageActive'));
	  }
  }
function offNext(id)
  {
	if($('#'+id).attr('data-pageActive')<$('#'+id+' > div').length-1)
	  {
		$('#'+id).attr('data-pageActive',parseInt($('#'+id).attr('data-pageActive'))+1);
		movePageActive(id,$('#'+id).attr('data-pageActive'));
	  }
  }
function checkFormsDataTypes()
  {
	$('.champsF').each(function ()
	  {
		if(!$(this).hasClass('verifie'))
		  {
			if($(this).hasClass('date'))
			  {
				$(this).attr('readonly','readonly');
				//$('#'+$(this).attr('id')).datepicker({dateFormat: 'dd/mm/yy'},$.extend({showMonthAfterYear: false},$.datepicker.regional[$(this).attr('data-langue')]));
				$('#'+$(this).attr('id')).datepicker({changeYear: true,yearRange: '1940:2020'},$.datepicker.regional[$(this).attr('data-langue')]);
			  }
			
			$(this).addClass('verifie');
		  }
	  });
  }
function ajouterGroupeChamps(idF,idC)
  {
	$.get(pathRoot+'library/ajax/donneesForm.php',{idForm:idF,idChp:idC,n:$('.bloc'+idC).length,type:'replication'},function(data) {$(data).insertAfter('#grpChamps'+idC+' table:last'); checkFormsDataTypes();})
  }
function checkMail(str)
  {
	var pattern = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
	if(pattern.test(str))
	  {return true;}
	else
	  {return false;}
  }
function controlCandidature(id,conteneur)
  {
	var i = parserChamps('.aParser,.groupeChampsRep');
	if(i[0])
	  {
		var objEnvoie = {id_offre:$('#idOffre').val(),id_formulaire:id,champs:i[1],type:'enregistrer'};
		$.post(pathRoot+'library/ajax/donneesForm.php', objEnvoie,function(data) {$('#'+conteneur+' form').remove();$(data).insertAfter('#'+conteneur+' h2');});
	  }
	
  }
function recupererGroupe(n)
  {
	var envoie = true;
	var sortie = {};
	$('.'+n).each(function() 
		{
		  var i = parserChamps('.aParser'+$(this).attr('data-niveau'));
		  sortie[$(this).attr('data-niveau')] = i[1];
		  if(!i[0])
		    {envoie = i[0];}
		}
	);
	return [envoie,sortie];
  }
function parserChamps(selecteur)
  {
	var envoie = true;
	var objSortie = {};
	$(selecteur).each(function ()
	  {
		if(envoie)
		  {
			if($(this).is(':text'))
			  {
				if(($(this).hasClass('obligatoire') && $(this).val()=='') || ($(this).hasClass('email') && !checkMail($(this).val())))
				  {
					alert($(this).attr('data-alerte'));
					envoie = false;
				  }
				objSortie[$(this).attr('id')] = $(this).val();
			  }
			else if($(this).is('.groupeChampsRep'))
			  {
				var t = recupererGroupe($(this).attr('data-groupe'));
				if(!t[0])
				  {envoie = t[0];}
				objSortie[$(this).attr('id')] = t[1];
			  }
			else if($(this).is('textarea'))
			  {
				if($(this).hasClass('obligatoire') && $(this).val()=='')
				  {
					alert($(this).attr('data-alerte'));
					envoie = false;
				  }
				objSortie[$(this).attr('id')] = $(this).val();
			  }
			else if($(this).is(':checkbox'))
			  {
				var tmp = $(this).attr('name');
				if($(this).hasClass('obligatoire') && $("input[name='"+tmp+"']:checked").length==0)
				  {
					alert($(this).attr('data-alerte'));
					envoie = false;
				  }
				objSortie[$(this).attr('name')] = listCheckSelected(tmp);
			  }
			else if($(this).is(':radio'))
			  {
				var tmp = $(this).attr('name');
				if($(this).hasClass('obligatoire') && $("input[name='"+tmp+"']:checked").length==0)
				  {
					alert($(this).attr('data-alerte'));
					envoie = false;
				  }
				objSortie[$(this).attr('name')] =$("input[name='"+tmp+"']:checked").val();
			  }
			else if($(this).is('select'))
			  {
				objSortie[$(this).attr('id')] =$(this).val();
			  }
			else if($(this).is(':hidden'))
			  {
				if($(this).hasClass('obligatoire') && $(this).val()=='')
				  {
					alert($(this).attr('data-alerte'));
					envoie = false;
				  }
				objSortie[$(this).attr('id')] =$(this).val();
			  }
		  }
	  });
	return [envoie,objSortie];
  }
function listCheckSelected(nom)
  {
	sortie = new Array();
	$("input[name='"+nom+"']:checked").each(function() 
		{sortie.push($(this).val());}
	  );
	return sortie.join(', ');
  }

function chkValChecked(n)
  {
	if($('#inpt'+n).is(':checked'))
	  {$('#ztxt'+n).css('display','block');}
	else
	  {$('#ztxt'+n).css('display','none');}
  }

