// IE workaround :/
Function.prototype.bindAsEventListener = function(object)
{
	var method = this;

	return function(event)
	{
		return method.call(object, event || window.event);
	}
}

var id;
var width;
var height;
var interval;
var popupWidth;
var popupHeight;
var popupFile;
var nextText;
var prevText;
var autoturn;
var ieEffect;
var ieDuration;
var ieTransition;
var currentImage;
var images;
var popupImages;
var loadedImages;
var undertitle;
var spacer;
var titlePosition;
var TITLE_TOP;
var TITLE_BOTTOM;
var TITLE_BOTTOM_MERGE;
var isInternetExplodierer;

function DiaShow(id, width, height)
{
	if(width == null || height == null)
	{
		alert("No width or height given!\nAborting.");
		return;
	}

	if(id == null)
	{
		alert("No unique identifier given!\nAborting.");
		return;
	}

	if(document.all && !window.opera)
		this.isInternetExplodierer = true;
	else
		this.isInternetExplodierer = false;

	this.id = id;
	this.width = width;
	this.height = height;
	this.interval = 1000;
	this.popupWidth = 300;
	this.popupHeight = 300;
	this.popupFile = "";
	this.nextText = " --> ";
	this.prevText = " <-- ";
	this.autoturn = false;
	this.ieEffect = 0;
	this.ieDuration = 0.5;
	this.ieTransition = 12;
	this.spacer = "images/spacer.gif";
	this.currentImage = 0;
	this.images = new Array();
	this.popupImages = new Array();
	this.loadedImages = new Array();
	this.undertitle = new Array();

	this.TITLE_TOP = 1;
	this.TITLE_BOTTOM = 2;
	this.TITLE_BOTTOM_MERGE = 3;

	this.titlePosition = this.TITLE_BOTTOM_MERGE;

	if(window.diaShow == null)
		window.diaShow = new Array();

	this.instance = window.diaShow.length;
	window.diaShow[this.instance] = this;

	if(!this.isInternetExplodierer)
		window.addEventListener("load", this, true);
	else
		window.onload = this.handleEvent.bindAsEventListener(this);
}

DiaShow.prototype.handleEvent = function(e)
{
	switch(e.type)
	{
		case "load":
			if(!document.getElementById(this.id + "_pic"))
			{
				alert("Use print() to display the diashow!");
				return;
			}

			if(this.loadedImages.length == 0)
				this.load();

			if(!this.autoturn)
			{
				if(this.isInternetExplodierer)
				{
					document.getElementById(this.id + "_next").onclick = this.handleEvent.bindAsEventListener(this);
					document.getElementById(this.id + "_prev").onclick = this.handleEvent.bindAsEventListener(this);
				}
				else
				{
					document.getElementById(this.id + "_next").addEventListener("click", this, false);
					document.getElementById(this.id + "_prev").addEventListener("click", this, false);
				}
			}

			if(this.isInternetExplodierer)
				document.getElementById(this.id + "_spacer").onclick = this.handleEvent.bindAsEventListener(this);
			else
				document.getElementById(this.id + "_spacer").addEventListener("click", this, false);

			if(this.autoturn)
				window.setInterval("window.diaShow[" + this.instance + "].next()", this.interval);
			break;
		case "click":
			if(this.isInternetExplodierer)
				targetId = e.srcElement.id;
			else
				targetId = e.target.id;

			switch(targetId)
			{
				case this.id + "_prev":
					this.prev();
					break;
				case this.id + "_next":
					this.next();
					break;
				case this.id + "_spacer":
					this.popup();
					break;
				default:
					alert("Unknown object!");
			}
			break;
	}
}

DiaShow.prototype.setAutoInterval = function(interval)
{
	this.interval = interval;
}

DiaShow.prototype.setPopupSize = function(width, height)
{
	this.popupWidth = width;
	this.popupHeight = height;
}

DiaShow.prototype.setPopupFile = function(filename)
{
	this.popupFile = filename;
}

DiaShow.prototype.setButtonPrevText = function(pText)
{
	this.prevText = pText;
}

DiaShow.prototype.setButtonNextText = function(nText)
{
	this.nextText = nText;
}

DiaShow.prototype.setExplodiererEffect = function(effect)
{
	this.ieEffect = effect;
}

DiaShow.prototype.setExplodiererDuration = function(duration)
{
	this.ieDuration = duration;
}

DiaShow.prototype.setExplodiererTransition = function(transition)
{
	this.ieTransition = transition;
}

