/* ******************************************
Copyright (c) 2009, Juraj Simlovic, Medvedik.
License: http://www.gnu.org/licenses/gpl.txt
Version: 0.0.4. Not tested on many browsers.
****************************************** */

TNPIPager = function (pagerName, contrName)
{
	this.hash = false;

	// get the pager & control
	this.pager = document.getElementById (pagerName);
	this.contr = document.getElementById (contrName);

	// do we have one?
	if (!this.pager) return;

	// get all pages
	var pages = this.pager.getElementsByTagName ('div');

	// filter all the pages
	this.pages = new Array ();
	for (i = 0; i < pages.length; i++)
		if (pages[i].id) this.pages.push (pages[i]);

	// show the current page
	this.current ();

	// setup the interval checking
	// support for back/forth navigation
	function setupInterval (pager)
	{
		// this creates local workspace
		// where this.update() is defined
		var interval = function()
		{ pager.current (); }

		setInterval(interval, 1000);
	}
	setupInterval(this);
}

TNPIPager.prototype.current = function ()
{
	// hide/show pages
	this.update (location.hash);
}

TNPIPager.prototype.set = function (link)
{
	// hide/show pages
	this.update (link.href);

	return false;
}

TNPIPager.prototype.update = function (hash)
{
	var found = false;

	// parse the given hash/url
	if (hash.indexOf ('#') >= 0)
		hash = hash.substring(hash.indexOf ('#') + 1);

	// compare with the last hash
	if (hash == this.hash) return;

	// remember the current choice
	this.hash = hash;

	// hide/show pages
	for (i = 0; i < this.pages.length; i++)
	{
		if (hash == this.pages[i].id)
		{
			this.pages[i].style.display = 'block';
			found = true;
			
		}
		else this.pages[i].style.display = 'none';
	}

	// no item found? display the first one..
	if (!found) this.pages[0].style.display = 'block';

	// re-scroll the pager
	this.pager.scrollTop = 0;
}

