var isDOM=document.getElementById?1:0;
var isIE=document.all?1:0;
var isNS4=navigator.appName=='Netscape'&&!isDOM?1:0;
var isIE4=isIE&&!isDOM?1:0;
var isOp=window.opera?1:0;
var isWin=navigator.platform.indexOf('Win')!=-1?1:0;
var isDyn=isDOM||isIE||isNS4;

var CSSmode=document.compatMode;
CSSmode=(CSSmode&&CSSmode.indexOf('CSS')!=-1)||isDOM&&!isIE||isOp?1:0;

if (!window.page)
 var page = { win: window, minW: 0, minH: 0, MS: isIE&&!isOp, db: CSSmode?'documentElement':'body' }

page.winW=function()
 { with (this) return Math.max(minW, MS?win.document[db].clientWidth:win.innerWidth) }
page.winH=function()
 { with (this) return Math.max(minH, MS?win.document[db].clientHeight:win.innerHeight) }
page.scrollY=function()
 { with (this) return MS?win.document[db].scrollTop:win.pageYOffset }

function _winW(){
 with (this) return Math.max(minW, MS?win.document[db].clientWidth:win.innerWidth)
}
function winH(){
 with (this) return Math.max(minH, MS?win.document[db].clientHeight:win.innerHeight)
}
function _scrollY(){
 with (this) return MS?win.document[db].scrollTop:win.pageYOffset
}

function _object(id, par) {
 par=!par?document:(par.navigator?par.document:par);
 return isIE ? par.all[id] :
  (isDOM ? (par.getElementById?par:par.ownerDocument).getElementById(id) :
  (isNS4 ? par.layers[id] : null));
}

function _style(id, par) {
 var r=_object(id, par);
 return r?(isNS4?r:r.style):null;
}

function _layer(id,par) {
 this.ref=_object(id, par);
 this.sty=_style(id, par);
 this.x=function(){var a=arguments,p=a[0],px=isNS4||isOp?0:"px";with (this) {if (!isNaN(p)) sty.left=p+px; else return parseInt(sty.left);}};
 this.y=function(){var a=arguments,p=a[0],px=isNS4||isOp?0:"px";with (this) {if (!isNaN(p)) sty.top=p+px; else return parseInt(sty.top);}};
 //this.w=function (){var a=arguments,p=a[0],px=isNS4||isOp?0:"px";with (this) {if (p) (isNS4?sty.clip:sty).width=p+px; else return (isNS4?ref.document.width:ref.offsetWidth);}};
 this.w=function (){var a=arguments,p=a[0],px=isNS4||isOp?0:"px";with (this) {if (p) (isNS4?sty.clip:sty).width=p+px; else return (isNS4?sty.clip.width:ref.offsetWidth);}};
 this.h=function (){var a=arguments,p=a[0],px=isNS4||isOp?0:"px";with (this) {if (p)(isNS4?sty.clip:sty).height=p+px;else return (isNS4?ref.document.height:ref.offsetHeight);}};
 this.vis=function (){var a=arguments,p=a[0],px=isNS4||isOp?0:"px";with (this) {sty.visibility=p;}};
 this.clip=function (){var a=arguments,p=a[0],px=isNS4||isOp?0:"px";with (this) {if (isNS4) with(sty.clip){left=a[0];top=a[1];right=a[2];bottom=a[3]} else sty.clip="rect("+a[1]+"px "+a[2]+"px "+a[3]+"px "+a[0]+"px)";}};
 return this;
}

function getLyr(id, par) { return new _layer(id, par); }

