// initialise menu

/**
 * Champ formulaire contenant leur nom
 * videChampForm(input) récupère la valeur courante et vide le input
 * initChampForm(input) réinsère la valeur initiale si le input est vide
 */
var val_champ = new Array();
function videChampForm(input) {
    name = $(input).attr("name");
    if (!val_champ[name]) {
        val_champ[name] = $(input).val();
    }
    current_val = $(input).val();
    if (current_val == val_champ[name]) {
        $(input).val('');
    }
}
function initChampForm(input) {
    name = $(input).attr("name");
    current_val = $(input).val();
    if (current_val == '') {
        $(input).val(val_champ[name]);
    }
}


/**
 * JQuery
 */
/* -------------- */
/* -- On Ready -- */
/* -------------- */
$(document).ready(function() {
    /* ----------------------------------------- */
    /* HACK JS pour affichage menu_page dans IE6 */
    /* ----------------------------------------- */
    if ($.browser.version == '6.0'){
        $("#div_bandeau").pngFix();
        $('ul#menu_page li:not([class=on]) a').hover(function() {
            $(this).parent().addClass('on');
        }, function() {
            $(this).parent().removeClass('on');
        });
    }
    /* ------------------------------------ */
    /* Affichage des sous menu du catalogue */
    /* ------------------------------------ */
    $("ul#menu_catalogue li").hover(function() {
        $("div", this).show();
    }, function() {
        $("div", this).hide();
    });
    $("ul#menu_catalogue_content li").hover(function() {
        $("div", this).show();
    }, function() {
        $("div", this).hide();
    });
    /* --------------------- */
    /* Bouton envoi à un ami */
    /* --------------------- */
    $("img#img_send_friend").hover(function() {
        default_src = $(this).attr('src');
        $(this).attr('src', 'static/images/envoi_ami_hover.gif');
    }, function() {
        $(this).attr('src', default_src);
    });
    /* ---------- */
    /* Promo code */
    /* ---------- */
    $('#promo_code input.bt').hover(function(){
        src_code = $(this).attr('src');
        $(this).attr('src', 'static/images/domeo/bt_ok_code_promo_on.png');
    }, function(){
        $(this).attr('src', src_code);
    });
    /* ---------- */
    /* Newsletter */
    /* ---------- */
    $('#newsletter input.bt').hover(function(){
        src_code = $(this).attr('src');
        $(this).attr('src', 'static/images/home/newsletter_bt_ok_hover.gif');
    }, function(){
        $(this).attr('src', src_code);
    });
    /* --------- */
    /* Recherche */
    /* --------- */
    $('#recherche input.bt').hover(function(){
        src_recherche = $(this).attr('src');
        $(this).attr('src', 'static/images/domeo/bt_ok_recherche_on.png');
    }, function(){
        $(this).attr('src', src_recherche);
    });
    class_cat = $('#cat_bandeau li:last').attr('class');
    width_cat_bandeau = $('#div_bandeau').width();
    for (i=1;i<=5;i++) {
        position = (i - 1) * width_cat_bandeau;
        $('ul#cat_bandeau li.'+i).css('left', position);
    }
    /* ---------------------- */
    /* Bandeau menu catégorie */
    /* ---------------------- */
    $('.cat_bouton').hover(function() {

        // On récupère les infos pour les changements
        img_src = $(this).attr('src');

        // Changement de bouton sur le hover
        reg = new RegExp('(boutonoff)', 'g');
        img_src_off = img_src.replace(reg,'boutonon');
        $(this).attr('src', img_src_off);

        parent_li = $(this).parent().parent().attr('class');

        new_position = (parent_li - 1) * width_cat_bandeau;

        $('ul#cat_bandeau').animate({
            left: 0 - new_position
        }, 750);

    }, function() {

        // On rétablie le bouton après le hover
        $(this).attr('src', img_src);

    });
    /* --------------------- */
    /* Ombrage sur menu_home */
    /* --------------------- */
    $('.menu_home_element').hover(function() {
        $('.ombr_vign_home', this).css({ visibility: 'visible' });
    }, function() {
        $('.ombr_vign_home', this).css({ visibility: 'hidden' });
    });

    /* ----------------------- */
    /* Effet sélection du menu */
    /* ----------------------- */
    position = $('ul#ssmenu_content li.on').offset();
    $('#ssmenu_back').css({
        'top': position.top+'px',
        'left': (position.left+7)+'px',
        'visibility': 'visible'
    });
    /* ------------------------ */
    /* Effet sélection du caddy */
    /* ------------------------ */
    position = $('ul#caddy_onglets li.on').offset();
    $('#ssmenu_caddy').css({
        'top':(position.top+25)+'px',
        'left': (position.left+1)+'px',
        'visibility': 'visible'
    });

    $('a#link_schema_produit').fancybox();
    $('a#link_send_friend').fancybox();
    $('a#link_add_caddy').fancybox({
        'frameWidth':400,
        'frameHeight':320
    });


    /* ---------------------------- */
    /* Survol second niveau de menu */
    /* ---------------------------- */
    $('ul.ssmenu_cata li.produit').hover(function() {
        $this = $(this);
        $rel = $this.attr('rel');
        if ($rel) {
            $this.data('old_bg_color', $this.css('backgroundColor'));
            $this.css('backgroundColor', $rel);
        }
    },function() {
        $this = $(this);
        $rel = $this.attr('rel');
        if ($rel) {
            $this.css('backgroundColor', $this.data('old_bg_color'));
        }
    });


    /* ---------------------------- */
    /*   Image menu fiche produit   */
    /* ---------------------------- */
    var $image_produit_menu = $('#image_produit_menu');
    var adjustment = null;
    if ($.browser.msie && parseInt($.browser.version) == 6) {
        adjustment = 34;
    } else {
        adjustment = 32;
    }
    $('img:first', $image_produit_menu).one("load",function(){
        var $ssmenu_content = $('#ssmenu_content');
        var $ssmenu_content_offset = $ssmenu_content.offset();
        $image_produit_menu.css({
           'position' : 'absolute',
           'display'  : 'block',
           'top'      : $ssmenu_content_offset.top    + $ssmenu_content.height(),
           'left'     : $ssmenu_content_offset.left + $ssmenu_content.width() - $image_produit_menu.width() + adjustment
        });
    })
    .each(function(){
        if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
        $(this).trigger("load");
    });

});