// VARIABLES //////////////////////////////////////////////////////////
// Langue  (X_LANG contient la langue courante)
var fr_i18n = {
    '1'		:	'Rechercher sur le site',
    '2'		:	'Veuillez verifier les champs',
    '3'		:	'Merci'
};
var en_i18n = {
    '1'		:	'Search Site',
    '2'		:	'Please check the fields',
    '3'		:	'Thank you'
};
var es_i18n = {
    '1'		:	'Búsqueda en el sitio',
    '2'		:	'Por favor, compruebe los campos',
    '3'		:	'Gracias'
};
var ca_i18n = {
    '1'		:	'Cerca al lloc',
    '2'		:	'Si us plau, comproveu els camps',
    '3'		:	'Gràcies'
};
var i18n = {'fr':fr_i18n, 'en':en_i18n, 'es':es_i18n, 'ca':ca_i18n};
var obj;
// Animation accueil
var starts = {
    1 : -640,
    2 : -631,
    3 : -622,
    4 : -613,
    5 : -604,
    6 : -595,
    7 : -586,
    8 : -577,
    9 : -568
}
var ends = {
    1 : 0,
    2 : 9,
    3 : 18,
    4 : 27,
    5 : 36,
    6 : 45,
    7 : 54,
    8 : 63,
    9 : 72
}
var slideActive = -1;
var sens = 1;
var nbre_diapos = 0;
var CCSS_affiche = 1;

// DEMARRAGE //////////////////////////////////////////////////////////
$(document).ready(function(){
    // Exple pour les fonctions spécifiques à un type d'écran...
    /*
    if($('body')[0].className == 'c13'){
            alert('ok');
    }
    */
    // Survol des tableaux (classe : tr.over)
    $('#corps tr').bind({
        mouseover: function(){this.className += ' over';},
        mouseout: function(){this.className = this.className.replace('over','');}
    });
    // Liens externes
    $('body a').bind('click',function(){
        if(this.href.search('^http://'+X_URL_SITE) == -1 || this.href.search('go-liens-') != -1){
            window.open(this.href,'','');
            return false;
        }
    });
    // Champ rechercher
    if($('#rechercher_input')[0]){
        $('#rechercher_input').bind(
            {
                focus:function(){if(this.value == i18n[X_LANG][1]){this.value='';}},
                blur:function(){if(this.value == ''){this.value = i18n[X_LANG][1];}}
            }
        );
        if($('#rechercher_input')[0].value == ''){
            $('#rechercher_input')[0].value = i18n[X_LANG][1];
        }
        //$('#rechercher_input')[0].lastChild.lastChild.bind('click', function(){$('#rechercher_input').focus();return false;});
    }
    // Animation accueil
    if($('#animation_acc')[0]){
        nbre_diapos = $('#animation_acc li').length;
        $('#animation_acc li, #animation_acc_masque').bind({
            click:function(){
                var objCible = 0;
                clearInterval(CCanimation_acc);
                if(this.id != 'animation_acc_masque'){
                    objCible = parseInt(this.id.replace('diapo',''));
                }
                showSlide(objCible);
                this.blur();
                return false;
            }
        });
        CCanimation_acc = setInterval('showSlide()', 3000);
    }
    // Slideshow autres pages
    if($('#slideshow')[0]){
        if($('#slideshow li').length > 1){
            CCSlideshow = setInterval('nextSlide()', 3500);
        }
        else if($('#slideshow li').length == 1){
            $('#slideshow').css('background','none');
            $('#slideshow_1').fadeTo(400,1);
        }
    }

    // DEBUG
    if($('#debug1')[0] && $('#debug2')[0]){
        sw('#debug1');
        sw('#debug2');
        $('#bt_debug1').bind('click', function(){sw('#debug1');sw('#bt_debug2');return false;});
        $('#bt_debug2').bind('click', function(){sw('#debug2');sw('#bt_debug1');return false;});
        $('#debug1').css('opacity','0.7');
        $('#debug2').css('opacity','0.7');
        $('#bt_debug1').css('opacity','0.8');
        $('#bt_debug2').css('opacity','0.8');
        var debugs = new Array($('div#debug1 li'),$('div#debug2 li'));
        for(var i = 0; i < debugs.length; i++){
            var LIs = debugs[i];
            for(var j = 0; j < LIs.length; j++){
                var node = LIs[j];
                if(node.lastChild && node.lastChild.nodeName == 'UL'){
                    node.lastChild.style.display = 'none';
                    var aEtiquette = node.firstChild;
                    var newA = document.createElement('A');
                    var newAText = document.createTextNode('[*] ');
                    newA.appendChild(newAText);
                    newA.setAttribute('href','#');
                    node.insertBefore(newA, aEtiquette);
                    newA.onclick = function(){
                        if(this.parentNode.lastChild.style.display != 'none'){
                            this.parentNode.lastChild.style.display = 'none'
                        }
                        else if(this.parentNode.lastChild.style.display != 'block'){
                            this.parentNode.lastChild.style.display = 'block';
                        }
                        return false;
                    };
                    newA.onfocus = function(){this.blur();}
                }
            }
        }
    }
});
// FONCTIONS //////////////////////////////////////////////////////////

