<form id="oc-search" class="navbar-form" role="search">
	<div class="input-group">
		<div class="input-group-btn">
			<a class="btn btn-default dropdown-toggle" data-toggle="dropdown" href="#"><i class="fa fa-search"></i><span class="caret"></span></a>
			<ul class="dropdown-menu dropdown-menu-left alerts-dropdown">
				<li class="dropdown-header">{{ text_search_options }}</li>
				<li><a onclick="setOption('catalog', '{{ text_catalog_placeholder }}'); return false;"><i class="fa fa-book"></i><span>{{ text_catalog }}</span></a></li>
				<li><a onclick="setOption('customers', '{{ text_customers_placeholder }}'); return false;"><i class="fa fa-group"></i><span>{{ text_customers }}</span></a></li>
				<li><a onclick="setOption('orders', '{{ text_orders_placeholder }}'); return false;"><i class="fa fa-credit-card"></i><span>{{ text_orders }}</span></a></li>
			</ul>
		</div>
		<input id="oc-search-input" type="text" class="form-control" placeholder="{{ text_search_placeholder }}" name="query" autocomplete="off">
		<input id="oc-search-option" type="hidden" name="search-option" value="catalog">
		<div id="loader-search"><img src="view/image/loader-search.gif" alt=""></div>
	</div>
</form>
<div id="oc-search-result"></div>
<script>
    function setOption(option, text) {
        $('#oc-search-option').val(option);
        $('#oc-search-input').attr('placeholder', text);
    }

    $('#oc-search-input').keyup(function(){
        var option = $('#oc-search-option').val();
        var length = 2;

        if(option == 'orders') {
            length = 1;
        }

        if(this.value.length < length) {
            return false;
        }

        if($.support.leadingWhitespace == false) {
              return false;
        }

        $('#loader-search').css('display', 'block');

        $.ajax({
            type: 'get',
            url: 'index.php?route=search/search/search' + '&user_token={{ user_token }}',		
			data: $('#oc-search').serialize(),
            dataType: 'json',
            success:function(json){
                $('#oc-search-result').css('display', 'block');
                $('#loader-search').css('display', 'none');

                if(json['error']) {
                    $('#oc-search-result').html(json['error'])
                    return;
                }

                $('#oc-search-result').html(json['result'])
            }
        });
    });

    $(document).mouseup(function (e) {
        var container = $('#oc-search-result');

        if (!container.is(e.target) && container.has(e.target).length === 0) {
            container.hide();
        }
    });

    $('#oc-search').submit(function(e) {
        e.preventDefault();
    });
</script>