$(document).ready(function()
{
    /* Custom combobox */

    $(".custom-combobox-dropspot").click(
        function (e) {
            initCombobox(this);

            var control = $(this).parent();         
            $(control).find("ul.combobox-value-all").toggle();
            $(control).find("ul.combobox-value-result").hide();
            
            showHideComboboxOverlay(control);
            e.stopPropagation();
        }
    );
    
    $(".custom-combobox-input").attr("autocomplete", "off");
    $(".custom-combobox-input").click(
        function () {
            var $this   = $(this);
            var control = $this.parent();
            
            if ($(control).find('input.custom-combobox-value').val()){
                $(control).find('input.custom-combobox-input').select();
            }
        }
    ).blur(
        function () {
            var _this = $(this);
            
            var firstValue     = _this.parent().find('li:first a').attr('rel');
            var firstValueText = _this.parent().find('li:first a').text();
            var controlValue   = _this.parent().find("input.custom-combobox-value").val();
            
            if (!controlValue) {
                _this.parent().find("input.custom-combobox-value").val(firstValue);
                _this.parent().find("input.custom-combobox-input").val(firstValueText);
            }
        }
    );
    
    
    $(".custom-combobox ul a").click(
        function() { return comboboxClickHandler($(this)); }
    );

    /* Custom combobox */
});

var c_dependent_elements = new Array();

function showHideComboboxOverlay(root, action)
{
    var $root = $(root);
    var key   = $root.attr('id');
    
    if (!action) {
        if ($root.find("div.combobox-overlay").css('display') == 'none') {
            action = 'show';    
        } else {
            action = 'hide';
        }
    }

    if (action == 'show') {
        if (c_dependent_elements && key && c_dependent_elements[key]) {
            for (var el_id in c_dependent_elements[key]) {
                $(_(el_id)).css('visibility', 'hidden');
            }
        }
        
        $root.find("div").show();
              
    } else if (action == 'hide') {
        if (c_dependent_elements && key && c_dependent_elements[key]) {
            for (var el_id in c_dependent_elements[key]) {
                $(_(el_id)).css('visibility', '')
            }
        }
        $root.find("div").hide();
    }
}

function initCombobox(initiator)
{
    var $initiator = $(initiator);
    
    if (!$initiator.attr('rel') || !window[$initiator.attr('rel')]) {
        return false;
    }
    
    var $ul_all_values = $initiator.parent().find('ul.combobox-value-all');
    var $li_item       = $ul_all_values.find('li:first');
    
    if (!$ul_all_values || !$li_item) {
        return false;
    }
    
    var li_item_id  = $li_item.attr('id');
    var init_values = window[$initiator.attr('rel')];
    
    for (var id in init_values) {
        var $li_item = $li_item.clone();
        var $a_item  = $li_item.find('a');
         
        $li_item.attr('id', li_item_id + '-' + id);
        $a_item.attr({'title' : init_values[id],
                      'rel'   : id});
        $a_item.html(init_values[id]);
        $a_item.click(function() { return comboboxClickHandler($(this)); });
        $li_item.appendTo($ul_all_values);   
    }
        
    $initiator.removeAttr('rel');
}

function comboboxKeyUpHandler(_this, combobox_values, id_chunk)
{
    if (!_this || !combobox_values) {
        throw new Exception('Bad arguments');   
    }
    
    if (!id_chunk) {
        if (_this.attr('id')) {
            id_chunk = _this.attr('id');
        } else {
            throw new Exception('Bad arguments: unknown id_chunk');
        }
    }
    
    initCombobox(_this.parent().find('a.custom-combobox-dropspot'));
            
    var string = _this.val();
    
    var $value_all    = _this.parent().find("ul.combobox-value-all");
    var $value_result = _this.parent().find("ul.combobox-value-result");
    
    _this.parent().find("input.custom-combobox-value").val('');

    if (string.length >= 2) {
        $value_result.children().remove();
        $value_all.hide();
        $value_result.show();

        for (var id in combobox_values) {
            if(combobox_values[id].toLowerCase().indexOf((string).toLowerCase()) >= 0) {
                var $new_item = $("#combobox-" + id_chunk + "-" + id).clone();
                $new_item.removeAttr('id');
                $new_item.find('a').click(function() { return comboboxClickHandler($(this)); });
                $new_item.appendTo($value_result);
            }
        }
    } else {
        $value_all.show();
        $value_result.hide();
    }
    
    showHideComboboxOverlay(_this.parent(), 'show');
    
    return true;
}

function comboboxClickHandler(_this)
{
    var control  = _this.parent().parent().parent().parent();
    var $control = $(control);                                      
    
    showHideComboboxOverlay(control, 'hide');
    $control.find("input.custom-combobox-value").val(_this.attr('rel'));
    $control.find("input.custom-combobox-input").val(_this.text());
                                            
    return false;
}
