<?php 
// no direct access

defined('_JEXEC') or die('Restricted access');

class djmenu
{

    var $name = null;
    
    var $params = null;
    
    var $tabakt = null;
	var $djsubwrap_style="style=\"left:0px;\"";

    
    function render(&$paramsa)
    {

    
        $this->params = $paramsa;

        
        if (is_null($this->name)) {
        
            $this->name = $this->params->get('menu');
            
        }
        
        echo '<ul id="dj-main">';
        
        $this->ShowMenu();
        
        echo '</ul>';
        
    }
    
    function ShowMenu()
    {
    
        $menu = &JSite::getMenu();
        
        $user = &JFactory::getUser();
        
        //get menu items
        
        $rows = $menu->getItems('menutype', $this->name);
        
        $children = array();
        
        foreach ($rows as $v) {
        
            $pt = $v->parent;

            
            $list = @$children[$pt] ? $children[$pt] : array();

            
            array_push($list, $v);

            
            $children[$pt] = $list;

            
        }

        
        $this->tabakt = $this->aktywne($children);

        
        $this->mosRecurseListMenu(0, 0, $children);

        
        return true;
        
    }

    
    function aktywne(&$children) {
    
        global $Itemid;

        
        foreach ($children as $tab) {
        
            foreach ($tab as $obj) {
            
                if ($obj->id == $Itemid)
                    return $obj->tree;

                    
            }
            
        }
        
        return array();
    }
    
    /**
    
     * Utility function for writing a menu link
     
     */
     
    function mosGetMenuLink($mitem, $level = 0, &$params, $havechild = null)
    {
    
        global $Itemid;
        
        $txt = '';
        
        // Menu Link is a special type that is a link to another item
        
        if ($mitem->type == 'menulink')
        {
        
            $menu = &JSite::getMenu();
            
            if ($tmp = $menu->getItem($mitem->query['Itemid'])) {
            
                $name = $mitem->name;
                
                $mid = $mitem->id;
                
                $parent = $mitem->parent;
                
                $mitem = clone ($tmp);
                
                $mitem->name = $name;
                
                $mitem->mid = $mid;
                
                $mitem->parent = $parent;
                
            } else {
            
                return;
                
            }
            
        }
        
        switch ($mitem->type)
        {
        
            case 'separator':
            
                break;
                
            case 'url':
            
                if (eregi('index.php\?', $mitem->link)) {
                
                    if (!eregi('Itemid=', $mitem->link)) {
                    
                        $mitem->link .= '&amp;Itemid='.$mitem->id;
                        
                    }
                    
                }
                
                break;
                
            default:
            
                $mitem->link = 'index.php?Itemid='.$mitem->id;
                
                break;
                
        }
        
        // Active Menu highlighting
        
        $current_itemid = intval($Itemid);

        
        $classplus = '';

        
        if (in_array($mitem->id, $this->tabakt)) {
        
            $classplus = 'active ';
            
        }

        
        $class = 'class="'.$classplus.'"';
        
        if ($level == 0)
            $class = 'class="dj-up_a '.$classplus.'"';
            
        if ($havechild && $level != 0) {
        
            if ( empty($classplus))
                $class = ' class="dj-more" ';
                
            else
                $class = ' class="dj-more-'.$classplus.'" ';
                
        }

        
        // replace & with amp; for xhtml compliance
        
        $menu_params = new stdClass ();
        
        $menu_params = new JParameter($mitem->params);
        
        $menu_secure = $menu_params->def('secure', 0);
        
        if (strcasecmp(substr($mitem->link, 0, 4), 'http')) {
        
            $mitem->url = JRoute::_($mitem->link, true, $menu_secure);
            
        } else {
        
            $mitem->url = $mitem->link;
            
        }

        
        $spanclass = '';
        
        if ($havechild && $level == 0) {
        
            $spanclass = ' class="dj-drop" ';
            
        }

        
        // replace & with amp; for xhtml compliance
        
        // remove slashes from excaped characters
        
        $mitem->name = stripslashes(htmlspecialchars($mitem->name));
        
        $mitemname = $mitem->name;
        
        if ($level == 0) {
        
            $mitemname = "<span $spanclass >$mitem->name</span>";
            
        }
        
        switch ($mitem->browserNav)
        {
        
            // cases are slightly different
            
            case 1:
            
                // open in a new window

                
                $txt = '<a href="'.$mitem->url.'" target="_blank"  '.$class.'>'.$mitemname.'</a>';
                
                break;
                
            case 2:
            
                // open in a popup window
                
                $txt = "<a href=\"#\" onclick=\"javascript: window.open('".$mitem->url."', '', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=780,height=550'); return false\" class=\"$menuclass\" ".$id.">".$mitem->name."</a>\n";
                
                break;
                
            case 3:
            
                // don't link it
                
                $txt = '<a '.$class.'>'.$mitem->name.'</a>';
                
                break;
                
            default:
            
                // open in parent window
                
                if ($level == 0) {
                
                    $mitemname = "<span $spanclass >$mitem->name</span>";
                    
                }
                
                $txt = '<a href="'.$mitem->url.'"  '.$class.'>'.$mitemname.'</a>';
                
                break;
                
        }

        
        return $txt;
        
    }
    
