/****************************************************************************   
* DHTML library
*   Copyright (C) 2001 Gérald Sédrati-Dinet 2001
*   Adapated from various public sources :
*     www.xs4all.nl/~ppk/js/    : Peter-Paul Koch
*     dhtmlnirvana.com/         : Eddie Traversa
*     www.htmlguru.com/         : Jeff Rouyer
*     www.dhtmlcentral.com/lib/ : Thomas Brattli
***************************************************************************/

/*****************************
* Part 1 : Support detection *
******************************/

function gibinit() {
  this.dom=document.getElementById?1:0;
  this.ie=(!this.dom && document.all)?1:0;
  this.ns=document.layers?1:0;

  // Browsers specific bugs
  nav=navigator.userAgent.toLowerCase();
  this.ns6=(nav.indexOf('netscape6')||nav.indexOf('gecko'))?1:0;
  this.opera=nav.indexOf("opera")>-1;
  this.css=(document.getElementsByTagName && document.getElementsByTagName('head')[0].style)?1:0;
  this.konq=this.dom && (nav.indexOf('konqueror')>-1);
  this.iemac=this.dom && (nav.indexOf('mac')>-1);
  this.iedom=!this.konq && !this.iemac && this.dom && document.all;
  this.nswin=this.ns && (nav.indexOf('win')>-1);
  px=this.ns||window.opera||this.iedom?"":"px";
  
  return this;
}

gibinit.prototype.getDimensions = function() {
  // Dimensions
  if (self.innerWidth) {
    this.availableWidth = self.innerWidth;
    this.availableHeight = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientWidth) {
	this.availableWidth = document.documentElement.clientWidth;
	this.availableHeight = document.documentElement.clientHeight;
  }
  else if (document.body) {
	this.availableWidth = document.body.clientWidth;
	this.availableHeight = document.body.clientHeight;
  }

  window.onresize=new Function("history.go(0);");
}

var bw=new gibinit();
if (bw.ns) {
  alert("You are currently using Netscape 4.\nThis browser is old and does not offer every facilities specified by W3C standards.\nYou are encouraged to upgrade to Mozilla or Netscape 6.\n\nVous utilisez actuellement Netscape 4.\nCe navigateur est un peu ancien et n'offre pas toutes les fonctionnalités spécifiées par les standards du W3C.\nNous vous conseillons vivement de mettre à jour votre navigateur avec Mozilla ou Netscape 6.");
}
else if (!bw.css) {
  alert("The browser you are currently using does not implement sufficient Cascading Style Sheets support.\nPlease upgrade to Mozilla, Netscape 6, Internet Explorer 5 or up...\n\nLe navigateur que vous utilisez actuellement n'implémente pas suffisamment de support pour les feuilles de styles en cascade.\nMerci de le mettre à jour avec Mozilla, Netscape 6, Internet Explorer 5 ou des versions supérieures...");
	document.location="about:blank";
}

var loaded=!(bw.ns6);
var gibimages = new Array;
var gibimageshi = new Array;

/**************************
* Part 2 : Object setting *
***************************/

function gibobj(name, w, h, v, nestlyr, scndnestlyr) {
  this.id=name;
  if (bw.dom) {
    this.evt=document.getElementById(name);
    this.css=this.evt.style;
    this.ref=document;
  }
  else if (bw.ie) {
    this.evt=document.all[name];
    this.css=document.all[name].style;
    this.ref=document;
  }
  else if (bw.ns) {
    if (!scndnestlyr) {
      nest=(!nestlyr) ? "":'document.'+nestlyr+'.';
    }
    else {
      nest='document.'+nestlyr+'.document.'+scndnestlyr+'.';
    }
    this.evt=eval(nest+"document.layers."+name);
    this.css=eval(nest+"document.layers."+name);
    this.ref=this.css.document;
  }
  this.obj = name+"Object";
  eval(this.obj+"=this");
  
  this.css.position='absolute';

  this.w=w||this.evt.offsetWidth||this.css.clip.width||this.ref.width||this.css.pixelWidth||0;
  if (w != null) this.css.width=w+px;
  this.h=h||this.evt.offsetHeight||this.css.clip.height||this.ref.height||this.css.pixelHeight||0;
  if (h != null) this.css.height=h+px;
  
  if (v=='visible') {
    this.show();
  } else {
    this.hide();
  }
  return this;
}

/******************************
* Part 3 : Visibility setting *
*******************************/