DiaShow.prototype.addImage = function(klein, gross, text)
{
	var key = this.images.length;
	this.images[key] = klein;

	if(gross != null)
		this.popupImages[key] = gross;

	if(text != null)
		this.undertitle[key] = text;
}

DiaShow.prototype.loadImages = function()
{
	if(loadedImages != null)
		return;

	for(i=0;i<this.images.length;i++)
	{
		this.loadedImages[i] = new Image();
		this.loadedImages[i].src = this.images[i];
	}
}

DiaShow.prototype.print = function()
{
	var style = "border:1px solid #000000; background:url(" + this.images[0] + ");float:none;padding:0;margin:0;";
	style+= "filter: blendTrans(Duration=" + this.ieDuration + ") revealTrans(Duration=" + this.ieDuration + ",Transition=" + this.ieTransition + ")";

	var diashow = "<div style=\"width:" + this.width + "px\" id=\"" + this.id + "\">\n";

	if(this.undertitle.length && this.titlePosition == 1)
		diashow+= "<div style=\"float: none\" id=\"" + this.id + "_text\">" +  (this.undertitle[0] != null?this.undertitle[0]:"") + "</div>\n";

	diashow+= "<div style=\"" + style + "\" id=\"" + this.id + "_pic\">\n";
	diashow+= "<img src=\"" + this.spacer + "\" style=\"width: " + this.width + "px; height: " + this.height + "px;float:none;border:0;padding:0;margin:0;";
	diashow+= (this.popupImages.length?"cursor:pointer":"") + "\" id=\"" + this.id + "_spacer\" />\n";
	diashow+= "</div>\n";

	if(this.undertitle.length && this.titlePosition == 2)
		diashow+= "<div style=\"float: none\" id=\"" + this.id + "_text\">" +  (this.undertitle[0] != null?this.undertitle[0]:"") + "</div>\n";

	if(this.undertitle.length && this.titlePosition == 3)
		diashow+= "<div style=\"float: left\" id=\"" + this.id + "_text\">" +  (this.undertitle[0] != null?this.undertitle[0]:"") + "</div>\n";

	if(!this.autoturn)
	{
		diashow+= "<div style=\"float: right\">";
		diashow+= "<a href=\"#\" id=\"" + this.id + "_prev\">" + this.prevText + "</a>&nbsp;|&nbsp;\n";
		diashow+= "<a href=\"#\" id=\"" + this.id + "_next\">" + this.nextText + "</a>\n";
		diashow+= "</div>\n";
	}

	diashow+= "</div>\n";
	document.writeln(diashow);
}

DiaShow.prototype.setTitlePosition = function(pos)
{
	this.titlePosition = pos;
}

DiaShow.prototype.setSpacer = function(imagepath)
{
	this.spacer = imagepath;
}

DiaShow.prototype.next = function()
{
	this.currentImage++;

	if(this.images[this.currentImage] == null)
		this.currentImage = 0;

	this.updateImage();
}

DiaShow.prototype.prev = function()
{
	this.currentImage--;

	if(this.images[this.currentImage] == null)
		this.currentImage = this.images.length-1;

	this.updateImage();
}

DiaShow.prototype.updateImage = function()
{
	if(document.all && !window.opera)
		document.getElementById(this.id + "_pic").filters[this.ieEffect].Apply();

	document.getElementById(this.id + "_pic").style.backgroundImage = "url(" + this.loadedImages[this.currentImage].src + ")";

	if(document.all && !window.opera)
		document.getElementById(this.id + "_pic").filters[this.ieEffect].Play();

	if(this.undertitle.length)
	{
		if(this.undertitle[this.currentImage] != null)
			document.getElementById(this.id + "_text").innerHTML = this.undertitle[this.currentImage];
		else
			document.getElementById(this.id + "_text").innerHTML = "";
	}
}

DiaShow.prototype.setAutoTurn = function(autoturn)
{
	this.autoturn = autoturn;
}

DiaShow.prototype.popup = function()
{
	if(!this.popupImages.length)
		return;

	if(this.popupImages[this.currentImage] == null)
		image = this.images[this.currentImage];
	else
		image = this.popupImages[this.currentImage];

	title = (this.undertitle[this.currentImage] != null?this.undertitle[this.currentImage]:this.images[this.currentImage]);

	if(this.popupFile != "")
		popup = this.popupFile + "?image=" + image + "&title=" + title;
	else
		popup = image;

  window.open(popup, null,"scrollbars=no,location=no,menubar=no,resizable=no, status=no,toolbar=no,width=" + this.popupWidth + ",height=" + this.popupHeight);
}

