/** Zoom */
function wnjs_zoomImageIn (img,image) {
  if(img.mode!="ZOOM") {
    img.mode = "ZOOM";
    img.oldInnerHTML = img.innerHTML;
    img.innerHTML= "<img style='border:1px solid #AAAAAA;' src='"+image+"'/>";
  }
}

function wnjs_zoomImageOut (img) {
  if(img.mode=="ZOOM") {
    img.mode="NORMAL";
    img.style.position = "relative";
    img.innerHTML= img.oldInnerHTML;
  }
}

/** Display the time... every minute */
function wnjs_displayDT (id) {
  var dt = new Date();
  var h =  dt.getHours();    
  var buf = "";
  if(h<10)
    buf = "0"+h;
  else
    buf = h;
  buf = buf + ":";
  var m =  dt.getMinutes();
  if(m<10)
    buf = buf + "0"+m;
  else
    buf = buf + m;
 
  // if(h==12)
  //    buf = buf + " <font style='color:red; font-weight:bold; font-style:normal; text-align:center;'>Time for lunch !</font>";
  var el = document.getElementById(id);
  if(el!=null) {
    el.innerHTML = buf;
    setTimeout( "wnjs_displayDT('"+id+"')", 60000 );
  }
}


/* An horizontal scroller */
var wnscroller = function (width,height,bg,color,content) {
  this.id = wnscroller.instances.length;
  wnscroller.instances[this.id] = this;

  this.width=width;
  this.height=height;
  this.content='<nobr>'+content+'</nobr>';
  
  this.speed=1;
  this.bgcolor=bg;
  this.color = color;
  this.pauseit=1;
  
  this.copyspeed=this.speed;
  this.pausespeed=(this.pauseit==0)? this.copyspeed: 0;

  this.actualwidth='';

  this.setContent = function (content) {
    this.content = '<nobr>'+content+'</nobr>';
    this.populate();
  }

  this.populate = function (){
    var marquee=document.getElementById(this.id+"iemarquee");
    marquee.style.left=parseInt(this.width)+8+"px";
    marquee.innerHTML=this.content;

    document.getElementById(this.id+"temp").innerHTML=this.content;
    this.actualwidth=document.getElementById(this.id+"temp").offsetWidth;
    this.lefttime=setInterval("wnscroller.instances["+this.id+"].scrollmarquee()",20);
  }
 
  this.scrollmarquee = function (){
    var marquee=document.getElementById(this.id+"iemarquee");
    var _i = parseInt(marquee.style.left);
    var _j = 8-this.actualwidth;
    var _k = 0;
    if( _i > _j )
      _k = _i - this.copyspeed;
    else
      _k = parseInt(this.width)+8;
   
    marquee.style.left= _k + "px";
  }

  this.pause = function () { this.copyspeed = this.pausespeed; }

  this.restart = function () { this.copyspeed = this.speed; }
  
  this.init = function () {
    
    document.write('<span id="'+this.id+'temp" style="visibility:hidden;position:absolute;top:-100px;left:-9000px">'+this.content+'</span>');

    document.write('<table border="0" cellspacing="0" cellpadding="0"><td>');
    document.write('<div style="position:relative;width:'+this.width+';height:'+this.height+';overflow:hidden">');
    document.write('<div style="position:absolute;width:'+this.width+';height:'+this.height+';background-color:'+this.bgcolor+';color:'+this.color+';" onMouseover="wnscroller.instances['+this.id+'].pause();" onMouseout="wnscroller.instances['+this.id+'].restart();">');
    document.write('<div id="'+this.id+'iemarquee" style="position:absolute;left:0px;top:0px;height:'+this.height+';"></div>');
    document.write('</div></div>');
    document.write('</td></table>');

    this.populate();
  }
}

wnscroller.instances = new Array();

/** Fading scroller */
function wnfading (stylediv,styletd,content) {
  this.id = wnfading.instances.length;
  wnfading.instances[this.id] = this;

  this.begintag = '<div style="'+styletd+'">';
  this.fcontent = content;
  this.closetag = '</div>';

  this.style = styletd;
  this.delay = 2000;
  this.maxsteps=30;
  this.stepdelay=40;
  this.startcolor= new Array(255,255,255);
  this.endcolor=new Array(0,0,0);

  this.fadelinks=1;
  this.faderdelay=0;

  this.index=0;

  var fadecounter;

  this.changecontent = function (){
    if(this.index>=this.fcontent.length)
      this.index=0;
    document.getElementById(this.id+"fscroller").style.color="rgb("+this.startcolor[0]+", "+this.startcolor[1]+", "+this.startcolor[2]+")";
    document.getElementById(this.id+"fscroller").innerHTML=this.begintag+this.fcontent[this.index]+this.closetag;
    if(this.fadelinks)
      this.linkcolorchange(1);
    this.colorfade(1, 15);
    this.index++
  }

  this.linkcolorchange = function (step){
    var obj=document.getElementById(this.id+"fscroller").getElementsByTagName("A");
    if (obj.length>0){
      for (i=0;i<obj.length;i++)
	obj[i].style.color=this.getstepcolor(step);
    }
  }

  this.colorfade = function (step) {
    if(step<=this.maxsteps) {	
      document.getElementById(this.id+"fscroller").style.color=this.getstepcolor(step);
      if (this.fadelinks)
	this.linkcolorchange(step);
      step++;
      this.fadecounter=setTimeout("wnfading.instances["+this.id+"].colorfade("+step+")",this.stepdelay);
    }else{
      clearTimeout(this.fadecounter);
      document.getElementById(this.id+"fscroller").style.color="rgb("+this.endcolor[0]+", "+this.endcolor[1]+", "+this.endcolor[2]+")";
      setTimeout("wnfading.instances["+this.id+"].changecontent()", this.delay);	
    }   
  }

  this.getstepcolor = function (step) {
    var diff;
    var newcolor=new Array(3);
    for(var i=0;i<3;i++) {
      diff = (this.startcolor[i]-this.endcolor[i]);
      if(diff > 0) {
	newcolor[i] = this.startcolor[i]-(Math.round((diff/this.maxsteps))*step);
      } else {
	newcolor[i] = this.startcolor[i]+(Math.round((Math.abs(diff)/this.maxsteps))*step);
      }
    }
    return ("rgb(" + newcolor[0] + ", " + newcolor[1] + ", " + newcolor[2] + ")");
  }

  this.init = function () {
    document.write('<div id="'+this.id+'fscroller" style="'+this.style+'"></div>');
    this.changecontent();
  }
}

wnfading.instances = new Array();


/*
 * Show a text:
 * - tid: toggle id (the show button)
 * - id: div id (the text + the hide button)
 */
function showText(tid,id) {
  var ele = document.getElementById(id);
  ele.style.display = "block";

  var ele = document.getElementById(tid);
  ele.style.display = "none";
}

/**
 * hide a text
 * id: div id (the text + the hide button)
 */
function hideText(tid,id) {
  var ele = document.getElementById(id);
  ele.style.display = "none";  

  var ele = document.getElementById(tid);
  ele.style.display = "block";
}

