var Moz = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1)

function ClassPage(z, Plan, Defil) {
	var S
	this.Cle = 0  // n° de la fiche actuelle
	this.T = {}   // Tableau des pages chargées avec mémo des positions
	this.Z = LID(z)  	// Page
	this.Plan	= Plan
	this.Defil = Defil
	this.hz   // Hauteur zone
	this.hp   // Hauteur page
	this.nb   // nb pages
	this.delta = 0		// si !Defil, le scroll se fait sur body et non volet2
	this.hmax

	if (this.Defil) {
		this.DivScroll = this.Z
		if (Moz) S='DOMMouseScroll';  else  S='mousewheel';   // http://www.toutjavascript.com/forums/index.php?topic=15148

		AttachEvent(this.Z, S, function(e) {
			var delta = 0;
			if (!e)  e = window.event;
			if (e.wheelDelta) { /* IE/Opera. */
				delta = e.wheelDelta/120;
			}else if (e.detail) { /** Mozilla case. */
				delta = -e.detail/3;
			}
			if (delta) { LID('volet2').scrollTop -= delta*50;  PointePage() }
		  //alert(delta+crr+LID('volet2').scrollTop)
			if (e.preventDefault)  e.preventDefault();
			//e.returnValue = false;
		})
	}else{
		this.DivScroll = (ie) ? document.documentElement : document.body
	}
}

ClassPage.prototype.Pos = function(h) {  this.Z.scrollTop = h }

ClassPage.prototype.onLoad = function(Cle, Titre) {
	var st, hz
	if (this.Defil) {
		this.Cle = Cle
		hz = this.hz;  this.hmax = hz - 10;  if (!hz) { Hide(this.Defil.Z);  return }
		st = this.Z.style;  st.height = '';
		this.hp = this.Z.offsetHeight
		this.nb = ze(this.hp / hz) + 1
		st.height = hz + 'px';  st.overflow = 'hidden'
		this.Defil.Write()
		if ((h=this.T[this.Cle]))  this.Pos(h);		//if (h)  alert(i+', '+h+crr+JSW(Pages)+crr+JSW(Pages[i]))
	}

	if (this.Plan) {
		var Plan, i, nb=0
		Plan = this.Plan
		Plan.Init(Titre)
		for (var i=1; i<6; i++) {
		  if (nb>=Plan.nbNiv)  break
			Plan.CreeRS(i)
			if (Plan.Fils)  nb++;
		}
		Plan.Write()
		Page.Plan.PointePlan()
	}
	if (this.Defil)  Page.Defil.PointePage()
}


// --------------------    PLAN    ------------------------------------
function ClassPlan(z) {
	this.Z = LID(z)   // Destination du plan
	this.RS = {}
	this.cl1
	this.Fils
	this.Titre
	if (LX(z,'nbNiv'))  this.nbNiv = ze(LX(z,'nbNiv'));  else  this.nbNiv = 3

	AttachEvent(this.Z, 'click', function(e) {
		var O = EventCtrl(e)
		if (O.tagName=='SPAN')  O=O.parentNode    // li de la tabmat
		Page.Plan.Clic(O)
	})
}

ClassPlan.prototype.Init = function(Titre) { delete(this.RS);  this.RS = {};  this.cl1 = 1;  this.Fils = null;  this.Titre = Titre;  this.Z.innerHTML = '' }

ClassPlan.prototype.Write = function() {
	var D, R, ul, li, sp, i
	D = document.createElement('div');  this.Z.appendChild(D);  D.innerHTML = this.Titre
	this.WritePlan(this.Fils, this.Z)
}

ClassPlan.prototype.WritePlan = function(Fils, Z) {
	var D, R, ul, li, sp, i
	ul = document.createElement('ul');  Z.appendChild(ul);
	for (i in Fils) {
	  R = this.RS[Fils[i]]
		li = document.createElement('li');  ul.appendChild(li);  sp = document.createElement('span');  li.appendChild(sp);  sp.innerHTML = R.Titre
		PX(li,'offset',R.h)
		if (R.Fils)  this.WritePlan(R.Fils, li)
	}
}

ClassPlan.prototype.CreeRS = function(no) {
	var T, thf, O, i, ul, li, kp=0, h, TFi=[], Fi = this.Fils, cl1
	T = LTags(Page.Z,'H'+no)
	for (i=0; i<T.length; i++) {
	  cl1 = this.cl1
		O = T[i];  h = O.offsetTop
		if (Fi)  kp = this.RechNod(Fi, no, h);
		thf = { cl1:cl1, clp:kp, Titre:O.innerHTML, niv:no, h:h, O:O };  //O.innerHTML += '--'+thf.h
		if (kp)  this.RS[kp].Fils.push(cl1)
		this.RS[cl1] = thf;		//alert(JSW(thf)+crr+JSW(this.RS))
		TFi.push(cl1)
		this.cl1++;
	}
	if (!Fi && TFi.length)  this.Fils = TFi
}

