Fadomatic.INTERVAL_MILLIS = 75;
Fadomatic.NUMBER_PICS = 3;
Fadomatic.WAIT_LENGTH= 20;
Fadomatic.CYCLE_LENGTH= 180;
tickCount=0;

function Fadomatic (element, rate, initialOpacity, minOpacity, maxOpacity,inpause,outpause) {
  this._element = element;
  this._intervalId = null;
  this._rate = rate;
  this._isFadeOut = true;
  this._inpause=inpause;
  this._outpause=outpause;

  // Set initial opacity and bounds
  // NB use 99 instead of 100 to avoid flicker at start of fade
  this._minOpacity = 0;
  this._maxOpacity = 99;
  this._opacity = 99;

  if (typeof minOpacity != 'undefined') {
    if (minOpacity < 0) {
      //this._minOpacity = 0;
    } else if (minOpacity > 99) {
      this._minOpacity = 99;
    } else {
      this._minOpacity = minOpacity;
    }
  }

  if (typeof maxOpacity != 'undefined') {
    if (maxOpacity < 0) {
      this._maxOpacity = 0;
    } else if (maxOpacity > 99) {
      this._maxOpacity = 99;
    } else {
      this._maxOpacity = maxOpacity;
    }

    if (this._maxOpacity < this._minOpacity) {
      this._maxOpacity = this._minOpacity;
    }
  }
  
  if (typeof initialOpacity != 'undefined') {
    if (initialOpacity > this._maxOpacity) {
      this._opacity = this._maxOpacity;
    } else if (initialOpacity < this._minOpacity) {
      this._opacity = this._minOpacity;
    } else {
      this._opacity = initialOpacity;
    }
  }

  // See if we're using W3C opacity, MSIE filter, or just
  // toggling visiblity
  if(typeof element.style.opacity != 'undefined') {

    this._updateOpacity = this._updateOpacityW3c;

  } else if(typeof element.style.filter != 'undefined') {

    // If there's not an alpha filter on the element already,
    // add one
    if (element.style.filter.indexOf("alpha") == -1) {

      // Attempt to preserve existing filters
      var existingFilters="";
      if (element.style.filter) {
        existingFilters = element.style.filter+" ";
      }
      temp=this._opacity;
      if (temp<0) { 
        temp=0; 
      }
      element.style.filter = existingFilters+"alpha(opacity="+temp+")";
    }
    this._updateOpacity = this._updateOpacityMSIE;
  } else {
    this._updateOpacity = this._updateVisibility;
  }
  this._updateOpacity(this);
}

// Initiates a fade out
Fadomatic.prototype.fadeOut = function () {
  this._isFadeOut = true;
  this._beginFade();
}

// Initiates a fade in
Fadomatic.prototype.fadeIn = function () {
  this._isFadeOut = false;
  this._beginFade();
}

// Makes the element completely opaque, stops any fade in progress
Fadomatic.prototype.show = function () {
  this.haltFade();
  this._opacity = this._maxOpacity;
  this._updateOpacity();
}

// Makes the element completely transparent, stops any fade in progress
Fadomatic.prototype.hide = function () {
  this.haltFade();
  this._opacity = 0;
  this._updateOpacity();
}

// Halts any fade in progress
Fadomatic.prototype.haltFade = function () {
  clearInterval(this._intervalId);
}

// Resumes a fade where it was halted
Fadomatic.prototype.resumeFade = function () {
  this._beginFade();
}

// Pseudo-private members
Fadomatic.prototype._beginFade = function () {
  this.haltFade();
  var objref = this;
  this._intervalId = setInterval(function() { objref._tickFade(); },Fadomatic.INTERVAL_MILLIS);
}

