function getFullProduct(id_menu,sens){
	if(sens > 0){
		current_full_id_p_pos++;
	}
	else{
		current_full_id_p_pos--;
	}
	if(current_full_id_p_pos >= ids_produits.length){
		current_full_id_p_pos = 0;
	}
	if(current_full_id_p_pos < 0){
		current_full_id_p_pos = ids_produits.length - 1;
	}
	var id_p = ids_produits[current_full_id_p_pos];
	
	jQuery.post("./index.php?action=getFullProduct&id_menu="+id_menu,"id_produit="+id_p,
				function (data,status){
					$("#fullProduct").html(data);
						prettyPhoto.init();
						refresh_mini();
						/*
					$("#fullProduct").fadeOut("normal",function (){
						
						$(this).fadeIn("normal");
					});*/
				}
			);
}

function selectProduct(id_menu,position){
	current_full_id_p_pos = position;
	var id_p = ids_produits[current_full_id_p_pos];

	jQuery.post("./index.php?action=getFullProduct&id_menu="+id_menu,"id_produit="+id_p,
				function (data,status){
					$("#fullProduct").html(data);
					prettyPhoto.init();
					refresh_mini();
						/*
					$("#fullProduct").fadeOut("normal",function (){
						
						$(this).fadeIn("normal");
					});*/
				}
			);
}


function refresh_mini(){
	var position = current_full_id_p_pos;
	$('span.res_mini').each(function (){
		if($('#mini').html().indexOf($(this).html()) == -1){
			$('#mini').html($('#mini').html()+$(this).html());
		}
	});
	$('#mini > a.mini_'+position).remove();
}


function getMinProduct(id_menu,sens){
	if(sens > 0){
		current_full_id_p_pos++;
	}
	else{
		current_full_id_p_pos--;
	}
	if(current_full_id_p_pos >= ids_produits.length){
		current_full_id_p_pos = 0;
	}
	if(current_full_id_p_pos < 0){
		current_full_id_p_pos = ids_produits.length - 1;
	}
	var id_p = ids_produits[current_full_id_p_pos];
	
	jQuery.post("./index.php?action=getMinProduct&id_menu="+id_menu,"ids_produit="+ids_p,
				function (data,status){
					$("#fullProduct").html(data);
					/*
					$("#fullProduct").fadeOut("normal",function (){
						
						$(this).fadeIn("normal");
					});*/
				}
			);
}



var old_pos = 0;
var new_pos = 0;


function getNextImg(sens){
	old_pos = new_pos;
	new_pos += sens;
	if(new_pos > 5){
		new_pos = 0;
	}
	if(new_pos < 0){
		new_pos = 5;
	}
	$('#img'+old_pos).fadeOut("normal",function (){
		$('#atheme_'+old_pos).attr('class','');
		$('#img'+new_pos).fadeIn("normal");
		$('#atheme_'+new_pos).attr('class','select');
	});
}

function setTheme(pos){
	if(document.location.href.indexOf('index.php?id_menu=116&id_gabarit=19') > -1){
		var g = 0;
		if(new_pos == 0){
			g = pos;
		}
		else{
			g = pos - new_pos;
		}
		getNextImg(g);
	}
	else{
		document.location.href = './index.php?id_menu=116&id_gabarit=19&t='+pos;
	}
}

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)


function chg_img(c,i){
	$('img.'+c).fadeOut(function (){
		$(this).attr('src',i);
		$(this).fadeIn(400);
	});
}


