
var fade_obj_id = "";

//
//
function Slide(id, duration)
{
	this._id = id;
	
	this.duration = duration;
	this.nextSlide = null;
	this.previousSlide = null;
	
	//
	//
	this.appear = function()
	{
		var element = document.getElementById(this._id);
		
		if (element) {		
			element.style.display = "block";

			// omg this is horribly hacky!!! but it works...
			fade_obj_id = this._id;
			
			resetFadeIn();
			fadeIn();
		}
		
	}
	
	//
	//
	this.disappear = function()
	{
		var element = document.getElementById(this._id);
		
		if (element) {
			element.style.display = "none";

		}
	}

}

//
//
function Slideshow(varname)
{
	
	this._varname = varname;
	
	this._all_slides = null;
	
	this._last_timeout_id = -1;
	
	this._is_auto_playing = false;
	this._current_slide = null;
	
	this.direction = 1; // 1 = forward;  -1 = backward

	//
	//
	this.init = function(slidesArray)
	{	
		_all_slides = slidesArray;

		if (!_all_slides || _all_slides.length < 1) {
			return;
		}
				
		var c = null;
		var p = null;
		var n = null;
		
		for (var i = 0; i < _all_slides.length; i++) {
			c = _all_slides[i];
						
			p = (i > 0 ? _all_slides[i - 1] : null);
			n = (i < _all_slides.length - 1 ? _all_slides[i + 1] : null);
			
			c.previousSlide = (p == null ? _all_slides[_all_slides.length - 1] : p); // if there's no previous slide, wrap around to the end slide
			c.nextSlide = (n == null ? _all_slides[0] : n); // if there is no next slide, wrap around to the beginning slide
			
			//alert(c.previousSlide._id + " ... " + c._id + " ... " + c.nextSlide._id);
			
		}
		
		_current_slide = _all_slides[0];
		
		this.play();
	}

	//
	//
	this.play = function()
	{
		if (!_current_slide) {
			_is_auto_playing = false;
			return;
		}
		
		_is_auto_playing = true;
		
		_current_slide.appear();

		this._prepareFutureSlide();	
	}

	//
	this.isPlaying = function()
	{
		return _is_auto_playing;
	}
	
	//
	//
	this.stop = function()
	{
		_is_auto_playing = false;
		
		clearTimeout(this._last_timeout_id);
	}

	//
	//
	this.showNextSlide = function()
	{
		if (!_current_slide || !_current_slide.nextSlide) {
			return;
		}
		
		_current_slide.disappear();
		
		_current_slide = _current_slide.nextSlide;
		_current_slide.appear();
		
		if (_is_auto_playing) {
			this._prepareFutureSlide();
		}
	}

	//
	//
	this.showPreviousSlide = function()
	{
		if (!_current_slide || !_current_slide.previousSlide) {
			return;
		}
		
		_current_slide.disappear();
		
		_current_slide = _current_slide.previousSlide;
		_current_slide.appear();
		
		if (_is_auto_playing) {
			this._prepareFutureSlide();
		}

	}
	
	//
	//
	this._prepareFutureSlide = function()
	{
		if (!_current_slide) {
			return;
		}
		
		// clear any previous timeouts
		clearTimeout(this._last_timeout_id);
		
		if (this.direction > 0) {
			this._last_timeout_id = setTimeout(this._varname + ".showNextSlide()", _current_slide.duration * 1000);
		}
		else {
			this._last_timeout_id = setTimeout(this._varname + ".showPreviousSlide()", _current_slide.duration * 1000);
		}
	}

	

}


//
// fading stuff
//

var objref, mozfadevar;

//
//
function resetFadeIn()
{
	objref = document.getElementById(fade_obj_id);
	objref.style.visibility = "hidden";
	
	if (!objref.filters) {
		objref.style.MozOpacity = 0;
	}
}



//
//
function fadeIn()
{
	objref = document.getElementById(fade_obj_id);
	
	if (objref.filters){
		objref.style.filter="blendTrans(duration=1)";
		objref.filters.blendTrans(duration=1).Apply();
		objref.filters.blendTrans.Play();
	}
		
	objref.style.visibility = "visible";
	
	if (objref.style.MozOpacity) {
		mozfadevar = setInterval("mozFadeInSupport()", 90);
	}
}

//
//
function mozFadeInSupport()
{	
	var Mozfadedegree = 0.05; //fade in degree for NS6+ (number between 0 and 1. Recommended max: 0.2)

	if (parseFloat(objref.style.MozOpacity) < 1) {
		objref.style.MozOpacity = parseFloat(objref.style.MozOpacity) + Mozfadedegree;
	}
	else{
		clearInterval(mozfadevar)
	}
}





