var urlBase = "http://195.68.18.133/";

var selectClientsFlagUp = 0;
var selectClientsFlagDown = 10;

var blocClient = '';
function selectClients(tabClient,select,pathBase)
{
    blocClient='';
    var cpt = 0;
    var end = false;
    
    for (i=0; i<tabClient["nom"].length; i++)
    {
        if (tabClient["idSecteur"][i]==select.value || select.value==0)
        {
            if (cpt%5==0){blocClient += '<div class="ligneDeClients"><div class="client_separateur"></div>';}
            blocClient += '<div class="client">';
            blocClient += '<h2 style="visibility:visible;">'+tabClient["nom"][i]+'</h2>';
            blocClient += '<img src="'+pathBase+'images_client/'+tabClient["idClient"][i]+'.jpg" alt="'+tabClient["nom"][i]+'" />';
            blocClient += '</div>';
            blocClient += '<div class="client_separateur"></div>';
            if (cpt%5==4 || i==((tabClient["nom"].length)-1)){blocClient += '</div>'; end=true;} else {end=false;}
            cpt++;
        }
    }
    if (end==false){blocClient += '</div>';}
    if (blocClient==''){blocClient = 'Aucun client dans ce secteur';}
    new Effect.Opacity($('clientList'), {duration:0.5, fps:24, from:1.0, to:0.0});
    window.setTimeout(selectClientsEffect,500);
}

function selectClientsEffect()
{
  $('clientList').innerHTML = blocClient;
  new Effect.Opacity($('clientList'), {duration:0.5, fps:24, from:0.0, to:1.0});
}


//précharge des images (utilisé pour les "rolls on")
//apellé lors d'un évenement "onload" du body
//data = tableau d'image à précharger
//path = le chemin vers la racine

function preload(data,path)
{
    for (var i=0; i<data.length; i++)
    {
        var loader = new Array();
        loader[i] = new Image;
        loader[i].src = path+data[i];
    }
}

//gére la prise de focus et la perte du focus sur certains input
//permet de réafficher la valeur par défaut si le champs est vide
//et de vider le champs lors de la prise de focus
//apellé lors d'un évenement "onblur" ou "onfocus" de l'input
//i = objet html input
//text = valeur par défaut

function inputTextonfocus(i,text){if(i.value==text){i.value='';}}
function inputTextonblur(i,text){if(i.value==''){i.value=text;}}

var bloc = '';

/*******************************************************************************
  getProblematiqueList
*******************************************************************************/

var getProblematiqueListFlagUp = 0;
var getProblematiqueListFlagDown = 10;
var menu=true;
var previous=true;
var next=true;

