/*

UN GROS MERCI À SYLVESTER ET À SON PACKAGE 
POUR CALCULER LES MATRICES / VECTEURS 
voir source complete et API : http://sylvester.jcoglan.com/

La partie animation a été réalisé par Yves Larocque
www.solutions-zen.com

*/
function fade_in(id,op,max) {      
   if (element = document.getElementById(id)) {         
       if (op>max) op=max;
       if (navigator.appName == "Microsoft Internet Explorer")
         element.style.filter = "alpha(opacity=" + op + ")";          
       else {
         element.style.opacity = (op / 100);
         element.style.MozOpacity = (op / 100); 
       }
       if (op<max) setTimeout('fade_in("'+id+'",'+(op+10)+','+max+');',75);      
       if (op==0) element.style.visibility = "visible";
   }
}

function fade_out(id,op,min) {
   if (element = document.getElementById(id)) {         
       if (op<min) op=min;                 
       if (navigator.appName == "Microsoft Internet Explorer")
         element.style.filter = "alpha(opacity="+op+")";
       else {
         element.style.opacity = (op / 100);                     
         element.style.MozOpacity = (op / 100);    
       }
       if (op>min) setTimeout('fade_out("'+id+'",'+(op-25)+','+min+');',65); 
       if (op==0) element.style.visibility = "hidden";      
   }
}
   
var MBezier = $M([
  [-1,  3, -3,  1],
  [ 3, -6,  3,  0],
  [-3,  3,  0,  0],
  [ 1,  0,  0,  0]
]);


var Matrices = Array();
var zListe = Array();
var pListe = Array();


function anime_lettre(t,id) {
    if (t > 1) t = 1;
    if (t <= 1) {                       
        var Vt = $V([ t*t*t,  t*t,  t, 1]);
            
        var x = Vt.dot(Matrices[id].col(1));
        var y = Vt.dot(Matrices[id].col(2));
        var z = Vt.dot(Matrices[id].col(3));       
        
        if (element = document.getElementById(id)) {
        
            element.style.left = x+"px";
            element.style.top = y+"px";
            element.style.width = (0.48*Math.round(z))+"px";
            element.style.height = (0.6*Math.round(z))+"px";
            if (t < 1) {
               t += 0.08;      
               setTimeout('anime_lettre('+t+',"'+id+'");',50);
            }
        }
    }             
}


