// page recette 
// clik vote redirection d'url
var currentPage = 0;
var objet_drop = 0;
var id_objet = 0;

$(document).ready(function(){
	
	$("ul.unit-rating li a").click(function(){
		if($(this).attr('href') == 'mon_compte.php') {
			$('#rating_form').submit();
			return false;
		}
	});

	$(".recette-send a").click(function(){
		if($(this).attr('href') == 'mon_compte.php') {
			$('#envoyer_ami').submit();
			return false;
		}
	});
	
	$(".notation a").click(function(){
		if($(this).attr('href') == 'mon_compte.php') {
			$('#rating_form').submit();
			return false;
		}
	});
	
	/*$('#bloc_origine').show();
	$('#bloc_recette').hide();
	$('#bloc_mascarpone').hide();*/
	
	
	/*$(".vrai_tiramisu_wrapper ul.submenu li a").click(function(){
		$(".vrai_tiramisu_wrapper ul.submenu li a").each(function(){
			$(this).parent('li').removeClass('active');
			$('#bloc_'+$(this).parent('li').attr('id')).hide();
		});
		$(this).parent('li').addClass('active');
		$('#bloc_'+$(this).parent('li').attr('id')).fadeIn('slow');
	});*/
	
	$(".wrapper_trucs_astuces ul.submenu li a").click(function(){
		$(".wrapper_trucs_astuces ul.submenu li a").each(function(){
			$(this).parent('li').removeClass('active');
			$('#bloc_'+$(this).parent('li').attr('id')).hide();
		});
		
		$('.wrapper_trucs_astuces').css('height','608px');
		$('.wrapper_trucs_astuces').css('background','url(/assets/images/trucsastuces/wrapper_astuces.jpg) no-repeat top left');
		
		if($(this).parent('li').attr('id') == 'que_faire') {
			$('.wrapper_trucs_astuces').css('background','url(/assets/images/trucsastuces/que_faire.jpg) no-repeat top left');
			$('.wrapper_trucs_astuces').css('height','798px');
		}
		
		$(this).parent('li').addClass('active');
		$('#bloc_'+$(this).parent('li').attr('id')).fadeIn('slow');
	});
	
	
	// gestion des champs de date naissance enfant
	var nb_child = parseInt($('.nbenfant option:selected').val());
	for(var i=1; i<=5; i++)
	{
		if(i > nb_child)
			$('.date-child-'+i).hide();
		else
			$('.date-child-'+i).show();
	}
	
	$('.nbenfant').change(function() {
		nb_child = parseInt($('.nbenfant option:selected').val());
		for(var i=1; i<=5; i++)
		{
			if(i > nb_child)
				$('.date-child-'+i).hide();
			else
				$('.date-child-'+i).show();
		}
	});
	
	//pagination autres recettes
	currentPage = 1;
	objet_drop = $('.autresrecettes');
	id_objet = objet_drop.attr('id');
	
	
	$.ajax({
		type: 'POST',
		url: '/recettes_autres.php',
		data: 'id='+id_objet+'&page='+currentPage,
		success: function(msg){
			$('.autresrecettes').html(msg);
			initBinds();
		}
	});
	
	
	// 2011 Nov
	verticalAlign('.container .header .menu ul li a');
	$('#slides').slides({
		preload: true,
		preloadImage: '/decoupe/assets/images/loading.gif',
		play: 5000,
		pause: 2500,
		hoverPause: true,
		effect: 'fade'
	});
	vote();
});


function initBinds()
{
	
	$("a.page").bind("click", function() {
		currentPage = this.id;
				
		$.ajax({
			type: 'POST',
			url: '/recettes_autres.php',
			data: 'id='+id_objet+'&page='+currentPage,
			success: function(msg){
				$('.autresrecettes').html(msg);
				initBinds();
			}
		});
	});	
	
	$( "a.previousPage" ).bind( 'click', 
	function() 
	{
		currentPage --;

		if( currentPage < 1 ) currentPage = 1;

		$.ajax( {
			type: 'POST',
			url: '/recettes_autres.php',
			data: 'id='+id_objet+'&page='+currentPage,
			success: 
			function( msg ){ 
				$( '.autresrecettes' ).html( msg ); 
				initBinds();
			}
		
		} );
	
	} );
	
	$( "a.nextPage" ).bind( 'click', 
	function() 
	{
		var totalpages = this.id;

		currentPage ++;

		if( currentPage > totalpages ) currentPage = totalpages;

		$.ajax( {
			type: 'POST',
			url: '/recettes_autres.php',
			data: 'id='+id_objet+'&page='+currentPage,
			success: 
			function( msg ){ 
				$( '.autresrecettes' ).html( msg );
				initBinds();
			}
		
		} );
	
	} );
	
	
}

