function $(id) {
	return document.getElementById(id);
}

function Slideshow(container, options) {
	this.imagecontainer = $(container.imagecontainer);
	this.titlecontainer = $(container.titlecontainer);	
	this.options = options;
	this.images = new Array();
	this.current = 0;
	this.status = 'play';
	this.position = options.position;
	
	this.init = function() {
		var top_pos_container = parseInt(MA_layerTop(this.imagecontainer));
		var left_pos_container = parseInt(MA_layerLeft(this.imagecontainer));	
		
		for (var i = 0; i < this.options.uri.length; i++) {			
			var objImage = new Image();
			objImage.src = this.options.uri[i];
			objImage.alt = this.options.title[i];
			objImage.xOpacity = 0;
			objImage.align = 'center';
			objImage.style.display = 'none';
			objImage.style.position = 'absolute';
			objImage.style.zIndex = 2;
			objImage.id = i;
			
			if (this.position == 'CENTER') {					
				var top_pos = top_pos_container + (parseInt(this.imagecontainer.offsetHeight) / 2) - (parseInt(objImage.height) / 2);
				var left_pos = left_pos_container + (parseInt(this.imagecontainer.offsetWidth) / 2) - (parseInt(objImage.width) / 2);
			} else if (this.position == 'TOPLEFT') {
				var top_pos = top_pos_container;
				var left_pos = left_pos_container;
			}		
			
			objImage.style.top = top_pos;
			objImage.style.left  = left_pos;
			
			var bg = document.createElement('div');
			bg.style.display = 'none';
			bg.style.position = 'absolute';			
			bg.style.xOpacity = 0;
			bg.id = 'bg' + i;
			bg.style.top = top_pos;
			bg.style.left  = left_pos;
			bg.style.width = parseInt(objImage.width) ;
			bg.style.height = parseInt(objImage.height);
			bg.style.zIndex = 1;
			bg.style.backgroundColor = '#000000';
			
			this.images.push(objImage);
			this.imagecontainer.appendChild(objImage);
			this.imagecontainer.appendChild(bg);	

			if (i == 0) {
				this.images[0].style.display = '';
				this.images[0].xOpacity = .99;
				
				$('bg' + this.images[0].id).style.display = 'inline';
				$('bg' + this.images[0].id).xOpacity = .99;
				
				if (this.titlecontainer) this.titlecontainer.innerHTML = this.images[0].alt;
			}
		}	
	};	
	
	this.pause = function() {
		this.status = 'pause';
	};
	
	this.play = function() {
		this.status = 'play';
		this.fade();
	};
	
	this.isPaused = function() {
		return this.status == 'play' ? false : true;
	};

	this.showCurrent = function() {
		cOpacity = this.images[this.current].xOpacity;
		nIndex = this.images[this.current + 1] ? this.current + 1 : 0;	
		
		if (cOpacity <= .5) {
			this.images[this.current].style.display = 'none';
			this.images[this.current].xOpacity = 0;
			this.images[nIndex].style.display = '';
			this.images[nIndex].xOpacity = .99;
			
			$('bg' + this.images[this.current].id).style.display = 'none';
			$('bg' + this.images[this.current].id).xOpacity = .0;
			$('bg' + this.images[nIndex].id).style.display = 'inline';
			$('bg' + this.images[nIndex].id).xOpacity = .99;			
			
			if (this.titlecontainer) this.titlecontainer.innerHTML = this.images[nIndex].alt;
		} else {
			this.images[this.current].style.display = '';
			this.images[this.current].xOpacity = .99;
			this.images[nIndex].style.display = 'none';
			this.images[nIndex].xOpacity = 0;
			
			$('bg' + this.images[this.current].id).style.display = 'inline';
			$('bg' + this.images[this.current].id).xOpacity = .99;
			$('bg' + this.images[nIndex].id).style.display = 'none';
			$('bg' + this.images[nIndex].id).xOpacity = .0;
			
			if (this.titlecontainer) this.titlecontainer.innerHTML = this.images[this.current].alt;
		}
		
		this.setOpacity(this.images[this.current]);
		this.setOpacity(this.images[nIndex]);
		
		this.setOpacity($('bg' + this.images[this.current].id));
		this.setOpacity($('bg' + this.images[nIndex].id));
	};
	
	this.fade = function() {
		if (!this.images.length) return;
		if (this.isPaused()) return;

		cOpacity = this.images[this.current].xOpacity;
		nIndex = this.images[this.current + 1] ? this.current + 1 : 0;	
		nOpacity = this.images[nIndex].xOpacity;
		
		cOpacity -= .01; 
		nOpacity += .01;
		
		this.images[nIndex].style.display = '';	
		$('bg' + this.images[nIndex].id).style.display = '';				
		
		this.images[this.current].xOpacity = cOpacity;
		$('bg' + this.images[this.current].id).xOpacity = cOpacity;
		
		this.images[nIndex].xOpacity = nOpacity;
		$('bg' + this.images[nIndex].id).xOpacity = nOpacity;
		
		if (cOpacity <= .5) {
			if (this.titlecontainer) this.titlecontainer.innerHTML = this.images[nIndex].alt;
		} else {
			if (this.titlecontainer) this.titlecontainer.innerHTML = this.images[this.current].alt;
		}
		
		this.setOpacity(this.images[this.current]);
		this.setOpacity(this.images[nIndex]);
		
		this.setOpacity($('bg' + this.images[this.current].id));
		this.setOpacity($('bg' + this.images[nIndex].id));
		
		if (cOpacity <= 0) {
			this.images[this.current].style.display = 'none';
			$('bg' + this.images[this.current].id).style.display = 'none';
			
			this.current = nIndex;
			setTimeout('fade()', 150);
		} else if (cOpacity >= .98) {
			this.pause();			
			setTimeout('play()', 3000);	
		} else {
			setTimeout('fade()', 50);
		}	
	};	

	this.setOpacity = function(obj) {
		if (obj.xOpacity > .99) {
			obj.xOpacity = .99;
			return;
		}
		
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = 'alpha(opacity=' + (obj.xOpacity * 100) + ')';
	};
	
	this.showCertainImage = function(obj) {
		this.pause();		
		var bigImg = obj.getAttribute('big');
		
		for (var i = 0; i < this.images.length; i++) {		
			if (this.images[i].src.indexOf(bigImg) != -1) {				
				this.images[i].style.display = '';
				this.images[i].xOpacity = .99;
				
				$('bg' + this.images[i].id).style.display = '';
				$('bg' + this.images[i].id).xOpacity = .99;
				
				if (this.titlecontainer) this.titlecontainer.innerHTML = this.images[i].alt;
				
				this.setOpacity(this.images[i]);
				this.setOpacity($('bg' + this.images[i].id));
				this.current = i;
			} else {				
				this.images[i].style.display = 'none';
				this.images[i].xOpacity = .0;
				
				$('bg' + this.images[i].id).style.display = 'none';
				$('bg' + this.images[i].id).xOpacity = .0;
				
				this.setOpacity(this.images[i]);
				this.setOpacity($('bg' + this.images[i].id));
			}
		}
	};
};
