<?php
if (!defined('_DESIGNER_FUNCTIONS'))
  require_once dirname(__FILE__) . str_replace('/', DIRECTORY_SEPARATOR, '/../functions.php');

function modChrome_drstyle($module, &$params, &$attribs)
{
    $document = JFactory::getDocument();
    if (false !== strpos($module->module, 'virtuemart')) {
        $document->view->combineVMScripts($document->_scripts, true);
        $document->view->combineVMInlineScript($document->_script, $module->module);
    } else {
        if (isset($document->_script['text/javascript']))
            $document->view->prevModuleScript = $document->_script['text/javascript'];
    }

    if (isset($attribs['items'])) {
        foreach($attribs['items'] as $key => $item) {
            $item = funcContentRoutesCorrector($item);
            if ($attribs['id'] == $key) {
                $module->content = $item;
            } else {
                $document->setBuffer($item, array('type' => 'modules', 'name' => $key));
            }
        }
    }
    $module->content = funcContentRoutesCorrector($module->content);

    if ('' === $attribs['drstyle']) {
        echo $module->content;
        return;
    }

    $parts = explode('%', $attribs['drstyle']);
    $style = $parts[0];

    if (!isset($attribs['funcStyling']))
        $attribs['funcStyling'] = count($parts) > 1 ? $parts[1] : '';

    $styles = array(
      'block' => 'modChrome_block',
      'nostyle' => 'modChrome_nostyle'
    );

    $classes = explode(' ', rtrim($params->get('moduleclass_sfx')));
    $keys = array_keys($styles);
    $dr = array();
    foreach ($classes as $key => $class) {
      if (in_array($class, $keys)) {
        $dr[] = $class;
        $classes[$key] = ' ';
      }
    }
    $classes = str_replace('  ', ' ', rtrim(implode(' ', $classes)));
    $style = count($dr) ? array_pop($dr) : $style;
    $params->set('moduleclass_sfx', $classes);
    call_user_func($styles[$style], $module, $params, $attribs);
}

function modChrome_block($module, &$params, &$attribs)
{
    if (!empty ($module->content)) {
        $args = array($module->showtitle != 0 ? $module->title : '',
            $module->content,
            $params->get('moduleclass_sfx'),
            $module->id);
        if ('' !== $attribs['funcStyling'])
            echo renderTemplateFromIncludes($attribs['funcStyling'], $args);
        else
            echo $module->content;
    }
}

function modChrome_nostyle($module, &$params, &$attribs)
{
if (!empty ($module->content)) : ?>
<div class="<?php echo $params->get('moduleclass_sfx'); ?>">
<?php if ($module->showtitle != 0) : ?>
    <h3><?php echo $module->title; ?></h3>
<?php endif; ?>
<?php echo $module->content; ?>
</div>
<?php endif;
}
?>