function vote()
{
	// On ajoute la classe 'js' à la liste pour mettre en place par la suite du code CSS uniquement dans le cas où le Javascript est activé
	$('.recette-vote ul').addClass('js');
	
	$('.recette-vote ul li').addClass('note-off');
	
	$('.recette-vote ul li').mouseover(function(){
		$(this).nextAll('li').addClass('note-off');
		$(this).prevAll('li').removeClass('note-off');
		$(this).removeClass('note-off');
	});
	
	$('.recette-vote ul').mouseout(function(){
		$(this).children('li').addClass('note-off');
		$(this).find('li input:checked').parent('li').trigger('mouseover');
	});
	
	// gestion du vote au clavier
	$('.recette-vote ul input').focus(function(){
		$(this).parent('li').nextAll('li').addClass('note-off');
		$(this).parent('li').prevAll('li').removeClass('note-off');
		$(this).parent('li').removeClass('note-off');
	})
	
	$('.recette-vote ul input').blur(function(){
		if($(this).parents('.recette-vote ul').find('li input:checked').length == 0){
			$(this).parents('.recette-vote ul').find('li').addClass('note-off');
		}
	});
	
	$('.recette-vote ul input').focus(function(){
		$(this).parents('.recette-vote ul').find('li').removeClass('note-focus');
		$(this).parent('li').addClass('note-focus');
	})
	
	$('.recette-vote ul input').blur(function(){
		$(this).parents('.recette-vote ul').find('li').removeClass('note-focus');
	})
	
	$('.recette-vote ul input').click(function(){
		$(this).parents('.recette-vote ul').find('li').removeClass('note-checked');
		$(this).parent('li').addClass('note-checked');
	});
	
	$('.recette-vote ul input').focus(function(){
		$(this).parents('.recette-vote ul').find('li').removeClass('note-focus');
		$(this).parent('li').addClass('note-focus');
	})
	
	$('.recette-vote ul input').blur(function(){
		$(this).parents('.recette-vote ul').find('li').removeClass('note-focus');
	})
	
	$('.recette-vote ul input').click(function(){
		$(this).parents('.recette-vote ul').find('li').removeClass('note-checked');
		$(this).parent('li').addClass('note-checked');
	});
	
	$('.recette-vote ul input:checked').parent('li').trigger('mouseover');
	$('.recette-vote ul input:checked').trigger('click');
}

//aligne verticalement un élément dans son bloc
function verticalAlign(cible)
{
	$(cible).each(function(){
		var elem = $(this);
		var container = elem.parent();
		
		var elemHeight = elem.height();
		var containerHeight = container.height();
		
		if (elemHeight == 0)
			return;
			
		var newPaddingTop = (Math.floor((containerHeight - elemHeight) / 2));
		var newHeight = (Math.floor(containerHeight - newPaddingTop));
				
		if (newPaddingTop > 0)
		{
			container.css('padding-top', newPaddingTop);
			container.css('height', newHeight);
		}
	});
}

//sauvegarde la valeur par défaut et vide lors du clic
function listenSearchInput(cible)
{
	$(".form-recherche").reset();
	
	$(cible).each(function(){
		var elem = $(this);
		var lockV = elem.val();
		
		elem.bind('click', function() {
			var classe = elem.attr('class');
			
			if(elem.val() == lockV)
			{
				elem.val('');
				if(classe == 'input-large')
				{
					elem.css('background-image', 'url(/assets/images/new/home/bg-input-text-empty.png)');
					
				}
			}
		});	
		
		elem.bind('blur', function() {
			var classe = elem.attr('class');
			if(elem.val() == '')
			{
				elem.val(lockV);
				if(classe == 'input-large')
					elem.css('background-image', 'url(/assets/images/new/home/bg-input-text.png)');
			}
		});
	});
}

jQuery.fn.reset = function(fn)
{
	return fn ? this.bind('reset', fn) : this.trigger('reset');
};

