gibobj.prototype.show = function() {
  this.v = 'visible';
  this.css.visibility = 'visible';
}

gibobj.prototype.hide = function() {
  this.v = 'hidden';
  this.css.visibility = 'hidden';
}

gibobj.prototype.flip = function() {
  if (this.v == 'hidden')
    this.show();
  else
    this.hide();
}

/****************************
* Part 4 : Position setting *
*****************************/

gibobj.prototype.moveTo = function(x,y,z) {
  if (x != null) {
    this.x=x;
    this.css.left=this.x+px;
  }
  if (y != null) {
    this.y=y;
    this.css.top=this.y+px;
  }
  if (z != null) {
    this.z=z;
    this.css.zIndex=this.z;
  }
}

gibobj.prototype.moveBy = function(dx,dy,dz) {
  if (dx != null) dx+=this.x;
  if (dy != null) dy+=this.y;
  if (dz != null) dz+=this.z;
  this.moveTo(dx,dy,dz);
}

/****************************
* Part 5 : Clipping setting *
*****************************/

gibobj.prototype.clipTo = function(t,r,b,l) {
  this.ct=t; this.cr=r; this.cb=b; this.cl=l;
  if (bw.ns){
    this.css.clip.top=t;
    this.css.clip.right=r;
    this.css.clip.bottom=b;
    this.css.clip.left=l;
  }else{
    if(t<0)t=0;if(r<0)r=0;if(b<0)b=0;if(b<0)b=0;
    this.css.clip="rect("+t+px+","+r+px+","+b+px+","+l+px+")";

    // Adjust width and height of the object to the same ones as the clip
    this.css.pixelWidth=this.css.width=r+px; 
    this.css.pixelHeight=this.css.height=b+px;
  }
}

gibobj.prototype.clipBy = function(t,r,b,l,ajust) {
  this.clipTo(this.ct+t,this.cr+r,this.cb+b,this.cl+l,ajust);
}

/*************************
* Part 6 : Color setting *
**************************/

gibobj.prototype.bg = function(col) {
  if (bw.opera) this.css.background=col;
  else if (bw.dom || bw.ie) this.css.backgroundColor=col;
  else if (bw.ns) this.css.bgColor=col;
}

gibobj.prototype.color = function(col,propagate) {
  // Does NOT work in Netscape 4
  if (!bw.ns) {
    this.css.color=col;
    if (propagate) {
      propagateColor(this.evt,col);
    }
  }
}

function propagateColor(domobj,col) {
  // Does NOT work in Netscape 4
  if (!bw.ns) {
    if (domobj.childNodes.lenght == 0) return;

    for (var i=0;i<domobj.childNodes.length;i++) {
      if (domobj.childNodes[i].style) {
        domobj.childNodes[i].style.color=col;
      }
      propagateColor(domobj.childNodes[i],col);
    }
  }
}

gibobj.prototype.rollovercolor = function(normal,hi) {
  this.cssclass(normal,1);
  this.evt.onmouseout = new Function(this.obj+".color(\""+normal+"\",1);");
  this.evt.onmouseover = new Function(this.obj+".color(\""+hi+"\",1);");
}

/*************************
* Part 7 : Class setting *
**************************/

gibobj.prototype.cssclass = function(cssclass,propagate) {
  this.evt.className=cssclass;
  if (propagate) {
    propagateClass(this.evt,cssclass);
  }
}

function propagateClass(domobj,cssclass) {
  // Does NOT work in Netscape 4
  if (!bw.ns) {
    if (domobj.childNodes.lenght == 0) return;

    for (var i=0;i<domobj.childNodes.length;i++) {
      if (domobj.childNodes[i].tagName) {
        domobj.childNodes[i].className=cssclass;
        propagateClass(domobj.childNodes[i],cssclass);
      }
    }
  }
}

gibobj.prototype.rollover = function(normal,hi) {
  this.cssclass(normal,1);
  this.evt.onmouseout = new Function(this.obj+".cssclass(\""+normal+"\",1);");
  this.evt.onmouseover = new Function(this.obj+".cssclass(\""+hi+"\",1);");
}

/***********************************
* Part 8 : Layer's content setting *
************************************/

gibobj.prototype.writeTo = function(text) {
  if (bw.ns) {
    this.ref.open("text/html");
    this.ref.write(text); 
    this.ref.close();
  }else {
    this.evt.innerHTML=text;
  }
}

