
function Gallery(name,w,h,showTime,fadeTime)
{
	this.name = name;
	this.w = w;
	this.h = h;
	this.noLinks = false;
	this.showTime = showTime;
	this.fadeTime = fadeTime;
	this.intervalTime = 40;
	
	this.nextInterval;
	this.playing = false;
	
	this.i = null;
	this.fadeLevel = 100;
	
	this.gallery = new Array();
	this.add = Gallery_add;
	this.place = Gallery_place;
	this.play = Gallery_play;
	this.pause = Gallery_pause;
	this.next = Gallery_next;
	this.fade = Gallery_fade;
}

function Gallery_add(imgSrc, aLink )
{
	var img = new ImageHolder(imgSrc, aLink );
	this.gallery.push( img );
}

function Gallery_place()
{
	if ( this.gallery.length > 0 ) 
	{
		this.i = 0;
		this.fadeLevel = 100;
		
		var buffer = "";
		
		buffer = buffer + "<div style='width: " + this.w + "px; text-align: left;'>";
		
		if (!this.noLinks) {
			buffer = buffer + "<a id='" + this.name + "_displayAreaLink'>";
		}
		buffer = buffer + "<img id='" + this.name + "_displayArea' onload='isDisplayComplete = true;' src='" + this.gallery[0].src + "' width='" + this.w + "' height='" + this.h + "' style='filter:alpha(opacity=100); position: absolute; z-index: 11; border: 0px;' />";
		if (!this.noLinks) {
			buffer = buffer + "</a>";	
		}
		
		if (!this.noLinks) {
			buffer = buffer + "<a id='" + this.name + "_loadingAreaLink'>";
		}
		buffer = buffer + "<img id='" + this.name + "_loadingArea' onload='isLoadingAreaComplete = true;' src='images/spacer.gif' width='" + this.w + "' height='" + this.h + "' style='filter:alpha(opacity=100); position: absolute; z-index: 10; border: 0px;' />";
		if (!this.noLinks) {
			buffer = buffer + "</a>";
		}

		buffer = buffer + "<div style='width: " + this.w + "px; height: " + this.h + "px'> </div>";
		buffer = buffer + "</div>";
	} else {
		buffer = buffer + "Gallery Empty";
	}
	
	document.write(buffer);
}

function Gallery_play()
{
	this.playing = true;
	this.next();
}

function Gallery_pause()
{
	this.playing = false;
	clearTimeout( this.nextInterval );
}

function Gallery_next()
{
	var linkHref = this.gallery[this.i].href;
	
	this.i = this.i + 1;
	
	if ( this.i >= this.gallery.length )
		this.i = 0;
	
	isLoadingAreaComplete = false;
	
	document.getElementById( this.name + "_loadingArea" ).src = this.gallery[this.i].src;
	
	if (!this.noLinks) {
		if ( linkHref == "" )
		{
			document.getElementById( this.name + "_loadingAreaLink" ).style.cursor = "default";
			document.getElementById( this.name + "_displayAreaLink" ).style.cursor = "default";
		}
		else
		{
			document.getElementById( this.name + "_loadingAreaLink" ).style.cursor = "pointer";
			document.getElementById( this.name + "_displayAreaLink" ).style.cursor = "pointer";
		}
	
		document.getElementById( this.name + "_loadingAreaLink" ).setAttribute("href", linkHref );
		document.getElementById( this.name + "_displayAreaLink" ).setAttribute("href", linkHref );
	}

	
	var self = this;
	this.nextInterval = setTimeout( function(){ self.fade(); }, this.showTime );
}

function Gallery_fade()
{
	var self = this;

	// don't fade until image under is loaded
	if ( !isLoadingAreaComplete )
	{
		this.nextInterval = setTimeout( function(){ self.fade(); }, this.intervalTime );
		return;
	}

	var adjustmentPerPeriod = 100 / ( this.fadeTime/this.intervalTime );
	
	this.fadeLevel = this.fadeLevel - adjustmentPerPeriod;
	
	var fadeElement = document.getElementById( this.name + "_displayArea" );
	var loadElement = document.getElementById( this.name + "_loadingArea" );
	
	if ( !setFadeLevel(fadeElement,this.fadeLevel) ) 
		this.fadeLevel = 0;
	
	if ( this.fadeLevel <= 0 )
	{
		// swapping src causes some flickering.  So, lets just swap details
		var tempId = fadeElement.id
		var tempZ = fadeElement.style.zIndex;
		var tempOnLoad = fadeElement.getAttribute("onload");
		
		fadeElement.setAttribute( "id", loadElement.id );
		fadeElement.setAttribute( "onload", loadElement.getAttribute("onload") );
		fadeElement.style.zIndex = loadElement.style.zIndex;

		loadElement.setAttribute( "id", tempId );
		loadElement.setAttribute( "onload", tempOnLoad );
		loadElement.style.zIndex = tempZ;
		
		//alert( loadElement.id + ", " + loadElement.style.zIndex );
		
		this.fadeLevel = 100;
		setFadeLevel(fadeElement,this.fadeLevel)
		setFadeLevel(loadElement,this.fadeLevel)
		this.next();
	}
	else
	{
		this.nextInterval = setTimeout( function(){ self.fade(); }, this.intervalTime );
	}
}

function setFadeLevel( fadeElement, level )
{
	if ( typeof fadeElement.style.MozOpacity != "undefined" ) 	
		fadeElement.style.MozOpacity = level / 100;
	else if ( typeof fadeElement.style.opacity != "undefined" ) 		
		fadeElement.style.opacity = level / 100;
	else if ( typeof fadeElement.filters == "object" ) 
		fadeElement.filters.alpha.opacity = level;
	else
		return false;
		
	return true;
}

function ImageHolder(src, href)
{
	this.src = src;
	this.href = href;
	
	if ( this.href == undefined )
		this.href = "";
		
	this.isLinked = ( this.href != "" );
		
	this.imageObject = new Image();

	this.loadImage = ImageHolder_loadImage;
}

function ImageHolder_loadImage()
{
	this.imageObject.src = this.src;
}


