// 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;

// DEMARRAGE //////////////////////////////////////////////////////////
$(document).ready(function(){
    // Exple pour les fonctions spécifiques à un type d'écran...
    /*
    if($('html')[0].className == 'c13'){
            alert('ok');
    }
    */
    // Animation accueil
    if($('html')[0].className == 'c2'){
        animation_list  = $('#animation_diapos li');
        image_list      = $('#animation_diapos li img');
        animation_visible = 0;
        $('#animation_diapos li').fadeTo(1,0);
        showSlide();
        autoSlideshow = setInterval('showSlide()', 5500);
    }
    // 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;});
    }
    // Google Analytics
    if(_gaq != ''){
        var ga = document.createElement('script');ga.type = 'text/javascript';ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga, s);
    }

    // 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 //////////////////////////////////////////////////////////

// Affichage de l'animation accueil
function showSlide(id_diapo){
    animation_visible--;
    if(animation_visible < 0){
        animation_visible = animation_list.length - 1;
    }
//window.status = animation_visible+' : '+'#'+image_list[animation_visible].id+' img';
    $('#'+image_list[animation_visible].id).animate({top:'0px'},1, function(){
        $('#'+animation_list[animation_visible].id).fadeTo(700,1, function(){
            $('#'+image_list[animation_visible].id).animate({top:'-80px'},3500, function(){
                $('#animation_diapos li').fadeTo(1200,0)
            });
        });
    });


    /*
    $('#'+image_list[animation_visible].id).animate({top:'-100px'},4500, function(){
        $('#animation_diapos li').fadeTo(500,0, function(){
            $('#animation_diapos li img').animate({top:'0px'},10, function(){;
                $('#'+animation_list[animation_visible].id).fadeTo(800,1);
            });
        });
    });
    $('#animation_diapos li').fadeTo(500,0, function(){
        $('#'+animation_list[animation_visible].id).fadeTo(800,1, function(){
            $('#'+image_list[animation_visible].id).animate({top:'-100px'},4500, function(){
                $('#animation_diapos li img').animate({top:'0px'},10);
            });
        });
    });
    */
}   


/*========================================*\
    Fonctions 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;
}
