$(document).ready(function() { // => Qd la page est charg�e
	// J'execute :

      
     $("#searchtext").click(function(){
		$("#Lsearchtext").text('');
    });
		
		
    $('.menu .mainMenu li').each(function() { // Comportement des menus d�roulants
		$(this).hover( // Si hover
		  function () {
			$(this).addClass('hover'); // Ajout de la classe 'hover' sur le li
		  },
		  function () { // Si relach�
			$(this).removeClass('hover'); // On enl�ve la class 'hover' de la li
		  }
		);
	});
  
  $('.galerie .galerie_image a, a.lightbox').lightBox();
	$('.validate_me').validate(); // Comportement de validation des formulaires
	$('.facebox_me').facebox({opacity: 0.5});
	$('.print_me').each(function() {window.print();});
	
	$('.empty_me').each(function() {
		$(this).prev().addClass("overlay").show();
		$(this).prev().click(function(){
			$(this).next().focus();
		});
		$(this).focus(function(){
			$(this).prev().fadeTo(200,0.5);
		}).keyup(function(){
		if($(this).val()==""){
			$(this).prev().show();
		}
		else
		{
			$(this).prev().hide();
		}
		}).blur(function(){
		if($(this).val()==""){
			$(this).prev().fadeTo(100,1);
		}
		});
    if($(this).val()!=""){
			$(this).prev().hide();
		}
	});

      
 $('#nav').append('<ul />');
	$('#slideHome').cycle({
		pager: '#nav ul',
                prev: '.prev',
		next: '.next',
                speed: 1000,
                timeout: 7000,
                fx: 'scrollHorz',
                pause: 1,
		pagerAnchorBuilder: function(idx, element) {
                   return '<li><span class="slideNav'+ (idx + 1)+'"><a class="" href="#">'+(idx+1)+'</a></span></li>';
                }
	});
	$('#playTool .play').addClass('playActive');
	
	$('#playTool .pause').click(function() { 
	    $('#slideHome').cycle('pause');
	    $('#playTool .pause').addClass('pauseActive');
	    $('#playTool .play').removeClass('playActive');	    
	});
	$('#playTool .play').click(function() { 
	    $('#slideHome').cycle('resume', true); 
	    $('#playTool .play').addClass('playActive');
	    $('#playTool .pause').removeClass('pauseActive');
	});
	
	
    $('#slideDetail').after('<ul id="navDetail">').cycle({
        pager: '#navDetail',
        timeout: 0,
        pagerAnchorBuilder: function(idx, slide) {
            return '<li><a href="#"><img src="' + slide.src + '" /></a></li>';
        }
    });

    /* HOVER SUBMIT */
    $('.contact .submit').hover(
    	function() {
        $(this).addClass('over');
    		//$(this).attr('src','../images/submitHover.jpg');
    	},
    	function() {
        $(this).removeClass('over');
    		//$(this).attr('src','../images/submit.jpg');
    	}
    );
    
	/* HOVER DE BALISES AUTRES QUE <a> QUI NE PASSENT PAS SOUS IE */
	if(navigator.appName == 'Microsoft Internet Explorer'){
		$('.headerHome .next').hover(
		  function () {
			$(this).addClass('nextHover'); 
		  },
		  function () { 
			$(this).removeClass('nextHover');
		  }
		);
		$('.headerHome .prev').hover(
		  function () {
			$(this).addClass('prevHover'); 
		  },
		  function () { 
			$(this).removeClass('prevHover');
		  }
		);
		$('.wrapper .descNoBg li').hover(
		  function () {
			$(this).addClass('hover'); 
		  },
		  function () { 
			$(this).removeClass('hover');
		  }
		);
		$('.centerCol .activites .elements .element .wrapper .desc').hover(
		  function () {
			$(this).addClass('descHover'); 
		  },
		  function () { 
			$(this).removeClass('descHover');
		  }
		);
		$('#footer .footerSep .liMain .liste-footer').hover(
		  function () {
			$(this).addClass('liste-footerHover'); 
		  },
		  function () { 
			$(this).removeClass('liste-footerHover');
		  }
		);
		$('#playTool .pause').hover(
		  function () {
			$(this).addClass('pauseHover'); 
		  },
		  function () { 
			$(this).removeClass('pauseHover');
		  }
		);
		$('#playTool .play').hover(
		  function () {
			$(this).addClass('playHover'); 
		  },
		  function () { 
			$(this).removeClass('playHover');
		  }
		);
	}
	
	initFormulaireRecrutement();
	initListeNews();
	initListeOffres();
});

