//librairie de base pour dessiner (lignes, rectangles, trapèzes simples)
//ne nécessite pas d'autre librairie

//accès aux éléments HTML
if(!document.getElementById && document.all)
  document.getElementById = function(id) {
    return document.all[id];
  }
//teste si Internet Explorer
var ie = false; /*@cc_on ie = true; @*/
var firefox = (navigator.appName == 'Netscape');
var opera = (navigator.appName == "Opera");
var chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
var safari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;

function DomRemove(element)
{
 element.parentNode.removeChild(element);
}

// Test de la méthode de mesure 
document.writeln("<div id='metricTest' style='visibility: visible; font-size: 0px; margin: 0px; border-top: 10px solid blue; border-left:10px solid transparent; border-bottom: 20px solid red; width: 10px; height:40px'></div>");
document.writeln("<div id='metricTest2' style='visibility: visible; font-size: 0px; margin: 0px; border-top: 10px solid blue; border-left:10px solid yellow; border-bottom: 20px solid red; width: 0px; height:0px'></div>");
var innerMetric = (document.getElementById('metricTest').offsetHeight==70);
var minimumVerticalInterior = document.getElementById('metricTest2').offsetHeight-30;
DomRemove(document.getElementById("metricTest"));
DomRemove(document.getElementById("metricTest2"));

//constantes
var transparent = "transparent";
var CurrentZOrder = 0;
var support = document.body;
var clip_minx,clip_miny,clip_maxx,clip_maxy;
var clipping = false;

//***************** taille de la fenêtre et donc de la zone de dessin
function largeur_fenetre()
{
 var result=0;
 if (document.body&&document.body.clientWidth) result = document.body.clientWidth;
  else if (window.innerWidth) result = window.innerWidth;
 if (document.documentElement&&document.documentElement.clientWidth)
   if (document.documentElement.clientWidth>result)
   result = document.documentElement.clientWidth;
 return result;
}

function hauteur_fenetre()
{
 var result=0;
 if (document.body&&document.body.clientHeight) result = document.body.clientHeight;
 else if (window.innerHeight) result = window.innerHeight;
 if (document.documentElement&&document.documentElement.clientHeight)
   if (document.documentElement.clientHeight>result)
   result = document.documentElement.clientHeight;
 return result; 
}

function SetClipRect(x,y,x2,y2)
{
 clip_minx = x;
 clip_miny = y;
 clip_maxx = x2;
 clip_maxy = y2;
 clipping = true;
}

function NoClipping()
{
 clipping = false;
}

//****************** fonction de base de dessin ************************
function HorizLine(x,y,x2, c)
{
 if (clipping)
 {
  if (y<clip_miny||y>clip_maxy) return;
  if (x<clip_minx) x = clip_minx;
  if (x2>clip_maxx) x2 = clip_maxx;
 }
 if (x2<x) return;
 var div = document.createElement("div"), st=div.style;
 st.borderTop = "1px solid "+c;
 st.position = "absolute";
 st.left = x+"px";
 st.top = y+"px";
 st.width = (x2-x+1)+"px";
 st.zIndex = CurrentZOrder;
 support.appendChild(div);
}

function BidiHorizLine(x,y,x2, c)
{
 if (x>x2) HorizLine(x2,y,x, c); else HorizLine(x,y,x2, c);
}

function DrawRect(x,y,x2,y2, c)
{
 if (x>x2)
 {
  var x3 = x;
  x = x2;
  x2 = x3;
 }
 if (y>y2)
 {
  var y3 = y;
  y = y2;
  y2 = y3;
 }
 HorizLine(x,y,x2, c);
 HorizLine(x,y2,x2, c);
 VertLine(x,y,y2, c);
 VertLine(x2,y,y2, c);
}

function FillRect(x,y,x2,y2, c)
{
 if (clipping)
 {
  if (y<clip_miny) y = clip_miny;
  if (y2>clip_maxy) y2 = clip_maxy;
  if (x<clip_minx) x = clip_minx;
  if (x2>clip_maxx) x2 = clip_maxx;
 }
 if (x2<x||y2<y) return;
 if (y==y2) HorizLine(x,y,x2,c);
 else if (x==x2) VertLine(x,y,y2,c);
 else
 {
  var div = document.createElement("div"), st=div.style;
  st.fontSize = 0;
  st.position = "absolute";
  st.left = x+"px";
  st.top = y+"px";
  st.width = (x2-x+1)+"px";
  st.height = (y2-y+1)+"px";
  st.backgroundColor = c;
  st.zIndex = CurrentZOrder;
  support.appendChild(div);
 }
}

function PutImage(x,y, filename)
{
  var img = document.createElement("img");
  img.style.position = "absolute";
  img.style.left = x+"px";
  img.style.top = y+"px";
  img.src = filename;
  img.style.zIndex = CurrentZOrder;
  support.appendChild(img);
}

function RepeatImage(x,y,x2,y2, filename, position)
{
 var div = document.createElement("div"), st=div.style;
 div.className = "pngfix";
 st.fontSize = 0;
 st.position = "absolute";
 st.left = x+"px";
 st.top = y+"px";
 st.width = (x2-x+1)+"px";
 st.height = (y2-y+1)+"px";
 st.zIndex = CurrentZOrder;
 st.backgroundImage = "url("+filename+")";
 if (position) st.backgroundPosition = position;
 support.appendChild(div);
}

function RepeatImageRight(x,y,x2,y2, filename)
{
 RepeatImage(x,y,x2,y2, filename, "100% 0%");
}

