var $selectedItem;
var selectItem = function(item)
{
    $selectedItem = $(item).first();
    var modal  = $('#ajaxModal');
    var params = v.params + 'customers=' + $('input[type=hidden][name*=customers]').val() + ',' + $selectedItem.data('key')
    var link   = createLink(v.module, v.method, params);
    modal.load(link, function()
    {
        $('#customers.chosen').chosen();
        $(this).find('.modal-dialog').css('width', $(this).data('width'));
        $.zui.ajustModalPosition()
    })
};

$(document).ready(function()
{
    var showSearchModal = function()
    {
        var key       = $('#customers_chosen .chosen-results > li.no-results > span').text();
        var customers = $('[name*=customer]').val().join(',').replace(',ajax_search_more', '');
        var link      = createLink('customer', 'ajaxSearchCustomer', 'key=' + key + '&relation=&customers=' + customers);
        $('#ajaxModal').load(link);
    };

    $(document).on('change', '#customers', function()
    {
        if($.inArray('ajax_search_more', $(this).val()) > -1)
        {
             showSearchModal();
        }
    });

    $(document).on('click', '#customers_chosen .chosen-results > li.no-results', showSearchModal);
})
