$(document).ready(function(){
	//on remplace dynamiquement les elements de titre par un element à coins arrondis,
	genereTitresArrondis("cartoucheMarron");
	genereTitresArrondis("cartoucheVerte");
	genereTitresArrondis("cartoucheRose");
	
	listenSelected("selectDepartement");
	
	//home gallery slider
	slideShowPic();
	//slideShowEncart();
	//alert($('#slides'));
	//console.log($('#slides'));
	if($('#slides').length != 0)
	{
		$('#slides').slides({
			preload: true,
			preloadImage: 'img/loading.gif',
			play: 0,
			pause: 2500,
			hoverPause: true
		});
	}
	//else
	//	console.log('ok');
});


function genereTitresArrondis(cible){
	$("."+cible).each(function(){
		$(this).replaceWith('<p class="'+ cible +'"><span class="left">' + $(this).html() + '</span><span class="right">&nbsp;</span></p><div class="clear">&nbsp;</div>');
	});
}

function listenSelected(cible){
	$("."+cible).change( function(){
		//alert('change');
		var currentLocation =  document.location.href;
		currentLocation = currentLocation.substring( 0 ,currentLocation.lastIndexOf( "/" ) );
		if($(".type").attr("value")=="salons"){
			var redirection = "salons";
		}
		else{
			var redirection = "points-de-vente";
		}
		document.location.href=currentLocation+"/"+redirection+"-"+$(this).val()+"-"+$(".page").attr("value")+"-carte.html" 
	});
}
	
function slideShowPic(){
    //Set the opacity of all images to 0
    $('#gallery span').css({opacity: 0.0});
     
    //Get the first image and display it (set it to full opacity)
    $('#gallery span:first').css({opacity: 1.0});
     
    //Set the caption background to semi-transparent
    $('#gallery .caption').css({opacity: 0.7});
 
    //Call the gallery function to run the slideshow, 6000 = change to next image after 6 seconds
    setInterval('gallery()',3000);
}
 
function gallery(){
    //if no IMGs have the show class, grab the first image
    var current = ($('#gallery span.show')?  $('#gallery span.show') : $('#gallery span:first'));
 
    //Get next image, if it reached the end of the slideshow, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery span:first') :current.next()) : $('#gallery span:first'));  
     
    //Get next image caption
    var caption = next.find('img').attr('rel');
     
    //Set the fade in effect for the next image, show class has higher z-index
    next.css({opacity: 0.0})
    .addClass('show')
    .animate({
		opacity: 1.0,
    	width: '100%'
	}, 1000);
 
    //Hide the current image
    current.animate({opacity: 0.0}, 1000)
    .removeClass('show');
}

function slideShowEncart()
{
	$("#tabs").tabs({fx:{opacity: "toggle"}}).tabs("rotate", 2000, true);
	
	$('.onglet1').removeClass('onglet1-selected').addClass('onglet1-selected');
	$('#tabs').bind('tabsselect', function(event, ui){
		var elem = ui.panel;
		var onglet = elem.id.split('-');
		
		$('.onglet1').removeClass('onglet1-selected');
		$('.onglet2').removeClass('onglet2-selected');
		$('.onglet3').removeClass('onglet3-selected');
		
		$('.onglet'+onglet[1]).removeClass('onglet'+onglet[1]+'-selected').addClass('onglet'+onglet[1]+'-selected');
		
	});
}
