/* Ce fichier permet de lancer les fonctions javascript sur les différentes pages où l'on en a besoin
 * Création : Damien Petitjean
 * Date : 21/09/2011
 */

var slideAuto = true;
var eltsRecherche = '';

isActive = new RegExp("active$");

$(document).ready(function () {
	
	// Création du slide sur la home
	if($(".slide_photo").length > 0)
	{
		// On crée les événements sur next / prev
		$(".slide_photo .slide_prev").click(function () {
			
			slideAuto = false;
			
			if(isActive.test($(".slide_photo a").first().attr("class")) === true)
			{
				$(".slide_photo a.active").fadeOut("slow", function () {
            	    $(this).removeClass("active");
            	    $("span", this).hide();
            	});
           		
            	$(".slide_photo a").last().fadeIn("slow", function () {
            	    $(this).addClass("active");
            	    $("span", this).fadeIn('slow');
            	});
			}
			else
			{
				$(".slide_photo a.active").fadeOut("slow", function () {
                	$(this).removeClass("active");
                	$("span", this).hide();
            	}).prev().fadeIn("slow", function () {
                	$(this).addClass("active");
                	$("span", this).fadeIn('slow');
            	});
			}
			
			slideAuto = true;
		});
		
		$(".slide_photo .slide_next").click(function () {
			
			slideAuto = false;
			
			if (isActive.test($(".slide_photo a").last().attr("class")) === true) 
			{
				$(".slide_photo a.active").fadeOut("slow", function () {
            	    $(this).removeClass("active");
            	    $("span", this).hide();
            	});
           		
            	$(".slide_photo a").first().fadeIn("slow", function () {
            	    $(this).addClass("active");
            	    $("span", this).fadeIn('slow');
            	});
			}
			else
			{
				$(".slide_photo a.active").fadeOut("slow", function () {
                	$(this).removeClass("active");
                	$("span", this).hide();
            	}).next().fadeIn("slow", function () {
                	$(this).addClass("active");
                	$("span", this).fadeIn('slow');
            	});
			}
			
			slideAuto = true;
		});

		// On crée le slide automatique
		getSlider();
	}
	
	if($(".options a").length > 0)
	{	
		// On va créer l'événement sur le click
		$(".options.schck a").click(function () {
			
			var isFound = 0;
			var tab_values = eltsRecherche.split('|');
			var new_values = new Array();
			
			$(this).removeClass('selected');
			tabclass = $(this).attr('class');
			
			for(x in tab_values)
			{				
				if(tab_values[x] == tabclass)
				{
					isFound = 1;
					delete(tab_values[x]);
				}
				else if(tab_values[x].length > 0)
				{
					new_values.push(tab_values[x]);
				}
			}
			
			if(isFound == 0)
			{
				// On ajoute l'élément dans le tableau
				new_values.push(tabclass);
				$(this).addClass('selected');
			}
			
			// On va afficher uniquement ce qui nous intéresse (et cacher le reste)
			if(new_values.length > 0)
			{
				$(".cadre_projets a").hide();
			
				for(x in new_values)
				{
					$(".cadre_projets a."+new_values[x]).show();
				}
			}
			else
			{
				$(".cadre_projets a").show();
			}
			
			eltsRecherche = new_values.join('|');
			
			return false;
		});
	}
});

function getSlider() 
{
	if (slideAuto == true) 
    {
        if (isActive.test($(".slide_photo a").last().attr("class")) === true)         {
            $(".slide_photo a.active").fadeOut("slow", function () {
                $(this).removeClass("active");
                $("span", this).hide();
            });
           
            $(".slide_photo a").first().fadeIn("slow", function () {
                $(this).addClass("active");
                $("span", this).fadeIn('slow');
            })
        } 
        else 
        {
        	if($(".slide_photo a.active").length == 0)
        	{
        		$(".slide_photo a").first().fadeIn("slow", function () {
        			$(this).addClass("active");
        			$("span", this).fadeIn('slow');
        		});
        	}
        	else
        	{
        		$(".slide_photo a.active").fadeOut("slow", function () {
                	$(this).removeClass("active");
                	$("span", this).hide();
            	}).next().fadeIn("slow", function () {
                	$(this).addClass("active");
                	$("span", this).fadeIn('slow');
            	});
        	}            
        }
    }
    
    setTimeout(getSlider, 7000);
}