Fadomatic.prototype._tickFade = function () {
if(this._element.id==="box1"){  
  //MM_findObj('sword').value+=(tickCount+','+this._opacity+':'+fader2._opacity+':'+fader3._opacity+','+this._isFadeOut+'|');
  if (tickCount>Fadomatic.CYCLE_LENGTH){
    tickCount=0;
      this._isFadeOut = true;
      this._opacity = 99;
     this._updateOpacity(this);

      fader2._isFadeOut = false;
      fader2._opacity = 0;
     fader2._updateOpacity(fader2);

      fader3._isFadeOut = false;
      fader3._opacity = 0;
     fader3._updateOpacity(fader3);

  }else{

  if(tickCount>=this._inpause && !this._isFadeOut){
    this._opacity += this._rate;
    if (this._opacity >= this._maxOpacity ) {
      this._opacity = this._maxOpacity;
      this._isFadeOut = true;
      //this.haltFade();
    }
    this._updateOpacity(this);
  }
    if(tickCount>=this._outpause && this._isFadeOut){
    this._opacity = this._opacity-this._rate;
//MM_findObj('sword').value+='++';
    if (this._opacity <= this._minOpacity) {
      this._opacity = this._minOpacity;
      this._isFadeOut = false;
      //this.haltFade();
    }
    this._updateOpacity(this);
  }
  if(tickCount>=fader2._inpause && !fader2._isFadeOut){
    fader2._opacity += fader2._rate;
    if (fader2._opacity >= fader2._maxOpacity ) {
      fader2._opacity = fader2._maxOpacity;
      fader2._isFadeOut = true;
      //this.haltFade();
    }
    fader2._updateOpacity(fader2);
  }
    if(tickCount>=fader2._outpause && fader2._isFadeOut){
    fader2._opacity -= fader2._rate;
    if (fader2._opacity <= fader2._minOpacity) {
      fader2._opacity = fader2._minOpacity;
      //fader2._isFadeOut = false;
      //this.haltFade();
    }
    fader2._updateOpacity(fader2);
  }
  if(tickCount>=fader3._inpause && !fader3._isFadeOut){
    fader3._opacity += fader3._rate;
    if (fader3._opacity >= fader3._maxOpacity ) {
      fader3._opacity = fader3._maxOpacity;
      fader3._isFadeOut = true;
      //this.haltFade();
    }
    fader3._updateOpacity(fader3);
  }
    if(tickCount>=fader3._outpause && fader3._isFadeOut){
    fader3._opacity -= fader3._rate;
    if (fader3._opacity <= fader3._minOpacity) {
      fader3._opacity = fader3._minOpacity;
      //fader3._isFadeOut = false;
      //this.haltFade();
    }
    fader3._updateOpacity(fader3);
  }

}
tickCount+=1;
}
}

Fadomatic.prototype._updateVisibility = function () {
  if (this._opacity > 0) {
    this._element.style.visibility = 'visible';
  } else {
    //this._element.style.visibility = 'hidden';
  }
}

Fadomatic.prototype._updateOpacityW3c = function (fader) {
  temp=fader._opacity;
  if (temp<0) { 
    temp=0; 
  }
  fader._element.style.opacity = temp/100;
  fader._updateVisibility();
}

Fadomatic.prototype._updateOpacityMSIE = function (fader) {
  temp=fader._opacity;
  if (temp<0) { 
    temp=0; 
  }
  fader._element.filters.alpha.opacity = temp;
  fader._updateVisibility();
}

Fadomatic.prototype._updateOpacity = null;

function MM_findObj(n, d) { //v4.01
  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 && d.getElementById) x=d.getElementById(n); return x;
}

//setup onload function
if(typeof window.addEventListener != 'undefined')
{
	//.. gecko, safari, konqueror and standard
	window.addEventListener('load', generic, false);
}
else if(typeof document.addEventListener != 'undefined')
{
	//.. opera 7
	document.addEventListener('load', generic, false);
}
else if(typeof window.attachEvent != 'undefined')
{
	//.. win/ie
	window.attachEvent('onload', generic);
}

//** remove this condition to degrade older browsers
else
{
	//.. mac/ie5 and anything else that gets this far
	
	//if there's an existing onload function
	if(typeof window.onload == 'function')
	{
		//store it
		var existing = onload;
		
		//add new onload handler
		window.onload = function()
		{
			//call existing onload function
			existing();
			
			//call generic onload function
			generic();
		};
	}else{
		//setup onload function
		window.onload = generic;
	}
}


var fader1,fader2,fader3,fader4
var box1,box2,box3,box4

function generic(){
	box1=MM_findObj('box1');
	box2=MM_findObj('box2');
	box3=MM_findObj('box3');
	//box4=MM_findObj('box4');
	
	fader1 = new Fadomatic(box1, 9, 99, 0, 99,170,40);
	fader1.fadeOut();
	fader2 = new Fadomatic(box2, 9, 0, 0, 99,40,100);
	fader2.fadeIn();
	fader3 = new Fadomatic(box3, 9, 0, 0, 99,100,170);
	fader3.fadeIn();
	//fader1 = new Fadomatic(box1, 9, 99, 0, 99,80,20);
	//fader1.fadeOut();
	//fader2 = new Fadomatic(box2, 9, 0, 0, 99,20,50);
	//fader2.fadeIn();
	//fader3 = new Fadomatic(box3, 9, 0, 0, 99,50,80);
	//fader3.fadeIn();
}