function TraceMsg(id,msg,zAvg,zVariance,effet,e0,ordre) {           
    
    var longueur = msg.length;
    
    var dist = (zAvg*(0.48))*(longueur);
    var zMin = zAvg-10;// - (zVariance*0.6);
    var zMax = zAvg+10;// + (zVariance*0.6);
    
    document.getElementById(id).style.width = (dist)+"px";
    document.getElementById(id).style.height = "65px";//zMax+"px";
    
    p1 = Array(4,                    8+(Math.random()*4),      zMin+(Math.random()*30));
    p2 = Array((dist*0.30),             14-(Math.random()*10),     (zMax+(zVariance*4)));
    p3 = Array((dist*0.60),             4-(Math.random()*10),     (zMin-(zVariance*4)));                                   
    p4 = Array(dist-(zAvg*(0.48))-6, 8+(Math.random()*4),     zMin+(Math.random()*20));
    
    var MPoints = $M([p1,p2,p3,p4]);  

    var MCompile = MBezier.x(MPoints);
    var t = 0; 
    var ang = e0*Math.PI;
    var t_inc = 1/(longueur-1);
    var a_inc = (Math.PI)/(longueur-1);
    var curseur = 0;
    
    while (curseur < longueur) {                       
        var Vt = $V([ t*t*t,  t*t,  t, 1]);
            
        var x = Vt.dot(MCompile.col(1));
        var y = Vt.dot(MCompile.col(2));
        var z = Vt.dot(MCompile.col(3));            
        
        var id_lettre = id+'_'+curseur;
        var dist_origine = 80+(Math.random()*80)
        
        
        switch (effet) {
            case "left" : 
                pl1 = Array(-4*(Math.random()*zMax), -zMax+(Math.random()*zMax*2) ,10+(Math.random()*zMin));
                pl2 = Array((dist/3)-(Math.random()*(dist/2)),  70-(Math.random()*160),         40+(Math.random()*(zMax*2)));
                pl3 = Array((dist/2)+(Math.random()*(dist/3)), 65-(Math.random()*145),         30+(Math.random()*(zMin*2)));
                break;
            case "right" : 
                pl1 = Array(dist+(4*(Math.random()*zMax)), -zMax+(Math.random()*zMax*2) ,10+(Math.random()*zMin));
                pl2 = Array((3*dist/4)+(Math.random()*(dist/3)),  70-(Math.random()*160),         40+(Math.random()*(zMax*2)));
                pl3 = Array((dist/2)+(Math.random()*(dist/4)), 65-(Math.random()*145),         30+(Math.random()*(zMin*2)));
                break;
            case "top" : 
                pl1 = Array(x, -3*(Math.random()*zMax) ,10+(Math.random()*zMin));
                pl2 = Array(x+30-(Math.random()*60),  65-(Math.random()*95),         40+(Math.random()*(zMax*2)));
                pl3 = Array(x+30-(Math.random()*60), 70-(Math.random()*160),         30+(Math.random()*(zMin*2)));
                break;
            case "bottom" : 
                pl1 = Array(x, zMax+(3*(Math.random()*zMax)) ,10+(Math.random()*zMin));
                pl2 = Array(x+40-(Math.random()*60),  70-(Math.random()*160),         40+(Math.random()*(zMax*2)));
                pl3 = Array(x+40-(Math.random()*60), 65-(Math.random()*95),         30+(Math.random()*(zMin*2)));
                break;
            case "angle" : 
                pl1 = Array(x+(Math.cos(ang)*dist_origine),    y-(Math.sin(ang)*dist_origine),   10+(Math.random()*zMin));
                pl2 = Array((dist/4)-(Math.random()*(dist/3)),  70-(Math.random()*160),         40+(Math.random()*(zMax*2)));
                pl3 = Array((3*dist/4)+(Math.random()*(dist/3)), 65-(Math.random()*145),         30+(Math.random()*(zMin*2)));
                break;                    
        }
        
                                           
        pl4 = Array(x,                                 y,                                z);
        
        var MPointsL = $M([pl1,pl2,pl3,pl4]);  

        Matrices[id_lettre] = MBezier.x(MPointsL);
        
        ang += a_inc;
                        
        lettre = msg.charCodeAt(curseur);    
        if (lettre != 32) { // 32 est le code de l'espace             
           
            document.getElementById(id).innerHTML += '<img id="'+id_lettre+'" class="lettre" src="lettres/'+lettre+'.gif" style="left: '+Math.round(pl1[0])+'px; top: '+Math.round(pl1[1])+'px; width:'+(0.48*Math.round(pl1[2]))+'px;  height:'+(0.6*Math.round(pl1[2]))+'px;">';
            
            if (ordre == "seq") setTimeout("fade_in('"+id_lettre+"',0,100); anime_lettre(0,'"+id_lettre+"');",100+(curseur*100));
            if (ordre == "has") setTimeout("fade_in('"+id_lettre+"',0,100); anime_lettre(0,'"+id_lettre+"');",100+(Math.random()*(longueur*100)));
           
            
        }
        curseur++;               
        t += t_inc;            
    }            
}  


/* ------------------  Fonction de base pour capture / drag de la souris..  ------ */
var nowX = 0;
var nowY = 0;

var sourisX1 = 0;
var sourisY1 = 0;
var objX1 = 0;
var objY1 = 0;
var initialise = false;

function getMouseXY(e) {
   var posX = 0;
   var posY = 0;
   var e = (!e) ? window.event : e;
   if (e.pageX || e.pageY) {
      posX = e.pageX;
      posY = e.pageY;
   }
   else if (e.clientX || e.clientY) {
      if (document.body.scrollLeft || document.body.scrollTop) {
         posX = e.clientX + document.body.scrollLeft;
         posY = e.clientY + document.body.scrollTop;
      }
      else {
         posX = e.clientX + document.documentElement.scrollLeft;
         posY = e.clientY + document.documentElement.scrollTop;
      }
   }
   nowX = posX;
   nowY = posY;
}