function getProblematiqueList(langue,changeornot)
{
    var idSecteur = $("cbSecteur").value;
    var idMetier = $("cbMetier").value;
    
    //var type = $("cbType").value;
    var type = 'indifferent';
    
    //url
    var url = 'http://www.antea-ingenierie.fr/getData_problematique.php?idMetier=';
    url += idMetier+'&idSecteur='+idSecteur+'&type='+type+'&langue='+langue;

    //récupérer les données
    var ajax = new Ajax.Request(url, {
    onSuccess: function(transport) 
    {
        bloc = '';
  
        if (changeornot=='yes')
        {
            getProblematiqueListFlagUp = 0;
            getProblematiqueListFlagDown = 10;
        }
        
        if (window.navigator.appName == 'Microsoft Internet Explorer'){var problematiques = transport.responseXML.childNodes[1];}
        else {var problematiques = transport.responseXML.childNodes[0];}

        var nbProblematique = problematiques.childNodes.length;

        if (nbProblematique>0)
        {
            //alert(nbProblematique);
            //alert(getProblematiqueListFlagUp);
            //alert(getProblematiqueListFlagDown);
            
            temp = getProblematiqueListFlagDown;
            
            if (nbProblematique<getProblematiqueListFlagDown) 
            {
                //alert('pg');
                getProblematiqueListFlagDown = nbProblematique;
            }
        
            for(i=getProblematiqueListFlagUp; i<getProblematiqueListFlagDown; i++)
            {
                var question = problematiques.childNodes[i].getElementsByTagName('question')[0].childNodes[0].nodeValue;
                var link = problematiques.childNodes[i].getElementsByTagName('link')[0].childNodes[0].nodeValue;
                bloc += '<li><a href="'+link+'">'+question+'</a></li>';
            }
          
           
            $('milieu').innerHTML = '';
            limit = Math.ceil(nbProblematique/10);
            
            for (i=1; i<limit+1; i++)
            {
                if (i==Math.ceil(getProblematiqueListFlagUp/10)+1){a = ' style="font-weight:bold;" '} else {a = "";}
                $('milieu').innerHTML += '<a '+a+' href="javascript:getProblematiqueListPage(\''+langue+'\','+i+')">'+i+'</a>';
            }
          
       
        }
        
        else
        {
            bloc += 'Aucune problématique ne correspond à votre tri, réessayez en élargissant votre recherche';
            if (menu){menu=false; new Effect.Opacity($('menu_navigation'),{duration:0.1, fps:24, from:1.0, to:0.0});}
        }

        getProblematiqueListFlagDown = temp;
        
        new Effect.Opacity($('problematique_list'),{duration:0.3, fps:24, from:1.0, to:0.0});
        window.setTimeout(getProblematiqueListEffect,500);
        
         if (nbProblematique<10 && menu)
        {
          //new Effect.Opacity($('menu_navigation'),{duration:0.3, fps:24, from:1.0, to:0.0});
          menu = false;
        }
        else if (!menu){menu=true; //new Effect.Opacity($('menu_navigation'),{duration:0.1, fps:24, from:0.0, to:1.0});
        }
        
        if (getProblematiqueListFlagUp==0 && previous)
        {
            //$('previous').style.cursor="auto";
            previous = false;
        }
        else if (!previous){previous=true; //$('previous').style.cursor="pointer";
        }
        
        if (nbProblematique<getProblematiqueListFlagDown && next)
        {
            //new Effect.Opacity($('next'),{duration:0.3, fps:24, from:1.0, to:0.0});
            next = false;
        }
        else if (!next){next=true; //new Effect.Opacity($('next'),{duration:0.1, fps:24, from:0.0, to:1.0});
        }
        
    }}); //fin onSuccess
}

function getProblematiqueListEffect()
{
    $('problematique_list').innerHTML = bloc;
    new Effect.Opacity($('problematique_list'),{duration:0.3, fps:24, from:0.0, to:1.0});
}

function getProblematiqueListPage(langue,page)
{
    getProblematiqueListFlagUp = (page-1)*10 ;
    getProblematiqueListFlagDown = ((page-1)*10)+10;
    getProblematiqueList(langue,'not');
}

function getProblematiqueListPrevious(langue)
{
    getProblematiqueListFlagUp = getProblematiqueListFlagUp - 10;
    getProblematiqueListFlagDown = getProblematiqueListFlagDown - 10;
    getProblematiqueList(langue,'not');
}

function getProblematiqueListNext(langue)
{
    getProblematiqueListFlagUp = getProblematiqueListFlagUp + 10;
    getProblematiqueListFlagDown = getProblematiqueListFlagDown + 10;
    getProblematiqueList(langue,'not');
}

/*******************************************************************************
  getRealsiationList
*******************************************************************************/

var getRealisationListFlagUp = 0;
var getRealisationFlagDown = 4;
var trisave =  'normal';

