/* Author: 
	@Quinze Mille
*/

$(document).ready(function() {
	$('#tabsJobs > li').each(function() {
		if($(this).find('> a').attr('alt') != "") {
			$(this).CreateBubblePopup({
				themeName: 'all-grey',
				themePath: 'js/plugins/jquerybubblepopup-theme/',
				dropShadow: false,
				distance: '20px',
				openingDelay: 25,
				innerHtml: $(this).find('> a').attr('alt').replace(/,\s/g, "<br/>")
			});
			
			$(this).bind('mouseenter', function() {
				$(this).ShowBubblePopup();
			});
		}
		
		$(this).bind('click', function() {
			$('#tabsJobs > li > a').removeClass();
			$(this).find('> a').addClass('current');
				
			if($('#tabsJobs > li > a.current').attr('rel') != "") {
				filtre($('#tabsJobs > li > a.current').attr('rel'));
				return false;
			}
		});
	});
		
	$('#listeOffres > li').each(function() {
		if($(this).attr('id') != 'intitule') {
			$(this).attr("alt", $(this).css('background-color'));
			
			$(this).bind('mouseenter', function() {
				$(this).clearQueue();
				
				$(this).css('background-color', '#7bb42d');
				$(this).find('p, p span').css('color', '#fff');
			});
			
			$(this).bind('mouseleave', function() {
				$(this).clearQueue();
				
				$(this).css('background-color', $(this).attr("alt"));
				$(this).find('p, p span').css('color', '');
			});
		}
	});
	
	$('#listeOffres > li').each(function() {
		$(this).css('position', 'relative');
		$(this).css('height', $(this).height() );
		$(this).css('width', $(this).width() );
		
		$(this).find('> a').css('position', 'absolute');
		$(this).find('> a').css('height', $(this).height() );
		$(this).find('> a').css('width', $(this).width() );
	});
	
	$('#noAnnonces').css('width', '700px' );

	
	////////////////////////////////////////////////////////////////////////////
	// Formulaire de contact

	$('#contactform').submit(function(){
		var action = $(this).attr('action');
		
		$('#contactform #submit').attr('disabled','disabled').after('<img src="images/form/ajax-loader.gif" class="loader" />');
		
		$("#message").slideUp(750,function() {
		$('#message').hide();			
		
		$.post(action, { 
			name: $('#name').val(),
			adress: $('#adress').val(),
			codepostal: $('#codepostal').val(),
			pays: $('#pays').val(),
			email: $('#email').val(),
			comments: $('#comments').val(),
		},
		  function(data){
			  document.getElementById('message').innerHTML = data;
			  $('#message').slideDown('slow');
			  $('#contactform img.loader').fadeOut('fast',function(){$(this).remove()});
			  $('#contactform #submit').attr('disabled',''); 
			  if(data.match('success') != null) $('#contactform').slideUp('slow');
			  
		  }
		);
		
		});
		
		return false; 
	});
});

var filtreActif = false;
function filtre(rel) {
	var delay = 0;
	var pair = 0;
	
	$('#listeOffres > li').each(function(i) {
		if($(this).attr('id') != 'intitule') {
			var color = (pair%2 == 1)?'#eeeeee':'#ffffff';
			
			$(this).attr("alt", color);
			
			if($(this).attr('rel') != rel && rel != 'all') {
				$(this).find('> a').clearQueue().delay(delay).fadeOut(300);
				
				$(this).clearQueue();
				$(this).delay(delay+15).slideUp(300);
				$(this).delay(delay+15).animate({backgroundColor: color}, 300);
			}
			else {
				$(this).find('> a').clearQueue().delay(delay+15).fadeIn(300);
				
				$(this).clearQueue();
				$(this).delay(delay).slideDown(300);
				$(this).delay(delay).animate({backgroundColor: color}, 300);
				
				pair++;
			}
			
			delay += 15;
		}
	});
	
	if(pair <= 0)
		$('#noAnnonces').slideDown(300);
	else
		$('#noAnnonces').slideUp(300);
}