function js_contenu(){
	if(document.getElementById('contenu')){
		document.getElementById('nav_secondaire').style.paddingBottom = '366px';
	
		if(document.getElementById('nav_secondaire').offsetHeight > document.getElementById('contenu').offsetHeight)
		{
			var value = (parseInt(document.getElementById('nav_secondaire').offsetHeight) - parseInt(document.getElementById('nav_secondaire').style.paddingBottom));
		 /*document.getElementById('contenu').style.height = document.getElementById('nav_secondaire').offsetHeight +'px';*/
		 document.getElementById('nav_secondaire').style.height = value +'px';
		}
		else
		{
			var value = (parseInt(document.getElementById('contenu').offsetHeight) - parseInt(document.getElementById('nav_secondaire').style.paddingBottom));
			 /* document.getElementById('contenu').style.height =  document.getElementById('contenu').offsetHeight +'px'; */
		 document.getElementById('nav_secondaire').style.height = value +'px';
		}
	}
	else{
		document.getElementById('nav_secondaire').style.paddingBottom = '366px';
		if(document.getElementById('contenu_editoriaux')) 
		{
			document.getElementById('contenu_editoriaux').style.paddingBottom = '30px';

			if(document.getElementById('nav_secondaire').offsetHeight > document.getElementById('contenu_editoriaux').offsetHeight)
			{
				var value = (parseInt(document.getElementById('nav_secondaire').offsetHeight) - parseInt(document.getElementById('nav_secondaire').style.paddingBottom));
			 document.getElementById('contenu_editoriaux').style.height = document.getElementById('nav_secondaire').offsetHeight +'px';
			 document.getElementById('nav_secondaire').style.height = value +'px';
			}
			else
			{
				var value = (parseInt(document.getElementById('contenu_editoriaux').offsetHeight) - parseInt(document.getElementById('nav_secondaire').style.paddingBottom) + parseInt(document.getElementById('contenu_editoriaux').style.paddingBottom));
				 document.getElementById('contenu_editoriaux').style.height =  document.getElementById('contenu_editoriaux').offsetHeight +'px';
			 document.getElementById('nav_secondaire').style.height = value +'px';
			}
		}
	}
}



window.onload = js_contenu;

current_pos_mini = 5;

function next_mini(nb){
	for(var i=0;i<4;i++){
		prev_mini(nb);
	}
}

function prev_mini(nb){
	var html = "";
	current_pos_mini = (current_pos_mini % nb) + 1;
	var n = 0;
	$('span.res_mini').each(function (){
		n++;
		if(n == current_pos_mini){
			html += $(this).html();
		}
	});
	
	
	
	current_pos_mini = (current_pos_mini % nb) + 1;
	var n = 0;
	$('span.res_mini').each(function (){
		n++;
		if(n == current_pos_mini){
			html += $(this).html();
		}
	});
	
	
	
	current_pos_mini = (current_pos_mini % nb) + 1;
	var n = 0;
	$('span.res_mini').each(function (){
		n++;
		if(n == current_pos_mini){
			html += $(this).html();
		}
	});
	
	
	
	
	current_pos_mini = (current_pos_mini % nb) + 1;
	var n = 0;
	$('span.res_mini').each(function (){
		n++;
		if(n == current_pos_mini){
			html += $(this).html();
		}
	});
	
	$('#mini').html(html);
}

function popup(page,largeur,hauteur){
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	window.open(page,"pop","top="+top+",left="+left+",width="+largeur+",height="+hauteur+",menubar=no,scrollbars=yes,statusbar=no");
}


function zoom(img,x,y){
	popup('./resize.php?type=html&f='+img+'&x='+x+'&y='+y,x,y);
}


function loader(){
	$('a').each(function (){
		if($(this).attr('href').indexOf('carte_xl.gif') > -1){
			$(this).bind('click',function(){
				popup('./resize.php?f=./images/page_contenu/carte_xl.gif&type=html',570,570);
				return false;
			});
		}
	});
}

function c_email(email){
	var verif = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{1,}[.][a-zA-Z]{2,5}$/
	if (verif.exec(email) == null){
		return false;
	}
	else{
		return true;
	}
}

