Interator = {
	interate: function( el , func , params , notExecString )
	{
		var isString = false;
		var execFor; 
		if( typeof el == "object" || ( isString = typeof el == "string" ) )
			size = el.length;
		else
		{
			if( !isNaN( el ) )
				size = el;
			else
				return false;
		}
		
		execFor = ( isString ) ? ( notExecString ) ? false : true : true;
		if( execFor )
		{
			for( var i = 0; i < size; i++ )
			{
				var param = ( isString ) ? ( el.charAt( i ) ) : el[i]; 
				func.call( this , param  , params , i );							
			}
		}
		else
			func.call( this , el , params );				
	}
};

function Transition( id ){

	this.root = ( typeof id == "object" ) ? ( id ) : ( ( typeof id == "string" ) ? document.getElementById( id ) : null ); 
	
	this.oIdElements   = { ids: [] , els: [] };
	this.toolbar 	   = null;
	this.confToolbar   = null;
	
	var iCallCreateElement = 100 , self = this;
	
	this.addElement = function( el , oParam , position )
	{
		oParam = oParam || {};   	
		oParam.length = position ? position : el.length; 
		Interator.interate( el , createElement , oParam , true );			
	}
	
	var createElement = function( el , param , i )
	{
		var temp = null;
		if( typeof i == "undefined" )
			temp = --iCallCreateElement;	
		else
		{	
			var qntObjects = ( !param.length ) ? iCallCreateElement : param.length; 
			temp = qntObjects / (i+1);				
		}
		
		var div = document.createElement( "DIV" ); 
		div.id = temp;
		div.innerHTML = el;
		var id = temp;				
			
		for( i in param )
		{
			if( typeof param[i] == "object" )
			{
				for( j in param[i] )
				{
					div[i][j] = param[i][j];
				}
			}
			else
			{
				if( i == "id" )
					id = param[i];
				
				div[i] = param[i];
			}
		}	
		self.oIdElements.ids.push( id );
		self.oIdElements.els.push( div );
	}
	
	this.render = function()
	{
		Interator.interate( this.oIdElements.els , this.appendRoot );
		this.toolbar = new Toolbar( this.oIdElements , this.root );
		try{
			this.toolbar.init( this.confToolbar.limit );
		}catch(e){this.toolbar.init();}
	}
	
	this.appendRoot = function( el , param , i ){ if( i == 0 ){ self.root.appendChild( el ); } }
	
	this.getToolbar = function()
	{
		return this.toolbar;
	}
}

function Toolbar( elements , root )
{ 
	var self 	  	  = this , timerIn;
	this.root     	  = root;
	
	/* tempos de transição */ 
	this.time		  = null;
	this.timeFuncExec = null;
	
	this.els 	  	  = elements;
	this.count 	  	  = elements.els.length;
	this.limitPage 	  = null;
	this.pages 	  	  = null;
	this.pageActual   = 1;
	this.elActual     = 0;
	this.positionPage = 0;
	this.iniPage 	  = 1;
	this.finalPage 	  = null;

	this.set = function( property , value )
	{	
		this[property] = value;	
	}

	this.init = function( elLimit )
	{
		if( this.count < elLimit || !elLimit )
			elLimit = this.count;
		this.set( "limitPage" , elLimit );
		this.set( "pages" ,  Math.ceil( this.count / this.limitPage ) );
		this.set( "finalPage" , elLimit );
	}			 

	this.render = function(){}

	this.current = function( position )
	{
		if( position < this.limitPage )
			this.set( "positionPage" , position );
		else
		{	
			var rs = position % this.pageActual;	
			this.set( "positionPage" , rs );
		}
		Interator.interate( this.els.els , this.remove );
		this.root.appendChild( this.els.els[position] );
		this.set( "elActual" , position );
		this.clearTime();
	}
	
	this.clearTime = function()
	{
		if( this.time )
		{
			clearTimeout( timerIn );
			this.timer( this.timeFuncExec , this.time );
		}
	}
	this.remove = function( el )
	{
		try{
			el.parentNode.removeChild( el );
		}
		catch( e )
		{}
	}
	
	this.timer = function( oFunc , time )
	{
		this.time 		  = time;
		this.timeFuncExec = oFunc;
		var params 		  = ( oFunc.params ) ? ( "'"+oFunc.params.join( "','" )+"'" ) : ( "" ); 
		var execFunc 	  = oFunc.sFunction+"("+params+");";
		timerIn 	  	  = setTimeout( function(){ 
											eval( execFunc );
											self.clearTime(); 
		} , time );
	}
	
	this.next = function()
	{
		if( ( this.elActual + 2 ) <= this.count )
		{	
			this.current( this.elActual + 1 );
			if( this.positionPage == 0 )
					this.nextPage();
		}
		else
		{
			this.current( this.elActual );
			this.nextPage();	
		}
	}
	
	this.previous = function()
	{
		if( this.elActual > 0 )
		{
			this.current( this.elActual-1 );
			if( ( this.positionPage+1) == this.limitPage )
				this.previousPage();	
		}
		else
		{
			this.previousPage();
			this.current( this.elActual );	
		}
	}

	this.nextPage = function( bCurrent )
	{
		var execCurrent = ( bCurrent ) ? true : false;
		var expression;
		if( expression = this.pageActual < this.pages )
			this.set( "pageActual" , this.pageActual+1 );
		this.setIniFinalPage();
		if( !execCurrent && expression )
			this.current( this.iniPage-1 );

		this.render();
	}
	
	this.previousPage = function(bCurrent)
	{
		var execCurrent = ( bCurrent ) ? true : false;
		var expression;
		if( expression = this.pageActual > 1 )
			this.set( "pageActual" , this.pageActual-1 );
		
		this.setIniFinalPage();
		if( !execCurrent && expression )
			this.current( this.finalPage-1 );

		this.render();
	}
	
	this.setIniFinalPage = function()
	{
		var finalPage = this.pageActual*this.limitPage;
		this.set( "iniPage" , finalPage-this.limitPage+1 );
		if( finalPage > this.count )
			finalPage = this.count;
		this.set( "finalPage" , finalPage );
	}	
}