<?php

/**
 *
 * @copyright  2010-2015 izend.org
 * @version    5
 * @link       http://www.izend.org
 */
?>
<?php if (!empty($search_url)): ?>
<form id="searchinput" action="<?php echo $search_url; ?>" method="post">
<input id="searchtext" name="searchtext" type="text" value="<?php echo htmlspecialchars($search_text, ENT_COMPAT, 'UTF-8'); ?>" />
<input id="search" name="search" type="submit" value="Chercher" title="Chercher" />
</form>
<?php if (!empty($suggest_url)): ?>
<?php head('javascript', 'jquery-ui'); ?>
<?php head('stylesheet', 'jquery-ui', 'screen'); ?>
<script type="text/javascript">
//<![CDATA[
$('input#searchtext')
.bind('keydown', function(event) {
    if (event.keyCode === $.ui.keyCode.TAB && $(this).data('autocomplete').menu.active) {
        event.preventDefault();
    }
})
.autocomplete({
    appendTo: '#searchinput',
    minLength: 3,
    source: function(request, response) {
        var pos = getcaretposition(this.element.get(0));
        var term = getwordat(request.term, pos);

        if (!term || term.length < this.options.minLength) {
            this.close();
        }
        else {
            $.getJSON('<?php echo $suggest_url; ?>', {term: term}, response);
        }
    },
    select: function(event, ui) {
        var pos = getcaretposition(this);
		var s = this.value;

        for (var i = pos-1; i >= 0 && s[i] != ' '; i--)
            ;
        var beg = i < 0 ? 0 : i+1;

        for (var i = pos; i < s.length && s[i] != ' '; i++)
             ;
        var end = i >= s.length ? s.length : i;

        this.value = s.substring(0, beg) + ui.item.value + s.substring(end);

        setcaretposition(this, beg + ui.item.value.length);

        return false;
    },
    focus: function() {
        return false;
    }
});
//]]>
</script>
<?php endif; ?>
<?php endif; ?>
