<?php
# Waiting for php 5.3 anonymous functions...
if (!function_exists('a'))
{
    function a(array $attribute)
    {
        $all = array();
        
        foreach ($attribute as $k => $v)
        {
            $all[] = $k . '="' . htmlspecialchars($v) . '"';
        }
        
        return ' ' . implode(' ', $all);
    }
}

if (!function_exists('theme'))
{
    function theme()
    {
        $css = array();
        $icon = '';
        
        foreach (setting::get_list('theme') as $s)
        {
            if ($s['name'] === 'favicon' && $s['value'])
            {
                $icon .= '<link rel="shortcut icon" href="' . htmlspecialchars(favicon::DIRECTORY . $s['value']) . '.ico" />';
                continue;
            }
            
            if ($s['name'] === 'header' && $s['value'])
            {
                $s['value'] = 'url(' . htmlspecialchars(header::DIRECTORY . $s['value']) . '.jpg) top left no-repeat';
            }
            
            if ($s['name'] === 'background' && $s['value'])
            {
                $s['value'] = 'url(' . htmlspecialchars(background::DIRECTORY . $s['value']) . '.jpg) top center no-repeat';
            }
            
            list ($selector, $attribute) = explode('|', $s['extra']);
            
            if ($s['value'])
            {
                $css[$selector][$attribute] = htmlspecialchars($s['value']);
            }
        }
        
        $content = '<style type="text/css">';
        
        foreach ($css as $selector => $property)
        {
            $content .= $selector . ' { ';
            
            foreach ($property as $k => $v)
            {
                $content .= $k . ':' . $v . '; ';
            }
            
            $content .= '}' . "\r\n";
        }
        
        $content .= '</style>';
        return $content . $icon;
    }
}

if (!function_exists('meta'))
{
    function meta($type, $type_id)
    {
        $info = meta::get_info($type, $type_id);
        $title = false;
        $description = false;
        $meta = '';
        
        if ($info)
        {
            foreach ($info as $k => $v)
            {
                if ($k === 'id' || $k === 'type' || $k === 'type_id')
                {
                    continue;
                }
                
                if (($k === 'title' || $k === 'description') && $v)
                {
                    $$k = true;
                }
                
                if ($v)
                {
                    if ($k === 'title')
                    {
                        $meta .= '<title>' . htmlspecialchars($v) . '</title>';
                    }
                    else
                    {
                        $meta .= '<meta name="' . htmlspecialchars(str_replace('_', '-', $k)) . '" content="' . htmlspecialchars($v) . '" />';
                    }
                }
            }
        }
        
        if (!$title)
        {
            if ($title = setting::get('default-title', 'site'))
            {
                $meta .= '<title>' . htmlspecialchars($title['value']) . '</title>';
            }
        }
        
        if (!$description)
        {
            if ($description = setting::get('default-description', 'site'))
            {
                $meta .= '<meta name="description" content="' . htmlspecialchars($description['value']) . '" />';
            }
        }
        
        return $meta;
    }
}
#
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html<?php if (isset($html)) : echo a($html); endif; ?>>
    <head<?php if (isset($head)) : echo a($head); endif; ?>>
        <?php if (isset($base)) : ?><base<?php echo a($base); ?> /><?php endif; ?>
        <?php echo meta($type, $type_id); ?>
        <?php if (isset($meta)) : foreach ($meta as $m) : ?><meta<?php echo a($m); ?> /><?php endforeach; endif; ?>
        <?php if (isset($link)) : foreach ($link as $l) : ?><link<?php echo a($l); ?> /><?php endforeach; endif; ?>
        <?php echo theme(); ?>
        <?php if (isset($script)) : foreach ($script as $s) : ?><script<?php echo a($s); ?>></script><?php endforeach; endif; ?>
        <script type="text/javascript" src="core/widget/jquery.js"></script>
    </head>
    <body<?php if (isset($body)) : echo a($body); endif; ?>>
        <?php echo $content; ?>
    </body>
</html>
