// ***********************************************
// JavaScript Document
// *****


//******************************************//
var gLINK        = ""; // selected link menu
var gCATEG       = ""; // project categorie
var gPROJ        = ""; // project list filter


// ***********************************************
// get element by id
// **********************************************
function getEl(_id){
  //***
  var o = window.document.getElementById(_id);
  if (o != null){
    return o;
  } else {
    return null; //no prob : the null case is not treated here !
    //alert("ERREUR : The element <" + _id + "> does not exist.");
  }
}


// ***********************************************
// load data file
// **********************************************
function load(_file) {

    // ****
    var request;
    
      // ****
    if (window.XMLHttpRequest) { // Firefox + IE7 !
    	request = new XMLHttpRequest();
      }
    else if (window.ActiveXObject) { // IE < 7
      request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
   	  alert("Votre navigateur ne peut afficher correctement ce site..."); 
    	return; // Non supporte
    }	
    
    // ****
    request.open('GET', _file, false); // Synchro
    request.send(null);
    
    // ****
    return request.responseText;

}



// ***********************************************
// fonctions switch selected link in the menu
// **********************************************
function hoverSwitch(){

    // ****
    //var o1 = getEl('link'+gCATEG);
    
    // ****
    //alert("gLINK.id : " + gLINK.id);
    if (gLINK.id != null) { //index page : no selected link
      var o1 = getEl(gLINK.id);
      //
      if (o1.className == '' ) {
        o1.className = 'selectedItem';
      } else {
        o1.className = '';
      };
    }
}


// ***********************************************
// fonctions affichage des boites superposees
// **********************************************
function showit(_id){
  //alert("_id : " + _id);
  //alert("gSWAP : " + gSWAP);
  // ****
  var o1 = window.document.getElementById(gSWAP);
  var o2 = window.document.getElementById(_id);

  // ****
  if( (o1 != null) && (o2 != null) ){
    // ****
    o1.style.display = "none";
    o2.style.display = "block";
    // ****
    gSWAP = _id;
  }  
}



// ***********************************************
// fonctions get / post
// **********************************************
function HTTP(){
    // ****
    this._http = new Object();

    // *******************************************************
    // **** PRIVATES
    // *******************************************************
    this.parse = function(_o){
      // ****
      // ****
      var rValue = "";

      // ****
      var key = "";
      var tmp = new Array();
      var s   = "";

      // ****
      for(key in _o){
        // ****
        if( _o.propertyIsEnumerable(key) ){
          // ****
          tmp.push(key + "=" + _o[key]);
        }
      }

      // ****
      s = tmp.join("\n");
      
      // ****
      alert(s);
    }
    
    // **** Query string to Object
    this.qsSplit = function(_qs){
      // ****
      var rValue = new Object();

      // ****
      var http       = new Object();
      var keysValues = null;
      var keyValue   = null;
      var i, iMax;

      // ****
      keysValues = _qs.split("&");
      iMax = keysValues.length;

      // ****
      for(i = 0; i < iMax; i++){
        // ****
        keyValue = keysValues[i].split("=");

        // ****
        http[keyValue[0]] = decodeURI(keyValue[1]);
      }

      // ****
      rValue = (iMax > 0) ? http : rValue;

      // ****
      return rValue;
    }

    // **** Object to query string
    this.qsMerge = function(_o){
      // ****
      var rValue = "";

      // ****
      var key = "";
      var tmp = new Array();

      // ****
      for(key in _o){
        // ****
        if( _o.propertyIsEnumerable(key) ){
          // ****
          tmp.push(key + "=" + encodeURI(_o[key]));
        }
      }

      // ****
      rValue = (tmp.length > 0) ? tmp.join("&") : rValue;

      // ****
      return rValue;
    }
    
    // ****
    this.prepare  = function(){
      // ****

      var qs = window.document.location.search;

      // ****
      this._http = ( qs.indexOf("?") != -1 ) ? this.qsSplit(qs.substr(1)) : new Object();
    }

    // ****
    this.init = function(){
      // ****
      this.prepare();
    }

    // ****
    this.init();

    // *******************************************************
    // **** PUBLICS
    // *******************************************************
    // ****
    this.Get = function(_key){
      // ****
      var rValue = undefined;

      // ****
      if( _key in this._http ){
        // ****
        rValue = this._http[_key];
      }
      
      // ****
      return rValue;
    }
    
    // ****
    this.set = function(_o, _key, _value){
      // ****
      var hasKey = (_key.replace(/(^\s*)|(\s*$)/g, "") != "");
      var href, qs;

      // ****
      if(hasKey && ("a" == _o.tagName.toLowerCase()) ){
        // ****
        href = _o.getAttribute("href");

        // ****
        if(href == null){
          // ****
          href = window.document.location.href.split("?", 2);
          href[1] = "";
        }
        else
        {
          // ****
          href     = href.split("?", 2);
          qs       = this.qsSplit(href[1]);
          qs[_key] = _value;
          href[1]  = this.qsMerge(qs);
          href     = href.join("?");

          // ****
          _o.setAttribute("href", href);
        }
      }
    }

    // ****
    this.Set = function(_type, _key, _value){
      // ****
      var type     = (_type === null) ? "null" : typeof(_type);
      var o        = null
      var c        = null;
      var id       = "";
      var href     = "";
      var qs       = "";
      var setToAll = (arguments[3] === true);
      var i, iMax;

      // ****
      switch(type){

        case "null" :
          // ****
          c = setToAll ? window.document.getElementsByTagName("a") : c;
          break;
          
        case "string" :
          // ****
          id = _type;
          o  = window.document.getElementById(id);
          break;

       case "object" :
          // ****
          o = _type;
          break;

        default :
          o = _type;
          // ****
          break;
      }

      // ****
      if( (o != null) || (c != null) ){
        // ****
        if( c != null )
        {
          // ****
          iMax = c.length;

          // ****
          for(i = 0; i < iMax; i++){
            // ****
            o = c[i];

            // ****
            this.set(o, _key, _value);
          }
        }
        else
        {
          // ****
          this.set(o, _key, _value);
        }
      }
    }
  }




