var aSlideshowId = 0;
function ASlideshow(target, images) {
	this.directory = '';
	this.images = images ? images : new Array();
	this.transition_type = 23;
	this._images = new Array();
	this._count = 0;
	this.delay = 5000; // in miliseconds
	this._timer_id = null;
	this._is_initialized = false;
	this._id = 'aSlideshows_' + aSlideshowId++;
	window[this._id] = this;
	
	this.target_id = isString(target) ? target : null;
	this._element = (isObject(target) && !isString(target)) ? target : null;
	this._img_element = null;

	
	this.init = function() {
		
		if (this._is_initialized) {
			return;
		}
		
		for( var i = 0; i < this.images.length; i++) {			
			var current = this.images[i];
			var temp = new Image();
			if (isString(current)) {
				temp.src = this.getImageUrl(current);
			} else {
				if (current.length >= 3) {
					temp.desc = current[2];
				}
				if (current.length >= 2) {
					temp.url = current[1];
				}
				temp.src = this.getImageUrl(current[0]);
			}
			this._images[i] = temp;
		}
		
		this._element = isNull(this._element)?getElement(this.target_id):this._element;
		if (!isNull(this._element)) {
			this._element.innerHTML = '<p>Loading Slideshow...</p>';
		}
		this._is_initialized = true;
	}

	this.getImageUrl = function(url) {
		var result = url;
		if (!isNull(this.directory)) {
			result = this.directory + '/' + url;
		}
		return result;
	}
	
	this.next = function() {
		if (this._count+1 < this._images.length) {
			this._count++;
		} else {
			this._count = 0;
		}
		this.displayCurrentImage();
	}
	
	this.prev = function() {
		if (this._count > 0) {
			this._count--;
		} else {
			this._count = this._images.length;
		}
		this.displayCurrentImage();
	}
	
	this.displayCurrentImage = function(initial) {
		var current = this._images[this._count];
		
		var title = '';
		if (current.desc) {
			title = current.desc;
		}
		var display = '<img id="' + this._id + '" src="' + current.src + '" alt="' + title + '" style="filter:revealTrans(duration=1,transition=1 );" />';
		
		if (current.url) {
			display = '<a href="' + current.url + '" target="_blank">' + display + '</a>'
		}
		if (!isNull(this._element)) {
			this._element.innerHTML = display;
		}
		// Now apply filter for smooth transitions.
		/*
		this._img_element = getElement(this._id);
		
		if (!isNull(this._img_element) && document.all && this._img_element.filters && this._img_element.filters.revealTrans) {
			this._img_element.filters.revealTrans.Transition = this.transition_type;
			this._img_element.filters.revealTrans.stop();
			this._img_element.filters.revealTrans.apply();
			this._img_element.filters.revealTrans.play();
		}*/
		
	}
	
	this.stop = function() {
		clearTimeout(this._timer_id);
		this._timer_id = null;
	}
	
	this.start = function() {
		if (!this._is_initialized) {
			this.init();
		}
		
		if (this._images.length == 0) {
			return;
		}
		if (this._timer_id) {
			this.next();
		} else {
			this.displayCurrentImage();
		}
		this._timer_id = setTimeout("window." + this._id + ".start()", this.delay);
	}
}

function init_all_slideshows() {
	for (var i = 0; i < aSlideshowId; i++) {
		window['aSlideshows_' + i].init();
	}
};
addEvent(window, 'load', init_all_slideshows);
//addEvent(window,'load', alert('wolololo' + getElement('slideshow')));

