//
// NETSCAPE / IE compatibility
//
var ISIE = true;
if (navigator.appName == "Netscape")
ISIE = false;





function getObject (id)
{
  if (!ISIE)
     return document.getElementById(id);
 
  return document.all[id];
}


function getImage (id)
{
  if (!ISIE)
     return document.images[id];

  return document.all[id];
}



function strReplace(s, r, w){
     return s.split(r).join(w);
}



function _check_post (s)
{
  s = strReplace (s, "\r\n", "<br>"); 
  s = strReplace (s, "\n", "<br>"); 
  s = strReplace (s, "&", "&amp;");  
  s = strReplace (s, "\"", "'");
  return s;
}	


  function input_digits (obj, cars) 
  { 
    size = obj.value.length;
    car = obj.value.charAt (size-1);
    process=1;
    if (((car >= '0') && (car <= '9')) || ((typeof (cars) == "string") && (car == cars))) process=0;
    
    if (process) obj.value = obj.value.substr (0, size-1);
  }


function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function openPhoto(myimg,title) 
{
width=640;
height=480;
window.open('../../tools/photo.php?PHOTO='+myimg+'&TITLE='+title,'_','left='+(screen.width/2 - width/2)+',top='+(screen.height/2 - height/2)+',screenX='+(screen.width/2 - width/2)+',screenY='+(screen.height/2 - height/2)+',height='+height+',width='+width+',status=no,location=no,toolbar=no,scrollbars=no,menubar=no,resizable=no').focus();
if (ISIE) event.returnValue=false;
}





//////////////////////////////////////////
//
// IMAGE OPACITY TOOL
//


function opacity_set (obj, opacity) 
{
  obj.style.visibility = "visible";
  
  // IE/Win
  if (ISIE)
    obj.style.filter = "alpha(opacity:"+opacity+")";
  else
  {
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity;
  
  }
}



//////////////////////////////////////////
//
// IMAGE ALPHA FADING TOOL
//

//
// All the tools above fadeIn and fadeOut are usefull for handling 
// the rollovers web icons
//

var OP_FADE_ID = new Array ();

var OP_FADE_OBJ = new Array ();
OP_FADE_OBJ.push (null); // first index will be '1'


function OP_FADE_OBJ_Item (id, opacity, step, cbk)
{
  this.id       = id;
  this.img      = getObject (id);
  
  if (this.img == null) this.img = getImage (id);
  
  this.timecode	= (new Date ()).getTime();
  this.lock     = this.timecode;
  this.opacity  = opacity;
  this.step     = step;
  this.cbk      = cbk;
}


function OP_FADE_OBJ_Build (id, opacity, step, cbk)
{
  var obj;
  
  // retreive the index of the this object
  idx = OP_FADE_ID[id];
  
  // doesn't exist yet, get a new index
  if (!idx)
  {
    idx = OP_FADE_OBJ.length;
    OP_FADE_ID[id] = idx;
  }
  
  // create the object and store it
  obj = new OP_FADE_OBJ_Item (id, opacity, step, cbk);  
  OP_FADE_OBJ[idx] = obj;
  
  return idx;  	
}


function OP_FADE_OBJ_Lock (idx)
{
  OP_FADE_OBJ[idx].lock = (new Date ()).getTime();	
}


function OP_FADE_OBJ_CLEAR ()
{
  for (i=1; i < OP_FADE_OBJ.length; i++)
  {
    OP_FADE_OBJ[i].lock=1;
  }
}



function opacity_fadeIn (idx, timecode) 
{
    obj = OP_FADE_OBJ[idx];
        
    if ((obj.opacity <= 100) && (obj.lock == timecode))
    {
      opacity_set(obj.img, obj.opacity);

      obj.opacity += obj.step;
       
      if (obj.lock == timecode)
      window.setTimeout("opacity_fadeIn("+idx+","+timecode+")", 80);
     }
    else if (obj.cbk) setTimeout (obj.cbk, 10);
}



function opacity_fadeOut (idx, timecode)
{
    obj = OP_FADE_OBJ[idx];
    
    if ((obj.opacity >= 0) && (obj.lock == timecode))
    {
      opacity_set(obj.img, obj.opacity);

      obj.opacity -= obj.step;
       
      if (obj.lock == timecode)
      window.setTimeout("opacity_fadeOut("+idx+","+timecode+")", 80);
     }
    else if (obj.cbk) setTimeout (obj.cbk, 10);
}



function opacity_anim_fadeIn (id, step, cbk)
{
  idx = OP_FADE_OBJ_Build (id, 0, step, cbk);
  obj = OP_FADE_OBJ[idx];
  opacity_fadeIn (idx, obj.timecode); 
}


