// 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 pf_travaux_width = 396;
var slide_travaux = 0;
var pf_affiche = 1;

// 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('-78-([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-78-'+id_video+'-vignette.php');
                // X_popup
                node.onclick = X_popup;
            }
        }
    }
    // Survol des tableaux (classe : tr.over)
    $('#corps tr').bind({
        mouseover: function(){this.className += ' over';},
        mouseout: function(){this.className = this.className.replace('over','');}
    });
    // Liens externes
    openExternals();
    // 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;});
    }
    // Portfolio portfolio
    if($('html')[0].className.search('c2_56') != -1){
        // Slide detail
        initSlideDetail();
        // Taille travaux
        var nb_travaux = $('#pf_travaux li').length;
        pf_travaux_width = 396 * Math.ceil(nb_travaux / 6);
        $('#pf_travaux').css('width', pf_travaux_width+'px');
        $('#precedent').hide();
        // Slide travaux
        $('#precedent_suivant li').bind({
            click:function(){
                if(this.id == 'precedent'){
                    slide_travaux += 396;
                }
                else{
                    slide_travaux -= 396;
                }
                var id_cible =      '#pf_travaux';
                var decalage =      slide_travaux;
                showSlide(id_cible, decalage);
                $('#precedent, #suivant').show();
                this.firstChild.blur();
                if(slide_travaux == 0){
                    $('#precedent').hide();
                }
                if(pf_travaux_width+slide_travaux-396 == 0){
                    $('#suivant').hide();
                }
            }
        });
        // travaux -> détail
        $('#pf_travaux li a').bind({
            click:function(){
                var url = this.href.replace('.php','-ajax.php');
                $('#pf_travaux li a.actif').removeClass('actif');
                this.className = 'actif';
                this.blur();
                // Chargements
                $('#pf_detail_123').fadeTo(200,0,function(){loadDetails('#pf_detail_123',url);});
                $('#pf_detail_view').fadeTo(200,0,function(){loadDetails('#pf_detail_view',url);});
                $('#client').fadeTo(200,0,function(){loadDetails('#client',url);});
                $('#texte').fadeTo(200,0,function(){loadDetails('#texte',url);});
                document.title = this.title;//+' - Réalisation de logos, plaquettes et sites internet dans L\'Hérault, le Gard, le Vaucluse et les Bouches-du-Rhône - '+X_SITE_NAME; //+X_SITE_TITLE+' - '
                return false;
            }
        });
    }
    // portfolio accueil
    if($('html')[0].className.search('c2') != -1){
        $('#vignettes li a').bind({
            click:function(){
                clearInterval(autoSlideshow);
                $('#vignettes li a.actif').removeClass('actif');
                this.className = 'actif';
                this.blur();
                pf_affiche = this.id.replace('pf_a_v_','');
                $('#visionneuse li').fadeTo(400,0);
                $('#pf_a_'+pf_affiche).fadeTo(400,1);
            }
        });
        $('#pf_precedent').bind({
            click:function(){
                clearInterval(autoSlideshow);
                this.blur();
                if(pf_affiche > 1){
                    pf_affiche--;
                    $('#visionneuse li').fadeTo(400,0);
                    $('#pf_a_'+pf_affiche).fadeTo(400,1);
                }
            }
        });
        $('#pf_suivant').bind({
            click:function(){
                clearInterval(autoSlideshow);
                this.blur();
                if(pf_affiche < $('#visionneuse li').length){
                    pf_affiche++;
                    $('#visionneuse li').fadeTo(400,0);
                    $('#pf_a_'+pf_affiche).fadeTo(400,1);
                }
            }
        });
        autoSlideshow = setInterval('nextSlide()', 3500);
    }
    // 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 //////////////////////////////////////////////////////////
// Liens externes fonction ouverture.
var openLink = function(){
    if(this.href.search(X_URL_SITE) == -1){
        window.open(this.href,'','');
        return false;
    }
}
// Liens externes
var openExternals = function(){
    $('#corps a').unbind('click',openLink);
    $('#corps a').bind('click',openLink);
}
// Créer la fausses popup
var X_popup = function(e){
    $('#corps').prepend('<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('-78-([0-9]+)\.php$');
    var m = re.exec(this.href);
    if(m != null) {
        var id_video = m[1];
        $('#X_popupTarget').load('/xml.video-78-'+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();
}
// Animations
function showSlide(id_cible, decalage){
    if(id_cible == undefined || decalage == undefined){
        return false;
    }
    else{
        $(id_cible).animate(
            {
                left:decalage+'px'
            }
            ,500
        );
    }
}
function initSlideDetail(){
    $('#pf_detail_123 li').live({
        click:function(){
            $('#pf_detail_123 li').removeClass('actif');
            this.className =    'actif';
            var nb_cible =      this.id.replace('v_detail','');
            var id_cible =      '#pf_detail';
            var decalage =      -1 * nb_cible * 400;
            this.firstChild.blur();
            showSlide(id_cible, decalage);
        }
    });
}
function loadDetails(id_cible, url){
    $(id_cible).load(url+' '+id_cible, function(){setTimeout(showDetails, 100, id_cible);openExternals();$(id_cible).children().unwrap();});
}
function showDetails(id_cible){
    $(id_cible).fadeTo(500,1, function(){initSlideDetail();});
}
function nextSlide(){
//window.status = pf_affiche;
    if(pf_affiche < $('#visionneuse li').length){
        pf_affiche++;
        $('#visionneuse li').fadeTo(400,0);
        $('#pf_a_'+pf_affiche).fadeTo(400,1);
    }
    else{
        pf_affiche=1;
        $('#visionneuse li').fadeTo(400,0);
        $('#pf_a_1').fadeTo(400,1);
    }
}

/*========================================*\
    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;
}
