<?php

/**
 *
 * Programme de demarrage
 *
 */

/*
 * Création du menu en coordination avec la feuille de style style.css
 *
 *
 *
 */
class Pmenu
{

    protected $tab_menu;

    protected $div;

    protected $name;

    protected $xslheader;

    protected $xslbody;

    protected $xslfooter;

    protected $record;

    protected $height;

    protected $width;

    /**
     *
     * @param : $div
     *            le div cible à accrocher
     * @param : $name
     *            nom
     */
    function __construct($div, $name = 'menu')
    {
        $this->div = $div;
        $this->setXslHeader($name);
        $this->setXslFooter();
        $this->height = '50';
        $this->width = '70';
    }

    /**
     * ajout d'option menu
     *
     * @param string $name
     *            : nom de l'option
     * @param string $label
     *            : label de l'option menu affiché
     * @param string $id
     *            : id <A>
     * @param string $class
     *            : class <A>
     * @param string $title
     *            : title <A>
     * @param string $onclick
     *            : fonction appelée lors du click souris
     * @param string $ondbclick
     *            : fonction appelée lors du double click souris
     * @param string $onkeypress
     *            : fonction appelée lorsque touche appuyée souris
     */
    public function addOption($name, $label, $id, $class, $title, $onclick, $ondbclick = '', $onkeypress = '')
    {
        $this->tab_menu[] = array(
            'name' => $name,
            'label' => $label,
            'id' => $id,
            'class' => $class,
            'title' => $title,
            'onclick' => $onclick,
            'ondbclick' => $ondbclick,
            'onkeypress' => $onkeypress
        );
    }

    public function getOption($id)
    {}

    /*
     * retourne le xsl a transformer en HTML pour affichage
     */
    public function getXsl()
    {
        unset($this->xslbody);
        $this->setXslBody();
        return $this->xslheader . $this->xslbody . $this->xslfooter;
    }

    public function affiche()
    {
        if (! isset($this->div))
            return false;
        
        $ajax = "document.getElementById('{$this->div}').innerHTML=\"" . HttpReplace($this->getXsl()) . '";';
        $ajax .= "document.getElementById('{$this->div}').height={$this->height};";
        $ajax .= "document.getElementById('{$this->div}').width={$this->width};";
        $ajax .= $this->initTimer();
        $ajax .= $this->clearTimer();
        $ajax .= "document.getElementById('{$this->div}').onmouseout 	= 	function(){menu_script.initTimer();};";
        $ajax .= "document.getElementById('{$this->div}').onmouseover 	= 	function(){menu_script.clearTimer();};";
        
        print($ajax);
        return true;
    }

    /*
     * construction de la chaine de début de template xsl
     * $name passé en paramètre : nom du menu et du template
     */
    private function setXslHeader($name)
    {
        $this->xslheader = // "<xsl:template name='{$name}'>".
"<html xmlns:php='http://php.net/xsl'>" . "<body>" . "<form name='{$name}' method='post' action='' enctype='x-www-form-urlencoded'>" . "<dl>" . "<dt>" . "<dd>" . "<ul onmouseover=\"function(){menu_script.clearTimer();};\" onmouseout=\"function(){menu_script.initTimer();};\">";
    }

    /*
     * contruction de la chaine de fin de template xsl
     */
    private function setXslFooter()
    {
        $this->xslfooter = "</ul>" . "</dd>" . "</dt>" . "</dl>" . "</form>" . "</body>" . "</html>";
        // "</xsl:template>";
    }

    private function setXslBody()
    {
        foreach ($this->tab_menu as & $key) {
            $this->xslbody .= "<li id=\"{$key['id']}\" class=\"{$key['class']}\" title=\"{$key['title']}\" onmouseover=\"function(){menu_script.clearTimer();};\" onmouseout=\"function(){menu_script.initTimer();};\" >" . "<a href=\"javascript://\" ";
            if (isset($key['onclick']) && $key['onclick'] != '') {
                $this->xslbody .= " onclick=\"{$key['onclick']};\" ";
            }
            if (isset($key['ondbclick']) && $key['ondbclick'] != '') {
                $this->xslbody .= " ondbclick=\"{$key['ondbclick']};\" ";
            }
            if (isset($key['ondbclick']) && $key['ondbclick'] != '') {
                $this->xslbody .= " onkeypress=\"{$key['onkeypress']};\" ";
            }
            $this->xslbody .= " >" . $key['label'] . '</a>' . '</li>';
        }
    }

    private function initTimer()
    {
        return " menu_script.initTimer = function (){" . "	if(document.menuTimer)" . "		{" . "		menu_script.clearTimer(document.menuTimer);" . "		}" . "	document.menuTimer=setTimeout(\"closePopup('{$this->div}')\", 500);};";
    }

    private function clearTimer()
    {
        return 'menu_script.clearTimer = function (){clearTimeout(document.menuTimer);};';
    }

    public function setHeight($h)
    {
        $this->height = $h;
    }

    public function getHeight()
    {
        return $this->height;
    }

    public function setWidth($w)
    {
        $this->width = $w;
    }

    public function getWidth()
    {
        return $this->width;
    }
}
