function cTraza( id ) 
{
	this.traza = document.getElementById( 'traza' ) ;
	this.count = 0 ;

	this.print = function( txt )
	{
		this.count ++ ;
		this.traza.innerHTML = this.count + ': ' + txt ;
	}
}

function cPaginador( id, callback, items, prop ) 
{
	this._init = function()
	{
		this.traza				= new cTraza( 'traza' ) ;

		this.div					= document.getElementById( this.id ) ;
		this.table				= document.createElement( 'table' ) ;
		this.table.id			= this.id + '_table' ;
		this.table.className = 'paginador' ;

		this.div.appendChild( this.table ) ;

		this.row = this.table.insertRow( 0 ) ;

		this.t_len = ( this.l_len < this.MAX ) ? this.l_len : this.MAX ;

		for ( i = 0; i < this.t_len ; i ++ )
		{
			this.row.insertCell( i ) ;
		}

		this.cells = this.row.cells ;

		this._set_limites() ;

		this.select( this.cursor ) ;
	}

	this._set_limites = function()
	{

		this.FRM_3		= this.t_len - this.FRM_1 - 1 ;

		this.lim = new Array() ;

		this.lim[1]		= new Array() ;

		this.lim[1][0]	= this.FRM_1 ;
		this.lim[1][2]	= this.t_len - this.FRM_3 + 1 ;
		this.lim[1][3]	= this.FRM_1 ;
		this.lim[1][5]	= this.l_len - this.FRM_3 + 1 ;

		this.lim[2]		= new Array() ;

		this.lim[2][0]	= this.FRM_3 ;
		this.lim[2][2]	= this.t_len - this.FRM_1 + 1 ;
		this.lim[2][3]	= this.FRM_3 ;
		this.lim[2][5]	= this.l_len - this.FRM_1 + 1 ;

		this.lim[3]		= new Array() ;

		this.lim[3][0]	= Math.round( ( this.t_len - this.FRM_2 ) / 2 ) - 1;
		this.lim[3][1]	= this.lim[3][0] + 2 ;
		this.lim[3][2]	= this.t_len - this.lim[3][0] - 1;
		this.lim[3][3]	= this.lim[3][0] ;
		this.lim[3][4]	= 0 ;
		this.lim[3][5]	= this.l_len - this.lim[3][3] + 1 ;
	}

	this._get_modo = function()
	{
		if ( this.l_len <= this.t_len )
		{
			return 0 ;
		}

		if ( this.cursor < this.FRM_1 )
		{
			return 1 ;
		}

		if ( this.cursor > this.l_len - this.FRM_1 + 1 )
		{
			return 2 ;
		}

		return 3 ;
	}

	this._cell_a = function( cell, i )
	{
		cell.innerHTML = i ;
		cell.num			= i ;
		cell.onclick = new Function( this.id + '.click(' + i + ')' ) ;
	}

	this._cell_g = function( cell )
	{
		cell.innerHTML = '...' ;
		cell.num			= 0 ;
		cell.onclick = new Function( this.id + '.click( 0 )' );
	}

	this._refresh_0 = function( pag )
	{
		for ( i = 0; i < pag.t_len ; i ++ )
		{
			pag._cell_a( pag.cells[ i ], i + 1 ) ;
		}
	}

	this._refresh_1_2 = function( pag )
	{
		var num = 1 ;

		for ( i = 0; i < pag.t_len ; i ++ )
		{
			if ( i == pag.lim[ pag.modo ][ 0 ] )
			{
				pag._cell_g( pag.cells[ i ] ) ;
				num = pag.lim[ pag.modo ][ 5 ] ;
			}
			else
			{
				pag._cell_a( pag.cells[ i ], num ++ ) ;
			}
		}
	}

	this._refresh_3 = function( pag )
	{
		var num = 1 ;

		for ( i = 0; i < pag.t_len ; i ++ )
		{
			if ( i == pag.lim[ pag.modo ][ 0 ] )
			{
				pag._cell_g( pag.cells[ i ] ) ;
				num = pag.lim[ pag.modo][ 4 ] ;
			}
			else if ( i == pag.lim[ pag.modo ][ 2 ] )
			{
				pag._cell_g( pag.cells[ i ] ) ;
				num = pag.lim[ pag.modo ][ 5 ] ;
			}
			else
			{
				pag._cell_a( pag.cells[ i ], num ++ ) ;
			}
		}
	}

	this._refresh_4 = function( pag )
	{
		var num	= pag.lim[ 3 ][ 4 ] ;
		var i 	= pag.lim[ 3 ][ 1 ] - 1 ;

		while ( i != pag.lim[ 3 ][ 2 ] )
		{
			pag._cell_a( pag.cells[ i ], num ++ ) ;
			i ++ ;
		}
	}

	this.refresh = function()
	{
		var modo = this._get_modo() ;

		if ( modo != this.modo ) 
		{
			this.modo = modo ;
		
			this.fnc_refresh[ this.modo ]( this ) ;
		}

		if ( this.modo == 3 )
		{
			this.lim[3][4]	= this.cursor - Math.round( ( this.FRM_2 / 2 ) ) + 1 ;
			this._refresh_4( this ) ;
		}
	}

	this.select = function( num )
	{
		this.cursor = ( num >= 1 && num <= this.l_len ) ? num : 1 ;

		this.refresh() ;


		for ( i = 0; i < this.t_len ; i ++ )
		{
			if ( this.cells[ i ].num == this.cursor )
			{
				this.cells[ i ].className = 'paginador_c' ;
			}
			else
			{
				this.cells[ i ].className = 'paginador_a' ;
			}
		}
	}

	this.click = function( num ) 
	{
		if ( num == 0 )
		{
			num = prompt( 'Ir a' ) ;
		}

		if ( num && this.cursor != num )
		{
			this.select( num ) ;

			if ( this.callback )
			{
				this.callback( this.cursor  ) ;
			}
		}
	}

	if ( !prop )
	{
		prop = {} ;
	}

	this.MAX		= ( prop.max )? prop.max : 7 ;
	this.FRM_1	= ( prop.frm1 ) ? prop.frm1 : this.MAX - 2 ;
	this.FRM_2  = ( prop.frm2 ) ? prop.frm2 : this.MAX - 4;
	this.FRM_3;

	this.cursor	= ( prop.sel ) ? prop.sel : 1 ;

	this.id			= id ;
	this.callback	= callback ;

	this.l_len = items ;
	this.t_len ;

	this.div	;
	this.table ;
	this.row ;
	this.cells ;

	this.lim ;
	this.modo = -1 ;

	this.fnc_refresh	= [ this._refresh_0, this._refresh_1_2, this._refresh_1_2, this._refresh_3 ] ;

	this.traza ;

	Iniciador.add( this.id + '._init()' ) ;
}


