
// Ce fichier est volontairement non-packé 

var $team;
var $slideshow;
var $nav;
var interval;
var $publications;

// On est prêts !
$(document).ready(function() {
    

    var pathname = $(location).attr('href').match(/#([a-z]+)/);
   

    // Population des variables (optimisation!)
    $team = $("#team");

    // Slideshow -----------------------------------------------------------
    var delay = 4000;
    var time_slide = 400;

    $slideshow = $("#slideshow");

    $nav = $(".menu ul");
    
    //Supperposition des slides
    var tabcss = {
        'position':'absolute',
        'top':'0',
        'left':'0'
    };
    $(".slide",$slideshow).css(tabcss);
    
    //Affichage unique de la 1er slide
    $(".slide:gt(0)",$slideshow).hide();
    
    //Ajout des btn next & prev
    $slideshow.append('<a href="#" id="previous"><img src="/demo/icone/theme/V1/img/theme/fleche-gauche.png" alt="slide précédente"></a><a href="#" id="next"><img src="/demo/icone/theme/V1/img/theme/fleche-droite.png" alt="slide suivante"></a>');

    
    // Classe active par défaut
    if(pathname){
        
        $(pathname[0],$nav).addClass("active");
        $(".slide",$slideshow).stop(true, true).hide(); 
        $(".slide[data-nav="+pathname[1]+"]",$slideshow).stop(true, true).fadeIn(time_slide);
    }else{
        $("a:first",$nav).addClass("active");
    }
    //$("a:first",$nav).addClass("active");
    
    // Passage à la slide suivante
    function slide_next(sens) {
        if(typeof sens=='undefined' || sens==undefined) {
            sens=1;
        }
        var $slideCurrent = $(".slide:visible",$slideshow);
        if(sens>0)
            var $suivante = $slideCurrent.last().next(".slide");
        else
            $suivante = $slideCurrent.first().prev(".slide");
        if ($suivante.length<1) {
            if(sens>0) $suivante = $(".slide:first",$slideshow);
            else $suivante = $(".slide:last",$slideshow);
        }
        $suivante.stop(true, true).fadeIn(time_slide);
        $slideCurrent.stop(true, true).fadeOut(time_slide);
        $("a",$nav).removeClass("active");
        $("a#"+$suivante.data("nav")).addClass("active");
    }
    
    //Survol btn
    if($("#previous",$slideshow).length) $("#previous",$slideshow).hover(function() {
        $("#previous img",$slideshow).attr('src','/demo/icone/theme/V1/img/theme/fleche-gauche-hover.png');
    },function() {
        $("#previous img",$slideshow).attr('src','/demo/icone/theme/V1/img/theme/fleche-gauche.png');
        }
    );
    
    if($("#next",$slideshow).length) $("#next",$slideshow).hover(function() {
        $("#next img",$slideshow).attr('src','/demo/icone/theme/V1/img/theme/fleche-droite-hover.png');
    },function() {
        $("#next img",$slideshow).attr('src','/demo/icone/theme/V1/img/theme/fleche-droite.png');
        }
    );
    
    //Action btn
    $("a#next",$slideshow).click(
        function(e) {
            slide_next(1);
            e.preventDefault();
            slide_stop();       
        });
            
            
    $("a#previous",$slideshow).click(
        function(e) {
            slide_next(-1);
            e.preventDefault();
            slide_stop();
        });
    
    //Survol nav
    $("a",$nav).click(
        function() {
            clearInterval(interval);
            $("a",$nav).removeClass("active");
            $(this).addClass("active");
            $(".slide",$slideshow).stop(true, true).fadeOut(time_slide);        
            var id = $(this).attr("id");
            $(".slide[data-nav="+id+"]",$slideshow).stop(true, true).fadeIn(time_slide);
            
        });

    $("a",$nav).mouseleave(slide_start);
    
    //Survol slide
    $slideshow.mouseenter(slide_stop);
    $slideshow.mouseleave(slide_start);
    
    
    // Auto
    function slide_start() {
        clearInterval(interval);
        interval = setInterval(function() { slide_next(1); },delay);
    }

    function slide_stop() {
        clearInterval(interval);
    }
    
    //slide_start();
    
    
    
    // Fin Slideshow -----------------------------------------------------------
    
    // Publications -----------------------------------------------------------
    $publications = $("#publications");
    
    // Masquer les div par défaut
    if($publications.length>0) {
        $(".descriptions div+div",$publications).hide();
        $(".illustrations li:first-child a").addClass("current");
        
        // Action click
        $('.illustrations a:not("a.current")', $publications).live("click", function(event) {
            var $lien_clique = $(this);
            $(".illustrations a", $publications).removeClass("current");
            var li_i = $lien_clique.parent('li').index();

            $lien_clique.parents(".illustrations").next().find("div:visible").stop(true, true).slideUp("slow", function(){
                $(this).parent().find("div:eq("+li_i+")").slideDown("slow");
                // console.log($(this));
                $lien_clique.addClass("current");
            });
            
            event.preventDefault();
        });

        $(".illustrations a.current",$publications).live("click", function(event){
            event.preventDefault();
        });
    }
    
    // Fin Publications -----------------------------------------------------------
    
    
    
    // Slider -----------------------------------------------------------
    
    $(window).resize(function() {
        slider_go();
    });
    
    function slider_go() {
        var taille = $(window).width();
        if (taille>1024) {
            
            // Insertion des éléments HTML
            if($("#container-slider").length==0) {
                $(".slider p").hide();
                $(".slider").append('<p class="nb-page">Nombre de maquettes ?<span>1</span></p><div id="container-slider"><div id="slider"><div id="slide"></div></div></div><div id="page"><img src="img/page.png" alt="page"></div><p class="nb-j">Jours nécessaires :<span>3</span></p>');
                $("#container-slider").prepend('<a href="#" class="moins">-</a><a href="#" class="plus">+</a>');
            }
            
            // Initialisation des variables
            var $nbpage = $(".nb-page span");
            var $nbjour = $(".nb-j span");
            var $slide = $("#slide");
            var max_number = 12;
            
            
            // Initialisation du slide
            /*$slide.slider({
                value:1,
                min: 1,
                max: max_number,
                step: 1,
                slide: function( event, ui ) {
                    $nbpage.html(ui.value);
                    var value = $slide.slider("value");
                    scan();
                }
            });*/


             // A optimiser

            
            function scan() {
                var nb_before = $slide.slider("value");
                var nb_current = $(".nb-page span").html();
                var xfois = nb_current-nb_before;
                if(nb_current<nb_before) {
                    if(nb_current>=1) {
                        while(xfois<0) {
                            calcul(-1);
                            xfois++;
                        }
                    }
                } else {
                    while(xfois>0) {
                        calcul(1);
                        xfois--;
                    }
                }
            }
            
            function calcul(operation) {
                if(operation>0) {
                    var nbcurrent = $slide.slider("value");
                    if(nbcurrent<max_number) {
                        var nb = nbcurrent+1;
                        $("#page").append('<img src="img/page.png" alt="page">');
                    }
                } else {
                     nbcurrent = $("#slide").slider("value");
                    if(nbcurrent>1) {
                         nb = nbcurrent-1;
                        $("#page img:last").remove();
                    }
                }
                $slide.slider({value: nb});
                $nbpage.html(nb);
                if(nb==1) var resultat = 3;
                if(nb>1) resultat = 3+nb-1;
                $nbjour.html(resultat);

            }
            
            // + & -
            $("a.moins").click( function(event) {
                calcul(-1);
                event.preventDefault();
            });
            
            $("a.plus").click( function(event) {
                calcul(1);
                event.preventDefault();
            });
            
            
        } else {
            $(".nb-page, #page, #container-slider, .nb-j").remove();
            $(".slider p").show();
        }
    }
    slider_go();
    
    // Fin slider -----------------------------------------------------------
    
    
    
    // Swap about
    var $aboutlink = $("#aboutlink");
    if($aboutlink.length) $($aboutlink).hover(function() {
        // Penser à mettre des images de la team à poil...
        $("#raph",$team).attr("src","/img/team/team_raphael_hover.jpg");
        $("#dew",$team).attr("src","/img/team/team_rodolphe_hover.jpg");
        $("#felipe",$team).attr("src","/img/team/team_philippe_hover.jpg");
        $("#simon",$team).attr("src","/img/team/team_simon_hover.jpg");
        $("#geoffrey",$team).attr("src","/img/team/team_geoffrey_hover.jpg");
        $("#coralie",$team).attr("src","/img/team/team_coralie_hover.jpg");
    },function() {
        // Hopladüüü
        $("#raph",$team).attr("src","/img/team/team_raphael.jpg");
        $("#dew",$team).attr("src","/img/team/team_rodolphe.jpg");
        $("#felipe",$team).attr("src","/img/team/team_philippe.jpg");
        $("#simon",$team).attr("src","/img/team/team_simon.jpg");
        $("#geoffrey",$team).attr("src","/img/team/team_geoffrey.jpg");
        $("#coralie",$team).attr("src","/img/team/team_coralie.jpg");
    });

    // Antispam
    var $contactmail = $("#contactmail");
    if($contactmail.length) {
        var onaimepaslespam; // et c'est vrai...
        if($contactmail.hasClass("forma")) onaimepaslespam = 'formations';
        else onaimepaslespam = 'contact';
        onaimepaslespam += '@';
        onaimepaslespam += 'alsacreations.fr';
        $contactmail.text(onaimepaslespam);
        $contactmail.wrapInner("<a></a>");
        $contactmail.find("a").attr("href","mailto:"+onaimepaslespam);
    }
    
    // Formulaire formations
    var $formation_list = $("#formation_list");
    
    if($formation_list.length>0) {
        $checkboxes_formation = $(".formation :checkbox",$formation_list);
        $checkboxes_formation.change(function(e) {
            e.stopPropagation();
        });
        load_formations();
    }
    
    $(".inscription .warning").hide();
    $("#inter").one("change",function() {
        $(".warning").slideDown();
    });
    

});

// Charge les formations dans le formulaire
function load_formations() {

    $checkboxes_formation.each(function() {
        var valville = $(this).val();
        if(valville.indexOf("Paris")>0 && $(this).attr("checked")) {
            var classe = $(this).attr("class");
            if(classe.length>0) {
                $checkboxes_formation.filter("."+classe).attr("checked","checked");
            }
        }
    });

}

// Dewplayer HTML generator
$("#dewplayeroptions input").bind("keyup change", function() {
    dewshowcode();
});



