var Moz = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1)

function ClassPage(z, Plan, Defil) {
	var S, Me
	this.Cle = 0  // n° de la fiche actuelle
	this.TPos = {}   // Tableau des pages chargées avec mémo des positions
	this.Z = LID(z)  	// Page
	this.Plan		= Plan
	this.Defil	= Defil;  Defil.Page = this
	this.TBtn   = []
	this.hz   // Hauteur zone
	this.hp   // Hauteur page
	this.no	= 0
	this.nb   // nb pages
	this.delta = 0		// si !Defil, le scroll se fait sur body et non volet2
	this.hmax
	Me = this

	if (this.Defil) {
		this.DivScroll = this.Z
		if (Moz) S='DOMMouseScroll';  else  S='mousewheel';   // 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) { Me.Move(-delta*50) }
			if (e.preventDefault)  e.preventDefault();
			//e.returnValue = false;
		})
	}else{
		this.DivScroll = (ie) ? document.documentElement : document.body
	}
}

ClassPage.prototype.CalcPage = function() {
  var re, Y
	Y = -ze(Page.Z.style.top)
	re = ze((Y / Page.hmax)+0.75);		//alert(Y+', '+Page.hmax+crr+pa)
	return re
}

ClassPage.prototype.PosPage = function(n) { this.no = n;  this.Z.style.top = -(n-1) * this.hmax+'px'; }
ClassPage.prototype.Pos = function(n) { this.Z.style.top = -n +'px';  PointePage() }    // reste à tester (jamais utilisé)

ClassPage.prototype.Move = function(delta, anim) {
	var c, Y
	Y = ze(this.Z.style.top)
	if (!anim) {
		Y = Y-delta
		if (Y>0)  return;			//alert(this.Z.style.top+crr+delta+crr+Y)
		$(this.Z).css({top:Y+'px'});  PointePage()
	}else{
		if (-Y+delta<0)  delta = Y
		if (delta<0) { c='+';  delta*=-1 }else  c='-'
		$(this.Z).animate({top:c+'='+delta+'px'}, "slow", function() {
			PointePage()
		});
	}
}

ClassPage.prototype.AffPos = function() {
	var P = this, Y = ze(this.Z.style.top)
	P.no = this.CalcPage()
	if (P.TBtn.length) {
		var i, D, bup, bdn
		for (i in P.TBtn) {
			D = P.TBtn[i]
			bup = $('.btnDefilUp',D).get(0);  bdn = $('.btnDefilDn',D).get(0)
			bup.style.visibility = (Y)?'visible':'hidden'
			bdn.style.visibility = (P.nb>1 && P.no<P.nb-1)?'visible':'hidden'
		}
		//wh(P.no+','+P.nb)
	}
}

ClassPage.prototype.addBtn = function(ids) { var D;  if ((D=LID(ids)))  this.TBtn.push(D) }

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.TPos[this.Cle]))  this.PosPage(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()
		this.Plan.PointePlan()
	}
	if (this.Defil)  this.Defil.PointePage()
	this.AffPos()
}


// --------------------    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, re=null, hy, h, i, li, L
	hy = Page.Z.scrollTop
  if ((i=Page.Cle))  Page.TPos[i] = hy
	T = $('#'+this.Z.id+' li.Sel').get();  for (i in T)  T[i].className=''
	T = $('#'+this.Z.id+' li').get()
	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 = $('#volet3h li.Sel').get();  for (i in T)  T[i].className=''
	li.className = 'Sel'
}


// --------------------    DEFIL    ------------------------------------
function ClassDefil(id) {
	this.id = id
	this.Z	= LID(id)   // Zone des n° de pages
	var Me	= this
	this.Z.OE = this
	var O = document.createElement("input");  O.setAttribute("type", "range")
	if (O.type!='text')  this.Slider = O;  else  AttachEvent(this.Z, 'click', function(e) { Me.Change(e)	})
}

ClassDefil.prototype.Change = function(e) {
	var S, n, Page = this.Page, O = EventCtrl(e)
	n = Page.CalcPage()
	if (this.Slider) {
		n = O.value
	}else{
	  switch(S) {
		  case '>':  n++;  break
		  case '<':  n--;  break
		  default:  if (!isNaN(S))  n = ze(LText(O))-1;  else  return
	  }
	}
  Page.PosPage(n)
	PointePage()
}

ClassDefil.prototype.Move = function(delta) { this.Page.Move(delta*Page.hmax,1) }

ClassDefil.prototype.Write = function() {
	var Me = this, H='', i, Page = this.Page, O
	if (Page.nb<2) { Hide(this.Z);  return }  else  Aff(this.Z)
	for (i=1; i<Page.nb+1; i++)  H+='<li>'+i+'</li>'
	if (this.Slider) {
		//this.Z.innerHTML = "<input type=range min=1 max="+Page.nb+" value=1 style='width:500px'>"
		O = this.Z.appendChild(this.Slider);  PX(O,'min',1);  PX(O,'max',Page.nb);  O.value = 1; 	O.style.width = '400px'
		if ((H=$E("input[type=range]",this.Z)))  AttachEvent(H, 'change', function(e) { Me.Change(e)	})
	}else
		this.Z.innerHTML = '<ul><li><</li>'+H+'<li>></li></ul>'
}

ClassDefil.prototype.PointePage = function() {
  var T, pa, Page = this.Page, no
	pa = Page.CalcPage()
	if (this.Slider) {
		this.Slider.value = pa+1
	}else{
		T = $('#'+this.Z.id+' li.Sel').get();  for (i in T)  T[i].className=''
		T = $('#'+this.Z.id+' li').get();  if (T && 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()
	Page.AffPos()
}

