function ChangeImage () { this.init.apply(this, arguments); }

//Initialize function
ChangeImage.prototype.init = function (elementId) 
{
  var c, s, t;
  
  this.sotowaku = document.getElementById(elementId);//out bounds
  this.list = [];//ImageList
  this.no = 0;//LI number
  this.timer = null;//initialize timer
  this.quit = null;//don't stop
  this.opacity = null;//initialize transparency
  
  s = this.sotowaku.style; 
  s.overflow = 'hidden'; 
  s.position = 'relative'; //out bounds CSS
  c = 0;
  while (t = this.sotowaku.childNodes[ c++ ]) 
  { //I add only LI children
	if ( t.nodeType == 1 && t.nodeName == 'LI' )
		{
		if (c != 1) t.style.visibility = 'hidden';
		this.list.push( { 'LI': t, 'IMG': this.setDef(t.getElementsByTagName('img')[0], c==1) } );//make transparent instead of first image
		}
  }
  return;
}


//img formatting
ChangeImage.prototype.setDef = function (e, f) 
{
  var s = e.style;
  
  s.top = '0px';
  s.left = '0px';
  s.width = this.sotowaku.offsetWidth + 'px';
  s.height = this.sotowaku.offsetHeight + 'px';
  s.position = 'absolute';
  if (!f) s.opacity = 0;
  return e;
}

// Main function method
// Setting initial delay, step change, display time, stop time
//ChangeImage.prototype.start = function (atime, dtime, step, stoptime) 
ChangeImage.prototype.start = function (atime, step, stoptime) 
{
  this.atime = atime || 5000; // initial delay
//  this.wait = Math.floor((dtime || 2000) / (100 / this.step)); // display time
//  this.wait = Math.floor((dtime || 2000)); // display time
  this.step = (step == undefined || step < 1) ? 1: step; // step change speed
  this.stoptime = stoptime || 10000; // stop time
  // main loop function
  this.loop0();
  return;
}

ChangeImage.prototype.loop0 = function () 
{
  this.setOpacity(100);
  this.step *= -1;
  if (!this.quit) // if the stop time has not been reached yet, run loop1 function after atime msec
  	setTimeout( (function (cb_) { return function () { cb_.loop1(); };})(this), this.atime);
  // if the stop time has been reached, run the stop funtion	
  setTimeout((function (cb_) { return function () { cb_.stop(); };})(this),this.stoptime);

}

ChangeImage.prototype.loop1 = function () 
{
  // change opacity of images	
  this.timer = setInterval( (function (cb_) { return function () { cb_.changeOpacity(); };})(this), this.quit);
}

clearInterval(this.timer);

//The front of transparency and the object
ChangeImage.prototype.setOpacity = function (op, z, v) 
{
  var t = this.list[ this.no ];
  t.IMG.style.opacity = op / 100;
  this.opacity = op;
  if(z != undefined) t.LI.style.zIndex = z;
  if(v != undefined) t.LI.style.visibility = ['hidden','visible'][z];
}

//Setting of the transparency
ChangeImage.prototype.changeOpacity = function () 
{
  var n = this.opacity + this.step;
  if (n < 0) 
  {
	this.setOpacity(0, 0, 0);
	this.no = ++this.no % this.list.length;
	this.setOpacity(0, 1, 1);
	this.step *= -1;
  } 
  else if (n > 100) 
  {
	clearInterval(this.timer);
	this.loop0();
  } 
  else 
  {
	this.setOpacity(n);
  }
  return;
}

ChangeImage.prototype.stop = function () 
{
	this.quit = true; // stop loop
	return;
}

// create new instance of this function
var p = new ChangeImage('photo');
//run the function
p.start(1000,2,6000);

