/*-----------------------------------------------------------
	Project: Project
	Date : 	September 2009
	Last Update : 11:15 01/09/2009
	Author:	C2iS - LGU
	Summary : JS for all pages	
-----------------------------------------------------------*/




/*:::::::: INITS ::::::::::*/

$(document).ready(function() {
/*functions for IE 6*/
	if ($.browser.msie && $.browser.version <= 6 ) {
		try {
		document.execCommand('BackgroundImageCache', false, true);
		} catch(e) {}
		/* sous-menu n2 */
		$('#menu > li').hover(function(){
			$(this).toggleClass("shover");
		}, function(){
			$(this).removeClass('shover');
		});	
		/* sous-menu n3 */
		$('#menu li dl dd').hover( function(){
			$('div', this).show();
		}, function(){
			$('div', this).hide();
		});
	}	
/*end functions for IE 6*/
	if ($.browser.msie && $('html').width() <= "1100" ) {
		$('#content').css({'margin':'0 5px'});
	}
/* jquery arguments */
	$("a[rel*='external']").click(function(){
		this.target = "_blank";
	});
	
	//labelInField
	$('.search input[type="text"]').labelValue();
	
	$('.triggerPopin').click(function() {
		var popinToShow = $(this).attr('href');			
		$.fn.colorbox({opacity:"0.7" , close:"FERMER" ,width:"500px" , scrolling:false , scalePhotos:false ,inline:true, href:popinToShow});
	});
	
	if( $('#rotator').length){
		theRotator();
	}
});

/* ==================================================================*/
/*							 FONCTIONS								 */
/* ==================================================================*/
/** labelInField function **/
$.fn.labelValue = function() {
	return this.focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});
};
function theRotator() {
	//Set the opacity of all images to 0
	$('div#rotator ul li').css({opacity: 0.0});
	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul li:first').css({opacity: 1.0});
	//Call the rotator function to run the slideshow, 3000 = change to next image after 6 seconds
	setInterval('rotate()',3000);
}

function rotate() {	
	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));
	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);
	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
};

// *** Fonctions SHU *** //

String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g,"");
}


function getFileExtension(filename) 
{
	return (/[.]/.exec(filename)) ? /[^.]+$/.exec(filename) : undefined;
}


function testObl(fieldValue,fieldName)
{	
	if(fieldValue.trim()=="")
	{	
		inlineMsg(fieldName,get_trad_champ(fieldName),2);
	    return false;
	}
	else
	{		
		return true;
	}
}

function testMatchRegex(fieldValue,fieldName,myRegex)
{	
	if(!fieldValue.match(myRegex)) 
  	{
	    inlineMsg(fieldName,get_trad_champ(fieldName+"_incorrect"),2);
	    return false;
  	}  
  	else
  	{
  		return true;
  	}
}

function testLength(fieldValue,fieldName,fieldLength)
{
	if( parseInt(fieldValue.trim().length) < fieldLength)
	{			
	  	inlineMsg(fieldName,get_trad_champ(fieldLength+"_caracteres_min"),2);
	  	return false;
	}
	else
	{
		return true;
	}
}

function validFormContact(form)
{
	

  var libelleRegex = /.{2,}/;
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,10}$/;
  var extensionRegex =  /^(doc|docx|pdf)$/i;
   
  // ==========================  
  // Déclaration des variables
  // ==========================
  var poste_recherche = form.poste_recherche.value;
  var nom = form.nom.value;
  var prenom = form.prenom.value;
  var email = form.email.value;
  var experience = form.experience.value;
  var cv = form.cv.value;
  var motivation = form.motivation.value;
  var extensionCv = getFileExtension(cv);
  var extensionLm = getFileExtension(motivation);
 
  //==========================  
  //controles JS
  //==========================      
  
  
  // Poste Recherché
  if(!testObl(poste_recherche,"poste_recherche")) return false;
  if(!testMatchRegex(poste_recherche,"poste_recherche",libelleRegex)) return false;  

  // Nom
  if(!testObl(nom,"nom")) return false;
  if(!testMatchRegex(nom,"nom",libelleRegex)) return false;  
  
  // Prénom
  if(!testObl(prenom,"prenom")) return false;
  if(!testMatchRegex(prenom,"prenom",libelleRegex)) return false;
  
  // Email
  if(!testMatchRegex(email,"email",emailRegex)) return false;
  
  // Expérience
  if(!testObl(experience,"experience")) return false;
  
  // CV
  if(!testObl(cv,"cv",false)) return false;
  if(!extensionRegex.test(extensionCv)) 
  {
  	inlineMsg('cv',get_trad_champ("extension_cv"),2);  
  	return false;
  }
  
  // Lettre de motivation
   /*if(!testObl(motivation,"motivation",false)) return false;
   if(!extensionRegex.test(extensionLm)) 
   {
  	inlineMsg('motivation',get_trad_champ("extension_lm"),2);  
  	return false;
   }*/
   
  return true;
}

function submitFormContact()
{		
	
	if(validFormContact(document.getElementById('myContactForm'))) 
	{			
		document.getElementById('myContactForm').submit();
	}
	else
	{			
		return false;
	}
}


function submitFormRequest(){
	if(validFormRequest(document.getElementById('myRequestForm'))) 
	{			
		document.getElementById('myRequestForm').submit();
	}
	else
	{			
		return false;
	}
}

function validFormRequest(form)
{
	

  var libelleRegex = /.{2,}/;//3
  var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,10}$/;
   
  // ==========================  
  // Déclaration des variables
  // ==========================
  
  var nom = form.nom.value;
  var prenom = form.prenom.value;
  var email = form.email.value;
  var message = form.message.value;
  
 
  //==========================  
  //controles JS
  //==========================      
    
  // Nom
  if(!testObl(nom,"nom")) return false;
  if(!testMatchRegex(nom,"nom",libelleRegex)) return false;  
  
  // Prénom
  if(!testObl(prenom,"prenom")) return false;
  if(!testMatchRegex(prenom,"prenom",libelleRegex)) return false;
  
  // Email
  if(!testMatchRegex(email,"email",emailRegex)) return false;
  
  // Message
  if(!testObl(message,"message")) return false;
  if(!testMatchRegex(message,"message",libelleRegex)) return false;
  
  
   
  return true; 
}



