<?php
goweb::loadClass('Zend_View_Helper_FormElement');
/**
 * Goweb Sitemanager 5 Framework
 *
 * @version    5.1.2
 * @category   Goweb
 * @package    Zend_Form_Element
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 */

/**
 *
 * @version    5.1.2
 * @category   Goweb
 * @package    Zend_View_Helper_FormFancySelect
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 */
class Zend_View_Helper_FormFancySelect extends Zend_View_Helper_FormElement {
    public function formFancySelect($name, $value = null, $attribs = null, $options = null)
    {
        Goweb::loadJavascript('/admin/media/js/prototype.js');
        Goweb::loadJavascript('/admin/media/js/scriptaculous/scriptaculous.js');
        Goweb::loadJavascript('/admin/media/js/scriptaculous/effects.js');
        Goweb::loadJavascript('/media/js/prototype/goweb/combobox.js');

        $info = $this->_getInfo($name, $value, $attribs, $options);
        extract($info); // name, id, value, attribs, options, listsep, disable

        $xhtml = '<div id="goweb_combo_' . $this->view->escape($id) . '" class="goweb_combobox">' . "\n";
        $xhtml .= '<input type="hidden"'
        . ' name="' . $this->view->escape($name) . '"'
        . ' id="' . $this->view->escape($id) . '"'
        . ' value="' . $this->view->escape($value) . '"'
        . $this->_htmlAttribs($attribs)
        . $this->getClosingBracket() . "\n    ";
        
        $xhtml .= '<div class="goweb_combobox_selected">' . "\n";
        if (isset($options[$value])){
            $xhtml .= $this->view->escape($options[$value]) . "\n";
        } else {
            $xhtml .= $this->view->escape($value) . "\n";
        }
        $xhtml .= '</div>' . "\n";
        $xhtml .= '<div class="goweb_combobox_button">&nbsp;</div>' . "\n";
        $xhtml .= '<div class="goweb_combobox_elements" style="display: none;">' . "\n";
        $xhtml .= '<div class="goweb_combobox_scroll">' . "\n";
        $xhtml .= '<ul>' . "\n";
        $onClick = '';
        if (isset($attribs['onchange'])){
            $onClick = $attribs['onchange'] . '; ';
        }
        foreach ((array) $options as $opt_value => $opt_label) {
            $xhtml .= '<li title="' . $this->view->escape($opt_label) . '"';
            // selected?
            if ($opt_value == $value) {
                $xhtml .= ' class="selected"';
            }
            $xhtml .= ' onclick="$(\'' . $this->view->escape($name) . '\').value=\'' . $this->view->escape($opt_value) . '\'; ' . $onClick . '">' . $this->view->escape($opt_label) . "</li>\n";
        }

        $xhtml .= '</ul>' . "\n";
        $xhtml .= '</div>' . "\n";
        $xhtml .= '<div class="controls"><div class ="button_less">&nbsp;</div><div class="track"><div class="handle">&nbsp;</div></div><div class="button_more">&nbsp;</div></div>' . "\n";
        $xhtml .= '</div>' . "\n";
        
        $xhtml .= '</div>' . "\n";
        $xhtml .= '<script type="text/javascript" defer="defer">
    //<![CDATA[
    new Goweb.ComboBox(\'goweb_combo_' . $this->view->escape($id) . '\');
    //]]>
</script>' . "\n    ";
        return $xhtml;
    }
}