function _scrollTo(pos, isStick) { with (this)
 {
  if (!isDyn || !loaded) return;
  cTop = Math.max(pos,0);
  if (!isStick) stickTop = cTop;
  divHeight = div.h();
  if (divHeight == 0) divHeight = 1;
  if (checkBounds)
  {
   if (cTop + cHeight > divHeight) cTop = divHeight - cHeight;
   if (cTop < 0) cTop = 0;
  }
  div.y(orgTop-cTop);
  div.clip(0, cTop, div.w(), cTop + cHeight);
  thmHeight = Math.ceil(barHeight * (cHeight / div.h()));
  if (thmHeight < minThmHeight) thmHeight = minThmHeight;
  if (thmHeight > barHeight) thmHeight = barHeight;
  thmHeight=24;
  thm.h(thmHeight);
  if (activeScr || isStick) return;
  var fracDivDown = (cTop / (divHeight - cHeight));
  thm.y(bar.y() + fracDivDown * (barHeight - thmHeight));
 }
}


function _scrollBy(amount) { with (this)
{
 scrollTo(cTop + amount);
}}


function _setScroll(newSpeed, steps) { with (this)
{
 if (!loaded) return;
 stepsLeft = steps;
 if (timer) clearInterval(timer);
 timer = setInterval('with (' + myName + ') { ' +
  'if (stepsLeft > 0) { ySpeed += ' + ((newSpeed-ySpeed)/steps) + '; stepsLeft-- } ' +
  'else if (parseInt(ySpeed)==0) {clearInterval(timer);timer=null} scrollBy(ySpeed) }', 50);
}}

function _thumbDown(evt) { with (this)
{
 var evt = evt?evt:window.event;
 activeScr = this;
 dragOffset = (isNS4 ? evt.layerY : page.scrollY() + evt.clientY - thm.y());
 if (timer) clearInterval(timer);
 timer = null;
 return false;
}}


function _thumbMove(evt)
{
 var evt = evt?evt:window.event;
 if (!activeScr) return true;
 else with (activeScr)
 {
  if ((cTop + cHeight > divHeight) || (thmHeight == barHeight)) return true;
  var thmTop = (isNS4 ? evt.pageY : page.scrollY() + evt.clientY) - dragOffset - bar.y();
  if (thmTop < 0) thmTop = 0;
  if (thmTop + thmHeight > barHeight) thmTop = barHeight - thmHeight;
  thm.y(bar.y() + thmTop);
  stickTop = (divHeight - cHeight) * (thmTop / (barHeight - thmHeight));
  if (stick == 1) scrollTo(stickTop);
  else if (!timer) timer = setInterval(myName + '.stickScroll()', 40);
  return false;
 }
}


function _stickScroll() { with (this)
{
 if (Math.abs(cTop - stickTop) > 1)
 {
  cTop += (stickTop - cTop) * stick;
  scrollTo(cTop, true);
 }
 else if (cTop != stickTop)
 {
  cTop = stickTop;
  scrollTo(cTop, true);
 }
}}

function _thumbUp(evt)
{
 activeScr = null;
}

function _barClick(evt) { with (this)
{
 var evt = evt?evt:window.event;
 clickPos = isNS4 ? evt.pageY : page.scrollY() + evt.clientY;
 if (clickPos < thm.y()) scrollBy(0 - cHeight);
 if (clickPos > (thm.y() + thmHeight)) scrollBy(cHeight);
 if (isNS4) return document.routeEvent(evt);
}}

function _layout() { with (this)
{
 if (!isDyn || !loaded) return;
 for (var i = 0; i < divs.length; i++) with (divs[i].lyr)
 {
  x(eval(divs[i].x)); w(Math.max(0,eval(divs[i].w)));
  if (i) {y(eval(divs[i].y)); h(Math.max(0,eval(divs[i].h)))}
 }
 cWidth = eval(div.w());
 barHeight = cHeight;
 barHeight = cHeight-up.h()-down.h();
 scrollBy(0);
}}

