//20100914
// Objet utilisateur
var loadACNow = typeof(window.loadAC) == "undefined";
if ( loadACNow )
{
    window.loadAC = 1;
function SynSuggests() {
    this.AddInput = AddInput;
    this.setChargementImmediat = setChargementImmediat;
    this.setChargementSurDelai = setChargementSurDelai;
    this.setChargementSurFocus = setChargementSurFocus;
    this.setChargementSurTouche = setChargementSurTouche;
    this.Start = Start;
}

// Fonction gestion chargement

function AddInput(Elt, className) {
    if ( Elt ) {
        GestionDom.debug("Ajout d'un element");
        TabSuggestElt.push(Elt);
        if ( typeof className != "undefined" )
            Elt.setAttribute("synsuggest", className);
    }
}

function setChargementImmediat(Val) {
    TabSuggestLancement["immediat"] = Val;
}

function setChargementSurDelai(Val) {
    TabSuggestLancement["delai"] = Val;
}

function setChargementSurFocus(Val) {
    TabSuggestLancement["surfocus"] = Val;
}

function setChargementSurTouche(Val) {
    TabSuggestLancement["surtouche"] = Val;
}

function Start() {
    GestionDom.DomReadySuggests(__Start);
}





// Variables globales diverses
RessourcesPath = "http://www.synomia-autocompletion.com/";

collection = new Array;
SynSuggestsParams = new Array;
var Main = null;
TabSuggestElt = new Array;
var TabSuggestLancement = new Array;
TabSuggestLancement["immediat"] = false;
TabSuggestLancement["delai"] = 0;
TabSuggestLancement["surfocus"] = false;
TabSuggestLancement["surtouche"] = false;






// Fonctions chargement

function SynInitFctSuggest() {
    Elt = document.getElementById("synTxtInput");
    if ( Elt )
        AddInput(Elt, "SynSuggestDefault");
    
};

function __Start() {
    for (j=0; j< TabSuggestElt.length; j++) {
        Elt = TabSuggestElt[j];
        if ( Elt ) {
            var Espace = "";
            if ( Elt.className != "" ) Espace = " "; 
            Elt.className += Espace + "SynSuggest";
            Elt.setAttribute("autocomplete","OFF");
        }
    }
    
    if ( TabSuggestLancement["immediat"] ) chargement_immediat();
    if ( TabSuggestLancement["surfocus"] ) chargement_onfocus();
    if ( TabSuggestLancement["surtouche"] ) chargement_onkeypress();
    if ( parseInt(TabSuggestLancement["delai"]) > 0 ) chargement_delai(parseInt(TabSuggestLancement["delai"]));
}

function debug_suggest(Ph) {
    //if ( window.console ) console.log(Ph);
};

var Cpt_Chargement = 0;
var ChargementTimer = null;
var Script_Data = false;
var Script_Lib = false;
var Script_CSS = false;
var Init_Fields = false;

function chargement(comm) {
    Ph = "Lancement ( "+comm+" )";
    GestionDom.debug(comm); 
    
    if ( ChargementTimer == null ) {
        ChargementTimer = chargement_engine();
    };
};

function chargement_engine() {
    if ( ChargementTimer != null ) { clearTimeout(ChargementTimer); ChargementTimer = null; };
    Cpt_Chargement++;
    Ph = "Mode auto => Essai "+Cpt_Chargement;
    debug_suggest(Ph);
        
    var ChargementNecessaire = ! ( Script_Data && Script_Lib && Script_CSS );

    if ( ChargementNecessaire && Cpt_Chargement <= 3) {
    
        debug_suggest("Necessite encore des ressources");
        // Rien pour le moment
        var Head = document.getElementsByTagName("head")[0];
        var Elt;
        
        if ( ! Script_Data ) {
            // on charge les donnees
            Elt = document.createElement("script");
            Elt.type = "text/javascript";
            Elt.src = RessourcesPath+"suggest_data.js.php?mid=a9b6bbc4ef1cbd66ad45d4f3de1fe8c0";
            Head.appendChild(Elt);
        }
        
        if ( ! Script_CSS  && Cpt_Chargement <= 1 ) {
            // on charge les styles
            Elt = document.createElement("link");
            Elt.type = "text/css";
            Elt.rel="stylesheet";
            Elt.href = RessourcesPath+"suggest.css.php?mid=a9b6bbc4ef1cbd66ad45d4f3de1fe8c0";
            Head.appendChild(Elt);
            Script_CSS = true;
        }
        
        
        if ( ! Script_Lib  && Cpt_Chargement <= 1 ) {
            // on charge la librairie suggest
            Elt = document.createElement("script");
            Elt.type = "text/javascript";
            Elt.src = RessourcesPath+"suggest_lib.js";
            Head.appendChild(Elt);
        }
        
        
        if ( ChargementTimer == null ) ChargementTimer = setTimeout("chargement_engine()", 1000);
    }
    else {
        debug_suggest("Toutes les ressources sont presentes");
    }
}


function chargement_immediat() {
    chargement("Chargement immediat");
};


function chargement_onfocus() {
    var inp;
    for ( i=0; i < TabSuggestElt.length; i++ ) {
        inp = TabSuggestElt[i];
        if (inp.addEventListener) {
            inp.addEventListener("focus", function() { chargement("Chargement sur focus"); }, false);
        } else {
            if (inp.attachEvent) {
                inp.attachEvent("onfocus", function() { chargement("Chargement sur focus"); });
            } else {
                if ( inp.onfocus && typeof inp.onfocus_tmp != 'function' ) eval("inp.onfocus_tmp = "+inp.onfocus.toString());
                inp.onfocus = function() {
                    if ( typeof this.onfocus_tmp == 'function' )
                        this.onfocus_tmp();
                    chargement("Chargement sur onfocus");
                    this.onfocus = this.onfocus_tmp;
                };
            }
        }
    }
};

var synTimer = null;
function chargement_delai(Val) {
    if ( Val == undefined ) Val = 5000;
    synTimer = setTimeout("chargement('Chargement sur delai')", Val);
};

function chargement_onkeypress() {
    var inp;
    for ( i=0; i < TabSuggestElt.length; i++ ) {
        inp = TabSuggestElt[i];
        if ( inp ) {
            if (inp.addEventListener) {
                inp.addEventListener("keydown", function() { chargement("Chargement sur onkeydown"); }, false);
            } else {
                if (inp.attachEvent) {
                    inp.attachEvent("onkeydown", function() { chargement("Chargement sur onkeydown"); });
                } else {
                    if ( inp.onkeydown && typeof inp.onkeydown_tmp != 'function' ) eval("inp.onkeydown_tmp = "+inp.onkeydown.toString());
                    inp.onkeydown = function() {
                        if ( typeof this.onkeydown_tmp == 'function' )
                            this.onkeydown_tmp();
                        chargement("Chargement sur onkeydown");
                    };
                    inp.onkeydown = function() { chargement("Chargement sur onkeydown"); };
                }
            }
        }
    }
    
};



function DomReadySuggests(fn)
{
    this.debug("DomReadySuggests : "+fn.name);
    this.TabFnDomReady.push(fn);
    if ( this.isReady ) {
        this.debug("DOM prete, on lance directement");
        this.__DomReadySuggests();
    }
    else {
        this.debug("DOM pas prete, on cree l'event");
        //W3C
        if(document.addEventListener)
        {
            document.addEventListener("DOMContentLoaded", function() { __DomReadySuggests_Callback(GestionDom, "__DomReadySuggests"); } , false);
        }
        //IE
        else
        {
            fn2 = document.onreadystatechange;
            if ( typeof fn2 == 'function' ) this.TabFnDomReady.unshift(fn2);
            document.onreadystatechange = function() { __DomReadySuggests_Callback(GestionDom, "readyStateSuggests"); };
        }
    }
}

//IE execute function
function readyStateSuggests()
{
    //dom is ready for interaction
    this.debug("readyStateSuggests");
    if(
        document.readyState == "complete"
        ||
        document.readyState == "interactive"
    )
    {
        this.__DomReadySuggests();
    }
}

function __DomReadySuggests() {
    this.debug("__DomReadySuggests");
    this.debug("Il y a "+this.TabFnDomReady.length+" fonctions a traiter");
    if(!this.isReady) { this.isReady = true; }
    for(i=0; i<this.TabFnDomReady.length; i++) {
        if ( this.TabFnDomReady[i] ) {
            this.debug("execution de la fonction "+(i+1)+" ("+this.TabFnDomReady[i].name+")");
            fn = this.TabFnDomReady[i];
            this.TabFnDomReady[i] = null;
            fn();
        }
    }
    this.TabFnDomReady = new Array;
    this.debug("trt domready ok");
}

function __DomReadySuggests_Callback(Classe, fn) {
    Classe[fn]();
}

if ( !window.GestionDom || typeof GestionDom != 'object' ) {
 
GestionDom = {};
// Fonctions interactions avec le browser
GestionDom.TabFnDomReady = new Array;
GestionDom.isReady = false;
GestionDom.DomReadySuggests = DomReadySuggests;
GestionDom.readyStateSuggests = readyStateSuggests; 
GestionDom.__DomReadySuggests = __DomReadySuggests;
GestionDom.debug = function(Val) {
    //if ( window.console ) console.log(Val);
};



if (    typeof document.getElementsByTagName != 'undefined'
    && typeof document.getElementById != 'undefined' 
    && ( document.getElementsByTagName('body')[0] != null
        || document.body != null ) ) {
    //GestionDom.isReady = true;
    GestionDom.isReady = false;
}

}

GestionDom.DomReadySuggests(SynInitFctSuggest);

SynSuggest = new SynSuggests();
}
if ( loadACNow ) { GestionDom.DomReadySuggests( function() {
// exemple qui prend tous les input dont le name vaut "q" ou "rechBandeau_mot"

// les associe pour l'utilisation des Suggest, et leur retire le focus 

var Elt=document.getElementsByTagName("input");

for (j=0; j< Elt.length; j++) {

    if ( Elt[j].name == "rechBandeau:mot" || Elt[j].name == "rechBandeau_mot" ) {

        SynSuggest.AddInput(Elt[j]);

        Elt[j].blur();

    }

};
SynSuggest.setChargementSurFocus(true);
SynSuggest.Start();
}); }
