var Paginator = function ()
{
	var self = this;

	self._tot_rows = 0;		// Total number of rows
	self._rows_per_page = 0;	// Rows for each page
	self._page = 0;			// Current Page
	self._tot_links = 10;		// Number of links to show
	self._tot_pages = 0;		// Total number of pages

	self._dest_id = null;

	self.templates = {};
	self.templates [ 'pag-first' ] = "&lt;&lt;&lt;";
	self.templates [ 'pag-first-lnk' ] = 'javascript:self._go(0)';
	self.templates [ 'pag-last' ] = "&gt;&gt;&gt;";
	self.templates [ 'pag-last-lnk' ] = 'javascript:self._go(%(_TOT_PAGES)s)';
	self.templates [ 'pag-prev' ] = "&lt;";
	self.templates [ 'pag-prev-lnk' ] = 'javascript:self._go(%(_PREV)s)';
	self.templates [ 'pag-next' ] = '&gt;';
	self.templates [ 'pag-next-lnk' ] = 'javascript:self._go(%(_CURR_PAGE)s+1)';
	self.templates [ 'pag-pos' ] = '%(_PAGE)s';
	self.templates [ 'pag-pos-lnk' ] = 'javascript:self._go(%(_NUM)s)';
	self.templates [ 'pag-sep' ] = '|';
	self.templates [ 'pag-link-space' ] = '';
	self.templates [ 'pag-left-info' ] = '';
	self.templates [ 'pag-right-info' ] = '';

	// _TOT_PAGES 
	// _CURR_PAGE
	// _NUM
	// _PAGE

	self.init = function ( tot_rows, rows_per_page, tot_links )
	{
		self._tot_pages = Math.ceil ( tot_rows / rows_per_page );
		self._tot_rows = tot_rows;
		self._rows_per_page = rows_per_page;
		self._tot_links = ( tot_links ? tot_links : 10 );

		self.templates [ '_TOT_PAGES' ] = self._tot_pages;
		self.set_page ( 0 );
	};

	self.set_page = function ( page )
	{
		if ( page > self._tot_pages ) page = self._tot_pages;

		self._page = page;

		self.templates [ '_CURR_PAGE' ] = self._page;

		return self.mk_str ();
	};

	self.mk_str = function ()
	{
		var s = '';
		var t, p;
		var start_page = 0;

		self.templates [ '_NUM' ] = self._page;
		self.templates [ '_PAGE' ] = self._page + 1;
		s += String.formatDict ( self.templates [ "pag-left-info" ], self.templates );

		if ( self._page > 0 )
		{
			s += String.formatDict ( '<a href="%(pag-first-lnk)s">%(pag-first)s</a>', self.templates ); 
			s += self.templates [ 'pag-link-space' ];

			self.templates [ '_PREV' ] = self._page - 1;
			self.templates [ '_PREV-LNK' ] = String.formatDict ( self.templates [ 'pag-prev-lnk' ], self.templates );
			s += String.formatDict ( '<a href="%(_PREV-LNK)s">%(pag-prev)s</a>', self.templates );
		} else {
			s += String.formatDict ( '%(pag-first)s', self.templates ); 
			s += self.templates [ 'pag-link-space' ];
			s += String.formatDict ( '%(pag-prev)s', self.templates );
		}

		//s += self.templates [ 'pag-sep' ];

		if ( self._page > ( self._tot_links / 2 ) )
		{
			start_page = self._page - ( Math.floor ( self._tot_links / 2 ) );
			if ( ( self._tot_pages - start_page ) < self._tot_links )
				start_page = self._tot_pages - self._tot_links;
		}

		for ( t = 0; t < self._tot_links; t ++ )
		{
			p = start_page + t;

			if ( ( p + 1 ) > self._tot_pages ) break;

			s += self.templates [ 'pag-link-space' ];

			if ( t > 0 )
			{
				s += self.templates [ 'pag-sep' ];
				s += self.templates [ 'pag-link-space' ];
			}

			self.templates [ '_NUM' ] = p;
			self.templates [ '_PAGE' ] = p + 1;
			self.templates [ '_POS' ] = String.formatDict ( self.templates [ 'pag-pos' ], self.templates );

			if ( p == self._page )
				s += String.formatDict ( '%(_POS)s', self.templates );
			else {
				self.templates [ '_POS-LNK' ] = String.formatDict ( self.templates [ 'pag-pos-lnk' ], self.templates );
				s += String.formatDict ( '<a href="%(_POS-LNK)s">%(_POS)s</a>', self.templates );
			}
		}

		if ( ( self._page +1 ) >= self._tot_pages ) 
		{
			s += self.templates [ 'pag-link-space' ];
			s += String.formatDict ( '%(pag-next)s', self.templates );
			s += self.templates [ 'pag-link-space' ];
			s += String.formatDict ( '%(pag-last)s', self.templates );
		} else {
			s += self.templates [ 'pag-link-space' ];

			self.templates [ '_NEXT-LNK' ] = String.formatDict ( self.templates [ 'pag-next-lnk' ], self.templates );
			s += String.formatDict ( '<a href="%(_NEXT-LNK)s">%(pag-next)s</a>', self.templates );

			s += self.templates [ 'pag-link-space' ];
			self.templates [ '_LAST-LNK' ] = String.formatDict ( self.templates [ 'pag-last-lnk' ], self.templates );
			s += String.formatDict ( '<a href="%(_LAST-LNK)s">%(pag-last)s</a>', self.templates );
		}

		self.templates [ '_NUM' ] = self._page;
		self.templates [ '_PAGE' ] = self._page + 1;
		s += String.formatDict ( self.templates [ "pag-right-info" ], self.templates );

		return s;
	};

	self._go = function ( page_no )
	{
		self.set_page ( page_no );
		self.render ();
	};

	self.render = function ( dest_id )
	{
		var s;

		if ( ! dest_id ) dest_id = self._dest_id;
		self._dest_id = dest_id;

		s = self.mk_str ();

		$( dest_id ).innerHTML = s;
	};
};

