/**
 * GOATHHERD.ORG
 * JS Pager logic for 2008 "wolf" theme
 * @copyright 2008
 * Wiebke Pandikow (IMAGES+LAYOUT)
 * Maik Kreutzfeldt (JS+CSS+HTML)
 * 
 * All rights reserved.
 */

/*
 * @version 19.11.2008
 * Pager does split content into several subpages wrapped within divs.
 * The algorithm is mainly based upon h2-headers for separation.
 * 
 * Functions include switches to next, previous, first and last page,
 * display of current and total page number and additionally a
 * paragraph will be activated in the menu to indicate the heading of the
 * current page.
 * 
 * Tested succesfully against:
 * Firefox 2+3
 * Opera 9
 * Safari 3
 * Konqueror 4
 * Internetexplorer 6
 * @todo: brwosercheck
 * @todo: subheader in menu
 */
var pager = null;

/**
 * Pager object
 */
function PAGER () {
	this.active = 0;
	this.nodes = [];

	PAGER.CSSactive = 'active_page';
	PAGER.CSSinactive = 'inactive_page';
};

/**
 * PAGER object constructor
 * @return
 */
PAGER.prototype.init = function() {
	this.active = 0;

	var view = document.getElementById("view");
	var nodes = view.childNodes;

	var pages = -1;
	for (idx = 0; idx < nodes.length; idx++) {
		if (nodes[idx].nodeType == 1 && nodes[idx].nodeName != 'h1') {
			if (pages == -1 && nodes[idx].nodeName == 'div') {
				nodes = nodes[idx].childNodes;
				idx = -1;
			} else if (nodes[idx].nodeName == 'h2') {
				this.nodes.push(new Array());
				pages++;
			}
			if (pages>-1)
				this.nodes[pages].push(nodes[idx]);
		}
	}
	for (var i = 1; i < this.nodes.length; i++)
		this.hide(i);
	var frag = /\#([\w_]+)/i.exec(document.baseURI);
	if (null != frag)
		this.showPageForId(frag[1]);
	this.setPageNumbers();
};
/**
 * Get pages Node array
 * @param num
 * @return Array of NodeElements
 */
PAGER.prototype.get_page = function(num) {
	if (num>=this.nodes.length)
		return false;
	return this.nodes[num];
};

/**
 * Hide page
 * @param num
 * @return
 */
PAGER.prototype.hide = function(num) {
	var page = this.get_page(num);
	for (var i=0; i < page.length; i++)
		page[i].setAttribute('class', PAGER.CSSinactive);
};

/**
 * Show page
 * @param num
 * @return
 */
PAGER.prototype.show = function(num) {
	var page = this.get_page(num);
	if (!page)
		return ;
	this.hide(this.active);
	for (var i=0; i < page.length; i++)
		page[i].setAttribute('class', PAGER.CSSactive);
	this.active=num;
	this.setPageNumbers();
};

/** Switch to first page */
PAGER.prototype.first = function() {this.show(0);};
/** Switch to first page */
PAGER.prototype.last = function() {this.show(this.nodes.length - 1);};
/** Switch to last page */
PAGER.prototype.next = function() {this.show(this.active + 1);};
/** Switch to previous page */
PAGER.prototype.previous = function() {this.show(this.active - 1);};

/**
 * Determines page on which the element ID is found
 * and changes active page.
 */
PAGER.prototype.showPageForId = function(id) {
	for (var page = 0; page < this.nodes.length; page++)
		for (var idx in this.nodes[page])
			if (id == this.nodes[page][idx].getAttribute('id')) {
				this.show(page);
				return ;
			}
};

/**
 * Display the actual page number.
 */
PAGER.prototype.setPageNumbers = function() {
	var str = (this.active+1) + "/" + Math.max(1,this.nodes.length);
	var num_field = document.getElementById("pager_numfield");
	num_field.textContent = str;
};

/**
 * Everything but h1-headers and first page will get "display: none;".
 */
function start_pager() {
	pager = new PAGER();
	pager.init();
};