function opacity_anim_fadeOut (id, step)
{
  idx = OP_FADE_OBJ_Build (id, 100, step, null);
  obj = OP_FADE_OBJ[idx];
  opacity_fadeOut (idx, obj.timecode); 
}

//////////////////////////////////////////////////



//////////////////////////////////////////
//
// DIV TOOLS
//



var DivMoveDataTab = new Array ();

function DivMoveData (id, x1, x2, y1, y2, step)
{
  ///////////////////////////////////////
  
  this.id    = id;
  this.div   = getObject (id);
  this.x1    = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2;
  
  this.vertical = (x1 == x2);
  
  // norm of (x1,y1) -- (x2,y2) vector
  this.norm  = Math.sqrt ((Math.pow ((x2-x1), 2) + Math.pow ((y2-y1), 2)), 2);
  
  // angle
  this.alpha = Math.asin ((y2 - y1) / this.norm);

  // line equation  
  this.a     = (y2 - y1) / (x2 - x1);
  this.b     = y1 - (this.a * x1);
  
  this.step  = step;
  if (x2 < x1) this.step = (-step);
  if ((this.vertical) && (y2 < y1)) this.step = (-step);
  
  this.r     = this.step;
  
  /////////////////////////////////////////
  
  if (this.vertical)
  this.computeX = function ()
                  { return this.x1; }
  else
  this.computeX = function ()
                  { return ((this.r * Math.cos(this.alpha)) + this.x1); }

  
  if (this.vertical)
  this.computeY = function (x)
                  { return (this.y1 + this.r); }
  else   
  this.computeY = function (x)
                  { return ((this.a * x) + this.b); }
                  
  //////////////////////////////////////////
  
  this.nextStep = function () {this.r += this.step};
  
  this.isOver = function () {return (Math.abs(this.r) >= this.norm);}
}



function div_anim_moveTo (index)
{
   process = DivMoveDataTab[index];
   
   if (process.isOver())
   {
   	process.div.style.left = process.x2;
   	process.div.style.top = process.y2;
   }
   else
   {
     x = process.computeX ();
     y = process.computeY (x);
     x = Math.floor (x);
     y = Math.floor (y);
     
     process.div.style.left = x;
     process.div.style.top = y;
     
     process.nextStep ();
     
     setTimeout ("div_anim_moveTo("+index+")", 50);
   }
}


function div_moveTo (id, x1, y1, x2, y2, step)
{
  index = DivMoveDataTab.length;
  
  DivMoveDataTab[index] = new DivMoveData (id, x1, x2, y1, y2, step);
  
  div_anim_moveTo (index);
}



var DivScrollTab = new Array ();
var DivScrollTabLength=0;

function DivScrollTabItem (name, w, h, step, timeout)
{
  this.name    = name;
  this.w       = w;
  this.h       = h;
  this.step    = step;
  this.timeout = timeout;
  this.pos     = 0;
  this.endpos  = 0;
  this.contents = new Array ();	
}


function div_scroll_addContents (name, contents)
{
  DivScrollTab[name].contents.push (contents);
}


function div_scroll_anim (name, idx)
{
  var item = DivScrollTab[name];
  
  child = getObject ("divchild"+name);
  
  timeout = item.timeout;
  step = item.step;
  
  // call the next one in 'timeout' secs.
  if ((item.pos-step) < item.endpos)
  {
  
      // has the whole contents scrolled?
      if (Math.abs(item.endpos) >= child.offsetHeight)
      {
        item.pos -= step;
        child.style.top = item.pos;
        setTimeout ("div_scroll_start('"+name+"', "+(idx+1)+")", timeout);
        return;
      }
      else
      {
        item.endpos -= item.h;   		
      }
  }
  else timeout=50;
  
  // scroll step
  item.pos -= step;
  child.style.top = item.pos;

  
  // continue animation  
  setTimeout ("div_scroll_anim ('"+name+"', "+idx+")", timeout);
}



function div_scroll_start (name, idx)
{
  var item = DivScrollTab[name];
  
  if ((idx == null) || (idx == item.contents.length))
    { idx= 0; }
  
  child = getObject ("divchild"+name);  
  
  child.innerHTML = item.contents[idx];
    
  item.endpos = 0-child.offsetHeight;
  
  item.pos = item.h;
  child.style.top = item.h;

  // start scrolling
  div_scroll_anim (name, idx);  
  
}


function div_scroll (name, w, h, step)
{
  DivScrollTab [name] = new DivScrollTabItem (name, w, h, step, 100);
  DivScrollTabLength++;
}


function div_position (div, x, y)
{
  	div.style.left = x;
  	div.style.top = y;	
}


