<?php
//Get the PHP Simple HTML DOM Parser. Manual: http://simplehtmldom.sourceforge.net/manual.htm
require_once('simple_html_dom.php');

class Navmenu
{
	private $html;
	private $toc;
	private $linkArray = array();
	private $completeLinkArray = array();
	
	private $title;
	
	private $linkPrefix;
	private $linkTab;
	private $linkSuffix;
	
	function Navmenu($linkPrefix = "navmenu.php?tab=", $linkTab = "&page=", $linkSuffix = "")
	{
		$tab=$_REQUEST['tab'];
		if($tab == null) {
			$tab = "1";
			$file = "print.htm";
		} else if ($tab == 1) {
			$file = "print.htm";
		} else {
			$file = "print_" . $tab . ".htm";
		}
		
		
		
		
		//echo "variabe 'file' has a value....:  $file";
		
		
		
			
		
	
		$this->linkPrefix = $linkPrefix;
		$this->linkTab = $linkTab;
		$this->linkSuffix = $linkSuffix;
		$this->html = new simple_html_dom();
		$this->html->load_file($file);
		$titleTag = $this->html->find('title', 0);
		$this->title = $titleTag->innertext;
	
		$this->toc = $this->html->find('div[id=tableofcontents]', 0);
		
		
		foreach($this->toc->find('a') as $a)
		{
			$link = $a->href;
			//$pageTab = substr($link, 4,1);
			$pageTab=$_REQUEST['tab'];
			$this->Tab = $pageTab;
			$pageNum = substr($link, -5);
			$a->href = $this->linkPrefix . $pageTab . $this->linkTab . $pageNum . $this->linkSuffix;
			if(!strstr($a->class,'innerNode'))
			{
				array_push ($this->linkArray, $pageNum);
			}
			array_push ($this->completeLinkArray, $pageNum);
		}
	}
	
	public function getTitle()
	{
		return $this->title;
	}
	
	public function getChapter()
	{
		return $this->toc;
	}
	
	

	
	public function getContent($page)
	{
				
		$content = $this->html->find('div[id=page_' . $page . ']', 0);
		
		//if there is a div with a class = swf then replace that content with the swf  
		$swfDivs = $content->find('div[class=swf]');
		foreach($swfDivs as $swfDiv)
		{     
			$params = split("_", $swfDiv->id);
			$swf = $params[0];
			$width = $params[1];
			$height = $params[2];
			$insert = "<script type='text/javascript'>AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0','bgcolor','FFFFFF','width','" . $width . "','height','" . $height . "','src','media/flash/". $swf ."','quality','high','pluginspage','http://www.macromedia.com/go/getflashplayer','movie','media/flash/". $swf ."' );</script>";
			$swfDiv->innertext = $insert;
		}
		
		
		//Add the back and next buttons.
		$bnCode = $this->getBackNextCode($page);
		$h2 = $content->find('h2',0);
		$h2->outertext = $h2->outertext . $bnCode;// add it to the top
		
		$content = $content->innertext . $bnCode;//add it to the bottom.
		return $content;
	}
	
	
	