/*========================================*\
    Slideshow accueil
\*========================================*/
function showSlide(id_diapo){
    if(slideActive < 1){
        sens = 1;
    }
    else if(slideActive == nbre_diapos-1){
        sens = -1;
    }
    else if(id_diapo != undefined){
        sens = 0;
    }
    slideActive = id_diapo == undefined ? slideActive+sens : id_diapo;
//window.status = id_diapo+' : '+slideActive+' : '+sens;
    for(var p = 1; p <= nbre_diapos; p++){
        if(p <= slideActive){
            $('#diapo'+p).animate(
                {
                    left:starts[p]+"px"
                }
            );
        }
        else{
            $('#diapo'+p).animate(
                {
                    left:ends[p]+"px"
                }
            );
        }
    }
    $('#animation_acc_frame').css('background','none');
}

/*========================================*\
    Slideshow autres pages
\*========================================*/
function nextSlide(){
    if(CCSS_affiche < $('#slideshow li').length){
        CCSS_affiche++;
        $('#slideshow li').fadeTo(400,0);
        $('#slideshow_'+CCSS_affiche).fadeTo(400,1);
    }
    else{
        CCSS_affiche=1;
        $('#slideshow li').fadeTo(400,0);
        $('#slideshow_'+CCSS_affiche).fadeTo(400,1);
    }
    $('#slideshow').css('background','none');
//window.status = '#slideshow_'+CCSS_affiche;
}


/*========================================*\
    Afficher / Masquer
\*========================================*/
function sw(qui){
    var obj = $(qui)[0];
    if(obj.style.display != 'none'){
        obj.style.display = 'none';
    }
    else if(obj.style.display != 'block'){
        obj.style.display = 'block';
    }
    return false;
}
/*========================================*\
    Fonctions Vérification avant envoi de données saisies
    ### !!! ### Gestion des cases à cocher et radios !!
    ### !!! ### Si fac le motif doit aussi etre pris en compte
\*========================================*/
function verifForm(champs, motifs, noms, conseils){
    var mess = '';
    if(champs.length > 0){
        for(i in champs){
            var n = champs[i];
            var id = '#ch_'+n;
            var label = '#label_'+n;
            var motif = motifs[n];
            var nom = noms[n];
            var conseil = conseils[n];
            $(label)[0].className = $(label)[0].className.replace('a_verifier','');
            if($(id)[0].type == 'text' || $(id)[0].type == 'textarea' || $(id)[0].type == 'select-one'){
                if($(id)[0].value.length < 1){
                    $(label)[0].className += ' a_verifier';
                    if(conseil.length > 0){
                        mess += '- '+nom+' : '+conseil+'\n';
                    }
                    else{
                        mess += '- '+nom+'\n';
                    }
                }
                else if(motif.length > 0){
                    if($(id)[0].value.search(motif) == -1){
                        $(label)[0].className += ' a_verifier';
                        if(conseil.length > 0){
                            mess += '- '+nom+' : '+conseil+'\n';
                        }
                        else{
                            mess += '- '+nom+'\n';
                        }
                    }
                }
            }
            else if($(id)[0].type == 'checkbox' && $(id)[0].checked == false){
                $(label)[0].className += ' a_verifier';
                if(conseil.length > 0){
                    mess += '- '+nom+' : '+conseil+'\n';
                }
                else{
                    mess += '- '+nom+'\n';
                }
            }
        }
        if(mess!=''){
            var mess2 = i18n[X_LANG][2]+' :\n\n'+mess + '\n'+i18n[X_LANG][3];
            window.alert(mess2);
            mess2='';
            var result = false;
        }
        else{
            var result = true;
        }
    }
    return result;
}
