<?php

/**
 * @package Flatnux_functions
 * @author Alessandro Vernassa <speleoalex@gmail.com>
 * @copyright Copyright (c) 1011
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
 */
defined('_FNEXEC') or die('Restricted access');

if (!function_exists("FN_HtmlNext"))
{
    /**
     * Open Table
     * @return string
     */
    function FN_HtmlNext($title = "")
    {
        return "<img title=\"$title\" style=\"vertical-align:middle\" src=\"".FN_FromTheme("images/right.png")."\" alt=\"$title\" />";
    }

}
if (!function_exists("FN_HtmlOpenTable"))
{
    /**
     * Open Table
     * @return string
     */
    function FN_HtmlOpenTable()
    {
        return "<table  width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" ><tr><td valign=\"top\">";
    }

}
if (!function_exists("FN_HtmlCloseTable"))
{
    /**
     * Close Table
     * @return string
     */
    function FN_HtmlCloseTable()
    {
        return "</td></tr></table>";
    }

}
if (!function_exists("FN_HtmlOpenTableTitle"))
{
    /**
     * Open Table
     * @return string
     */
    function FN_HtmlOpenTableTitle($title)
    {
        return "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\"><tr>\n<td valign=\"top\">$title</td></tr>\n<tr><td>";
    }

}
if (!function_exists("FN_HtmlCloseTableTitle"))
{
    /**
     * Close Table
     * @return string
     */
    function FN_HtmlCloseTableTitle()
    {
        return "</td></tr></table>\n";
    }

}
if (!function_exists("FN_HtmlHeader"))
{
    /**
     *
     * @global array $_FN
     * @param bool $tags
     * @return string 
     */
    function FN_HtmlHeader($include_theme_css = true,$include_section_css = true)
    {
        global $_FN;
        $html = "";
        $sectionvalues = FN_GetSectionValues($_FN['mod']);
        if (!empty($sectionvalues['type']) && file_exists("modules/{$sectionvalues['type']}/header.php"))
        {
            require_once "modules/{$sectionvalues['type']}/header.php";
        }
        if (file_exists("sections/{$_FN['mod']}/header.php"))
        {
            require_once "sections/{$_FN['mod']}/header.php";
        }
        if (!empty($_FN['section_header']))
        {
            $html .= $_FN['section_header'];
        }
        $html .= "\n\t<title>{$_FN['site_title']}</title>";
        $html .= FN_IncludeCSS($include_theme_css,$include_section_css);
        $html .= FN_IncludeJS();
        $html .="\n\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=".$_FN['charset_page']."\" />";
        $html .="\n\t<meta name=\"KEYWORDS\" content=\"{$_FN['keywords']}\"  />";
        $html .= "\n\t<meta http-equiv=\"EXPIRES\" content=\"0\" />";
        $html .= "\t<meta name=\"REVISIT-AFTER\" content=\"1 DAYS\" />\n";
        $html .= "\t<meta name=\"RATING\" content=\"GENERAL\" />\n";
        $html .="\t<script type=\"text/javascript\">
\t//<!--
\tcheck = function (url)
\t{
\t\tif(confirm (\"".FN_i18n("are you sure you want to do it?")."\"))
\t\t\twindow.location=url;
\t}
\t// -->
\t</script>";
        if (!empty($_FN['section_header_footer']))
        {
            $html .= $_FN['section_header_footer'];
        }

        return $html;
    }

}
if (!function_exists("FN_HtmlBlocks"))
{
    /**
     *
     * @global array $_FN
     * @param string $where
     * @return string
     */
    function FN_HtmlBlocks($where)
    {
        global $_FN;
        $ret = "";
        $blocks = FN_GetBlocks($where);
        foreach ($blocks as $block)
        {
            $html = FN_HtmlBlock($block['id']);
            if ($html != "")
            {
                $ret .= FN_HtmlOpenBlock($block['title']);
                $ret .= $html;
                $ret .= FN_HtmlCloseBlock();
            }
        }
        return $ret;
    }

}

if (!function_exists("FN_HtmlOpenBlock"))
{
    /**
     * Open block
     * @return string
     */
    function FN_HtmlOpenBlock($title)
    {
        $html = "<div class=\"block\">";
        if ($title !== "")
            $html .= "<div class=\"blocktitle\">$title</div>";
        $html .= "<div class=\"blockcontents\">";
        return $html;
    }

}
if (!function_exists("FN_HtmlCloseBlock"))
{
    /**
     * default close block
     * @param string $title
     * @return string
     */
    function FN_HtmlCloseBlock($title = "")
    {
        return "</div></div>\n";
    }

}

if (!function_exists("FN_HtmlMenu"))
{
    /**
     *
     * @param string $separator
     * @param string $sectionroot
     * @return string
     */
    function FN_HtmlMenu($separator = "&nbsp;|&nbsp;",$sectionroot = false)
    {
        $menu = array();
        $sections = FN_GetSections($sectionroot);
        foreach ($sections as $section)
        {
            $accesskey = FN_GetAccessKey($section['title'],"index.php?mod={$section['id']}",$section['accesskey']);
            if ($accesskey != "")
                $accesskey = " accesskey=\"$accesskey\"";
            $menu[] = "<a $accesskey href=\"{$section['link']}\">{$section['title']}</a>";
        }
        $ret = implode($separator,$menu);
        return $ret;
    }

}

if (!function_exists("FN_HtmlSectionMenu"))
{
    /**
     *
     * @param string $separator
     * @param string $sectionroot
     * @return string
     */
    function FN_HtmlSectionMenu($sectionroot = false)
    {
        $menu = array();
        $menu = FN_GetSections($sectionroot);
        if (count($menu) == 0)
            return "";
        $ret = "<ul>\n";
        foreach ($menu as $item)
        {
            $ret .= FN_HtmlSectionMenuItem($item);
        }
        $ret .= "</ul>";
        return $ret;
    }

}
if (!function_exists("FN_HtmlSectionMenuItem"))
{
    /**
     *
     * @param string $separator
     * @param string $sectionroot
     * @return string
     */
    function FN_HtmlSectionMenuItem($menuitem)
    {
        $ret = "";
        $title = $menuitem['title'];
        $accesskey = FN_GetAccessKey($title,"index.php?mod={$menuitem['id']}",$menuitem['accesskey']);
        if ($accesskey != "")
            $accesskey = " accesskey=\"$accesskey\"";
        $ret .= "<li><a $accesskey href=\"{$menuitem['link']}\">{$menuitem['title']}</a></li>";
        return $ret;
    }

}
/**
 *
 * @global array $_FN
 * @staticvar int $lev
 * @staticvar string $html
 * @param string $parent
 * @return string
 */
function FN_HtmlMenuTree($parent = "",$recursive = true)
{
    global $_FN;
    static $lev = 0;
    $html = "";
    $ret = array();
    $current = $_FN['mod'];
    $sections = FN_GetSections($parent);
    if (empty($sections) || count($sections) == 0)
        return "";
    foreach ($sections as $section)
    {
        $html .= "<span style=\"white-space: nowrap\">";
        for ($i = 0; $i < $lev; $i++)
        {
            $html .= "&nbsp;&nbsp;&nbsp;&nbsp;";
        }

        $accesskey = FN_GetAccessKey($section['title'],"index.php?mod={$section['id']}");
        if ($accesskey != "")
            $accesskey = " accesskey=\"$accesskey\"";
        $title = (empty($section['description'])) ? "" : "title=\"{$section['description']}\"";
        if ($current == $section['id'])
        {
            $html .= "<a $title $accesskey href=\"".fn_rewritelink("index.php?mod={$section['id']}")."\">".$section['title']."</a>";
        }
        else
        {
            $html .= "<a $title $accesskey href=\"".fn_rewritelink("index.php?mod={$section['id']}")."\">".$section['title']."</a>";
        }
        $html .= "</span><br />";
        $lev++;
        if ($recursive)
            $html .= FN_HtmlMenuTree($section['id']);
        $lev--;
    }
    return $html;
}

if (!function_exists("FN_HtmlMenuTreeUl"))
{
    /**
     *
     * @global array $_FN
     * @staticvar int $lev
     * @staticvar string $html
     * @param string $parent
     * @return string
     */
    function FN_HtmlMenuTreeUl($parent = "",$recursive = true)
    {
        global $_FN;
        static $lev = 0;
        $html = "";
        $ret = array();
        $current = $_FN['mod'];
        $sections = FN_GetSections($parent);
        if (empty($sections) || count($sections) == 0)
            return "";
        $html.="<ul>\n";
        foreach ($sections as $section)
        {
            $html .= "<li>";
            $accesskey = FN_GetAccessKey($section['title'],"index.php?mod={$section['id']}");
            if ($accesskey != "")
                $accesskey = " accesskey=\"$accesskey\"";
            $title = (empty($section['description'])) ? "" : "title=\"{$section['description']}\"";
            if ($current == $section['id'])
            {
                $html .= "<a $title $accesskey href=\"".fn_rewritelink("index.php?mod={$section['id']}")."\">".$section['title']."</a>";
            }
            else
            {
                $html .= "<a $title $accesskey href=\"".fn_rewritelink("index.php?mod={$section['id']}")."\">".$section['title']."</a>";
            }
            $lev++;
            if ($recursive)
                $html .= FN_HtmlMenuTreeUl($section['id']);
            $html .= "</li>";
            $lev--;
        }
        $html.="\n</ul>";
        return $html;
    }

}


if (!function_exists("FN_HtmlCredits"))
{
    /**
     *
     * @return string 
     */
    function FN_HtmlCredits()
    {
        global $_FN;
        if (!isset($_FN['credits']))
        {
            $html = "Powered by <a href=\"http://www.flatnux.org\">Flatnux</a>";
        }
        else
        {
            $html = $_FN['credits'];
        }
        return $html;
    }

}
/* * *********************************TEMPLATE********************************** */
/* * *********************************TEMPLATE********************************** */
/* * *********************************TEMPLATE********************************** */
/* * *********************************TEMPLATE********************************** */
/**
 *
 * @global array $_FN
 * @param string $templatefile
 * @return type 
 */
function FN_TPL_html_MakeThemeFromTemplate($templatefile)
{
    global $_FN;
    $conf = FN_LoadConfig("themes/{$_FN['theme']}/config.php");
    $header = FN_HtmlHeader(false);
    $vars = array();

    $vars['lang'] = $_FN['lang'];
    $vars['fnblocksright'] = FN_HtmlBlocks("right");
    $vars['fnblocksleft'] = FN_HtmlBlocks("left");
    $vars['site_title'] = $_FN['site_title'];
    $vars['site_subtitle'] = $_FN['site_subtitle'];
    $vars['siteurl'] = $_FN['siteurl'];
    $vars['credits'] = FN_HtmlCredits();
    $vars['navbar'] = FN_HtmlNavbar();
    $vars['section_title'] = $_FN['sectionvalues']['title'];
    $vars['rss_link'] = isset($_FN['rss_link']) ? $_FN['rss_link'] : "#";

    $html = FN_TPL_include_tpl(FN_TPL_ApplyTplFile($templatefile,$vars),$vars);
    //dprint_r("strend=".  htmlspecialchars($html));
    foreach ($vars as $key=>$value)
    {
        $html = str_replace("{".$key."}",htmlspecialchars("{".$key."}"),$html);
        $html = str_replace("{".$key."}",$value,$html);
    }
    $html = preg_replace('/<title>[^<]*<\/title>/is',"",$html);
    $html = str_replace("<head>","<head>".$header,$html);

    return $html;
}

/**
 *
 * @return string
 */
function FN_TPL_tp_create_hmenu($str)
{
    return FN_HtmlMenu("&nbsp;|&nbsp;");
}

/**
 *
 * @return string
 */
function FN_TPL_tp_create_section($str)
{
    return FN_HtmlSection();
}

/**
 *
 * @param string $str
 * @param array $vars
 * @return string
 */
function FN_TPL_include_tpl($str,$vars)
{
    $strout = $str;
    $array = preg_match_all('/<!-- include.*?<!-- end include -->/s',$str,$out);
    $array = preg_match_all('/<!-- include ([\w]+) -->(.*?)(<!-- end include (\\1) -->)/s',$str,$out);
    if (is_array($out[0]))
        foreach ($out as $k=>$v)
        {
            if (is_array($v))
            {
                foreach ($v as $toreplace)
                {

                    $tpname = explode("-->",$toreplace);
                    $tpname = str_replace("<!-- include ","",$tpname[0]);
                    $tpname = trim(ltrim($tpname));

                    if (function_exists("FN_TPL_tp_create_".$tpname))
                    {
                        $fname = "FN_TPL_tp_create_".$tpname;
                        $replace = $fname($toreplace);
                        //dprint_r(htmlspecialchars("replace = ".$replace));

                        $strout = str_replace($toreplace,$replace,$strout);
                        //dprint_r($tpname . ":::\n" . htmlspecialchars($toreplace));
                        //dprint_r("NEWSTRING:<br />".htmlspecialchars($replace));
                        //dprint_r("----");
                    }
                }
            }
            break;
        }
    return $strout;
}

/**
 *
 * @param string $tplname
 * @param array $vars
 * @return string
 */
function FN_TPL_ApplyTplFile($tplname,$vars)
{
    global $_FN;
    $str = file_get_contents($tplname);
    $basepath = dirname($tplname)."/";
    return FN_TPL_ApplyTplString($str,$vars,$basepath);
}

/**
 *
 * @global array $_FN
 * @param string $str
 * @param array $vars
 * @param string $basepath
 * @return string 
 */
function FN_TPL_ApplyTplString($str,$vars,$basepath = false)
{
    global $_FN;

    $old = "";
    if ($basepath)
    {
        while($old != $str)
        {
            $old = $str;
            $str = preg_replace("/<([^>]+)( background| href| src)=(\")([^:^{]*)(\")/im","<\\1\\2=\\3{$_FN['siteurl']}$basepath\\4\\3",$str);
            $str = preg_replace("/<([^>]+)( background| href| src)=(\')([^:^{]*)(\')/im","<\\1\\2=\\3{$_FN['siteurl']}$basepath\\4\\3",$str);
        }
    }
    $strout = $str;
    foreach ($vars as $key=>$value)
    {
        if (is_string($value))
        {
            //$strout = str_replace("{" . $key . "}", htmlspecialchars("{" . $key . "}"), $strout);
            $strout = str_replace("{".$key."}",$value,$strout);
        }
    }
    $i18n = array();
    preg_match_all("/{i18n:([^\}]+)}/",$strout,$i18n);
    if (isset($i18n[1]))
    {
        foreach ($i18n[1] as $i18n_item)
        {
            $mode = "AA";
            if (preg_match("/^[A-Z]/s",$i18n_item) && preg_match("/[a-z]$/s",$i18n_item))
            {
                $mode = "Aa";
            }
            elseif (preg_match("/^[a-z]/s",$i18n_item) && preg_match("/[a-z]$/s",$i18n_item))
            {
                $mode = "aa";
            }
            elseif (preg_match("/^[A-A]/s",$i18n_item) && preg_match("/[A-Z]$/s",$i18n_item))
            {
                $mode = "AA";
            }


            $strout = str_replace("{i18n:$i18n_item}",FN_Translate(strtolower("$i18n_item"),$mode),$strout);
        }
    }
    return $strout;
}

/**
 *
 * @global array $_FN
 * @return string 
 */
function FN_TPL_tp_create_menu($str)
{
    global $_FN;
    static $sections = false;
    /*
      $cachefile = "misc/_cache/".md5($str).$idsection.$_FN['mod'].$_FN['theme'];
      if (file_exists($cachefile))
      {
      return file_get_contents($cachefile);
      }
     * 
     */
    if ($str == "")
        return "";
    $tp_menuitem = preg_match('/<!-- menuitem -->(.*)<!-- endmenuitem -->/is',$str,$out);
    $tp_menuitem = empty($out[0]) ? "<a href=\"link\">title</a><br />" : $out[0];
    $tp_menuitemactive = preg_match('/<!-- menuitemactive -->(.*)<!-- endmenuitemactive -->/is',$str,$out);
    $tp_menuitemactive = empty($out[0]) ? $tp_menuitem : $out[0];
    //$tp_menuitemwithsubmenu = preg_match('/<!-- menuitemwithsubmenu -->(.*)<!-- menuitemwithsubmenu -->/is',$str,$out);   
    //$tp_menuitemwithsubmenu = empty($out[0]) ? $tp_menuitem : $out[0];
    $tp_menuitem = preg_replace("/<a([^>]+)(href)=(\")([^\"]*)(\")/im","<a\\1\\2=\\3{link}\\3",$tp_menuitem);
    $tp_menuitem = preg_replace("/<a([^>]+)(href)=(\')([^\']*)(\')/im","<a\\1\\2=\\3{link}\\3",$tp_menuitem);
    $tp_menuitemactive = preg_replace("/<a([^>]+)(href)=(\")([^\"]*)(\")/im","<a\\1\\2=\\3{link}\\3",$tp_menuitemactive);
    $tp_menuitemactive = preg_replace("/<a([^>]+)(href)=(\')([^\']*)(\')/im","<a\\1\\2=\\3{link}\\3",$tp_menuitemactive);
    //$tp_menuitemwithsubmenu = preg_replace("/<a([^>]+)(href)=(\")([^\"]*)(\")/im","<a\\1\\2=\\3{link}\\3",$tp_menuitemwithsubmenu);
    //$tp_menuitemwithsubmenu = preg_replace("/<a([^>]+)(href)=(\')([^\']*)(\')/im","<a\\1\\2=\\3{link}\\3",$tp_menuitemwithsubmenu);



    if (strpos($tp_menuitem,'{title}') === false)
    {
        $tp_menuitem = preg_replace("/(<a.*>)(.*)(<\/a)/im","\\1{title}\\3",$tp_menuitem);
        $tp_menuitemactive = preg_replace("/(<a.*>)(.*)(<\/a)/im","\\1{title}\\3",$tp_menuitemactive);
    }
    //add title
    if (false == strpos($tp_menuitem,"title="))
    {
        $tp_menuitem = str_replace("<a","<a title=\"{description}\" ",$tp_menuitem);
    }
    //add title
    if (false == strpos($tp_menuitemactive,"title="))
    {
        $tp_menuitemactive = str_replace("<a","<a title=\"{description}\" ",$tp_menuitemactive);
    }
    //add accesskey
    if (false == strpos($tp_menuitem,"{accesskey"))
    {
        $tp_menuitem = str_replace("<a","<a accesskey=\"{accesskey}\" ",$tp_menuitem);
    }
    //add accesskey
    if (false == strpos($tp_menuitemactive,"{accesskey"))
    {
        $tp_menuitemactive = str_replace("<a","<a accesskey=\"{accesskey}\" ",$tp_menuitemactive);
    }

    $htmlout = "";
    if (!$sections)
        $sections = FN_GetSections();

    foreach ($sections as $sectionvalues)
    {
        $sectionvalues['accesskey'] = FN_GetAccessKey($sectionvalues['title'],"index.php?mod={$sectionvalues['id']}");
        if ($_FN['mod'] == $sectionvalues['id'] || FN_SectionIsInsideThis($sectionvalues['id']))
        {
            $htmlmenuitem = FN_TPL_ApplyTplString($tp_menuitemactive,$sectionvalues,false);
            $tp_submenuitem_ori_template = preg_match('/<!-- submenu -->(.*)<!-- endsubmenu -->/is',$tp_menuitemactive,$out);
            $tp_submenuitem_ori_template = empty($out[0]) ? "" : $out[0];
        }
        else
        {
            $htmlmenuitem = FN_TPL_ApplyTplString($tp_menuitem,$sectionvalues,false);
            $tp_submenuitem_ori_template = preg_match('/<!-- submenu -->(.*)<!-- endsubmenu -->/is',$tp_menuitem,$out);
            $tp_submenuitem_ori_template = empty($out[0]) ? "" : $out[0];
        }

        $tp_submenuitem = preg_match('/<!-- submenu -->(.*)<!-- endsubmenu -->/is',$htmlmenuitem,$out);
        $tp_submenuitem_ori = empty($out[0]) ? "" : $out[0];

        $tp_submenuitem_new = $tp_submenuitem_ori;

        $submenu_str = FN_TPL_tp_create_submenu_($tp_submenuitem_ori_template,$sectionvalues['id']);
        $tp_submenuitem_new = str_replace($tp_submenuitem_ori,$submenu_str,$tp_submenuitem_ori);
        $htmlmenuitem = str_replace($tp_submenuitem_ori,$tp_submenuitem_new,$htmlmenuitem);
        $htmlout .=$htmlmenuitem;
    }
    //FN_Write($htmlout,$cachefile);
    //die();
    return $htmlout;
}

/**
 *
 * @global array $_FN
 * @return string 
 */
function FN_TPL_tp_create_submenu_($str,$idsection)
{
    global $_FN;
    static $cache_tp_menuitem = "";
    static $cache_tp_menuitemactive = "";
    static $cache_tp_menuitem_old = "";
    $idcache = md5($str);
    if ($str == "" || $idsection == "")
        return "";
    $sections = FN_GetSections($idsection);
    if (!$sections)
        return "";

    if (empty($cache_tp_menuitem[$idcache]))
    {
        preg_match('/<!-- submenuitems -->(.*)<!-- endsubmenuitems -->/is',$str,$out);
        $tp_menuitem_old = empty($out[0]) ? "<a href=\"link\">title</a><br />" : $out[0];
        $tp_menuitem = preg_match('/<!-- submenuitem -->(.*)<!-- endsubmenuitem -->/is',$str,$out);
        $tp_menuitem = empty($out[0]) ? "<a href=\"link\">title</a><br />" : $out[0];
        $tp_menuitemactive = preg_match('/<!-- submenuitemactive -->(.*)<!-- endsubmenuitemactive -->/is',$str,$out);
        $tp_menuitemactive = empty($out[0]) ? $tp_menuitem : $out[0];
        $tp_menuitem = preg_replace("/<a([^>]+)(href)=(\")([^\"]*)(\")/im","<a\\1\\2=\\3{link}\\3",$tp_menuitem);
        $tp_menuitem = preg_replace("/<a([^>]+)(href)=(\')([^\']*)(\')/im","<a\\1\\2=\\3{link}\\3",$tp_menuitem);
        $tp_menuitemactive = preg_replace("/<a([^>]+)(href)=(\")([^\"]*)(\")/im","<a\\1\\2=\\3{link}\\3",$tp_menuitemactive);
        $tp_menuitemactive = preg_replace("/<a([^>]+)(href)=(\')([^\']*)(\')/im","<a\\1\\2=\\3{link}\\3",$tp_menuitemactive);
        if (strpos($tp_menuitem,'{title}') === false)
        {
            $tp_menuitem = preg_replace("/(<a.*>)(.*)(<\/a)/im","\\1{title}\\3",$tp_menuitem);
            $tp_menuitemactive = preg_replace("/(<a.*>)(.*)(<\/a)/im","\\1{title}\\3",$tp_menuitemactive);
        }
        $cache_tp_menuitem[$idcache] = $tp_menuitem;
        $cache_tp_menuitemactive[$idcache] = $tp_menuitemactive;
        $cache_tp_menuitem_old[$idcache] = $tp_menuitem_old;
        //add title
        if (false == strpos($tp_menuitem,"title="))
        {
            $tp_menuitem = str_replace("<a","<a title=\"{description}\" ",$tp_menuitem);
        }
        //add title
        if (false == strpos($tp_menuitemactive,"title="))
        {
            $tp_menuitemactive = str_replace("<a","<a title=\"{description}\" ",$tp_menuitemactive);
        }
        //add accesskey
        if (false == strpos($tp_menuitem,"{accesskey"))
        {
            $tp_menuitem = str_replace("<a","<a accesskey=\"{accesskey}\" ",$tp_menuitem);
        }
        //add accesskey
        if (false == strpos($tp_menuitemactive,"{accesskey"))
        {
            $tp_menuitemactive = str_replace("<a","<a accesskey=\"{accesskey}\" ",$tp_menuitemactive);
        }
    }
    else
    {
        $tp_menuitem_old = $cache_tp_menuitem_old[$idcache];
        $tp_menuitem = $cache_tp_menuitem[$idcache];
        $tp_menuitemactive = $cache_tp_menuitem_old[$idcache];
    }
    $htmlout = "";
    foreach ($sections as $sectionvalues)
    {
        $sectionvalues['accesskey'] = FN_GetAccessKey($sectionvalues['title'],"index.php?mod={$sectionvalues['id']}");
        if ($_FN['mod'] == $sectionvalues['id'])
            $htmlout.= FN_TPL_ApplyTplString($tp_menuitemactive,$sectionvalues,false);
        else
            $htmlout.= FN_TPL_ApplyTplString($tp_menuitem,$sectionvalues,false);
        if (strpos($htmlout,'{submenu}') !== false)
        {
            $htmlout = str_replace("{submenu}",FN_TPL_tp_create_submenu_($str,$sectionvalues['id']),$htmlout);
        }
    }
    if ($htmlout != "")
        $htmlout = str_replace($tp_menuitem_old,$htmlout,$str);
    return $htmlout;
}

/**
 *
 * @return string
 */
function FN_TPL_tp_create_blocks_right($str)
{
    return FN_TPL_tp_create_blocks($str,"right");
}

/**
 *
 * @return string
 */
function FN_TPL_tp_create_blocks_left($str)
{
    return FN_TPL_tp_create_blocks($str,"left");
}

/**
 *
 * @return string
 */
function FN_TPL_tp_create_blocks_top($str)
{
    return FN_TPL_tp_create_blocks($str,"top");
}

/**
 *
 * @return string
 */
function FN_TPL_tp_create_blocks_bottom($str)
{
    return FN_TPL_tp_create_blocks($str,"bottom");
}

/**
 *
 * @global array $_FN
 * @param string $where
 * @return string
 */
function FN_TPL_tp_create_blocks($str,$where)
{
    global $_FN;
    //$conf = FN_LoadConfig("themes/{$_FN['theme']}/config.php");
    preg_match('/<!-- blockitem -->(.*)<!-- endblockitem -->/is',$str,$out);
    $tp_block = empty($out[0]) ? "" : $out[0];
    $tp_block = preg_replace("/<!-- blocktitle -->(.*)<!-- endblocktitle -->/is","{title}",$tp_block);
    $tp_block = preg_replace("/<!-- blockcontents -->(.*)<!-- endblockcontents -->/is","{contents}",$tp_block);
    $blocks = FN_GetBlocks("$where");
    $htmlout = "";
    foreach ($blocks as $block)
    {
        $block['contents'] = FN_HtmlBlock($block['id']);
        if ($block['contents'] != "")
            $htmlout.= FN_TPL_ApplyTplString($tp_block,$block,false);
    }
    return $htmlout;
}

if (!function_exists("FN_HtmlNavbar"))
{
    /**
     *
     * @param string $sections
     * @return string 
     */
    function FN_HtmlNavbar($sections = "")
    {
        if ($sections == "")
            $sections = FN_GetSectionsTree();
        if (!is_array($sections))
            return "";
        $htmls = array();
        foreach ($sections as $section)
        {
            $htmls[] = "<a title=\"{$section['description']}\" accesskey=\"".FN_GetAccessKey($section['title'],$section['link'])."\" href=\"{$section['link']}\">{$section['title']}</a>";
        }
        $html = implode("&nbsp;&#187;&nbsp;",$htmls);
        return $html;
    }

}
if (!function_exists("FN_HtmlMainteanceMode"))
{
    /**
     *
     * @global array $_FN
     * @return string 
     */
    function FN_HtmlMainteanceMode()
    {
        global $_FN;
        $html = "<html><head><title>{$_FN['site_title']}</title></head><body>";
        $html.= "<h2>".FN_Translate("site in maintenance")."</h2>";
        $html.= FN_HtmlLoginForm();
        $html.= "</body>";
        return $html;
    }

}
if (!function_exists("FN_HtmlRight"))
{
    /**
     *
     * @param string $alt
     * @param string $title
     * @return string 
     */
    function FN_HtmlRight($alt = "",$title = "")
    {
        
        $html = "<img style=\"vertical-align:middle\" alt=\"$alt\" title=\"$title\" src=\"".FN_FromTheme("images/fn_right.png")."\" />";
        return $html;
    }

}
if (!function_exists("FN_HtmlLeft"))
{
    /**
     *
     * @param string $alt
     * @param string $title
     * @return string 
     */
    function FN_HtmlLeft($alt = "",$title = "")
    {
        $html = "<img style=\"vertical-align:middle\" alt=\"$alt\" title=\"$title\" src=\"".FN_FromTheme("images/fn_left.png")."\" />";
        return $html;
    }

}
if (!function_exists("FN_HtmlUp"))
{
    /**
     *
     * @param string $alt
     * @param string $title
     * @return string 
     */
    function FN_HtmlUp($alt = "",$title = "")
    {
        
        $html = "<img style=\"vertical-align:middle\" alt=\"$alt\" title=\"$title\" src=\"".FN_FromTheme("images/fn_up.png")."\" />";
        return $html;
    }

}
if (!function_exists("FN_HtmlDown"))
{
    /**
     *
     * @param string $alt
     * @param string $title
     * @return string 
     */
    function FN_HtmlDown($alt = "",$title = "")
    {
        $html = "<img style=\"vertical-align:middle\" alt=\"$alt\" title=\"$title\" src=\"".FN_FromTheme("images/fn_down.png")."\" />";
        return $html;
    }

}
if (!function_exists("FN_HtmlArrowRight"))
{
    /**
     *
     * @param string $alt
     * @param string $title
     * @return string 
     */
    function FN_HtmlArrowRight($alt = "",$title = "")
    {
        $html = "<img style=\"vertical-align:middle\" alt=\"$alt\" title=\"$title\" src=\"".FN_FromTheme("images/right.png")."\" />";
        return $html;
    }

}
if (!function_exists("FN_HtmlArrowLeft"))
{
    /**
     *
     * @param string $alt
     * @param string $title
     * @return string 
     */
    function FN_HtmlArrowLeft($alt = "",$title = "")
    {
        $html = "<img style=\"vertical-align:middle\" alt=\"$alt\" title=\"$title\" src=\"".FN_FromTheme("images/left.png")."\" />";
        return $html;
    }

}
if (!function_exists("FN_HtmlArrowUp"))
{
    /**
     *
     
     * @param string $alt
     * @param string $title
     * @return string 
     */
    function FN_HtmlArrowUp($alt = "",$title = "")
    {
        
        $html = "<img style=\"vertical-align:middle\" alt=\"$alt\" title=\"$title\"src=\"".FN_FromTheme("images/up.png")."\" />";
        return $html;
    }

}
if (!function_exists("FN_HtmlArrowDown"))
{
    /**
     *
     
     * @param string $alt
     * @param string $title
     * @return string 
     */
    function FN_HtmlArrowDown($alt = "",$title = "")
    {       
        $html = "<img alt=\"$alt\" title=\"$title\" style=\"vertical-align:middle\" src=\"".FN_FromTheme("images/down.png")."\" />";
        return $html;
    }

}

if (!function_exists("FN_HtmlLanguages"))
{
    /**
     *
     * @global array $_FN
     * @param string $sep
     * @return string 
     */
    function FN_HtmlLanguages($sep = "&nbsp;")
    {
        global $_FN;
        $langs = array();
        foreach ($_FN['listlanguages'] as $lang)
        {
            $link = FN_RewriteLink("index.php?lang=$lang&amp;mod={$_FN['mod']}");
            $icon = FN_FromTheme("images/flags/$lang.png");
            $langtitle = FN_GetFolderTitle("languages/$lang",$lang);
            $langs[] = "<a title=\"$langtitle\" href=\"$link\"><img alt=\"$lang\" style=\"border:0px;\" src=\"$icon\"/></a>";
        }
        if (count($langs) > 1)
            return implode($sep,$langs);
    }
}
?>