	public function getBackNextCode($page)
	{
		$next = $previous = $page;
		$size=sizeof($this->linkArray);
		for($i = 0; $i < $size; $i++) 
		{
			if($this->linkArray[$i] == $page)
			{
				if($i < $size - 1)
				{
					$next =  $this->linkArray[$i + 1];
				}
				if($i > 0)
				{
					$previous =  $this->linkArray[$i - 1];
				}
			}
		}	
		
		$prevChapter = $this->Tab - 1;
		$nextChapter = $this->Tab + 1;
		
		if($previous == $page) {
			//$h2->innertext = $h2->innertext . "<span><a href=\"". $this->linkPrefix . $next . $this->linkSuffix ."\" >Next &#187;</a></span>";
			
			if ($prevChapter == -1 || $prevChapter == 0) {
			
			if ($this->linkSuffix == 0) {
				$nextChapter = "2";
				return "<div class=\"stage_nav group\"><ul><li class=\"next chapter\"><a href=\"" . $this->linkPrefix . $nextChapter . $this->linkTab . "1.0.0" . $this->linkSuffix . "\">Next Chapter &#187;</a></li></ul></div>";
				
			} elseif (($nextChapter = 1)&&($this->linkSuffix == 2)) {
				$nextChapter = "2";
				return "<div class=\"stage_nav group\"><ul><li class=\"previous\"><a href=\"" . $this->linkPrefix . $this->Tab . $this->linkTab . $previous . $this->linkSuffix .  "\">&#171; Previous</a></li><li class=\"toc\"><a href=\"index.htm\">Table of Contents</a></li> <li class=\"next chapter\"><a href=\"" . $this->linkPrefix . $nextChapter . $this->linkTab . "1.0.0" . $this->linkSuffix . "\">Next Chapter &#187;</a></li></ul></div>";
			} else {
				return "<div class=\"stage_nav group\"><ul><li class=\"previous chapter\"><a href=\"" . $this->linkPrefix . $prevChapter . $this->linkTab . "1.0.0" . $this->linkSuffix . "\">&#171; Previous Chapter</a></li><li class=\"toc\"><a href=\"index.htm\">Table of Contents</a></li><li class=\"next\"><a href=\"" . $this->linkPrefix . $this->Tab . $this->linkTab . $next . $this->linkSuffix . "\">Next &#187;</a></li></ul></div>";
			}
			}
			
	// THIS IS A PART THAT I HAD TO CHANGE IN ORDER TO MAKE NAVIGATION FIT TO THIS MODULE
			

			
			if ($nextChapter == 2 || $nextChapter == 3 || $nextChapter == 4 || $nextChapter == 5 || $nextChapter == 6 || $nextChapter == 7 || $nextChapter == 8 || $nextChapter == 9) {
			return "<div class=\"stage_nav group\">
				<ul>
					<li class=\"previous chapter\"><a href=\"" . $this->linkPrefix . $prevChapter . $this->linkTab . "1.0.0" . $this->linkSuffix . "\">&#171; Previous Chapter</a></li>
					<li class=\"toc\"><a href=\"index.htm\">Table of Contents</a></li>
					 <li class=\"next chapter\"><a href=\"" . $this->linkPrefix . $nextChapter . $this->linkTab . "1.0.0" . $this->linkSuffix . "\">Next Chapter &#187;</a></li>
				</ul></div>";
				} 


				
				if ($nextChapter == 6 || $nextChapter == 7 || $nextChapter == 8) {
			return "<div class=\"stage_nav group\"><ul><li class=\"previous chapter\"><a href=\"" . $this->linkPrefix . $prevChapter . $this->linkTab . "1.0.0" . $this->linkSuffix . "\">&#171; Previous Chapter</a></li><li class=\"toc\"><a href=\"index.htm\">Table of Contents</a></li> <li class=\"next chapter\"><a href=\"" . $this->linkPrefix . $nextChapter . $this->linkTab . "1.0.0" . $this->linkSuffix . "\">Next Chapter &#187;</a></li></ul></div>";
			} 	
				
				//SOLUTION FOR THE LAST PAGE WITHOUT 'NEXT' OR 'NEXT CHAPTER'
				
				elseif ($nextChapter == 10) {//&& $next == 3) {  // && $next==3
				return "<div class=\"stage_nav group\">
				<ul>
					<li class=\"previous chapter\"><a href=\"" . $this->linkPrefix . $prevChapter . $this->linkTab . "1.0.0" . $this->linkSuffix . "\">&#171; Previous Chapter</a></li>
					<li class=\"toc\"><a href=\"index.htm\">Table of Contents</a></li>
				</ul></div>";
				} 
				
			else {
			return "<div class=\"stage_nav group\">
				<ul>
					<li class=\"previous chapter\"><a href=\"" . $this->linkPrefix . $prevChapter . $this->linkTab . "1.0.0" . $this->linkSuffix . "\">&#171; Previous Chapter</a></li>
					<li class=\"toc\"><a href=\"index.htm\">Table of Contents</a></li>
					<li class=\"next\"><a href=\"" . $this->linkPrefix . $this->Tab . $this->linkTab . $next . $this->linkSuffix . "\">Next &#187;</a></li>
				</ul></div>";
			}
			
			
			
			
			
			
			
		
		} else if($next == $page) {
			//$h2->innertext = $h2->innertext . "<span><a href=\"". $this->linkPrefix . $previous . $this->linkSuffix ."\" >&#171; Back</a></span>";
			
			///////////// THIS IS THE THING I HAD ALSO TO CHANGE (UNCOMMMENT) I ORDER TO MAKE NAVIGATION RIGHT FOR Icing module
			if ($nextChapter == 12) {
				return "<div class=\"stage_nav group\">
				<ul>
					<li class=\"previous\"><a href=\"" . $this->linkPrefix . $this->Tab . $this->linkTab . $previous . $this->linkSuffix . "\">&#171; Previous</a></li>
				</ul></div>";
			}
			/////////////
			
			
			else if ($nextChapter == 1) {
			$nextChapter = "2";
			}
			return "<div class=\"stage_nav group\">
			<ul>
				<li class=\"previous\"><a href=\"" . $this->linkPrefix . $this->Tab . $this->linkTab . $previous . $this->linkSuffix .  "\">&#171; Previous</a></li>
				<li class=\"toc\"><a href=\"index.htm\">Table of Contents</a></li>
				<li class=\"next chapter\"><a href=\"" . $this->linkPrefix . $nextChapter . $this->linkTab . "1.0.0" . $this->linkSuffix . "\">Next Chapter &#187;</a></li>
			</ul>
			</div>";
		} else {
			 return "<div class=\"stage_nav group\">
			 <ul>
			 	<li class=\"previous\"><a href=\"" . $this->linkPrefix . $this->Tab . $this->linkTab . $previous . $this->linkSuffix . "\">&#171; Previous</a></li>
			 	<li class=\"toc\"><a href=\"index.htm\">Table of Contents</a></li>
				<li class=\"next\"><a href=\"" . $this->linkPrefix . $this->Tab . $this->linkTab . $next . $this->linkSuffix . "\">Next &#187;</a></li>
			</ul>
		</div>";									 
		}
	}
	
	
	public function getPageList()
	{
		return $this->linkArray;
	}
	
