/*
**  Utilisation de la librairie jQuery
**  http://docs.jquery.com/Downloading_jQuery#Download_jQuery
*/
jQuery.noConflict();
jQuery(function($)
{

    /** Lightbox */

    $(".btLightbox").click(function(){
        $("#overlayLightbox").show();
        $("#"+$(this).attr('id')+"Box").show();
        this.blur();
        return false;
    });

    $(".lightBox .fermer a").click(function(){
        $("#overlayLightbox").hide();
        $(this).parent().parent().hide();
        this.blur();
        return false;
    });

    if ($('.autoShow').length)
    {
       $("#overlayLightbox").show();
       $('.autoShow').show();
    }
    /** end Lightbox */

    $('.externe').click(function(){
            window.open($(this).attr('href'));
            return false;
        }
    );

    if ($("#indexAnim").length) loadFlash("/swf/carroussel.swf", "indexAnim", "588", "401", "8.0.0", { xmlFile: "/carroussel/prehome"});
    if ($("#profilAnim.profil_grossesse").length) loadFlash("/swf/carroussel.swf", "profilAnim", "588", "401", "8.0.0", { xmlFile: "/carroussel/grossesse"});
    if ($("#profilAnim.profil_3mois").length) loadFlash("/swf/carroussel.swf", "profilAnim", "588", "401", "8.0.0", { xmlFile: "/carroussel/3mois"});
    if ($("#profilAnim.profil_11mois").length) loadFlash("/swf/carroussel.swf", "profilAnim", "588", "401", "8.0.0", { xmlFile: "/carroussel/11mois"});
    if ($("#profilAnim.profil_17mois").length) loadFlash("/swf/carroussel.swf", "profilAnim", "588", "401", "8.0.0", { xmlFile: "/carroussel/17mois"});
    if ($("#profilAnim.profil_24mois").length) loadFlash("/swf/carroussel.swf", "profilAnim", "588", "401", "8.0.0", { xmlFile: "/carroussel/24mois"});
    if ($("#profilAnim.jeu_smartbox").length) loadFlash("/swf/jeu.swf", "profilAnim", "494", "338", "8.0.0", { varFile:"/jeu/init", cgv:"/jeu/cgv", smartbox:"http://www.smartbox.com/fr/nos-smartbox/bien-etre.html?utm_nooverride=1&utm_source=google&utm_medium=cpc&utm_term=lien1coffretBE&utm_content=Trademark%20-%20Smartbox&utm_campaign=CC_GOO_SEM_TM_Smartbox&sitelink=lien1coffretBE&gclid=CPyKr-OvmKwCFYQm3godti5"});
    if ($("#reducAnimContent").length) loadFlash("/swf/reduc.swf", "reducAnimContent", "135", "117", "8.0.0");

    $("select.error, input.error, textarea.error").live('focus', function(e) {
        var span = $(this).parent().find('span.error');
        span.css('left', ($(this).position().left + $(this).outerWidth() + 11) +"px");
        span.css('top', $(this).position().top +"px");
        span.show();
    });

    $("select.error, input.error, textarea.error").live('focusout', function(e) {
        $(this).parent().find('span.error').hide();
    });
    $("select.error, input.error, textarea.error").live('change', function(e) {
        console.log($(this));
        $(this).parent().find('span.error').hide();
    });

    $("input[name='consommateur[question_id]']").bind('change', function(e)
    {
      $('#consommateur_autre_question').attr('disabled', 8 != $(this).val());
      if (8 == $(this).val())
      {
        $('#consommateur_autre_question').addClass('required');
      }
      else {
        $('#consommateur_autre_question').removeClass('required');
        $('#consommateur_autre_question').removeClass('error');
        $('#consommateur_autre_question').attr('value', '');
      }
    });

    if ($('#consommateur_nb_enfants').length)
    {
        $('#consommateur_nb_enfants').change(function() {
            refreshEnfantsList();
        });
        refreshEnfantsList();
    }

    $('#consommateur_autre_question').attr('disabled', 8 != $("input[name='consommateur[question_id]']:checked").val());

    if ($("#loginForm").length || $("#contactForm").length || $("#unregisterForm").length || $("#invitationForm").length)
    {
        $.validator.messages.date      = "Merci d'insérer une date au format jj/mm/aaaa.";
        $.validator.messages.required  = "Ce champs est obligatoire.";
        $.validator.messages.email     = "L'email n'est pas correcte.";
        $.validator.messages.digits    = "Merci de n'insérer que des chiffres";
        $.validator.messages.maxlength = $.validator.format("Merci d'insérer au maximum {0} caractères.");
        $.validator.messages.minlength = $.validator.format("Merci d'insérer au moins {0} caractères.");
        $.validator.messages.equalTo   = $.validator.format("Merci d'insérer la même valeur.");
        $("#loginForm, #contactForm, #unregisterForm, #invitationForm").validate({
            //focusInvalid: "false",
            //onfocusout: "true",
            onkeyup: true,
            errorElement: "span",
            invalidHandler: function(e, validator) {
                var errors = validator.numberOfInvalids();
                if (errors) {
                    var message = 'Vous avez oublié ' + errors + ' champs. Ils ont été mis en évidence.';
                    if (errors == 1)
                    {
                        message = 'Vous avez oublié 1 champ. Il a été mis en évidence.';
                    }
                    $("#errorBox div.error").html(message);
                    showLightbox("error");
                } else {
                    hideLightbox("error");
                }
            },
            submitHandler: function(form){
                form.submit();
            },
            messages: {},
            debug:false
        });
    }
    $("li#smartbox").click(function(){
        if ($("li#smartbox").css('display') == 'block') {
            openWindowToolSmartbox('popin');
        }
    });
}); /* jQuery end */
function closeWindowTool(maDiv){
    jQuery("#"+maDiv).hide();
}

function closeWindowToolSmartbox(maDiv){
    jQuery("#"+maDiv).hide();
    jQuery("li#smartbox").show();
}

function openWindowToolSmartbox(maDiv){
    jQuery("#"+maDiv).show();
    jQuery("li#smartbox").hide();
    jQuery("#"+maDiv).restart();//fonction definie dans flash
}

function loadFlash(swf, target, width, height, version, flashvars)
{
    //console.log(swf);
    var flashparams = {
            wmode: "transparent",
            scale: "noscale",
            menu: "false"
    };
    swfobject.embedSWF(swf, target, width, height, version, "/swf/expressInstall.swf", flashvars, flashparams);
}

function showLightbox(id)
{
    jQuery("#overlayLightbox").show();
    jQuery("#"+id+"Box").show();
}
function hideLightbox(id)
{
    jQuery("#overlayLightbox").hide();
    jQuery("#"+id+"Box").hide();
}
function beforeShowDate(a)
{
    var b = new Date();
    var c = new Date(b.getFullYear(), b.getMonth(), b.getDate());
    return {maxDate: c}
}

function hideElement(maDiv)
{
    jQuery("#"+maDiv).hide();
}

/**
// Debug mode
**/
function log($message) {
    if (console && console.log)
        console.log($message);
    else
        alert($message);
}

function info($message) {
    if (console && console.info)
        console.info($message);
    else
        alert($message);
}

function warn($message) {
    if (console && console.warn)
        console.warn($message);
    else
        alert($message);
}

function error($message) {
    if (console && console.error)
        console.error($message);
    else
        alert($message);
}
/* Debug end */