function _setup() { with (this)
{
 if (!isDyn) return;
 cHeight=200;
 for (var i = 0; i < divs.length; i++)
  divs[i].lyr = getLyr(divs[i].id, eval(divs[i].par));
 div = divs[0].lyr;
 div.x(div.x());
 div.y(div.y());
 div.w(150);
 div.h(div.h());
 up = divs[3].lyr;
 up.h(up.h())
 up.w(up.w())
 up.y(div.y());
 down = divs[4].lyr;
 down.h(down.h())
 down.w(down.w())
 bar = divs[1].lyr;
 bar.x(div.x()+div.w());
 bar.y(div.y()+up.h());
 bar.w(bar.w());
 bar.h(cHeight-up.h()-down.h());
 down.y(bar.y()+cHeight-up.h()-down.h());
 thm = divs[2].lyr;
 thm.w(thm.w());
 thm.x(bar.x()+(bar.w()/2)-(thm.w()/2));
 up.x(bar.x()+(bar.w()/2)-(up.w()/2));
 down.x(bar.x()+(bar.w()/2)-(down.w()/2));

 //alert("div x : " + div.x() + "\ny : " + div.y() + "\nw : " + div.w() + "\nh : " + div.h() +
 // "\nbar x : " + bar.x() + "\ny : " + bar.y() + "\nw : " + bar.w() + "\nh : " + bar.h() +
 // "\nthm x : " + thm.x() + "\ny : " + thm.y() + "\nw : " + thm.w() + "\nh : " + thm.h());
 cTop=orgTop=div.y()
 if (isNS4) {
  bar.ref.captureEvents(Event.CLICK);
  thm.ref.captureEvents(Event.MOUSEDOWN);
 }
 bar.ref.onclick = new Function('evt', 'return ' + myName + '.barClick(evt)');
 thm.ref.onmousedown = new Function('evt', 'return ' + myName + '.thumbDown(evt)');
 var noSel = new Function('if (activeScr) return false');
 if (isIE) document.onselectstart = noSel;
 else if (isDOM) document.onselect = noSel;
 loaded = true;
 layout();
 activeScr = null;
 scrollTo(0);

 for (var i = 0; i < divs.length; i++) if (i != 2) divs[i].lyr.vis('visible');
 divs[2].lyr.vis(div.h() > cHeight ? 'visible' : 'hidden');

}}

function ipkListBox()
{
 this.myName = arguments[0];
 this.bufRef=null;
 this.div=null;
 this.bar=null;
 this.thm=null;
 this.loaded=null;
 this.timer=null;
 this.divHeight=null;
 this.thmHeight=null;
 this.barHeight=null;
 this.cHeight=null;
 this.cWidth=null;
 this.dragOffset=null;
 this.cTop = 0;
 this.orgTop=0;
 this.divs = new Array();
 this.minThmHeight = 20;
 this.checkBounds = true;
 this.ySpeed = this.stepsLeft = 0;
 this.stick = 1;
 this.stickTop = 0;
 this.scrollTo = _scrollTo;
 this.scrollBy = _scrollBy;
 this.setScroll = _setScroll;
 this.thumbDown = _thumbDown;
 this.stickScroll = _stickScroll;
 this.barClick = _barClick;
 this.setup = _setup;
 this.layout = _layout;
 this.Init=_init;
}
//Objet _div : chacun des layers 
function _div() {  this.id=arguments[0] }

var activeScr = null;
//Initialisation de l'objet ipklistbox
function _init(divContent,divBar,divThumb,divUp,divDown) { with (this) {
 divs[0] = new _div(divContent);
 divs[1] = new _div(divBar);
 divs[2] = new _div(divThumb);
 divs[3] = new _div(divUp);
 divs[4] = new _div(divDown);
 setup();
 }
}

if (isNS4) document.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);

document.onmousemove = function(evt)
{
 var ret = _thumbMove(evt);
 return (ret ? (isNS4?document.routeEvent(evt):true) : false);
}

document.onmouseup = function(evt)
{
 _thumbUp(evt);
 if (isNS4) return document.routeEvent(evt);
}