var paginatorPageHeight="500px";
var paginatorHeight="500px";
var rollupPageheight="";

$(document).ready(function(){

if (document.getElementById("rollup"))
{
	paginateRollups("#rightPage");
	paginateRollups("#leftPage");
}
else
{
	paginate(['#rightPage','#leftPage'],paginatorPageHeight,paginatorHeight);
}


});

function paginateRollups(targetpage)
{
	rollupPageheight=500;
	var rollupFlag=false;
	$(targetpage+' h2').each(function(i){
		var node,nodeHeight;
		if(i==0){
			node=this.previousSibling;
			while(node)
			{
				if (node && node.nodeType==1);
				{
					nodeHeight=node.offsetHeight/1;
					if (nodeHeight) rollupPageheight=rollupPageheight-nodeHeight;
					node=node.previousSibling;

				}
				
			}
		}
		rollupPageheight=rollupPageheight-(this.offsetHeight+2); //height of h2 with 2 px margin;
		rollupFlag=true;
	});
	var rollupPaginatorHeight=(rollupPageheight-41)+"px";
	rollupPageheight=rollupPageheight+"px";
	if (rollupFlag)
	{
			$(targetpage+' h2').each(function(i){
			var node=this.nextSibling;
			var parent=this.parentNode;
			var div=document.createElement("div");
			div.className="rollupContent";
			div.id="rollupContent_"+i;
			this.contentLink="rollupContent_"+i;
			parent.insertBefore(div, node);
			var nextNode=node.nextSibling;
			while (node.nodeName!="H2")
			{
				div.appendChild(node);
				if (!nextNode)
				{
					break;
				}
				else
				{
					node=nextNode;
					nextNode=nextNode.nextSibling;
				}
			}
			paginate(['#rollupContent_'+i],rollupPaginatorHeight,rollupPaginatorHeight);
			
			$(this).click(function(){
				$("h2",this.parentNode).not(this).each(function(){
					this.className="closed";
					$("#"+this.contentLink).hide();
				});
				this.className="open";
				$("#"+this.contentLink).show();
			});
			if (i==0)
			{
				this.className="open";
			}
			else
			{
				this.className="closed";
				$("#"+this.contentLink).hide();
			}
		});

	}
	else
	{
		paginate([targetpage],paginatorPageHeight,paginatorHeight);
	}
}

function paginate(targets,paginatorPageHeight,paginatorHeight)
{
	var i;
	for(i=0; i<targets.length; i++)
	{
		$(targets[i]).pager($(targets[i]+' > *'), { navId: 'arrowNav_right', pageHeight: paginatorPageHeight, height :paginatorHeight, memory: false });
	}
}