function displayMenuTransport(name){
	$(".tab").hide();
	$(".link").removeClass('active');
	$("#btn-"+name).addClass('active'); 
	$("#tab-"+name).show();	
}
	
function initFormulaireRecrutement(){

        
		
	$("#formulaire_recrutement").submit(function(){
		var nom = $("#nom").val();
		var prenom = $("#prenom").val();
		var message = $("#message").val();
		var email = $("#email").val();
		var cv = $("#cv").val();
		var upload_message = $("#upload_message").html();
		
		var validation = $("#validation").val();
		var contactId = $("#contactId").val();
		var offreId = $("#offreId").val();

		$.ez( 'contact::submit_recrutement', {
		nom : nom,
		prenom: prenom,
		message : message,
		email : email,
		contactId : contactId,
		upload_message : upload_message,
		cv : cv,
		offreId : offreId,
		validation: validation}, function( data )
		{ 
			if ( data.error_text )
				$( '#formulaire_offre' ).html( data.error_text );
			else
				$( '#formulaire_offre' ).html( data.content.design );
				
			initFormulaireRecrutement();
		});

	}); 
}

function goToByScroll(id){
	if(id != undefined)
		$('html,body').animate({scrollTop: $("#"+id).offset().top-50},'slow');
}

function initListeOffres(){
	$('.boutton_offre').each(function(){
		$(this).bind('click', function(){
			var params = $(this).attr('href');
			params = params.split(':');
			categorie_id = params[0];
			page = params[1];
			
			var height_content = ($('#liste_offre_content').height())/2;
			var loadingMsg = "<div style='height:"+height_content+"px; padding-top:"+height_content+"px;' class='produitSearch text_align_center'><img src='/extension/ezbrangeon/design/ezwebin/images/loader.gif' /></div>";
			$( '#liste_offre_content' ).html( loadingMsg );
				
			$.ez( 'offres::get_list', {categorie_id: categorie_id, page:page }, function( data )
			{ 
				if ( data.error_text )
				{
					$( '#liste_offre_content' ).html( data.error_text );
				}
				else
				{
					$( '#liste_offre_content' ).html( data.content.design );
				}
				initListeOffres();
			});
			return false;
		});
	});
}

function initListeNews(){
	$('.boutton_news_actu').each(function(){
		$(this).bind('click', function(){
			var params = $(this).attr('href');
			params = params.split(':');
			categorie_id = params[0];
			page = params[1];
			
			var height_content = ($('#center_column_white_content').height())/2;
			var loadingMsg = "<div style='height:"+height_content+"px; padding-top:"+height_content+"px;' class='produitSearch text_align_center'><img src='/extension/ezbrangeon/design/ezwebin/images/loader.gif' /></div>";
			$( '#center_column_white_content' ).html( loadingMsg );
				
			$.ez( 'news::get_list', {categorie_id: categorie_id, page:page }, function( data )
			{ 
				if ( data.error_text )
				{
					$( '#center_column_white_content' ).html( data.error_text );
				}
				else
				{
					$( '#center_column_white_content' ).html( data.content.design );
				}
				initListeNews();
			});
			return false;
		});
	});
}

function ajaxFileUpload()
{
	$("#upload_message")
	.ajaxStart(function(){
		$(this).html('Envoi en cours');
	})

	$.ajaxFileUpload
	(
		{
			url:'/contactmodule/doajaxfileupload',
			secureuri:false,
			fileElementId:'fileToUpload',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						$("#upload_message").html(data.error);
					}else
					{
						$("#upload_message").html('Fichier envoyé : <a href="'+data.fileurl+'">'+data.file+'</a>');
						$("#cv").val(data.fileurl);
					}
				}
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
	)
	
	return false;

}