function drag(obj) {  
    /* initialise les positions relatives au début du drag */
    sourisX1 = nowX;
    sourisY1 = nowY;
    objX1 = obj.offsetLeft;
    objY1 = obj.offsetTop; 
    /* redéfinis la fonction temporairement (jusqu'à "onmouseup") */
    document.onmousemove = function(e) {
        getMouseXY(e);
        newX = (objX1-sourisX1+nowX);
        newY = (objY1-sourisY1+nowY);
        obj.style.left = newX+"px";            
        obj.style.top = newY+"px";            
    }    
}

function drag_page(obj) {  
    /* initialise les positions relatives au début du drag */
    sourisX1 = nowX;    
    objX1 = obj.offsetLeft;
    last_pos = objX1;
    /* redéfinis la fonction temporairement (jusqu'à "onmouseup") */
    document.onmousemove = function(e) {
        getMouseXY(e);
        newX = (objX1-sourisX1+nowX);        
        
        if (newX < 1) newX = 1;
        if (newX > 199) newX = 199;
        obj.style.left = newX+"px"; 
        
        nb_pages = listes[last_liste].length;
        new_page = Math.floor((newX/200)*nb_pages);
        obj.innerHTML = ""+(new_page+1);
        
    }    
}



/* onmousemove appelle toujours getMouseXY, */
/* pour s'assurer que lorsqu'on commence un drag, on a une valeur de nowX et nowY valide */

document.onmousemove = function(e) { getMouseXY(e); }

/* ------------------  Fin des fonctions de capture de la souris..  ------ */





/********************* fonctions pour les dimensions de la fenêtre *************************************/

/* ces 2 fonctions marchent pour IE et FireFox pour aider à positionner au centre */ 
/* Tout ca parce que IE ne prend pas l'attribut CSS 'position : fixed' fonctionnant sous firefox, évidemment */
function getWindowHeight() {
 var windowHeight = 0;
   if (typeof(window.innerHeight) == 'number') { windowHeight = window.innerHeight; }
    else {
      if (document.body && document.body.clientHeight) { windowHeight = document.body.clientHeight; }
    else {
      if (document.documentElement && document.documentElement.clientHeight) {
        windowHeight = document.documentElement.clientHeight;
      }
    }
   }
 return windowHeight;
}
function getWindowWidth() {
 var windowWidth = 0;
   if (typeof(window.innerWidth) == 'number') { windowWidth = window.innerWidth; }
        else {
          if (document.body && document.body.clientWidth) {  windowWidth = document.body.clientWidth; }
        else {
          if (document.documentElement && document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;
          }
        }
   }
 return windowWidth;
}

/* ------------------  essayer de garder la page plus grande que 800 de large ! ------ */

var dimOk = true;

function TestDimensionsMinimum() {
/*    var w = getWindowWidth();
    if (w < 776) {
         document.getElementById('sz_oups_msg').innerHTML = "<br />Présentement:&nbsp;<b>"+(w+24)+"</b>&nbsp;pixels."; 
         if (dimOk) {            
            element = document.getElementById('sz_oups');
            element.style.display = "block";                
            document.getElementById('sz_filtre_bg').style.display = "block";             
               
            sz_CentrerElement(element);
            
            sz_Filtre_Fade(50);             
         }
         dimOk = false;
    }
    else dimOk = true;
    
    if (dimOk) {
        if (document.getElementById('sz_oups').style.display == "block") sz_Fermer('sz_oups');    
    }
  */        
}
window.onresize = function() {TestDimensionsMinimum();};  

/* ****************************************************************************** */
var last_contenu = -1;