function sendNews(){
	var prenom = $('#prenom').val();
	var nom = $('#nom').val();
	var email = $('#email').val();
	var email2 = $('#email2').val();
	var bd = $('#birthdate').val();
	var fidel = $('#fidel').val();
	
	var mobile = $('#mobile').val();
	var adresse = $('#adresse').val();
	var cp = $('#cp').val();
	var ville = $('#ville').val();
	var pays = $('#pays :selected').text();
	
	if(prenom == ''){
		$('#err_p').show();
		return false;
	}
	else{
		$('#err_p').hide();
	}
	if(nom == ''){
		$('#err_n').show();
		return false;
	}
	else{
		$('#err_n').hide();
	}
	if(email == ''){
		$('#err_e').show();
		return false;
	}
	else{
		$('#err_e').hide();
	}
	if(!c_email(email)){
		$('#err_eb').show();
		return false;
	}
	else{
		$('#err_eb').hide();
	}
	if(email != email2){
		$('#err_e2').show();
		return false;
	}
	else{
		$('#err_e2').hide();
	}
	if(!$('#cb').attr('checked')){
		alert("Veuillez cocher la dernière case afin de valider votre inscription.");
		return false;
	}
	var vars = "prenom="+prenom+"&nom="+nom+"&email="+email+"&bd="+bd+"&fidel="+fidel+"&mobile="+mobile+"&adresse="+adresse+"&cp="+cp+"&ville="+ville+"&pays="+pays;
	jQuery.post('./ajax.php?action=sendNews',vars,function (data,status){
		if(data == 'ok'){
			$("#conf").html("Votre inscription a bien été prise en compte");
			$('input').each(function (){
				$(this).attr('value','');
			});
		}
		else{
			$("#conf").html("Cette adresse email a déjà  été enregistrée !");
		}
	});
}





function err(id){
	if($('#'+id).val() == ''){
		$('#err_'+id).show();
		return false;
	}
	else{
		$('#err_'+id).hide();
		return true;
	}
}


function checkRecrutement(){
	var poste = $('#poste').val();
	var nom = $('#nom').val();
	var prenom = $('#prenom').val();
	var adresse = $('#adresse').val();
	var ville = $('#ville').val();
	var email = $('#email').val();
	var fixe = $('#fixe').val();
	var portable = $('#portable').val();
	var cv = $('#cv').val();
	if(!err('nom')){
		return false;
	}
	if(!err('prenom')){
		return false;
	}
	if(!err('adresse')){
		return false;
	}
	if(!err('ville')){
		return false;
	}
	if(!err('email')){
		return false;
	}
	if(!err('cv')){
		return false;
	}
	if(!c_email(email)){
		$('#err_email2').show();
		return false;
	}
	else{
		$('#err_email2').hide();
	}
	var vars = 'poste='+poste;
	vars += '&nom='+nom;
	vars += '&prenom='+prenom;
	vars += '&adresse='+adresse;
	vars += '&ville='+ville;
	vars += '&email='+email;
	vars += '&fixe='+fixe;
	vars += '&portable='+portable;
	return vars;
}


function sendRecrutement(){
	var vars = checkRecrutement();
	if(!vars){
		return false;
	}
		
	$("#loading")
      .ajaxStart(function(){
          $(this).show();
      });
     
	/*
  prepareing ajax file upload
  url: the url of script file handling the uploaded files
              fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
  dataType: it support json, xml
  secureuri:use secure protocol
  success: call back function when the ajax complete
  error: callback function when the ajax failed
 
	*/
  $.ajaxFileUpload({
    url:'./doajaxfileupload.php',
    secureuri:false,
    fileElementId:'cv',
    dataType: 'json',
    success: function (data, status){
      if(typeof(data.error) != 'undefined'){
        if(data.error != ''){
        	alert(data.error);
        }
        else{
        	var cv = data.msg;
        	if($('#lettre').val() != ''){
        		$.ajaxFileUpload({
					    url:'./doajaxfileupload.php',
					    secureuri:false,
					    fileElementId:'lettre',
					    dataType: 'json',
					    success: function (data, status){
					      if(typeof(data.error) != 'undefined'){
					        if(data.error != ''){
					        	alert(data.error);
					        }
					        else{
					        	execSendRecrutement(vars,cv,data.msg);
					        }
					      }
							},
							error: function (data, status, e){
					    	alert(e);
					  	}
						});
        	}
        	else{
        		execSendRecrutement(vars,cv);
        	}
        }
      }
		},
		error: function (data, status, e){
    	alert(e);
  	}
	});
  return false;
}
	
function execSendRecrutement(vars,cv,lettre){
	vars += "&cv="+cv+"&lettre="+lettre;
	jQuery.post(
							'./ajax.php?action=sendRecrutement'
							,vars
							,function (data,status){
								$("#loading").hide();
								$('input').each(function (){
									$(this).attr('value','');
								});
								$('#confirm').html("Candidature envoyée");
							});
}