ClassPlan.prototype.RechNod = function(Fi, no, h) {
	var i, i0, R, re=0
	for (i in Fi) {
		R = this.RS[Fi[i]]
		if (no>R.niv) {
			if (R.h>h) { re = i0;  break; }
			i0 = Fi[i]
		}
	}
	if (!re)  re = i0
	if (re) {
	  R = this.RS[re]
	  if (R.Fils) {
			i = this.RechNod(R.Fils, no, h);  if (i)  re = i
		}else{
			R.Fils = []
		}
	}
	return re
}

ClassPlan.prototype.PointePlan = function() {
	var T, pa, re=null, hy, h, i, li, L
	hy = Page.Z.scrollTop;  pa = ze((hy / Page.hmax)+0.75);
  if ((i=Page.Cle))  Page.T[i] = hy
	T = $G('#'+this.Z.id+' li.Sel');  for (i in T)  T[i].className=''
	T = $G('#'+this.Z.id+' li')
	L = T.length
	for (i in T) {
	  li = T[i];  h = ze(LX(li,'offset'))
		if (h>hy+50) { if (i>0) re = i-1; }
		else if (i==L-1)  re = i
		if (re!=null) { li = T[re];  li.className='Sel';  break }
	}
}

ClassPlan.prototype.Clic = function(li) {
	var T, i
	Page.DivScroll.scrollTop = ze(LX(li,'offset')) - 10 + Page.delta
	PointePage()
	T = $G('#volet3h li.Sel');  for (i in T)  T[i].className=''
	li.className = 'Sel'
}


// --------------------    DEFIL    ------------------------------------
function ClassDefil(z) {
	this.Z = LID(z)   // Zone des n° de pages
	//this.Z.attachEvent('onclick', )
	AttachEvent(this.Z, 'click', function(e) {
		var S, n, O = EventCtrl(e)
		n = ze(Page.Z.scrollTop/Page.hmax);  S = LText(O);  //alert(n+cr+S)
	  switch(S) {
		  case '>':  n++;  break
		  case '<':  n--;  break
		  default:  if (!isNaN(S))  n = ze(S)-1;  else  return
	  }
		Page.Z.scrollTop = n * Page.hmax
		PointePage()
	})
}

ClassDefil.prototype.Move = function(delta) {
	var n
	n = ze(Page.Z.scrollTop/Page.hmax) + delta
	Page.Z.scrollTop = n * Page.hmax
	PointePage()
}

ClassDefil.prototype.Write = function() {
	var H='', i
	if (Page.nb<2) { Hide(this.Z);  return }  else  Aff(this.Z)
	for (i=1; i<Page.nb+1; i++)  H+='<li>'+i+'</li>'
	this.Z.innerHTML = '<ul><li><</li>'+H+'<li>></li></ul>'
}

ClassDefil.prototype.PointePage = function() {
  var T, pa
	pa = ze((Page.Z.scrollTop / Page.hmax)+0.75)
	T = $G('#'+this.Z.id+' li.Sel');  for (i in T)  T[i].className=''
	T = $G('#'+this.Z.id+' li');  if (T.length && T.length>pa+1)  T[pa+1].className = 'Sel'
}



// --------------------    LEXIQUE    ------------------------------------
function ClassLex(Zone, DivLex) {
	var O, T
	this.Z = LID(Zone)
	O = $G("P:contains([Tableau des définitions])",this.Z,true);  //alert(Lout(O))
	T = ElemNext(O);  if (!T)  return;
	Hide(O);  Hide(T)
  this.TDef		= T
  this.DivLex = LID(DivLex)
}

ClassLex.prototype.Aff = function(A) {
	var O, v2 = LID('volet2'), D, TR, li
	O = ElemNext(A);
	if (!(O && O.className=='LexicDef')) {
		O = null
		for (i=0; i<this.TDef.rows.length; i++) {
			TR = this.TDef.rows[i]
			li = LText(TR.cells[0])
			if (Trim(li.toLowerCase())==Trim(LText(A)).toLowerCase() && TR.cells.length>1) {
				O = TR.cells[1]
				break
			}
		}
	}
	if (O) {
	  D = this.DivLex
		if (A.offsetHeight<28) {
			D.style.top = A.offsetTop - v2.scrollTop + CalcPos(v2,'Top') + A.offsetHeight;  D.style.left = A.offsetLeft + CalcPos(v2,'Left') - 75;
		}else{
			D.style.top = Y+10;
			D.style.left = X-80;
		}
		D.innerHTML = O.innerHTML  //X+', '+Y
	  Aff(D)
	  setTimeout(function(){ Lexic.HideLex() },800)
	}
}

ClassLex.prototype.HideLex = function() {
	if (ContainsXY(this.DivLex,0,0,20))  setTimeout(function(){ Lexic.HideLex() },800)
	else  Hide(this.DivLex)
}

// --------------------    SS-PGM    ------------------------------------
function PointePage() {
	if (Page.Plan)  Page.Plan.PointePlan()
	if (Page.Defil)  Page.Defil.PointePage()
}