function div_update (id, imgid)
{
  div = getObject (id);
  img = getImage (imgid);	
  
  div.style.width = img.width;
  div.style.height = img.height;
}





function div_alpha_animation (id, path, nb, step, idx)
{
  if ((!idx) || (idx == nb)) idx=0;
  
  if (idx == (nb-1)) { fst = idx; snd = 0; }
  else {fst = idx; snd = idx+1; }

  id0 = "div"+id+"0";
  id1 = "div"+id+"1";
  div0 = getObject (id0);
  div1 = getObject (id1);
  
  opacity_set (div1, 0);
  opacity_set (div0, 100);
  
  img0 = getImage (id+"0");
  img1 = getImage (id+"1");
  
  img0.src = path + "/" + fst + ".jpg";
  img1.src = path + "/" + snd + ".jpg";
 
  cbk = "div_alpha_animation('"+id+"', '"+path+"', "+nb+", "+step+", "+(idx+1)+")";
  
  idx0 = OP_FADE_OBJ_Build (id0, 100, step, null);	
  obj0 = OP_FADE_OBJ[idx0];
  
  idx1 = OP_FADE_OBJ_Build (id1, 0, step, cbk);	
  obj1 = OP_FADE_OBJ[idx1];
  
  opacity_fadeOut (idx0, obj0.timecode);
  opacity_fadeIn (idx1, obj1.timecode); 
}

////////////////////////////////////////////////////



//////////////////////////////////////////
//
// PICTURE SHADOW AND OPACITY TOOL, for image web links
//

function picture_update_aux (id)
{
	img = getImage ("i"+id);
	imgshd = getImage ("i"+id+"shadow");
	divimg = getObject ("div"+id);
	divshd = getObject ("div"+id+"shadow");

	imgshd.width = img.width;
	imgshd.height = img.height;
	divimg.style.width = img.width;
	divshd.style.width = img.width;
	divimg.style.height = img.height;
	divshd.style.height = img.height;
	
	opacity_set (imgshd, 30);
	opacity_set (img, 70);	
}

function picture_update (id)
{
  img = getImage ("i"+id);
  
  if ((!img) || (!img.width))
    setTimeout ("picture_update('"+id+"')", 100);
  else picture_update_aux(id);
}


function picture_over (id, x, y)
{
  img = getImage ("i"+id);
  div = getObject ("div"+id);
  divshd = getObject ("div"+id+"shadow");
  
  opacity_set (img, 100);  	
  div_position (div, x-2, y-2);
  div_position (divshd, x+7, y+7);
}

function picture_out (id, x, y)
{
  img = getImage ("i"+id);
  div = getObject ("div"+id);
  divshd = getObject ("div"+id+"shadow");
  
  opacity_set (img, 70);	
  div_position (div, x, y);
  div_position (divshd, x+5, y+5);
}



//////////////////////////////////////////////////
//
// image tools for web image links, with fade effect
//


function image_swap (id, newsrc)
{
  img = getImage (id);
  if (img)  img.src = newsrc;	
}


function image_switch (id, newsrc)
{
  idx = OP_FADE_OBJ_Build (id, 0, 25, null);
    
  obj = OP_FADE_OBJ[idx];
  
  opacity_set (obj.img, 0);
  obj.img.src = newsrc;
  opacity_fadeIn (idx, obj.timecode);  	
}



function image_switch_out (id)
{
  idx = OP_FADE_OBJ_Build (id, 100, 20, null);
  obj = OP_FADE_OBJ[idx];
      
  opacity_fadeOut (idx, obj.timecode);  	
}

function weblink_over (id, path)
{
  img = getImage (id);
  img.src = path+"/"+id+".jpg";
  
  idx = OP_FADE_OBJ_Build ("label", 0, 25, null);
  imglabel = OP_FADE_OBJ[idx];
  imglabel.img.src = path+"/label_"+id+".jpg";
  opacity_fadeIn (idx, imglabel.timecode); 
}


function weblink_out (id, path)
{
  img = getImage (id);
  imglabel = getImage ("label");
  
  img.src = path+"/"+id+".gif";
  imglabel.src = path+"/"+"label.jpg";	
}


function weblink_stop ()
{
  OP_FADE_OBJ_CLEAR();	
}

function panel_resize_from_contents (id, contentsID)
{
var contents = getObject (contentsID);

  if (contents.offsetHeight <= 0)
  {
    setTimeout ("panel_resize_from_contents("+id+", '"+contentsID+"')", 100);
    return;
  } 
  
  var panel = getObject ("panel"+id+"mh");
   
  panel.style.height = contents.offsetHeight;
}
