/******************************************
* WI_PagesetMenu - pageset menu system - By Lawrence Carvalho (lawrence@nodetraveller.com)
* http://www.nodetraveller.com
******************************************/

function pageSet(objName,owner,divId,styleSelector,linesToShow) {
	this.objName = objName;
	this.owner = owner;
	this.pageObj = document.getElementById(divId);

	this.pageStyle = getObjStyle(styleSelector);
	this.numOfLinesToShow = linesToShow;
	//this.linesToShow = linesToShow;
	this.mainPageHTML = "";
	this.subPagesHTML = new Array();
	this.currentPage = 1;
	
	//Will always leave two lines available, top and bottom as this is specified in innerHTML by the "<BR>"'s

	// Specific variables not needed by generic pageSet object
	this.currentChannel = -1; //subclass
	this.categoryCount = 0;  //subclass
	this.numOfPages = null;
	this.numOfArticles = null;
	
	this.generateMainPage();
	this.generateSubPageHTML();
	this.showMainPage();
}
pageSet.prototype.nextPage = function () {
	this.currentPage++;
	this.writePage();
}

pageSet.prototype.prevPage = function () {
	this.currentPage--;
	this.writePage();
}

pageSet.prototype.goToPage = function(pageNum) {
	if (pageNum<=this.numOfPages)  {
		this.currentPage = pageNum;
		this.writePage();
	}
}

pageSet.prototype.showFirstPage = function(newChannel) {
	this.currentPage=1;
	this.currentChannel = newChannel;
	this.numOfPages = Math.ceil(subMenu[this.currentChannel].length/this.numOfLinesToShow);
	this.numOfArticles = subMenu[this.currentChannel].length; 
	this.writePage();
}

pageSet.prototype.showMainPage = function()  {
	if (this.owner!=null) {
		if (!isUnDefined(eval(this.owner)))
			eval(this.owner+".setTitle('Main Menu')");
	}
	this.pageObj.innerHTML = this.mainPageHTML;
}

pageSet.prototype.generateMainPage = function()  {
	this.mainPageHTML += "<p class='pageSetMenuTitle'>&nbsp;Main Menu</p>";
	for (i=0;i<mainMenu.length;i++) {
		this.mainPageHTML +="<a href='#' onclick='"+this.objName+".showFirstPage("+i+");'>" + mainMenu[i]+"</a>";
	}
}

pageSet.prototype.generateSubPageHTML = function()  {
	for(i=0;i<mainMenu.length;i++) {
		var numOfItems = subMenu[i].length;
		this.subPagesHTML[i] = new Array();
		for(j=0;j<numOfItems;j++) {
			this.subPagesHTML[i][j] = "<a  href='#' onclick='moreoverComponent.loadContent("+(this.categoryCount+j)+");' >"+subMenu[i][j]+"</a>";
		}
		this.categoryCount+=numOfItems;
	}
	this.categoryCount=0;

}

pageSet.prototype.writePage = function()  { 
	
	//how many channels 
	//A number of the last element being shown on the current page.
	var lastVisibleIndex = (this.currentPage*this.numOfLinesToShow);
	var	pagehtml = "";//"<span>The <b>"+channelName+"</b> channel</span><br><br>";
	
	//Showing how many pages there are and what page the user is currently on.
	pagehtml += "<span style='float:right;' >Pg "+this.currentPage+"/"+this.numOfPages+"</span>";
	//Number indicating the last line to show or the number of last element in the channel
	var loopEnd = ((lastVisibleIndex)>this.numOfArticles) ? this.numOfArticles : lastVisibleIndex;
	//output the range showing. eg "Showing 1 - 13;
	pagehtml += "<p class='pageSetMenuTitle'>&nbsp;Main Menu - <strong>" + mainMenu[this.currentChannel] +"</strong><br />&nbsp;Showing " + ((lastVisibleIndex-this.numOfLinesToShow)+1) + " - " + loopEnd + " out of " + this.numOfArticles + "</p>";

	//outputing categories...
	for(i=lastVisibleIndex-this.numOfLinesToShow;i<loopEnd;i++) {
		pagehtml += this.subPagesHTML[this.currentChannel][i];
	}
	
	//output div that holds the navigation links.
	pagehtml += "<div class='pageSetMenuFooter' >"
	//output the "next" link if not on the last page.
		if(loopEnd<this.numOfArticles) {
			pagehtml += "<a style='float:right;' href='#' onclick='"+this.objName+".nextPage()'>Next</a>";	
		}
	//output the link to the main menu
	menuLinkXPos = (parseInt(this.pageStyle.width)/2)-(parseInt(this.pageStyle.fontSize)* parseInt("Main Menu".length)/2) + "px";
	pagehtml += "<a style='position:absolute;left:"+menuLinkXPos+";' href='#' onclick='"+this.objName+".showMainPage()'>Main menu</a>";
	//output the "prev" link if not on page 1.
	if (this.currentPage>1)
		pagehtml += "<a href='#' onclick='"+this.objName+".prevPage()' style='float:left;'>Prev</a>"

	pagehtml += "</div>";

	//output html.
	this.pageObj.innerHTML = pagehtml;
	
}

function getObjStyle(selectorText) {
	if (!document.styleSheets) 
		return;
	else var numOfStyleSheets = document.styleSheets.length;
	for (i=0;i<numOfStyleSheets;i++) {
		var rules = (document.styleSheets[0].cssRules) ? document.styleSheets[i].cssRules : document.styleSheets[i].rules;
		for(j=0;j<rules.length;j++) {
			if (rules[j].selectorText == selectorText)
				return rules[j].style;
		}
	}
}

