/******************************************
* 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.mainPageHTML = "";
	this.subPagesHTML = new Array();
	this.currentPage = 1;
	
	// 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(channel_array[this.currentChannel].count/this.numOfLinesToShow);
	this.numOfArticles = channel_array[this.currentChannel].count;
	eval(this.owner+".setTitle('"+channel_array[this.currentChannel].name+"')");
	this.writePage();
}

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

pageSet.prototype.generateMainPage = function()  {
	for (i=0;i<channel_array.length-1;i++) {
		
		this.mainPageHTML += "<span class='pageSetMenuMainContent'>" + channel_array[i].count + " articles</span>"
		this.mainPageHTML +="<span class='categoryLinksOff' onmouseover='this.className=\"categoryLinksOn\"' onmouseout='this.className=\"categoryLinksOff\"'  onclick='"+this.objName+".showFirstPage("+i+");' title='" + channel_array[i].count +" articles'>" + channel_array[i].name +"</span>";
	}
	this.mainPageHTML += "<div class='newsFeedFunctions'>"
	this.mainPageHTML += "<span  onclick='"+this.owner+".cancelNewsfeedMenu()' class='pageSetButton'>Cancel</span>"
	this.mainPageHTML += "</div>"
}

pageSet.prototype.generateSubPageHTML = function()  {
	for(i=0;i<channel_array.length-1;i++) {
		var numOfCats = channel_array[i].count;
		this.subPagesHTML[i] = new Array();
		for(j=0;j<numOfCats;j++) {
			this.subPagesHTML[i][j] = "<span class='categoryLinksOff' onmouseover='this.className=\"categoryLinksOn\"' onmouseout='this.className=\"categoryLinksOff\"' onclick='"+this.owner+".loadContent("+(this.categoryCount+j)+");' >"+category_array[(this.categoryCount+j)].full_name+"</span>";
		}
		this.categoryCount+=numOfCats;
	}
	this.categoryCount=0;
}

pageSet.prototype.writePage = function()  { 
	
	//how many channels 
	var channelCount = channel_array[this.currentChannel].count;
	//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 += "<div class='pageSetHeader'><span class='pageSetHeaderRight'>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 += "Showing " + ((lastVisibleIndex-this.numOfLinesToShow)+1) + "-" + loopEnd + " of " + this.numOfArticles + "</div><br />";

	//outputing categories...
	for(i=lastVisibleIndex-this.numOfLinesToShow;i<loopEnd;i++) {
		pagehtml += this.subPagesHTML[this.currentChannel][i];// + "<br>";
	}
	
	//output div that holds the navigation links.
	pagehtml += "<div class='newsFeedFunctions'>"
	//output the "next" link if not on the last page.
		
		if(loopEnd<this.numOfArticles) {
			pagehtml += "<span class='pageSetButton' onclick='"+this.objName+".nextPage()'>Next</span>";	
		}
	//output the link to the main menu
	menuLinkXPos = (parseInt(this.pageStyle.width)/2)-42 + "px";

	pagehtml += "<span class='pageSetSubContent' onclick='"+this.objName+".showMainPage()'>Main menu</span>";
	//output the "prev" link if not on page 1.
	if (this.currentPage>1)
		pagehtml += "<span  onclick='"+this.objName+".prevPage()' class='pageSetButtonLeft'>Prev</span>"

	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;
		}
	}
}

