if ( window.addEventListener ) {
	window.addEventListener( "load", SlideShow_onload, false);
}
else {
	window.attachEvent( "onload", SlideShow_onload );
}

function SlideShow_onload () {
	if ( !document.getElementById ) {
		return;
	}
	var shows = document.getElementsByName( "slideshow" );
	for ( var i = 0; i < shows.length; i++ ) {
		var show = new SlideShow( shows[i] );
	}
	return;
}

var SlideShow_DEF_INTERVAL = 2;
var SlideShow_DEF_PACE = 0.05;

var SlideShow_showArray = new Array();

function SlideShow ( show ) {
	SlideShow_create( this );
	this.init( show );
	if ( this.auto ) {
		this.start();
	}
}

function SlideShow_create ( obj ) {
	obj.init     = SlideShow_init;
	obj.initArgs = SlideShow_initArgs;
	obj.start    = SlideShow_start;
	obj.pause	 = SlideShow_pause;
	obj.goFirst    = SlideShow_goFirst;
	obj.goLast     = SlideShow_goLast;
	obj.goNext     = SlideShow_goNext;
	obj.goPrevious = SlideShow_goPrevious;
	obj.goPage     = SlideShow_goPage;
	obj.change     = SlideShow_change;
	obj.setPageNumber = SlideShow_setPageNumber;
	obj.getCurrentPage       = SlideShow_getCurrentPage;
	obj.getCurrentPageNumber = SlideShow_getCurrentPageNumber;
}

function SlideShow_init ( show ) {
	this.initArgs( show );
	
	this.cIndex = 0;
	this.show = show;
	this.slides = SlideShow_getSlides( show );
	for ( var i = 0; i < this.slides.length; i++ ) {
		if ( i == this.cIndex ) {
			this.slides[i].style.display = this.display;
			this.slides[i].xOpacity = 1;
		}
		else {
			this.slides[i].style.display = "none";
			this.slides[i].xOpacity = 0;
		}
	}
	
	this.id = SlideShow_showArray.length;
	SlideShow_showArray[ this.id ] = this;
	
	this.setPageNumber();
}

function SlideShow_initArgs ( show ) {
	this.auto     = false;
	this.repeat	  = false;
	this.fade     = false;
	this.interval = SlideShow_DEF_INTERVAL;
	this.pace     = SlideShow_DEF_PACE;	
	this.display  = "block";
	
	if ( show.attributes["auto"] && show.attributes["auto"].value == "true" ) {
		this.auto   = true;
		this.repeat = true;
		this.fade   = true;
	}
	if ( show.attributes["repeat"] ) {
		this.repeat = show.attributes["repeat"].value;
	}
	if ( show.attributes["fade"] ) {
		this.fade = show.attributes["fade"].value;
	}
	if ( show.attributes["interval"] ) { 
		this.interval = Number( show.attributes["interval"].value );
	}
	if ( show.attributes["pace"] ) { 
		this.pace = Number( show.attributes["pace"].value );
	}
	if ( show.attributes["display"] ) {
		this.display = show.attributes["display"].value;
	}
}

function SlideShow_getSlides ( show ) {
	var slideDiv = getChildById( show, "slides" );
	var children = getChildren( slideDiv );
	var slides = new Array();
	var j = 0;
	for ( var i = 0; i < children.length; i++ ) {
		if ( children[i].nodeName != "#comment" && children[i].nodeName != "#text" ) {
			slides[j++] = children[i];
		}
	}
	return slides;
}

function getChildren ( parent ) {
	var children = null;
	if ( parent.children ) {
		children = parent.children;
	}
	else {
		children = parent.childNodes;
	}
	return children;
}

function getChildById ( parent, elementId ) {
	var element = null;
	if ( parent.children ) {
		element = parent.children[ elementId ];
	}
	else {
		for ( var i = 0; i < parent.childNodes.length; i++ ) {
			if ( parent.childNodes[i].id == elementId ) {
				element = parent.childNodes[i];
				break;
			}
		}
	}
	return element;
}

function SlideShow_getCurrentPage () {
	return this.slides[ this.cIndex ];
}

function SlideShow_getCurrentPageNumber () {
	return this.cIndex + 1;
}

function SlideShow_start () {
	this.auto = true;
	this.timer = setTimeout( "SlideShow_wakeup(" +  this.id + ");", this.interval * 1000 );
}

function SlideShow_pause () {
	this.auto = false;
	clearTimeout( this.timer );
}

function SlideShow_goFirst () {
	this.nIndex = 0;
	this.change();
}

function SlideShow_goLast () {
	this.nIndex = this.slides.length - 1;
	this.change();
}

function SlideShow_goNext () {
	this.nIndex = this.cIndex + 1;
	if ( this.nIndex == this.slides.length ) {
		this.nIndex = 0;
		if ( !this.repeat ) {
			return;
		}
	}
	this.change();
}

function SlideShow_goPrevious () {
	this.nIndex = this.cIndex - 1;
	if ( this.nIndex < 0 ) {
		this.nIndex = this.slides.length - 1;
		if ( !this.repeat ) {
			return;
		}
	}
	this.change();
}

function SlideShow_goPage ( page ) {
	this.nIndex = page - 1;
	this.change();
}

function SlideShow_change () {
	if ( this.nIndex == this.cIndex ) {
		return;
	}
	if ( this.fade ) {
		var nOpacity = this.slides[ this.nIndex ].xOpacity + this.pace;
		var cOpacity = this.slides[ this.cIndex ].xOpacity - this.pace;
		
		this.slides[ this.nIndex ].style.display = this.display;
		this.slides[ this.nIndex ].xOpacity = nOpacity;
		this.slides[ this.cIndex ].xOpacity = cOpacity;
		
		SlideShow_setOpacity( this.slides[ this.nIndex ] );
		SlideShow_setOpacity( this.slides[ this.cIndex ] ); 
		
		if ( cOpacity <= 0 ) {
			this.slides[ this.cIndex ].style.display = "none";
			this.cIndex = this.nIndex;
			this.setPageNumber();
			if ( this.auto ) {
				this.start();
			}
		} 
		else {
			setTimeout( "SlideShow_fade(" +  this.id + ");", 50 );
		}
	}
	else {
		this.slides[ this.cIndex ].style.display = "none";
		this.slides[ this.nIndex ].style.display = this.display;
		this.cIndex = this.nIndex;
		this.setPageNumber();
		if ( this.auto ) {
			this.start();
		}
	}
}

function SlideShow_setPageNumber () {
	var browser = getChildById( this.show, "browser" );
	if ( browser ) {
		var page = getChildById( browser, "page" );
		if ( page ) {
			page.innerHTML = this.cIndex + 1;
		}
		var npages = getChildById( browser, "npages" );
		if ( npages ) {
			npages.innerHTML = this.slides.length;
		}
	}
}

function SlideShow_setOpacity ( img ) {
	if ( img.xOpacity > 1 ) {
		img.xOpacity = 1;
	}
	img.style.opacity = img.xOpacity;
	img.style.MozOpacity = img.xOpacity;
	img.style.filter = "alpha(opacity=" + (img.xOpacity*100) + ")";
}

function SlideShow_wakeup ( showId ) {
	SlideShow_showArray[ showId ].goNext();
}

function SlideShow_fade ( showId ) {
	SlideShow_showArray[ showId ].change();
}