function ChangeContenu(num) {     
  
  if (last_contenu != num) { 
    
    if (navigator.appName == "Microsoft Internet Explorer") {
      
        document.getElementById('contenu'+num).style.visibility = "visible";
        if (last_contenu != -1) 
            document.getElementById('contenu'+last_contenu).style.visibility = "hidden";
    }
    else { /* Effet de fondu Seulement sur FIREFOX, hehe */
        
        setTimeout("fade_in('contenu"+num+"',0,100)",360);
        if (last_contenu != -1) 
            fade_out('contenu'+last_contenu,100,0);                                 
    }
    
    last_contenu = num;
  }      

}        

/* ------------------  fonctions utiles aux pop-ups ! ------ */
function AfficheLogin() {

    element = document.getElementById('sz_login');
    element.style.display = "block";                
    document.getElementById('sz_filtre_bg').style.display = "block";             
    
    document.body.style.overflow = "hidden";       
    sz_CentrerElement(element);
    
    sz_Filtre_Fade(50);             
    
    document.getElementById('sz_form_login').pwd.focus();     
}


function sz_Filtre_Fade(gray) {
    document.getElementById('sz_filtre_bg').style.backgroundColor = "#"+gray+""+gray+""+gray;        
    if (gray > 15)
        setTimeout('sz_Filtre_Fade('+(gray-2)+');',60);
}

    
function sz_Fermer(id) {
    document.getElementById(id).style.display = "none";    
    document.getElementById('sz_filtre_bg').style.display = "none"; 
    document.getElementById('sz_filtre_bg').onclick = "";
    document.body.style.overflow = "auto"; 
    
    TestDimensionsMinimum();
    window.onresize = function() {TestDimensionsMinimum();};      
}

function sz_CentrerElement(element) {
    var h = getWindowHeight();
    var w = getWindowWidth();
    if (element.scrollHeight < h)            
        element.style.top = (document.body.scrollTop + Math.ceil((h-element.scrollHeight)/3))+"px";
    else 
        element.style.top = '0px';
        
    if (element.scrollWidth < w)            
        element.style.left = (document.body.scrollLeft + Math.ceil((w-element.scrollWidth)/2))+"px"; 
    else
        element.style.left = '0px';
    
    if (h < 940) h = 940;    
    if (w < 1430) w = 1430;
    if  (document.body.style.overflow == "hidden") {
        document.getElementById('sz_filtre_bg').style.height = h+"px";
        document.getElementById('sz_filtre_bg').style.width = w+"px";
    }
    else {        
        if (image = document.getElementById('sz_grosse')) {   
            imageH = image.scrollHeight;     
            imageW = image.scrollWidth;    
            if (h < imageH) h = imageH;
            if (w < imageW) w = imageW;
        }
        document.getElementById('sz_filtre_bg').style.height = h+"px";
        document.getElementById('sz_filtre_bg').style.width = w+"px";  
    }
         
    window.onresize = function() {
        var h = getWindowHeight();
        var w = getWindowWidth();
        if (element.scrollHeight < h)            
            element.style.top = (document.body.scrollTop + Math.ceil((h-element.scrollHeight)/3))+"px";
        else 
            element.style.top = '0px';
            
        if (element.scrollWidth < w)            
            element.style.left = (document.body.scrollLeft + Math.ceil((w-element.scrollWidth)/2))+"px"; 
        else
            element.style.left = '0px';
        
        if (h < 940) h = 940;    
        if (w < 1430) w = 1430;
        if  (document.body.style.overflow == "hidden") {
            document.getElementById('sz_filtre_bg').style.height = h+"px";
            document.getElementById('sz_filtre_bg').style.width = w+"px";
        }
        else {        
            if (image = document.getElementById('sz_grosse')) {   
                imageH = image.scrollHeight;     
                imageW = image.scrollWidth;    
                if (h < imageH) h = imageH;
                if (w < imageW) w = imageW;
            }
            document.getElementById('sz_filtre_bg').style.height = h+"px";
            document.getElementById('sz_filtre_bg').style.width = w+"px";  
        }
                
        
        TestDimensionsMinimum();
        

    };      
}


/* ------------------  Fin des fonctions de pup up..  ------ */
