Function.prototype.bind = function(object) {
  var __method = this;
  return function() {
    return __method.apply(object, arguments);
  }
}

function Gallery(id, width, height)
{
	this.id = id + '_';
	this.width = width;
	this.height = height;
	this.images = new Array();
} // constructor

Gallery.prototype.add = function(srcURL, linkURL, windowOptions) {
	var index = this.images.length;
	
	this.images[index] = [srcURL, linkURL, windowOptions];
} // add

Gallery.prototype.preload = function() {
	var preloadedimages = new Array();
	for (p = 0; p < this.images.length; p++)
	{
		preloadedimages[p] = new Image();
		preloadedimages[p].src = this.images[p][0];
	}
} // preload

Gallery.prototype.display = function(index) {
	if (this.currentIndex == index) return; // Do nothing...
	if (index > this.images.length - 1 || index < 0) return; // Index out of range
	
	// Send old one to the bottom
	this.insertImage(this.id + 'canvas0', index);
	
	// Begin the fade-in of the top image
	this.fadeValue = 0;
	this.interval = setInterval(this.fadeIn.bind(this), 25);
	
} // display

Gallery.prototype.fadeIn = function()
{
	var tempObj = document.getElementById(this.id + 'canvas0');
	
	if (this.fadeValue < 100)
	{
		this.fadeValue += 10;
		
		if (tempObj.filters)
			tempObj.filters.alpha.opacity = this.fadeValue;
		else if (tempObj.style.MozOpacity)
			tempObj.style.MozOpacity = this.fadeValue / 101;
		
	}
	else
	{
		clearInterval(this.interval);
		
		// Copy front to back
		document.getElementById(this.id + 'canvas1').innerHTML = tempObj.innerHTML;
		
		// Make the front image transparent again
		if (tempObj.filters)
			tempObj.filters.alpha.opacity = 0;
		else if (tempObj.style.MozOpacity)
			tempObj.style.MozOpacity = 0;
	}
	
} // fadeIn

Gallery.prototype.getHTML = function() {
	return '<div style="position: relative; width:' + this.width + 'px; height:' + this.height + 'px; overflow: hidden">' +
		'<div id="' + this.id + 'canvas0" style="z-index: 2; position: absolute; width: ' + this.width + 'px; height: ' + this.height + 'px; top: 0; left: 0; filter: alpha(opacity=0); -moz-opacity:0;"></div>' +
		'<div id="' + this.id + 'canvas1" style="z-index: 1; position: absolute; width: ' + this.width + 'px; height: ' + this.height + 'px; top: 0; left: 0; filter: alpha(opacity=100); -moz-opacity:1;"></div>' +
		'</div>';
} // getHTML

Gallery.prototype.insertImage = function(ID, index)
{
//	var temp = this.images[index][1] != "" ? "<a href='javascript: void(0);' onClick=\"window.open('" + this.images[index][1] + "','', '" + this.images[index][2] + "');\">" : "";
	var temp = this.images[index][1] != "" ? "<a href='" + this.images[index][1] + "'>" : "";
	temp += '<img src="' + this.images[index][0] + '" border="0">';
	temp = this.images[index][1] != "" ? temp + '</a>' : temp;
	
	document.getElementById(ID).innerHTML = temp;
} // insertImage