function getRealisationList(tri,langue)
{
    trisave = tri;
    
    //url
    var url = 'http://www.antea-ingenierie.fr/getData_realisation.php?tri='+tri+'&langue='+langue;

    //récupérer les données
    var ajax = new Ajax.Request(url, {
    onSuccess: function(transport) 
    {
        bloc = '';
        
        if (window.navigator.appName == 'Microsoft Internet Explorer'){var realisations = transport.responseXML.childNodes[1];}
        else {var realisations = transport.responseXML.childNodes[0];}

        var nbRealisations = realisations.childNodes.length;
        
          if (nbRealisations<getRealisationFlagDown) 
          {
                getRealisationFlagDown = nbRealisations;
          }
        
        
        for(i=getRealisationListFlagUp; i<getRealisationFlagDown; i++)
        {
            var descriptif = realisations.childNodes[i].getElementsByTagName('descriptif')[0].childNodes[0].nodeValue;
            var link = realisations.childNodes[i].getElementsByTagName('link')[0].childNodes[0].nodeValue;
            var linkPhoto = realisations.childNodes[i].getElementsByTagName('linkPhoto')[0].childNodes[0].nodeValue;
            var idMetier = realisations.childNodes[i].getElementsByTagName('idMetier')[0].childNodes[0].nodeValue;
            var linkImageMetier = realisations.childNodes[i].getElementsByTagName('linkImageMetier')[0].childNodes[0].nodeValue;
            var titre = realisations.childNodes[i].getElementsByTagName('titre')[0].childNodes[0].nodeValue;
            var problematique = realisations.childNodes[i].getElementsByTagName('problematique')[0].childNodes[0].nodeValue;
            var annee = realisations.childNodes[i].getElementsByTagName('annee')[0].childNodes[0].nodeValue;
            var pays = realisations.childNodes[i].getElementsByTagName('pays')[0].childNodes[0].nodeValue;
            var secteur = realisations.childNodes[i].getElementsByTagName('secteur')[0].childNodes[0].nodeValue;

            bloc += '<div class="realisationsListe_out">';
            bloc += '<div class="realisationsListe_in">';
            bloc += '<p class="right">'+descriptif;
            bloc += '<a href="'+link+'" class="bt_detail_fr"></a>';
            bloc += '</p>';
            bloc += '<div class="realisation">';
            
            bloc += '<div class="floatLeft"><a href="'+link+'"><img class="photo" src="'+linkPhoto+'" alt="" /></a></div>';
            bloc += '<h2 style="visibility:visible;"><a href="'+link+'"><span class="metier'+idMetier+'"><img src="'+linkImageMetier+'" alt=""/>'+titre+'</a></span></h2>';
            bloc += '<ul>';
            bloc += '<li>'+problematique+'</li>';
            bloc += '<li>'+annee+'   '+pays+'</li>';
            bloc += '<li><b>'+secteur+'</b></li>';
            bloc += '<ul></div></div></div><br />';
        }
        
            $('milieu').innerHTML = '';
            limit = Math.ceil(nbRealisations/4);
            
            for (i=1; i<limit+1; i++)
            {
                if (i==Math.ceil(getRealisationListFlagUp/4)+1){a = ' style="font-weight:bold;" '} else {a = "";}
                $('milieu').innerHTML += '<a '+a+' href="javascript:getRealisationListPage(\''+langue+'\','+i+')">'+i+'</a>';
            }  
        
      
    new Effect.Opacity($('realisation_list'),{duration:0.3, fps:24, from:1.0, to:0.0});
    window.setTimeout(getRealisationListEffect,500);

    }});
}

function getRealisationListEffect()
{
  $('realisation_list').innerHTML = bloc;
  new Effect.Opacity($('realisation_list'),{duration:0.3, fps:24, from:0.0, to:1.0});
}

function getRealisationListPage(langue,page)
{
    getRealisationListFlagUp = (page-1)*4 ;
    getRealisationFlagDown = ((page-1)*4)+4;
    getRealisationList(trisave,langue);
}

function getRealisationListPrevious(langue)
{
    getRealisationListFlagUp = getRealisationListFlagUp - 4;
    getRealisationFlagDown = getRealisationFlagDown - 4;
    getRealisationList(trisave,langue);
}

function getRealisationListNext(langue)
{
    getRealisationListFlagUp = getRealisationListFlagUp + 4;
    getRealisationFlagDown = getRealisationFlagDown + 4;
    getRealisationList(trisave,langue);
}