// ***********************************************
// fonctions special caracters
// **********************************************    
var tableau= new Array();  // tableau global
//tableau['“']= "&quot;" ;
tableau['“']= "*" ;
tableau['’']= "*" ;
//tableau['"']= "&quot;" ;
tableau["^"]= "&#136;" ;
//tableau["<"]= "&lt;" ;
tableau["~"]= "&#152;" ;
//tableau[">"]= "&gt;" ;
tableau["¡"]= "&iexcl;" ;
tableau["¢"]= "&cent;" ;
tableau["£"]= "&pound;" ;
tableau["¤"]= "&curren;" ;
tableau["¥"]= "&yen" ;
tableau["¦"]= "&brvbar;" ;
tableau["§"]= "&sect;" ;
tableau["¨"]= "&uml;" ;
tableau["©"]= "&copy;" ;
tableau["ª"]= "&ordf;" ;
tableau["«"]= "&laquo;" ;
tableau["¬"]= "&not;" ;
tableau["­"]= "&shy;" ;
tableau["®"]= "&reg;" ;
tableau["¯"]= "&masr;" ;
tableau["°"]= "&deg;" ;
tableau["±"]= "&plusmn;" ;
tableau["²"]= "&sup2;" ;
tableau["³"]= "&sup3;" ;
tableau["´"]= "&acute;" ;
tableau["µ"]= "&micro;" ;
tableau["¶"]= "&para;" ;
tableau["·"]= "&middot;" ;
tableau["¸"]= "&cedil;" ;
tableau["¹"]= "&sup1;" ;
tableau["º"]= "&ordm;" ;
tableau["»"]= "&raquo;" ;
tableau["¼"]= "&frac14;" ;
tableau["½"]= "&frac12;" ;
tableau["¾"]= "&frac34;" ;
tableau["¿"]= "&iquest;" ;
tableau["À"]= "&Agrave;" ;
tableau["Á"]= "&Aacute;" ;
tableau["Â"]= "&Acirc;" ;
tableau["Ã"]= "&Atilde;" ;
tableau["Ä"]= "&Auml;" ;
tableau["Å"]= "&Aring;" ;
tableau["Æ"]= "&Aelig" ;
tableau["Ç"]= "&Ccedil;" ;
tableau["È"]= "&Egrave;" ;
tableau["É"]= "&Eacute;" ;
tableau["Ê"]= "&Ecirc;" ;
tableau["Ë"]= "&Euml;" ;
tableau["Ì"]= "&Igrave;" ;
tableau["Í"]= "&Iacute;" ;
tableau["Î"]= "&Icirc;" ;
tableau["Ï"]= "&Iuml;" ;
tableau["Ð"]= "&eth;" ;
tableau["Ñ"]= "&Ntilde;" ;
tableau["Ò"]= "&Ograve;" ;
tableau["Ó"]= "&Oacute;" ;
tableau["Ô"]= "&Ocirc;" ;
tableau["Õ"]= "&Otilde;" ;
tableau["Ö"]= "&Ouml;" ;
tableau["×"]= "&times;" ;
tableau["Ø"]= "&Oslash;" ;
tableau["Ù"]= "&Ugrave;" ;
tableau["Ú"]= "&Uacute;" ;
tableau["Û"]= "&Ucirc;" ;
tableau["Ü"]= "&Uuml;" ;
tableau["Ý"]= "&Yacute;" ;
tableau["Þ"]= "&thorn;" ;
tableau["ß"]= "&szlig;" ;
tableau["à"]= "&agrave;" ;
tableau["á"]= "&aacute;" ;
tableau["â"]= "&acirc;" ;
tableau["ã"]= "&atilde;" ;
tableau["ä"]= "&auml;" ;
tableau["å"]= "&aring;" ;
tableau["æ"]= "&aelig;" ;
tableau["ç"]= "&ccedil;" ;
tableau["è"]= "&egrave;" ;
tableau["é"]= "&eacute;" ;
tableau["ê"]= "&ecirc;" ;
tableau["ë"]= "&euml;" ;
tableau["ì"]= "&igrave;" ;
tableau["í"]= "&iacute;" ;
tableau["î"]= "&icirc;" ;
tableau["ï"]= "&iuml;" ;
tableau["ð"]= "&eth;" ;
tableau["ñ"]= "&ntilde;" ;
tableau["ò"]= "&ograve;" ;
tableau["ó"]= "&oacute;" ;
tableau["ô"]= "&ocirc;" ;
tableau["õ"]= "&otilde;" ;
tableau["ö"]= "&ouml;" ;
tableau["÷"]= "&divide;" ;
tableau["ø"]= "&oslash;" ;
tableau["ù"]= "&ugrave;" ;
tableau["ú"]= "&uacute;" ;
tableau["û"]= "&ucirc;" ;
tableau["ü"]= "&uuml;" ;
tableau["ý"]= "&yacute;" ;
tableau["þ"]= "&thorn;" ;
tableau["ÿ"]= "&yuml;" ;






