/**********************************************************
	               P a g e   O b j e c t
***********************************************************/

/*      P a g e ( )     */
function page(ID, parentID, access, title, url, isOpen, colorID)	{

	this.ID = ID;						// ocr.page.ID
	this.parentID = parentID;			// ocr.page.parentID
	this.access = access;				// ocr.page.access
	this.title = title;					// ocr.page.navTextLink
	this.url = url;						// webpath + sub + page
	this.isOpen = isOpen;				// is the pages child menu open	
	this.colorID = colorID;				// is the page in the active group	
	
	this.isRoot = isRoot;				// 
	this.isChild = isChild;				// 	
	this.getLevel = getLevel;			// 
	this.hasChildren = hasChildren;		// 
	this.getChildren = getChildren;		// 
	this.getParent = getParent;			// 
	this.getRoot = getRoot;				//
	
}

/*      P a g e   M e t h o d s     */

function isRoot()
{
	return (this.parentID == 0);
}

function isChild()
{
	return (this.parentID > 0);
}

function getLevel()
{
	if( this.isRoot() ){	// if its a root level page
		return 0;
	}
	else					// else make recursive call 
	{
		var	parentPage = getPage( this.parentID );
		return 1 + parentPage.getLevel();	
	}
}

function getChildren()
{

	var childPages = new Array();
	var count = 0;
	
	// l o o p   t h r o u g h   p a g e s
	for( var i = 0; i < pages.length; i++ ){
		if( pages[i].parentID == this.ID  )		
		{
			childPages[count] = pages[i];	// if page in group add to childPages[]
			count++;						// move index to next open position
		}
	}
	return childPages;						// return results array
}

function hasChildren()
{
	var childPages = this.getChildren();
	return ( childPages.length > 0 );
}

function getParent()
{
	if( this.parentID == 0 ) { return null; }
	else { 	
		return getPage(this.parentID); 
	}	
}

function getRoot()
{
	if( this.parentID == 0 ) { return this; }
	else { 
		var parentPage = this.getParent();
		return parentPage.getRoot();				// recursive call
	}
}

/**********************************************************
	               P a g e s [ ]    M e t h o d s
***********************************************************/

/*     i n i t P a g e s ( )      */
function initPage( currentPage )
{
	if( doDOM ){
	
		// r e c u r s e   t o   t o p   o f   n a v
		if( !currentPage.isRoot() )		
		{
			initPage( currentPage.getParent() );
		}
		
		
		// h a n d l e  p a r e n t  p a g e
		if( currentPage.hasChildren() )
		{
			// i s  O p e n 
			currentPage.isOpen = true;
			
			// c o l o r I D
			var newColorID = currentPage.getLevel() + 1;
			if( newColorID <= menuColorLimit )
			{
				currentPage.colorID = newColorID;
			}
			else
			{
				currentPage.colorID = menuColorLimit;		
			}
			
			fixChildColor( currentPage, currentPage.colorID );	
		
		}
		
		// h a n d l e   c h i l d l e s s   p a g e
		else
		{
		
			// c o l o r I D
			var newColorID = currentPage.getLevel();
			if( newColorID <= menuColorLimit )
			{
				currentPage.colorID = newColorID;
			}
			else
			{
				currentPage.colorID = menuColorLimit;		
			}
		
		}		
		
	}
}


function fixChildColor( parentPage , colorID )
{
	var childPages = parentPage.getChildren();
	
	for( var i = 0; i < childPages.length; i++ )
	{
		childPages[i].colorID = colorID;
		fixChildColor( childPages[i] , colorID );
	}
}



/*     g e t P a g e ( )      */
function getPage(ID)
{
	var foundIt = false;
	var i = 0;
	// l o o p   t h r o u g h   p a g e s
	while( i < pages.length && !foundIt )
	{
		if( pages[i].ID == ID ){	// if the page is found stop loop
			foundIt = true;
		}
		else {						// else move to next page
			i++;
		}
	}
	if( foundIt ){					// return page reference
		return pages[i];
	}
	else {							// not found return null
		return null;
	}
}



/*     s h o w P a g e s ( )     */
/*
n o t  u s e d  -  for viewing pages[]
requires element with id="pagesArrayTable"
*/

function showPages()	{

	var str = '<table>';
	for( var i = 0 ; i < pages.length ; i++ ){
		str += '<tr>';
		str += '<td>' + pages[i].ID + '</td>';
		str += '<td>' + pages[i].parentID + '</td>';
		str += '<td>' + pages[i].access + '</td>';
		str += '<td>' + pages[i].title + '</td>';
		str += '<td>' + pages[i].url + '</td>';
		str += '<td>' + pages[i].isOpen + '</td>';
		str += '<td>' + pages[i].colorID + '</td>';
		str += '</tr>';
	}
	str += '</table>'; 
	getRef('pagesArrayTable').innerHTML = str;
}