function RepeatImageBottomRight(x,y,x2,y2, filename)
{
 var ty = y2-y+1;
 if (ty < minimumVerticalInterior) { y -= minimumVerticalInterior - ty; ty = minimumVerticalInterior; }
 
 RepeatImage(x,y,x2,y2, filename, "100% 100%");
}

function RepeatImageBottomLeft(x,y,x2,y2, filename)
{
 var ty = y2-y+1;
 if (ty < minimumVerticalInterior) { y -= minimumVerticalInterior - ty; ty = minimumVerticalInterior; }
 
 RepeatImage(x,y,x2,y2, filename, "0% 100%");
}

function TextOut(x,y,x2,y2, fs,c, text)
{
 if (x2<x||y2<y+minimumVerticalInterior) return;
 var div = document.createElement("div"), st=div.style;
 st.fontSize = fs;
 st.position = "absolute";
 st.left = x+"px";
 st.top = y+"px";
 st.width = (x2-x+1)+"px";
 st.height = (y2-y+1)+"px";
 st.color = c;
 st.zIndex = CurrentZOrder;
 div.innerHTML = "<center>"+text+"</center>";
 support.appendChild(div);
}

function TextOutEx(x,y,x2,y2, fs,fn,fc,bc, text)
{
 if (x2<x||y2<y+minimumVerticalInterior) return;
 var div = document.createElement("div"), st=div.style;
 st.fontFamily = fn;
 st.fontSize = fs;
 st.position = "absolute";
 st.left = x+"px";
 st.top = y+"px";
 st.width = (x2-x+1)+"px";
 st.height = (y2-y+1)+"px";
 st.color = fc;
 st.zIndex = CurrentZOrder;
 st.backgroundColor = bc;
 div.innerHTML = text;
 support.appendChild(div);
}

function VertLine(x,y,y2, c)
{
 if (clipping)
 {
  if (x<clip_minx||x>clip_maxx) return;
  if (y<clip_miny) y = clip_miny;
  if (y2>clip_maxy) y2 = clip_maxy;
 } 
 if (y==y2) 
 {
  HorizLine(x,y,x,c);
 } else
 {
  if (y2<y) return;
  var div = document.createElement("div"), st=div.style;
  st.borderLeft = "1px solid "+c;
  st.fontSize = 0;
  st.position = "absolute";
  st.left = x+"px";
  st.top = y+"px";
  st.height = (y2-y+1)+"px";
  st.zIndex = CurrentZOrder;
  support.appendChild(div);
 }
  
}
function BidiVertLine(x,y,y2, c)
{
 if (y>y2) VertLine(x,y2,y, c); else VertLine(x,y,y2, c);
}

function PutPixel(x,y,c)
{
 HorizLine(x,y,x,c);
}

/******************* tracé d'une ligne (assez lent en diagonale) ********************/
function sign(val)
{
 if (val >= 0) return 1;
 else return -1; 
}

//*********************** fonction relatives aux couleurs *************************

function htmlColor(color)
{
 return "#" + (color+0x1000000).toString(16).substring(1,7);
}

function rgb(r,g,b)
{
 r = Math.round(r);
 if (r<0) r = 0;
 if (r>255) r = 255;
 g = Math.round(g);
 if (g<0) g = 0;
 if (g>255) g = 255;
 b = Math.round(b);
 if (b<0) b = 0;
 if (b>255) b = 255;
 return htmlColor((r<<16) + (g<<8) + b);
} 

//**************** pour le dessin sur écran virtuel 

var ecran_invisible = null;
var ecran_en_cours = null;
var ecran_visible = null;
var ancien_support = null;

function NouvelEcranVirtuel()
{
 if (ecran_en_cours)
   alert("Il y a déjà un écran virtuel en cours de dessin.")
 else
 {
  ancien_support = support;
  var nouvel_ecran = document.createElement("div");
  nouvel_ecran.style.position = "absolute";
  nouvel_ecran.style.left = "0px";
  nouvel_ecran.style.top = "0px";
  nouvel_ecran.style.visibility = "hidden";
  document.body.appendChild(nouvel_ecran);
  support = nouvel_ecran;
  ecran_en_cours = nouvel_ecran;
 }
}

function AfficherEcranVirtuel()
{
 if (ecran_en_cours)
 {
  ecran_en_cours.style.visibility = "";
  if (ecran_invisible) 
  {
   document.body.removeChild(ecran_invisible);
   ecran_invisible = null;
  }
  if (ecran_visible)
  {
   ecran_invisible = ecran_visible;
   ecran_invisible.style.visibility = "hidden";
   ecran_visible = null;
  }
  ecran_visible = ecran_en_cours;
  ecran_en_cours = null;
  support = ancien_support;
 }
 else
  alert("Il faut créer l'écran virtuel avant de l'afficher.")
}

function CommencerLayer()
{
 ancien_support = support;
 var layer = document.createElement("div");
 layer.style.position = "absolute";
 layer.style.left = "0px";
 layer.style.top = "0px";
 document.body.appendChild(layer);
 support = layer;
}

function ModifierLayer(layer)
{
 ancien_support = support;
 support = layer;
}

function FinirLayer()
{
 var layer = support;
 support = ancien_support;
 return layer;
}

function AnnulerLayer()
{
 var layer = support;
 support = ancien_support;
 document.body.removeChild(layer);
}

function SupprimerLayer(layer)
{
 document.body.removeChild(layer);
}

