//	----------------------------------------------------------------------------
//	SLIDESHOW HANDLER
//	----------------------------------------------------------------------------
	var slideshow = {

		defaults: {
			container_class: '.slideshow_container'
		},

		items: [],

	//	------------------------------------------------------------------------
		add: function( container ){

			var slideshow_element = new class_slideshow_element_basic();
			slideshow_element.init( container, this );
			this.items.push( slideshow_element );
		},

	//	------------------------------------------------------------------------
		init: function(){

			var obj = this;

			$( this.defaults.container_class ).each(

				function(){

					obj.add( this );
				}
			);
		}
	}

//	----------------------------------------------------------------------------
//	PARENT ELEMENT
//	----------------------------------------------------------------------------
	var class_slideshow_element = function(){}

	class_slideshow_element.prototype.container = null;
	class_slideshow_element.prototype.handler = null;
	class_slideshow_element.prototype.items = [];
	class_slideshow_element.prototype.position = 1;
	class_slideshow_element.prototype.itemHeight = 1;

	class_slideshow_element.prototype.init = function( container, handler ){

		this.container = container;
		this.handler = handler;

		this.setup();
	}

	class_slideshow_element.prototype.reset = function(){}
	class_slideshow_element.prototype.setup = function(){}
	class_slideshow_element.prototype.setupItem = function(){}
	class_slideshow_element.prototype.setupItems = function(){

		var obj = this;
		obj.items = [];

		this.itemHeight = $( this.links.items ).children( 'li:first' ).height();

		$( this.links.items ).css(
			{
				position: 'relative',
				overflow: 'hidden',
				height: obj.itemHeight + 'px'
			}
		);

		$( this.links.items ).children( 'li' ).each(
			function(){
				obj.items.push( this );
				obj.setupItem( this )
			}
		);
 
	}

	class_slideshow_element.prototype.next = function(){}

	class_slideshow_element.prototype.setPosition = function( position ){}

//	----------------------------------------------------------------------------
//	ELEMENT BASIC
//	----------------------------------------------------------------------------
	var class_slideshow_element_basic = function(){}
	class_slideshow_element_basic.prototype = new class_slideshow_element();

//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.links = {
		controlBar: null,
		next: null,
		prev: null,
		pagerHolder: null,
		pagers: []
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.defaults = {
		zindex_base: 1000,
		modules_enabled: {
			controlBar: true,
			pagers: true
		},
		speed: {
			animation: 1000
		}
	}

//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.setup = function(){

		$( this.container ).css(
			{
				position: 'relative'
			}
		);

		this.links.items = $( this.container ).children( 'ul.items' );
		this.setupItems();

		if( this.defaults.modules_enabled.controlBar )
			this.setupControlBar();

		if( this.defaults.modules_enabled.pagers )
			this.setupPagers();

		this.setPosition( this.position, 'next' );
	}

//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.setupControlBar = function(){

		var obj = this;

		$( this.container ).append(
			this.links.controlBar = $('<div class="slideshow_contolbar"></div>')
		);

		$( this.links.controlBar ).append(
			this.links.next = $('<div class="slideshow_next"><a href="#">Next</a></div>')
		);
		$( this.links.controlBar ).append(
			this.links.prev = $('<div class="slideshow_prev"><a href="#">Prev</a></div>')
		);

		$( this.links.controlBar ).css(
			{
				'z-index': this.defaults.zindex_base + 1
			}
		);

		$( this.links.next ).click(
			function(){
				obj.next( obj );
				return false;
			}
		);

		$( this.links.prev ).click(
			function(){
				obj.prev( obj );
				return false;
			}
		);
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.setupPagers = function(){

		var obj = this;

		$( this.links.controlBar ).append(
			this.links.pagerHolder = $('<ul class="slideshow_pagers"></ul>')
		);

		for( i = 0; i < this.items.length; i++ ){

			$( this.links.pagerHolder ).append(
				this.links.pagers[ i ] = $('<li><a href="#">' + (i + 1) + '</a></li>')
			);

			$( this.links.pagers[ i ] ).bind(
				'click',
				{ position: i+1 },
				function( event ){
					obj.stepToPage( obj, event.data.position );
					return false;
				}
			);
		}		
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.reset = function(){

		var obj = this;

		$( this.container ).children( 'li' ).each(
			function(){
				obj.setupItem( this );
			}
		);
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.setupItem = function( item ){

		$( item )
		.css(
			{
				position: 'absolute',
				top: '0px'
			}
		)
		.hide();
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.setPosition = function( position1, position2 ){

		this.reset();

		this.showItem( position1, this.defaults.zindex_base );
		this.showItem( position2, this.defaults.zindex_base - 1 );
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.showItem = function( position, zindex ){

		var obj = this;
		var elementIndex = position - 1;

		if( position > obj.items.length )
			elementIndex = 0;

		if( position < 1 )
			elementIndex = (obj.items.length - 1);

		$( this.items[ elementIndex ] )
		.css(
			{
				'top':  0 + 'px',
				'left':  0 + 'px',
				'z-index':  zindex
			}
		)
		.fadeTo(0, 1 )
		.show();
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.stepToPage = function( obj, position ){

		this.setPosition( obj.position, position );
		this.page(
			obj,
			function(){
				obj.position = position;
			}
		);
	}

//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.prev = function( obj ){

		this.setPosition( obj.position, obj.position - 1 );
		this.page(
			obj,
			function(){
				obj.position--;
				if( obj.position < 1 )
					obj.position = obj.items.length;
			}
		);		
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.next = function( obj ){

		this.setPosition( obj.position, obj.position + 1 );
		this.page(
			obj,
			function(){
				obj.position++;
				if( obj.position > obj.items.length )
					obj.position = 1;
			}
		);		
	}
//	----------------------------------------------------------------------------
	class_slideshow_element_basic.prototype.page = function( obj, callback ){

		$( obj.items[ obj.position - 1 ] ).animate(
			 {
				 top: obj.itemHeight + 'px',
				 opacity: 0.5
			 },
			obj.defaults.speed.animate,
			callback
		);
	}


//	----------------------------------------------------------------------------
//	SLIDESHOW INIT
//	----------------------------------------------------------------------------
	$( document ).ready(
		function(){
			slideshow.init();
		}
	);
