// 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;
var animation_list;
var animation_visible;
// DEMARRAGE //////////////////////////////////////////////////////////
$(document).ready(function(){
    // A vidéo -> X_popup
    if($('div#corps a')[0]){
        var liens = $('div#corps a');
        for(var i = 0; i < liens.length; i++){
            var node = liens[i];
            var re = new RegExp('-52-([0-9]+)\.php$');
            var m = re.exec(node.href);
            if(m != null) {
                var id_video = m[1];
                // Charger vignette
                var random_id = 'marker'+Math.floor(Math.random()*101);
                node.className = 'video '+random_id;
                $('a.'+random_id).prepend('<span></span>');
                $('a.'+random_id+' span').load('/xml.video-52-'+id_video+'-vignette.php');
                // X_popup
                node.onclick = X_popup;
            }
        }
    }
    // animation accueil
    if($('html')[0].className.search('c2') != -1){
        animation_list = $('#animation li');
        animation_visible = animation_list.length - 2;
        $('#m2 li a').bind({
            mouseover:function(){
                var id_cible = $(this).parent()[0].id.replace('em','diapo_');
				if($('#animation #'+id_cible)[0]){
					showSlide(id_cible);
				}
            }
        });
        autoSlideshow = setInterval('showSlide()', 3500);
    }
    // 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){
    if(id_diapo != undefined){
        $('#animation li').fadeTo(400,0);
        $('#animation #'+id_diapo).fadeTo(600,1);
        clearInterval(autoSlideshow);
    }
    else{
        $('#animation li').fadeTo(400,0);
        $('#'+animation_list[animation_visible].id).fadeTo(600,1);
    }
    animation_visible--;
    if(animation_visible < 0){
        animation_visible = animation_list.length - 1;
    }
}


// Créer la fausses popup
var X_popup = function(e){
    $('#corps').append('<div id="X_popupCorps"><div id="X_popupTarget"></div><a href="#" title="Fermer" id="X_popupClose"><span>Fermer</span></a></div> <div id="X_popupMasque"></div>');
    $('#X_popupCorps').hide();
    var re = new RegExp('-52-([0-9]+)\.php$');
    var m = re.exec(this.href);
    if(m != null) {
        var id_video = m[1];
        $('#X_popupTarget').load('/xml.video-52-'+id_video+'-video.php');
        $('#X_popupClose, #X_popupMasque').bind({
            click:function(){
                $('#X_popupMasque').remove();
                $('#X_popupCorps').remove();
                return false;
            }
        });
        setTimeout(showX_popup,750);
    }
    return false;
}
// Positionnement et affichage
var showX_popup = function(){
    var Le = window.innerWidth  ? window.innerWidth : document.documentElement.clientWidth;
    var He = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
    var Lx = $('#X_popupCorps').width();
    var Hx = $('#X_popupCorps').height();
    // Ecran > X_popup
    if(He > Hx){
        var Xx = (Le - Lx) /2;
        var Yx = (He - Hx) /2;
        var Px = 'fixed';
    }
    // Ecran < X_popup
    else{
        var Xx = 0;
        var Yx = 0;
        var Px = 'absolute';
    }
    $('#X_popupCorps').css('position',          Px);
    $('#X_popupCorps').css('left',              Xx+'px');
    $('#X_popupCorps').css('top',               Yx+'px');
    $('#X_popupCorps').show();
}
/*========================================*\
    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;
}