    /**
    
     * Search for Itemid in link
     
     */
     
    function ItemidContained($link, $Itemid)
    {
    
        $link = str_replace('&amp;', '&', $link);
        
        $temp = explode("&", $link);
        
        $linkItemid = "";
        
        foreach ($temp as $value) {
        
            $temp2 = explode("=", $value);
            
            if ($temp2[0] == "Itemid") {
            
                $linkItemid = $temp2[1];
                
                break;
                
            }
            
        }
        
        if ($linkItemid != "" && $linkItemid == $Itemid) {
        
            return true;
            
        } else {
        
            return false;
            
        }
        
    }
    
    /**
    
     *  Module Menu
     
     */
     
    function mosRecurseListMenu($id, $level, &$children) {
    
        global $Itemid;
        
        global $HTTP_SERVER_VARS,$mosConfig_live_site;
        
        if (@$children[$id]) {
        
            $elements = count($children[$id]);
            
            $counter = 0;
            
            foreach ($children[$id] as $row)
            {
            
                $counter++;
                
                $separator = false;
                
                switch ($row->type) {
                
                    case 'separator':
                    
                        // do nothing
                        
                        $row->link = "seperator";
                        
                        $separator = true;
                        
                        break;
                        
                    case 'url':
                    
                        if (eregi('index.php\?', $row->link)) {
                        
                            if (!eregi('Itemid=', $row->link)) {
                            
                                $row->link .= '&Itemid='.$row->id;
                                
                            }
                            
                        }
                        
                        break;
                        
                    default:
                    
                        if (!eregi('Itemid=', $row->link))
                        {
                        
                            $row->link .= "&Itemid=$row->id";
                            
                        }
                        
                        break;
                        
                }

                
                $havechild = is_array($children[$row->id]);
                
                $classname = "";

                
                if ($level == 0) {
                
                    $classname .= "dj-up Itemid".$row->id." ";

                    
                    if ($counter == 1) {
                    
                        $classname .= "first ";
                        
                    } else if ($counter == $elements) {
                    
                        $classname .= "last ";
                        
                    }
                    
                }

                
                if (in_array($row->id, $this->tabakt))
                {
                
                    $classname .= "active";
                    
                }

                
                $class = "";
                
                if (! empty($classname)) {
                
                    $class = " class=\"".$classname."\"";
                    
                }
                
                if ($separator) {
                
                    echo "\n";
                    
                    echo "<li".$class."><span class='separator'>".$row->name."</span></li>";
                    
                } else
                if ($havechild) {
                
                    if ($level == 1) {
                        if ($li_count > 0 && $li_count % $this->params->get('column_n') == 0) {
                            echo '</ul><ul class="dj-submenu">';
                        }
                        $li_count++;
                    }
                    echo "<li".$class.">".$this->mosGetMenuLink($row, $level, $this->params, 1);
                    
                    echo "\n";
                    
                    if ($level == 0) {
                        if ($this->params->get('submenu') == 1) {
                            $li_count = 0;

							if($this->params->get('submenu_right')==$row->id){
								$this->djsubwrap_style = "style=\"right:0px;\"";
							}
                            echo "<div class=\"djsubwrap\" ".$this->djsubwrap_style."  ><div class=\"col_".$this->params->get('column_n')."\"><div class=\"div_3\"><div class=\"clearfix\">
									<ul class=\"dj-submenu\" >\n";
									
                            $this->mosRecurseListMenu($row->id, $level + 1, $children);
                            
                            echo "</ul>\n </div></div></div></div>";
                        }
                    } else {
                        $lev = $level + 1;
                        echo "<ul class=\"dj-submenu".$lev."\">\n";
                        
                        $this->mosRecurseListMenu($row->id, $level + 1, $children);
                        
                        echo "</ul>\n";

                        
                    }
                    
                    echo "</li>\n";
                    
                } else {
                    if ($level == 1) {
                        if ($li_count > 0 && $li_count % $this->params->get('column_n') == 0) {
                            echo '</ul><ul class="dj-submenu">';
                        }
                        $li_count++;
                    }
                    
                    echo "<li".$class.">".$this->mosGetMenuLink($row, $level, $this->params)."</li>";
                    
                    echo "\n";
                    
                }
                
            }
            
        }
        
    }
    
} /* end module menu */

$menu = new djmenu();

$menu->render($params);

?>
		