$.fn.pager = function(element_list, options) {
	
	var settings = {		
		navId: 'nav',
		navClass: 'nav',
		pageClass: 'page',
		navAttach: 'append',
		highlightClass: 'highlight',
		prevText: 'Prev Page',
		nextText: 'Next Page',
		linkText: null,
		linkWrap: null,
		height: null,
		pageHeight: null,
		memory: null
	}
	
	if(options) $.extend(settings, options);
	
		
	return this.each( function () {
		
		var me = $(this);
		var size;
	  	var i = 0;		
		var navid = '#'+settings.navId;
		
		function init () {
			//size = $(clas, me).not(navid).size();
			clas='.'+settings.pageClass;
			size = element_list.not(navid).size();
			if(settings.height == null) {			
				settings.height = getHighest();
			}
			if(size > 1) {
				if (settings.pageHeight)
				{
					buildPages();
				}
				if (size>1)
				{
					makeNav();
					if (settings.memory)
					{
						recall();
					}
					show();
					highlight();
				}
			}			
			sizePanel();
			if(settings.linkWrap != null) {
				linkWrap();
			}
			
		}
		function buildPages()
		{
			settings.pageHeight=settings.pageHeight.replace(/px$/,"");
			var pageCount=1;
			var div=document.createElement("div");
			div.className=settings.pageClass;
			me.append(div);
			element_list.each(function(i){
			//$(clas,me).each(function(i){
				div.appendChild(this);
				//alert(div.offsetHeight); - note div.clientHeight returns 0 in IE 6 in this build at least - replacing with offsetHeight
				if (div.offsetHeight>settings.pageHeight)
				{
					//rescuing orphan headings
					var orphanNode = this;
					do 
					{
					orphanNode = orphanNode.previousSibling;
					}
					while (orphanNode && orphanNode.nodeType != 1);
					//end rescuing orphan headings;
					
					me.prepend(this);
					
					div=document.createElement("div");
					div.className=settings.pageClass;
					me.append(div);
					pageCount++;
					//repositioning orphan headings
					if (orphanNode.nodeName.indexOf("H")>-1)
					{
						div.appendChild(orphanNode);
					}
					
					div.appendChild(this);
					
				}
				
			});
			clas="."+settings.pageClass;
			size=pageCount;
			
		}
		function buildPage()
		{
			var div=document.createElement("div");
			div.className=settings.pageClass;
			me.append(div);
			return div;
		}
		function makeNav () {		
			var str = '<div id="'+settings.navId+'" class="'+settings.navClass+'">';
			str += '<a href="#" rel="prev" class="prev">'+settings.prevText+'</a>';
			for(var i = 0; i < size; i++) {
				var j = i+1;
				str += '<a href="#" rel="'+j+'">';
				str += (settings.linkText == null) ? j : settings.linkText[j-1];				
				str += '</a>';
			}
			str += '<a href="#" rel="next" class="next">'+settings.nextText+'</a>';
			str += '</div>';
			switch (settings.navAttach) {		
				case 'before':
					$(me).before(str);
					break;
				case 'after':		
					$(me).after(str);
					break;
				case 'prepend':
					$(me).prepend(str);
					break;
				default:
					$(me).append(str);
					break;
			}
		}
		function show () {
			$(me).find(clas).not(navid).hide();
			var show = $(me).find(clas).not(navid).get(i);
			$(show).show();
		}		
		function highlight () {
		
			$(me).find(navid).find('a').removeClass(settings.highlightClass);
			var show = $(me).find(navid).find('a').get(i+1);			
			$(show).addClass(settings.highlightClass);
		}
		function remember()
		{
			var pageKey,date,expires;
			pageKey="page|"+location.pathname;
			var a = $(me).find(navid).find('a').get(i+1);

			date = new Date();
  		  	date.setTime(date.getTime()+(1*60*20*1000)); //20 minute duration
  		  	expires = "; expires="+date.toGMTString();

  		  	document.cookie = pageKey+"="+navid+"|"+a.rel+expires+";";
		}
		function recall()
		{
			var pageKey,cookie,j,c;
			pageKey="page|"+location.pathname+"="+navid;
			cookie= document.cookie.split(';');
    		for (j=0;j<cookie.length;j++){
        		c = cookie[j];
        		while (c.charAt(0)==' ') c = c.substring(1,c.length);
        		if (c.indexOf(pageKey) == 0){
            		cookie= c.substring(pageKey.length+1,c.length);
            		i = cookie-1;

            		break;
        		}
    		}
    		
    		
		}

		function sizePanel () {
			if($.browser.msie) {
				$(me).find(clas).not(navid).css( {
					height: settings.height
				});	
			} else {
				$(me).find(clas).not(navid).css( {
					minHeight: settings.height
				});
			}
		}
		function getHighest () {
			var highest = 0;
			$(me).find(clas).not(navid).each(function () {
				
				if(this.offsetHeight > highest) {
					highest = this.offsetHeight;
				}
			});
			highest = highest + "px";
			return highest;
		}
		function getNavHeight () {
			var nav = $(navid).get(0);
			return nav.offsetHeight;
		}
		function linkWrap () {
			$(me).find(navid).find("a").wrap(settings.linkWrap);
		}
		init();
		$(this).find(navid).find("a").click(function () {

			if($(this).attr('rel') == 'next') {
				if(i + 1 < size) {
					i = i+1;
				}
			} else if($(this).attr('rel') == 'prev') { 
				if(i > 0) {	
					i = i-1;
				}
			} else {		
				var j = $(this).attr('rel');	
				i = j-1;		
			}
			show();
			highlight();
			if (settings.memory)
			{
				remember();
			}
			return false;
		});
	});	
};