gibobj.prototype.loadInto = function(url) {
  iframe = this.id+"Iframe";
  if (bw.ns) {
    this.evt.load(url,this.w)
  }
  else if (bw.ie) {
    parent.iframe.location = url;
  }
  else if (bw.dom) {
    document.getElementById(iframe).src = url;
  }
}

gibobj.prototype.unloadInto = function() {
  iframe = this.id+"Iframe";
  if (bw.ie) {
    this.evt.innerHTML = "";
    parent.iframe.location = "";
  }
  else if (bw.dom) {
    this.evt.innerHTML = "";
    document.getElementById(iframe).src = "";
  }
  else if (bw.ns) {
    //this.evt.load('about:blank',this.w)
  }
}


gibobj.prototype.showPage = function() {
  argv = this.showPage.arguments;
  argc = argv.length;
  include = argv[0];
  func=null;
  if (argc > 1) {
    func = argv[1]+"(";
	for (i=2;i<argc;i++) {
	  func += '"'+argv[i]+'"';
	  if (i+1 != argc) func += ", ";
	}
    func += ");";
  }

  iframe = this.id+"Iframe";

  if (bw.ie) {
    this.evt.innerHTML = parent.iframe.document.body.innerHTML;
  }
  else if (bw.dom) {
    this.evt.innerHTML = window.frames[iframe].document.getElementById(include).innerHTML;
  }
  else if (bw.ns) {
    this.css.clip.width=this.w;
    this.css.clip.height=this.h;
  }

  if (func) {
    eval(func);
  }
}

gibobj.prototype.getWidth = function() {
  w=this.evt.offsetWidth||this.css.clip.width||this.ref.width||this.css.pixelWidth||0;
  return w;
}

gibobj.prototype.getHeight = function() {
  h=this.evt.offsetHeight||this.css.clip.height||this.ref.height||this.css.pixelHeight||0;
  if (bw.dom) {
    if (bw.iedom) { h-=30; }
    else { h-=42; }
  }
  else if (bw.ie) { h-=20; }
  else if (bw.ns) { h-=50; }
  return h;
}

/*************************
* Part 9 : Image setting *
**************************/

gibobj.prototype.setimg = function(type) {
  if (bw.dom) {
    img = this.evt.getElementsByTagName('img')[0];
  } else {
    img=this.evt.document.images[0];
  }
  if (type=='hi') {
    img.src=gibimageshi[this.id].src;
  }
  else if (type=='normal') {
    img.src=gibimages[this.id].src;
  }
  else {
    img.src=type;
  }
}

gibobj.prototype.imgrollover = function(normal,hi,loadfunc,noauto) {
  gibimages[this.id] = new Image();
  
  //if (bw.konq) loadfunc+="()";
  
  //gibimages[this.id].onload=eval(loadfunc);
  gibimages[this.id].onload=loadCheck;
  gibimages[this.id].onerror=loadCheck;
  if (bw.ns && normal.indexOf(".png")) {
    withoutext = normal.substr(0, normal.length - 4);
    normal = withoutext+".gif";
  }
  gibimages[this.id].src=normal;

  gibimageshi[this.id] = new Image();
  //gibimageshi[this.id].onload=eval(loadfunc);
  gibimageshi[this.id].onload=loadCheck;
  gibimageshi[this.id].onerror=loadCheck;
  if (bw.ns && hi.indexOf(".png")) {
    withoutext = hi.substr(0, hi.length - 4);
    hi = withoutext+".gif";
  }
  gibimageshi[this.id].src=hi;

  if (!noauto) {
    this.evt.onmouseout = new Function(this.obj+".setimg('normal')");
    this.evt.onmouseover = new Function(this.obj+".setimg('hi')");
  }
  this.setimg('normal');
}

/**********************
* Part 10 : Scrolling *
***********************/

var timerScrollV=null;

gibobj.prototype.scrollV = function(cond,step,speed) {
  if (speed <=0) speed=1;
  if (cond == true) {
    if (((step > 0) && (this.y < 0))
     || ((step < 0) && (this.y > -(this.getHeight())))) {
      this.moveTo(null, this.y+step, null);
      timerScrollV = setTimeout(this.obj+".scrollV("+cond+","+step+","+speed+")",1000/speed);
    } else {
      clearTimeout(timerScrollV);
    }
  } else {
    clearTimeout(timerScrollV);
  }
}

/**************************
* Part 11 : Drag and drop *
***************************/

