$(document).ready(function(){
	
	//Target blank
	$("a[class^='_blank']").click(function(){
		window.open(this.href, '_blank');
		return false;
    });
	
	//Menu déroulant (désactivé sous IE6)
	if( jQuery.browser.version != '6.0' ){
    	$('#menu_niveau_un').droppy({speed: 100});
	};
	
	//Formulaire newsletter
	$('#bt_valid_newsletter').click(function(){
		
		var nom 	= $('#nom').val();
	    var prenom 	= $('#prenom').val();
	    var email 	= $('#email').val();	    
	    var choix 	= $('input[type=radio][name=choix_news]:checked').attr('value');
	    
	    $.post('/cgi/inscription-newsletter.php', {'email': email, 'choix': choix, 'nom': nom, 'prenom': prenom}, function(data){
	    	$('#message_validation').css('opacity', 1);
	    	$("#message_validation").css("display","block");
	    	$('#message_validation').empty();
	    	$('#message_validation').append(data);
	    	setTimeout(function(){
				$('#message_validation').animate({opacity: 0}, 500);
				$("#message_validation").css("display","none");
    		}, 5000);
	    });
    });
	
	//Menu image rollover
	$("ul#menu_niveau_deux li").hover(function(){
		var fade = $('> a.rollover img.survol', this);
		if (!fade.hasClass('animated')) {
			fade.dequeue().stop().fadeIn(250);
		}
	}, function() {
		var fade = $('> a.rollover img.survol', this);
		fade.addClass('animated').fadeOut(250, function() {
			fade.removeClass('animated');
		});
	});
	/*
	//Effet de zoom sur les blocs du catalogue (+20px)
	if( $('div.zoomBox').size() ){
		
		$('div.zoomBox').hover(function(){
			$(this).stop().animate({
				top: '-10px',
				left: '-10px',
				width: '147px',
				height: '165px',
				fontSize: '12px'
			}).css('z-index', 50).css('background', "url('/image/interface/catalogue/bg-box_shadow_large.png') no-repeat center bottom");
			$('div.picture', this).stop().animate({
				width: '147px',
				height: '118px'
			}).css('z-index', 50).css('background', "url('/image/interface/catalogue/bg-box_shadow_large.png') no-repeat center bottom");
			$('img.zoom', this).stop().animate({
				width: '139px',
				height: '110px'
			});
			$('p.description', this).stop().animate({
				marginTop: '118px',
				paddingLeft: '6px'
			});
		}, function(){
			$(this).stop().animate({
				top: '0px',
				left: '0px',
				width: '127px',
				height: '135px',
				fontSize: '11px'
			}).css('z-index', 0).css('background', "url('/image/interface/catalogue/bg-box_shadow_normal.png') no-repeat center bottom");
			$('div.picture', this).stop().animate({
				width: '127px',
				height: '98px'
			}).css('z-index', 0).css('background', "url('/image/interface/catalogue/bg-box_shadow_normal.png') no-repeat center bottom");
			$('img.zoom', this).stop().animate({
				width: '119px',
				height: '90px'
			});
			$('p.description', this).stop().animate({
				marginTop: '98px',
				paddingLeft: '4px'
			});
		});
	}
	*/
	//Recherche dynamique
	if( $('form#recherche').size() ){
		
		$('select').change(function(){
			parametre = getParametre('recherche');
			rechercheAjax(parametre);
		});
		
		$('input:text').keyup(function(){
			parametre = getParametre('recherche');
			rechercheAjax(parametre);
		});
	}
	
	//Slider
	if( $("#slider-size").size() && $("#slider-price").size() ){
		
		$("#slider-size").slider({
			range: true,
			min: 5,
			max: 34,
			values: [5, 34],
			slide: function(event, ui) {
				$("#size-min").val(ui.values[0]);
				$("#size-max").val(ui.values[1]);
				parametre = getParametre('recherche');
				rechercheAjax(parametre);
			}
		});

		$("#slider-price").slider({
			range: true,
			min: 1000,
			max: 300000,
			values: [1000, 300000],
			slide: function(event, ui) {
				$("#price-min").val(ui.values[0]);
				$("#price-max").val(ui.values[1]);
				parametre = getParametre('recherche');
				rechercheAjax(parametre);
			}
		});
	}
	
	//Slideshow en accueil
	if( $('.slideshow').size() ){
		
		$('.slideshow').tabSwitch('create',{
			type: 'toggle',
			toggle: 'fade',
			width: 700,
			height: 318
		});
		
		//Initialise la pagination
		$('a#select-0').addClass('activ');
		
		//Mode auto
		$('.slideshow').tabSwitch('startAuto',{
			interval: 4000, 
			speed: 2000
		}, function(){
			var index = $('.slideshow').tabSwitch('index');
			$('a.tabSelect').removeClass('activ');
			$('a#select-'+index).addClass('activ');
		});
		
		//Fleches suivante et précédente
		$('.nav-slideshow').click(function(e){
			$('.slideshow').tabSwitch('moveStep',{step: parseInt($(this).attr("rel"))});
			$('a.tabSelect').removeClass('activ');
			$('a#select-'+parseInt($(this).attr("rel"))).addClass('activ');
			e.preventDefault();
		});
		
		//Pagination type bouton image
		$('.tabSelect').click(function(e){
			$('.slideshow').tabSwitch('moveTo',{index: parseInt($(this).attr("rel"))});
			$('a.tabSelect').removeClass('activ');
			$('a#select-'+parseInt($(this).attr("rel"))).addClass('activ');
			e.preventDefault();
		});
		
		//Hover sur le slideshow, fait apparaitre la légende
		$('.slideshow').hover(function(){
			$('p.legend').show();
		},function(){
			$('p.legend').hide();
		});
	}
});

function rechercheAjax(parametre){
	
	$.ajax({
		url: "/cgi/recherche-ajax.php",
		global: false,
		type: 'POST',
		data: parametre,
		success: function(data){
			$('span#nb-result').html(data);
		}
	});
}
