function bloc_deroulant(objet) {
    objet.next().slideToggle("normal");
    objet.toggleClass("selected");
    objet.parent().toggleClass("bloc_deroulant_selected");

    return false;
}

function document_ready() {
    // Blocs déroulants
    $(".zone_deroulante:not(.ouvert)").hide();

    $("a.lien_deroulant").click(
        function() {
            return bloc_deroulant($(this));
    });

    // Pré-dérouler un bloc si une ancre est passée dans l'url
    ancre = document.location.hash;
    if (ancre) {
        bloc_id = ancre.substr(1, ancre.length);
        obj_lien = $("#" + bloc_id).children("a.lien_deroulant");
        bloc_deroulant(obj_lien);
    }

    // Submit d'un form sur le keyup (sinon ça bug pour IE et Safari)
    $("input").keyup(function(e) {
        if (e.keyCode == 13) {
            $(this).parents("form").submit();
            return false;
        }
    });

    populate_inputs();

    if ($.fancybox) {
        $("a.fancybox").fancybox({
            'type': 'iframe',
            'titleShow': false,
            'showCloseButton': false,
            'width': 750,
            'height': 800,
            'padding': 5,
            'overlayShow': true,
            'overlayColor': '#D7DAE0',
            'overlayOpacity': 0.7

        });
    }

    $(".bouton a").css("display", "inline-block");
    $(".bouton input").hide();

    $(".editeur .tmpl_tableau_colore tbody tr:odd").addClass("paire");

    $(".imprimer").click(function(e) {
        window.print();
        e.preventDefault();
    });

    // Background vidéo
    sizeFondVideo(true);

    $(window).resize(function(){
        sizeFondVideo();
    });

    /*
     * Changer la taille de la police
     *
     * @author J-F, Emilie
     * @copyright egzakt.com
     * @version 2010/07/07
     */
    if (!($.cookie("font-size"))) {
        $.cookie("font-size", "100%", { expires: 365, path: '/' });
    }

    switch($.cookie("font-size")) {
        case "100%": changeTaillePolice($.cookie("font-size"),$("a#taille_normale")); break;
        case "110%": changeTaillePolice($.cookie("font-size"),$("a#taille_moyenne")); break;
        case "120%": changeTaillePolice($.cookie("font-size"),$("a#taille_grande")); break;
    }

    $("a#taille_normale").click(function(){
        changeTaillePolice("100%",$(this));
        return false;
    });

    $("a#taille_moyenne").click(function(){
        changeTaillePolice("110%",$(this));
        return false;
    });

    $("a#taille_grande").click(function(){
        changeTaillePolice("120%",$(this));
        return false;
    });

    function changeTaillePolice(taille,objet_lien) {
        $('body').css('font-size', taille);
        $.cookie("font-size", taille, { expires: 365, path: '/' });

        $("#taille_police a").removeClass("selected");
        objet_lien.addClass("selected");
    }

}


/*
 * Vérifier si une valeur alt="" est fournis au champs input et textarea
 * et si le input/textarea a l'attribut value vide, mettre la valeur du
 * alt dans l'attribut value.
 *
 * Était direct dans le .ready jusqu'a se que je remarque qu'il ne
 * s'appliquait pas au formulaire loader en ajax, maintenant on n'a qu'a
 * caller populate_inputs() sur le callback du load.
 */
function populate_inputs() {
    $("input,textarea").each(function(index) {
        if ($(this).attr("alt") != "") {
            if ($(this).val() == "") {
                $(this).val($(this).attr("alt"));
                $(this).addClass("unfocus");
            }
        }
    });

    $("input,textarea").focus(function() {
        if ($(this).attr("alt") != "") {
            if ($(this).val() == $(this).attr("alt")) {
                $(this).val("");
                $(this).removeClass("unfocus");
            }
        }
    });

    $("input,textarea").blur(function() {
        if ($(this).attr("alt") != "") {
            if ($(this).val() == "") {
                $(this).val($(this).attr("alt"));
                $(this).addClass("unfocus");
            }
        }
    });
}


//Resize background vidéo
function sizeFondVideo(load) {

    if (!$('#fond_video').length || $.browser.mozilla) {
        return;
    }

    $('.fond_image_1').remove();

    largeur_flash = $("body").width();

    if ( ($.browser.msie) && ($.browser.version == "6.0") ) {
        hauteur_flash =  $(document).height();
    }
    else {
        hauteur_flash =  $(window).height();
    }

    if (load) {
        var videoNo = Math.ceil(Math.random() * 2);
        var so = new SWFObject("/bundles/projectfrontendcore/videos/fond_section_2.swf", "fond_video", largeur_flash, hauteur_flash, "8", "#ffffff");
        so.addParam("wmode", "transparent");
        so.write("fond_video");
    }
    else {
        $("#fond_video embed").attr("width",largeur_flash);
        $("#fond_video embed").attr("height",hauteur_flash);
        $("#fond_video object").attr("width",largeur_flash);
        $("#fond_video object").attr("height",hauteur_flash);
    }
}
