function SlideShow( x, option ) {
	this.$ = function( s ) {
		return document.getElementById( s );
	};
	
	this.$$ = function( s ) {
		return document.getElementsByTagName( s );
	};
	
	this.empty = function( v ) {
		if ( v == undefined ) return true;
		if ( v.length == 0 ) return true;
		return false;
	};
	
	var x = ( typeof( x ) == 'object' ? x : this.$( x ) );
	this.x = x;
	
	/* OPTIONS */
	this.w = ( option.width != undefined ? option.width : x.offsetWidth );
	this.h = ( option.height != undefined ? option.height : x.offsetHeight );
	this.duration = ( option.duration == undefined ? 5000 : option.duration * 1000 );
	this.fadeRate = ( option.fadeRate == undefined ? 50 : option.fadeRate );
	this.useMatte = ( option.useMatte != undefined ? option.useMatte : true );
	this.matteColor = ( option.matteColor != undefined ? option.matteColor : '#000' );
	this.slideNumbers = ( option.slideNumbers != undefined ? option.slideNumbers : false );
	this.slideNumberBorderColor = ( option.slideNumberBorderColor != undefined ? option.slideNumberBorderColor : '#fff' );
	this.slideNumberBG = ( option.slideNumberBG != undefined ? option.slideNumberBG : 'transparent' );
	this.slideNumberColor = ( option.slideNumberColor != undefined ? option.slideNumberColor : '#fff' );
	this.slideNumberHoverBG = ( option.slideNumberHoverBG != undefined ? option.slideNumberHoverBG : '#fff' );
	this.slideNumberHoverColor = ( option.slideNumberHoverColor != undefined ? option.slideNumberHoverColor : '#000' );
	
	this.images = [];
	this.captions = [];
	this.links = [];
	this.cache = [];
	this.activeStage = 1;
	this.cursor = 0;
	this.mode = 0;
	this.wrapper = new Object;
	this.stage1 = new Object;
	this.stage2 = new Object;
	this.bg1 = new Object;
	this.bg2 = new Object;
	this.slide_nos = new Object;
	this.slide = [];
	this.durationTimer = null;
	this.fadeTimer = null;
	
	this.addSlide = function( i, w, h, c, l ) {
		var i = i;
		var c = ( c != undefined ? c : '' );
		var l = ( l != undefined ? l : '' );
		
		this.images.push( i );
		this.captions.push( c );
		this.links.push( l );
		
		var cursor = this.images.length - 1;
		
		this.cache[ cursor ] = new Image();
		this.cache[ cursor ].src = i;
		this.cache[ cursor ].width = w;
		this.cache[ cursor ].height = h;
	};
	
	this.build = function() {
		this.x.innerHTML = '';
		var t = this;
		if ( this.cache[ 0 ].complete && this.cache[ 1 ].complete ) {
			var c;
			var preloads = [];
			for ( c = 0; c < this.images.length; c++ ) {
				preloads[ c ] = new Image();
				preloads[ c ].src = this.images[ c ];
			}
			
			var x = this.x;
			
			var d = document.createElement( 'div' );
			d.id = 'ss_outer_wrapper';
			d.style.position = 'relative';
			d.style.width = this.w + 'px';
			d.style.height = this.h + 'px';
			d.onmouseover = function() {
				t.stopTimer();
			}
			d.onmouseout = function() {
				t.startTimer();
			}
			this.wrapper = x.appendChild( d );
			
			this.bg1 = this.buildBackground();
			this.bg1.style.zIndex = 1002;
			
			this.stage1 = this.buildImage( 0 );
			this.stage1.style.zIndex = 1003;
			
			if ( this.images.length > 1 ) {
				this.bg2 = this.buildBackground();
				this.bg2.style.zIndex = 1000;
				
				this.stage2 = this.buildImage( 1 );
				this.stage2.style.zIndex = 1001;
				
				this.cursor = 1;
				
				this.durationTimer = setTimeout( function(){ t.nextSlide(); }, this.duration );
			}
			
			if ( this.slideNumbers ) {
				var sn = document.createElement( 'div' );
				sn.id = 'ss_slide_numbers';
				sn.style.position = 'absolute';
				sn.style.top = '20px';
				sn.style.right = '20px';
				sn.style.width = '22px';
				sn.style.zIndex = 1005;
				this.slide_nos = this.wrapper.appendChild( sn );
				
				var i;
				for ( i = 0; i < this.images.length; i++ ) {
					var tmp = document.createElement( 'div' );
					tmp.id = 'slide_no_' + i;
					tmp.style.marginBottom = '5px';
					tmp.style.width = '20px';
					tmp.style.height = '20px';
					tmp.style.lineHeight = '20px';
					tmp.style.border = '1px solid ' + this.slideNumberBorderColor;
					tmp.style.verticalAlign = 'middle';
					tmp.style.textAlign = 'center';
					tmp.style.cursor = 'pointer';
					tmp.style.fontSize = '10px';
					tmp.style.color = this.slideNumberColor;
					tmp.style.backgroundColor = this.slideNumberBG;
					if ( i == 0 ) {
						tmp.style.color = this.slideNumberHoverColor;
						tmp.style.backgroundColor = this.slideNumberHoverBG;
					}
					tmp.innerHTML = i + 1;
					tmp.onclick = function() {
						t.jumpTo( this.id.replace( /slide_no_/, '' ) );
					}
					this.slide[ i ] = this.slide_nos.appendChild( tmp );
				}
			}
		} else {
			setTimeout( function(){ t.build(); }, 100 );
		}
	};
	
	this.buildImage = function( cursor ) {
		var img = this.cache[ cursor ];
		img.src = this.images[ cursor ];
		img.alt = this.captions[ cursor ];
		if ( !this.empty( this.links[ cursor ] ) ) {
			var l = this.links[ cursor ];
			img.style.cursor = 'pointer';
			img.onclick = function() { location.href = l; };
		}
		img.style.position = 'absolute';
		img.style.top = '50%';
		img.style.left = '50%';
		console.log( img.height );
		img.style.marginTop = '-' + Math.floor( img.height / 2 ) + 'px';
		img.style.marginLeft = '-' + Math.floor( img.width / 2 ) + 'px';
		img.style.opacity = 1;
		img.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=100)';
		img.style.zIndex = 0;
		return this.wrapper.appendChild( img );
	};
	
	this.buildBackground = function() {
		var background = document.createElement( 'div' );
		background.style.position = 'absolute';
		background.style.width = this.w + 'px';
		background.style.height = this.h + 'px';
		background.style.backgroundColor = this.bgcolor;
		background.style.opacity = 1;
		background.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=100)';
		background.style.zIndex = 0;
		return this.wrapper.appendChild( background );
	}
	
	this.nextSlide = function() {
		var t = this;
		var stage, alt, bg, bga;
		
		switch ( this.mode ) {
			case 0:
				stage = this.stage1;
				alt = this.stage2;
				bg = this.bg1;
				bga = this.bg2;
				break;
			case 1:
				stage = this.stage2;
				alt = this.stage1;
				bg = this.bg2;
				bga = this.bg1;
				break;
		}
		
		var o = parseFloat( stage.style.opacity );
		var ie = ( o * 100 ) - 5;
		
		if ( o > 0.05 ) {
			stage.style.opacity = o - 0.05;
			bg.style.opacity = o - 0.05;
			
			stage.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + ie + ')';
			bg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=' + ie + ')';

			this.fadeTimer = setTimeout( function(){ t.nextSlide(); }, this.fadeRate );
		} else {
			if ( this.cache[ this.nextImage() ].complete ) {
				this.fadeTimer = null;
				if ( this.slideNumbers ) this.setSlideNumber();
				
				this.cursor = this.nextImage();
				
				this.wrapper.removeChild( stage );
				stage = this.buildImage( this.cursor );
				switch ( this.mode ) {
					case 0:
						this.stage1 = stage;
						break;
					case 1:
						this.stage2 = stage;
						break;
				}
				
				this.mode = ( this.mode ? 0 : 1 );
				
				bg.style.zIndex = 1000;
				bga.style.zIndex = 1002;
				stage.style.zIndex = 1001;
				alt.style.zIndex = 1003;
				
				bg.style.opacity = 1;
				bg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity=100)';
				
				this.durationTimer = setTimeout( function(){ t.nextSlide(); }, this.duration );
			} else {
				this.fadeTimer = setTimeout( function(){ t.nextSlide(); }, 1000 );
			}
		}
	};
	
	this.nextImage = function() {
		var i = this.cursor  + 1;
		if ( i == this.images.length ) i = 0;
		return i;
	};
	
	this.setSlideNumber = function() {
		var i;
		for ( i = 0; i < this.images.length; i++ ) {
			this.slide[ i ].style.color = this.slideNumberColor;
			this.slide[ i ].style.background = this.slideNumberBG;
		}
		
		this.slide[ this.cursor ].style.color = this.slideNumberHoverColor;
		this.slide[ this.cursor ].style.backgroundColor = this.slideNumberHoverBG;
	};
	
	this.stopTimer = function() {
		clearTimeout( this.durationTimer );
	};
	
	this.startTimer = function() {
		var t = this;
		this.durationTimer = setTimeout( function(){ t.nextSlide(); }, t.duration );
	}
	
	this.jumpTo = function( s ) {
		this.stopTimer();
		this.cursor = parseInt( s );
		var stage, alt;
		
		switch ( this.mode ) {
			case 0:
				stage = this.stage1;
				alt = this.stage2;
				break;
			case 1:
				stage = this.stage2;
				alt = this.stage1;
				break;
		}
		
		this.wrapper.removeChild( stage );
		this.wrapper.removeChild( alt );
		
		stage = this.buildImage( this.cursor );
		stage.style.zIndex = 1003;
		this.setSlideNumber();
		
		this.cursor = this.nextImage();
		alt = this.buildImage( this.cursor );
		alt.style.zIndex = 1001;
		
		switch ( this.mode ) {
			case 0:
				this.stage1 = stage;
				this.stage2 = alt;
				break;
			case 1:
				this.stage2 = stage;
				this.stage1 = alt;
				break;
		}
	};
}
