/**
 * Slideshow that shows a series of images, sliding either
 * by opacity, verical sliding or horizontal sliding.
**/
var slideshow = function ( pelement, options )
{
	this.seconds = 6;
	this.method = "horizontal";
	this.speed = 50;
	if ( !options ) options = false;
	if ( pelement ) this.init ( pelement, options );
}

// Initialize the slideshow
slideshow.prototype.init = function ( pelement, options )
{
	if ( options )
		for ( var o in options ) this[o] = options[o];
	if ( this.speed <= 0 ) this.speed = 1;
	else if ( this.speed >= 100 ) this.speed = 99;
	pelement = $(pelement);	
	if ( !pelement ) return false;
	if ( !pelement.id ) pelement.id = "element" + Math.floor ( ( Math.random () * 99999999 ) );
	this.container = pelement;
	this.container.slideshow = this;
	if ( getElementsByClassName ( 'ArrowNext', pelement ).length )
	{
		this.anext = getElementsByClassName ( 'ArrowNext', pelement )[0];
		this.aprev = getElementsByClassName ( 'ArrowPrev', pelement )[0];
	}
	else this.anext = false;
	var images = getElementsByClassName ( 'Item', pelement );
	this.images = new Array ();
	for ( var a = 0; a < images.length; a++ )
	{
		this.images[a] = document.createElement ( 'div' );
		this.images[a].innerHTML = images[a].innerHTML;
	}
	this.content = getElementsByClassName ( 'Content', pelement )[0];
	this.content.innerHTML = '<div class="Image"></div><div class="Image"></div>';
	this.s1 = getElementsByClassName ( 'Image', this.content )[0];
	this.s2 = getElementsByClassName ( 'Image', this.content )[1];
	this.content.className = 'ContentInitialized';
	this.index = 0;
	if ( this.anext )
		{
		this.anext.s = this; this.aprev.s = this;
		this.anext.onclick = function () { if ( this.s.minterval ) return; clearInterval ( this.s.autoval ); this.s.slide ( ++this.s.index, 1 ); }
		this.aprev.onclick = function () { if ( this.s.minterval ) return; clearInterval ( this.s.autoval ); this.s.slide ( --this.s.index, -1 ); }
	}
	if ( this.method == "horizontal" )
	{
		this.s1.style.left = (0-this.content.offsetWidth) + "px";
		this.s2.style.left = "0px";
	}
	else if ( this.method == "vertical" )
	{
		this.s1.style.top = (0-this.content.offsetHeight) + "px";
		this.s2.style.top = "0px";
	}
	this.s2.innerHTML = this.images[0].innerHTML;
	this.autoval = setInterval ( "var s = $('"+this.container.id+"').slideshow; s.slide(++s.index,1);", this.seconds*1000 );
}

// Move slides into position
slideshow.prototype.move = function ( )
{
	if ( this.i < 100 )
	{
		this.i += 100/(100-this.speed);
		if ( this.i > 100 ) this.i = 100;
		var power = Math.pow(Math.sin ( this.i / 100 * 90 * Math.PI / 180 ),3);
		if ( this.method == "horizontal" )
		{
			this.s1.style.left = Math.floor(this.s1.cx-((this.s1.cx-this.s1.tx)*power))+"px";
			this.s2.style.left = Math.floor(this.s2.cx-((this.s2.cx-this.s2.tx)*power))+"px";
		}
		else if ( this.method == "vertical" )
		{
			this.s1.style.top = Math.floor(this.s1.cy-((this.s1.cy-this.s1.ty)*power))+"px";
			this.s2.style.top = Math.floor(this.s2.cy-((this.s2.cy-this.s2.ty)*power))+"px";
		}
		else if ( this.method == "opacity" )
		{
			setOpacity ( 1 * power, this.s2 );
		}
	}
	else { clearInterval ( this.minterval ); this.minterval = false; }
}

// Slide to index
slideshow.prototype.slide = function ( ind, dir )
{
	if ( this.minterval )
		clearInterval ( this.minterval );
	if ( this.images.length <= 1 )
	{
		clearInterval ( this.minterval );
		return false;
	}
	this.index = ind;
	if ( this.index < 0 ) this.index = this.images.length - 1;
	if ( this.index >= this.images.length ) this.index = 0;
	
	if ( this.method == "horizontal" )
	{
		this.s1.cx = 0;
		this.s2.tx = 0;
		if ( dir == 1 )
		{
			this.s1.tx = 0-this.content.offsetWidth;
			this.s2.cx = this.content.offsetWidth;
		}
		else
		{
			this.s2.cx = 0-this.content.offsetWidth;
			this.s1.tx = this.content.offsetWidth;	
		}
	}
	else if ( this.method == "vertical" )
	{
		this.s1.cy = 0;
		this.s2.ty = 0;
		if ( dir == 1 )
		{
			this.s1.ty = 0-this.content.offsetHeight;
			this.s2.cy = this.content.offsetHeight;
		}
		else
		{
			this.s2.cy = 0-this.content.offsetHeight;
			this.s1.ty = this.content.offsetHeight;	
		}
	}
	
	this.i = 0;
	this.move();
	this.s1.innerHTML = this.s2.innerHTML;
	this.s2.innerHTML = this.images[this.index].innerHTML;
	this.minterval = setInterval ( "$('"+this.container.id+"').slideshow.move()", 25 );
}