	public function getMenu($page)
	{
		$this->closeMenu();
		$node = $this->toc->find('a[href*=' . $page . ']', 0);	

		$node->class = "menu_here";
		$this->openMenu($node);
		$node = $this->toc->find('ul[id=ul_' . $page . ']', 0);
		if($node != NULL)
		{
			$node->style = "display:block;";
		}
		return $this->toc->innertext;
	}
	
	private function closeMenu()
	{
		foreach($this->toc->find('ul') as $ul)
		{
			$ul->style = "display:none;";
		}
		
		foreach($this->toc->find('a') as $a)
		{
			$a->style = null; 
		}
	}

	
	private function openMenu(&$node)
	{
		if($node->tag == 'ul')
		{
			$node->style = "display:block;";
		}

		$p = $node->parent();
		
		
		
		
		//echo "variabe 'p' has a value....:  $p";
		
		

		
		if($p != NULL)
		{
			$this->openMenu($p);
		}

	}
	
	//Check to see if the page requested is content or a structure node, if its a structure node then get the next in line.
	public function checkPage($page)
	{
		$node = $this->toc->find('a[href*=' . $page . ']', 0);
		if(isset($node) && strstr($node->class,'innerNode')) 
		{		
			$size=sizeof($this->completeLinkArray);
			for($i = 0; $i < $size; $i++) 
			{
				if($this->completeLinkArray[$i] == $page)
				{
					$next = $this->completeLinkArray[$i + 1];
					return $this->checkPage($next);
				}
			}	
		}
		return $page;
	}	
}




?>