<?php

class MBoxTitle extends MDiv
{
    protected $icon;
    protected $close;
    protected $help;
    protected $minimize;
    protected $move;
    protected $boxId;

    public function __construct( $cssClass, $text, $close = '', $icon = '', $help = NULL, $showHelp=true, $showMinimize=true, $showClose=true, $boxId = '' )
    {
        parent::__construct( '' , '', $cssClass );
        $this->boxId = $boxId;
        $this->help  = $showHelp ? $help : '';
        $this->minimize = $showMinimize;
        $this->move = MUtil::getBooleanValue( $this->manager->getConf('theme.options.move') );
        $this->close = $showClose ? $close : '';
        $this->icon = $icon;
        $this->text = $text;
    }

    /**
     *
     */
    public function setIcon( $icon )
    {
        $this->icon = $icon;
    }

    /**
     *
     */
    public function setClose( $action )
    {
        $this->close = $action;
    }

    /**
     *
     */
    public function setHelp( $help, $module=null, $action=null )
    {
        $this->help = $help;
    }

    /**
     *
     */
    public function generateInner()
    {
        $this->page->onload("miolo.box.setBoxPositions();");
        $title = $this->text;

        if ( $this->icon == '' )
        {
            $title = '&nbsp;&nbsp;' . $title;
            $icon  = NULL;
        }
        else
        {
            $icon = new MImage('', '', $this->icon );
            $icon->setClass('mBoxTitleIcon');
        }
        $caption = new MSpan( '', $title, 'mBoxTitleCaption' );
        $help = $close = $minimize = '';


        if ( $this->help != '' &&
             MUtil::getBooleanValue( $this->manager->getConf('theme.options.help') ) )
        {
                $help = new MButtonHelp( $this->help );
                $help->setClass('mBoxTitleButton');
        }

        $this->minimize = $this->minimize && MUtil::getBooleanValue( $this->manager->getConf('theme.options.minimize'));

        if ( $this->close != '' &&
             MUtil::getBooleanValue( $this->manager->getConf('theme.options.close') ) )
        {
            if ( strpos( $this->close, 'javascript') === false)
            {
                if ($this->minimize )
                {
                    $minimize = new MButtonMinimize( '' );
                    $minimize->onMouseUp($this->boxId);
                    $minimize->setClass('mBoxTitleButton');
                }
            }
            $close = new MButtonClose( $this->close );
            $close->setClass('mBoxTitleButton');

        }
        elseif ($this->minimize )
        {
            $minimize = new MButtonMinimize( '' );
            $minimize->onMouseUp($this->boxId);
            $minimize->setClass('mBoxTitleButton');
        }

        $spacer = new MSpacer();

        if ( $this->getClass() == '' )
        {
            $this->setClass( 'mBoxTitle' );
            if ( $this->move )
            {
                $this->addAttribute('title', _M('Double click to hide') . " | " . _M('Click and drag to move') . " | " . _M('Right click to minimize') );
                $this->addAttribute('onMouseDown', 'return miolo.box.moveBox(event, this.parentNode.parentNode, true )');
                $this->addAttribute('onMouseUp'  , 'miolo.box.moveBox(event, this.parentNode.parentNode, false)');
                $this->addAttribute('onDblClick' , 'miolo.box.hideBoxContent(this.parentNode.parentNode)');
            }
        }

        $this->inner = array( $icon, $caption, $close, $minimize, $help );
    }
}

?>