/**
 * NOIX Framework
 *
 * Default NOIX SlideShow effect
 * @dependences Mootools 1.2+
 * @version 1.0
 * @author Joao Neto
 * NOIX Internet 2009
 */
var NoixSlideShow = function(){
	this.init.apply( this, arguments );
};

NoixSlideShow.prototype = {

	childrenBanner: 'li',
	
	bannersElements: [],

	currentIndex: 0,

	nameThumb: 'thumb',

	nameImage: 'image',

	delayTime: 3000,

	activeThumbClass: 'active',

	inactiveThumbClass: 'inactive',

	mouseUp: false,

	bannerActive: null,

	timeOutId: 0,
	
	init: function( ulId, nameThumb, nameImage )
	{
		this.bannersElements = $( ulId ).getChildren( this.childrenBanner );
		this.nameThumb = nameThumb;
		this.nameImage = nameImage;
	},

	start: function()
	{
		this.next();
	},

	startImageOnly: function()
	{
		this.imageOnly = true;
		this.next();
	},

	next: function()
	{
		if( this.currentIndex == this.bannersElements.length ){
			this.currentIndex = 0;
		}

		this.activateBanner( this.bannersElements[ this.currentIndex ] );
		this.currentIndex ++;
		this.registerTask();
	},

	activateBanner: function( bannerId )
	{
		var that = this;

		if( this.nameThumb ){
			var thumb = $( bannerId ).getChildren( '[name=' + this.nameThumb + ']');
			thumb.forEach( function( key ){
				that.bannerActive = key;
				that.activateThumb( key );
			});
		}

		if( this.nameImage ){
			var image = $( bannerId ).getChildren( '[name=' + this.nameImage + ']');
			image.forEach( function( key ){
				$( key ).fade( 'in' );
			});
		}
		for( var i = 0; i < this.bannersElements.length; i++ ){
			if( this.bannersElements[ i ].id != bannerId.id ){
				this.deactivateBanner( this.bannersElements[ i ] );
			}
		}
	},

	deactivateBanner: function( bannerId )
	{
		var that = this;
		if( this.nameThumb ){
			var thumb = $( bannerId ).getChildren( '[name=' + this.nameThumb + ']');
			thumb.forEach( function( key ){
				that.deactivateThumb( key );
			});
		}
		if( this.nameImage ){
			var image = $( bannerId ).getChildren( '[name=' + this.nameImage + ']');
			image.forEach( function( key ){
				$( key ).fade( 'out' );
			});
		}
	},

	registerTask: function( )
	{
		var that = this;
		this.timeOutId = window.setTimeout( function() {
			that.next()
		}, this.delayTime );
	},

	unregisterTask: function( )
	{
		window.clearTimeout( this.timeOutId );
	},

	/*
	 * Override this method to change default comportament
	 */
	activateThumb: function( key )
	{
		key.className = this.activeThumbClass;
	},
	/*
	 * Override this method to change default comportament
	 */
	deactivateThumb: function( key )
	{
		key.className = this.inactiveThumbClass;
	},

	/**
	 * Add this event to the Thumb to Change
	 * @param key Banner Id
	 */
	chooseBannerUp: function( key )
	{
		this.unregisterTask();
		this.activateBanner( key );
	},

	chooseBannerOut: function( key )
	{
		this.registerTask();
		this.mouseUp = false;
	},

	addEventThumb: function( evt, fnc )
	{
		var that = this;
		this.bannersElements.forEach( function( key ){
			var thumb = key.getChildren( '[name=' + that.nameThumb + ']');
			thumb.forEach( function( key ){
				key.addEvent( evt, fnc );
			});
		})
	},

	addEventImage: function( evt, fnc )
	{
		var banners = $( bannerId ).getChildren( '[name=' + this.nameThumb + ']');
		banners.forEach( function( key ){
			that.addEvent( evt, fnc );
		});
	}
}