dd_is_active=0; dd_obj=0; dd_mobj=0;
gibobj.prototype.dragdrop = function(top,right,bottom,left,func) {
  if (!dd_is_active) {
    dd_is_active=1;
    if(bw.ns) {
      document.captureEvents(Event.MOUSEMOVE|Event.MOUSEDOWN|Event.MOUSEUP);
    }
    document.onmousemove=gibdd_move;
    document.onmousedown=gibdd_down;
    document.onmouseup=gibdd_up;
  }
  this.evt.onmousedown=new Function("gibdd_over("+this.obj+")");
  this.evt.onmouseover=new Function("gibdd_over("+this.obj+")");
  this.evt.onmouseout=new Function("dd_mobj=0;");

  this.dragLimitTop=top;
  this.dragLimitRight=right;
  this.dragLimitBottom=bottom;
  this.dragLimitLeft=left;

  this.dragFunc=func;
}

gibobj.prototype.nodragdrop = function() {
  this.evt.onmouseover="";
  this.evt.onmouseout="";
  this.evt.onmousedown="";
  dd_obj=0;
  dd_mobj=0;
}

function gibdd_over(object) {
  dd_mobj=object;
}

function gibdd_up(e) {
  dd_obj=0;
  if(bw.ns4) routeEvent(e)
}

function gibdd_down(e) {
  if(dd_mobj) {
    x=(bw.ns || (bw.dom && !bw.iedom))?e.pageX:event.x||event.clientX;
    y=(bw.ns || (bw.dom && !bw.iedom))?e.pageY:event.y||event.clientY;
    dd_obj=dd_mobj;
    dd_obj.clX=x-dd_obj.x; 
    dd_obj.clY=y-dd_obj.y;
  }
  if(bw.ns4) routeEvent(e);
}

function gibdd_move(e,y,rresize) {
  if (dd_obj) {
    x=(bw.ns || (bw.dom && !bw.iedom))?e.pageX:event.x||event.clientX;
    y=(bw.ns || (bw.dom && !bw.iedom))?e.pageY:event.y||event.clientY;

    nx=x-dd_obj.clX;
	ny=y-dd_obj.clY;

    if (dd_obj.ddobj) to_move=dd_obj.ddobj
    else to_move=dd_obj

	if ((to_move.dragLimitTop != null) && (ny < to_move.dragLimitTop)) {
		ny = to_move.dragLimitTop;
	}

	if ((to_move.dragLimitRight != null) && (nx+to_move.w > to_move.dragLimitRight)) {
		nx = to_move.dragLimitRight-to_move.w;
	}

	if ((to_move.dragLimitBottom != null) && (ny+to_move.h > to_move.dragLimitBottom)) {
		ny = to_move.dragLimitBottom-to_move.h;
	}

	if ((to_move.dragLimitLeft != null) && (nx < to_move.dragLimitLeft)) {
		nx = to_move.dragLimitLeft;
	}

	to_move.moveTo(nx,ny);

	if (to_move.dragFunc != null) {
		eval(to_move.dragFunc);
	}
  }
  if(!bw.ns) return false;
}

/*****************
* Part 12 : Misc *
******************/

function displayWindow(theURL,winName,width,height,features) {
  //if (bw.nswin) {
    window.location=theURL;
    return;
  //}
  /*
  window_width = (screen.width < width)?screen.width:width;
  window_height = (screen.height < height)?screen.height:height;
  window_top = (screen.height-window_height)/2;
  window_left = (screen.width-window_width)/2;
  newWindow=window.open(''+ theURL + '',''+ winName + '','width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + ',features=' + features + '');
  newWindow.focus();
  */
}

function showLayer(id, nestLyr) {
  if (bw.dom) {
    document.getElementById(id).style.visibility = 'visible';
  }
  else if (bw.ie) {
    document.all[id].style.visibility='visible';
  }
  else if (bw.ns) {
	nest=(!nestlyr) ? "":'document.'+nestlyr+'.';
    sty=eval(nest+"document.layers."+name);
    sty.visibility='visible';
  }
}

function hideLayer(id, nestLyr) {
  if (bw.dom) {
    document.getElementById(id).style.visibility = 'hidden';
  }
  else if (bw.ie) {
    document.all[id].style.visibility='hidden';
  }
  else if (bw.ns) {
	nest=(!nestlyr) ? "":'document.'+nestlyr+'.';
    sty=eval(nest+"document.layers."+name);
    sty.visibility='hidden';
  }
}
