<?php

class renderNavigationClass extends renderClass_abstract
{
	private $_navData = array();
	private $_node = null;
	private $_prefix = '';
	private $_firstKey = 0;

	public function createNavigation($navData)
	{
		$this->_navData = $navData;
		$this->_node = $this->_tpl->createDocumentFragment();
		$navKeys = array_keys($navData);
		$firstKey = $navKeys[0];
		$this->_firstKey = $firstKey;
		$this->_prefix = renderNavigationClass:: getNavigationPrefix();

		$node = renderNavigationClass:: getNavigationNode($this->_firstKey);
		if ($node !== false)
		{
			$this->_node->appendChild($node);
		}

		return $this->_node;
	}

	private function getNavigationPrefix()
	{
		foreach ($this->_navData[$this->_firstKey][0] as $key => $value)
		{
			if (strpos($key, '_') !== false)
			{
				return substr($key, 0, strpos($key, '_') + 1);
			}
			else
			{
				return '';
			}
		}
		return '';
	}

	private function getNavigationNode($navStart)
	{
		if (!isset ($this->_navData[$navStart]) || count($this->_navData [$navStart]) == 0)
		{
			return false;
		}

		$newNode = $this->_tpl->createElement('ul');
		foreach ($this->_navData [$navStart] AS $key => $value)
		{
			if ($this->_textsFromConfig)
			{
				if (array_key_exists($value [$this->_prefix . 'navId'], $this->_website))
				{
					$navTitle = $this->_website[$value [$this->_prefix . 'navId']]['names'];
				}
				else
				{
					$navTitle = $this->_langArray ['navi']['site_' . $value [$this->_prefix . 'navId']];
				}
			}
			else
			{
				$navTitle = $value[$this->_prefix . 'navName'];
			}
			if ($this->_modrewrite)
			{
				$navHref = $value[$this->_prefix . 'navLink'];
			}
			else
			{
				$navHref = (!isset ($value[$this->_prefix . 'navType']) || (isset ($value[$this->_prefix . 'navType']) && $value[$this->_prefix . 'navType'] === 'html'))
					? $value [$this->_prefix . 'navTarget']
					: 'javascript:void( 0 );';
			}

			$newLi = $this->_tpl->createElement('li');
			$newHref = $this->_tpl->createElement('a');
			if (in_array($value[$this->_prefix . 'navId'], $this->_navpath))
			{
				$newHref->setAttributeNode(new DOMAttr ('class', 'active'));
			}
			$newHref->appendChild(new DOMText ($navTitle));
			$newHref->setAttributeNode(new DOMAttr ('href', $navHref));
			if (isset ($value[$this->_prefix . 'navType']) && $value[$this->_prefix . 'navType'] === 'js')
			{
				$newHref->setAttributeNode(new DOMAttr ('onclick', $value[$this->_prefix . 'navTarget']));
			}
			$newLi->appendChild($newHref);

			$sub_node = renderNavigationClass:: getNavigationNode($value[$this->_prefix . 'navId']);
			if ($sub_node !== false)
			{
				$newLi->appendChild($sub_node);
			}
			$newNode->appendChild($newLi);

		}

		unset ($this->_navData [$navStart]);

		return $newNode;

	}
}