HH=22;

function insertAfter(parent, node, referenceNode) {
    parent.insertBefore(node, referenceNode.nextSibling);
}

function backgr() {
  n=Math.floor(Math.random()*liste.length);
  f=liste[n];
  var n=f.indexOf("-");
  this.hex='#'+f.substring(0,n);
  this.url="url(R/"+f+")";
}

function setdiv(thed,b) {
  //alert("f "+f+" hex "+hex);

  //alert("coucou");
  thed.style.backgroundColor=b.hex;
  thed.style.backgroundImage=b.url;
  thed.style.backgroundRepeat='repeat';
}


function paragraphe(d,lmax) {
    var thenewdiv=document.createElement("div");
 d.appendChild(thenewdiv);
 thenewdiv.innerHTML+="<img src=\"trans.gif\" height=\"22\" width=\""+lmax+"\">";
}

function paragrapheblanc(d,lmax) {
    var thenewdiv=document.createElement("div");
 d.appendChild(thenewdiv);
 thenewdiv.innerHTML+="<img src=\"blanc.gif\" height=\"22\" width=\""+lmax+"\">";
}



function interligne(d,lmax) {
    var thenewdiv=document.createElement("div");
 d.appendChild(thenewdiv);
 thenewdiv.innerHTML+="<img src=\"trans.gif\" height=\"2\" width=\""+lmax+"\">";
}

function interligneblanc(d,lmax) {
    var thenewdiv=document.createElement("div");
 d.appendChild(thenewdiv);
 thenewdiv.innerHTML+="<img src=\"blanc.gif\" height=\"2\" width=\""+lmax+"\">";
}


var next_word_html;

function next_word_length(str,cc) {
    var l=0;
    var strl=str.length;
    var i;
    var n;
    next_word_html="";
    next_word_i=0
    for(i=cc;i<strl;i++) {
	var c=str.charAt(i);
	n=str.charCodeAt(i);
	if (n==32) {
	    var ii=i+1;
	    if (ii<strl) {
		nn=str.charAt(ii);
		nnc=str.charCodeAt(ii);
		if (nn==":" || nn==";" || nn=="!" || nn=="?") {
		    l+=taille[n]+taille[nnc];
		    //	    next_word_html+="<img src=\"B/"+n+".png\" width=\""+tn+"\ height=\""+HH+"\ alt=\""+c+"\">" ;
		    next_word_html+=insertpng("B/"+n+".png",tn,HH,"alt=\""+c+"\"");
		    //	    next_word_html+="<img src=\"B/"+nnc+".png\" width=\""+tnn+"\ height=\""+HH+"\" alt=\""+nn+"\">" ;
		    next_word_html+=insertpng("B/"+nnc+".png",tnn,HH,"alt=\""+nn+"\"");
		    next_word_i=ii;
		    return l;
		}
	    }
	    next_word_i=i;
	    return l;
	}
	//	alert (str.charAt(i)+" "+n+" "+taille[n]+" "+l);
	l+=taille[n];
	//	next_word_html+="<img src=\"B/"+n+".png\" width=\""+taille[n]+"\ height=\""+HH+"\" alt=\""+c+"\">" ;
	next_word_html+=insertpng("B/"+n+".png",taille[n],HH,"alt=\""+c+"\"");
    }
    next_word_i=i;
    return l;
}
	    


function mprint(str,d,lmax,indent,pre,post,padding,start) {
    var thenewdiv=document.createElement("span");
    var l=start;
    d.appendChild(thenewdiv);
    var startline=true;
    var strl=str.length;
    var i=0;
    var res=""
    wl=0;
    for(i=0;i<strl;i++) {
	n=str.charCodeAt(i);
	if (startline) {
	    if (n==32) continue;
	    else {
		if (indent>0) res+="<img src=\"trans.gif\" height=\"22px\" width=\""+indent+"px\">";
		l+=indent;
		//		res+="<img src=\"B/32.png\" height=\"22px\" width=\"2px\">";
		res+=insertpng("B/32.png",2,HH,"");
		l+=2;
		startline=false;
	    }
	}
	if (wl==0) wl=next_word_length(str,i);
	//	alert(str.substring(i,strl)+" "+wl);
	if ((l+wl)>lmax) {
	    lrest=lmax-l;
	    if (lrest>0 && padding)
		//res+="<img src=\"B/32.png\" height=\"22px\" width=\""+lrest+"\">";
		res+=insertpng("B/32.png",lrest,HH,"");
	    startline=true;
	    l=0; i--;
	} else {
	    res+=next_word_html;
	    next_word_html="";
	    l+=wl;
	    wl=0;
	    i=next_word_i;
	    n=str.charCodeAt(i);tn=taille[n];
	    if (n==32) {	    
		if ((l+tn)<lmax) {
		    //		    res+="<img src=\"B/"+n+".png\" width=\""+tn+"\ height=\""+HH+"\">" ;
		    res+=insertpng("B/"+n+".png",tn,HH,"alt=\" \"");
		    l+=tn;
		} else {
		    lrest=lmax-l;
		    if (lrest>0 && padding) // res+="<img src=\"B/32.png\" height=\"22px\" width=\""+lrest+"\">";
			res+=insertpng("B/32.png",lrest,HH,"");
		    startline=true;l=0;
		}
	    }
	}
    }
    lrest=lmax-l;
    if (lrest>0 && padding) { //	res+="<img src=\"B/32.png\" height=\"22px\" width=\""+lrest+"\">";
	res+=insertpng("B/32.png",lrest,HH,"");
	l=lmax;
    }
    thenewdiv.innerHTML+=pre+res+post;
    return l;
}

	
function insertpng(png,w,h,alt) {
    var strNewHTML="";
  /*@cc_on
    @if (@_jscript_version >= 5.5 && @_win32) 
    strNewHTML = "<span  style=\" width:" + w + "px; height:" + h + "px; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + png + "', sizingMethod='scale');\"></span>" ;
    return strNewHTML;
  @end @*/
    return "<img src=\""+png+"\" height=\""+h+"\" width=\""+w+"px\" "+alt+">";
}

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 or higher.
{
  /*@cc_on
@if (@_jscript_version >= 5.5 && @_win32)
//  return;
//  if (document.all) {
    for(var i=0; i<document.images.length; i++)  {
      var img = document.images[i];
      var imgName = img.src.toUpperCase();
      if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
	var imgID = (img.id) ? "id='" + img.id + "' " : "";
	var imgClass = (img.className) ? "class='" + img.className + "' " : "";
	var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
	var imgStyle = "display:inline-block;" + img.style.cssText ;
	if (img.align == "left") imgStyle = "float:left;" + imgStyle;
	if (img.align == "right") imgStyle = "float:right;" + imgStyle;
	if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
	//var name="name=imgStyle + "src:"+img.src+";";
	var strNewHTML = "<span " + imgID + "name=\""+ img.src +"\" "+ imgClass + imgTitle
	  + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
	  + "(src=\'" + img.src + "\', sizingMethod='Scale');\"></span>" ;
	img.outerHTML = strNewHTML;
	i = i-1;
      }
    }
//  }
  @end @*/
}