// ***********************************************
// fonctions convert text
// **********************************************
function convert(_text){

    // ***
    var converted_text="";
    
    // ***
    alert('_text:'+_text);
    //alert('_text.length:'+_text.length);
    

    // ***
    // ***
    // ***

    for (i=0;i<_text.length;i++){
      //match
      //if(tableau[_text.charAt(i)]!=undefined){
      if(_text.charAt(i)=='’'){
        alert('match:'+tableau[_text.charAt(i)]);
        //converted_text+=tableau[_text.charAt(i)];
        converted_text+=tableau['’'];
      }else{
        converted_text+=_text.charAt(i);
      }
    }

    // ****
//    alert('converted_text:'+converted_text);
    return converted_text;     
}

//http://christiansarda.free.fr/VB/js_intro.html






// ***********************************************
// global vars
// **********************************************

// data / params
var gPARAMFILE   = "projects/parameters.txt"; // data file name
var gWARNING     = "";
var gEMAIL       = "";
var gTEL1        = "";
var gTEL2        = "";

// html tags
var gCONTACTID     = "__contact__";
//...
// ***********************************************




// ***********************************************
// parse data file into variables
// **********************************************
function loadParam(_folder) {

    // ****
    c=new Array();

    // ****
    if(_folder == null) _folder=''; // default value
    e=load(_folder+gPARAMFILE);

	a=e.split('\n');
    //alert ('load ok ?' + a);
    	
    gWARNING=a[0].substr(8,3); // yes or no
    gEMAIL=a[1];
    gTEL1=a[2];
    if(a[3]){ // 2eme telephone ou pas ?
      gTEL2=a[3];
    }else{
      gTEL2='';
    };
    
    
    // **** id
    this.contact = window.document.getElementById(gCONTACTID);
    
    // **** Data
    //alert(gWARNING);
    gEMAIL='<a href="mailto:'+gEMAIL.toLowerCase()+'">'+gEMAIL+'</a>'
    this.contact.innerHTML = gEMAIL+'<BR />'+gTEL1+'<BR />'+gTEL2;
    
}
