$(document).ready(
    function()
    {
        initCustomSelectBoxes();
        $(document).click(closeAllOpendCustomSelectBoxes);
    });

function initCustomSelectBoxes(){
    $('.custom_select').each(function(){
        this['doExpand'] = true;
    });

    $('.custom_select').click(function(){
        customSelectBoxClick(this);
        return false;
    });

    $('.custom_select_expand_btn').click(function(){
        customSelectBoxClick(this.parentNode);
        return false;
    });

    $('.custom_select_expand_all').click(function(){
        //TODO:
    });
    
    $('.custom_select_expand_all').mousedown(function(){$(this).attr('src', "/static/css/tour_search/field_small_all_btn_active.png");});
    $('.custom_select_expand_all').mouseup(function(){$(this).attr('src', "/static/css/tour_search/field_small_all_btn_passive.png");});

    $('.custom_select_expand_btn').mousedown(function(){
        $(this).removeClass();
        $(this).addClass('custom_select_expand_btn_clicked');
    });
    $('.custom_select_expand_btn').mouseup(function(){
        $(this).removeClass();
        $(this).addClass('custom_select_expand_btn');
    });

    initListClickEvent();
    
    $('.custom_select').each(function(){
        if ($(this).find('a').eq(0).text() == "")
            $(this).find('a').eq(0).text($(this).find('ul').eq(0).find('li').eq(0).text());
    });
}

function initListClickEvent(){
    $('.custom_select li').click(function(){
        $('#'+this.parentNode.parentNode.parentNode.id+'_select_box_val').removeClass('currentValue');
       
        $('#'+this.parentNode.parentNode.parentNode.id+'_select_box_val').text(this.innerHTML);
        $('#'+this.parentNode.parentNode.parentNode.id+'_select_box_val').text(this.innerHTML);
        if (this.value != -1)
            $('#'+this.parentNode.parentNode.parentNode.id+'_id_val').val(this.value);
        else
            $('#'+this.parentNode.parentNode.parentNode.id+'_id_val').val('');
    });
}

function closeAllOpendCustomSelectBoxes(){
    $('.custom_select').each(function(){
            if (this['doExpand']==false){
                $('#'+this.id+'_select_box_val').removeClass('currentValue');
                $('#'+this.id+'_select_box_ulc').removeClass();
                $('#'+this.id+'_select_box_ulc').addClass('custom_select_uld');
                this['doExpand'] = true;
            };
        });
}

function customSelectBoxClick(container){
    //save current select state, ither it'll be closed
    var tBoolean = container['doExpand'];
    closeAllOpendCustomSelectBoxes();
    //restore current select state
    container['doExpand'] = tBoolean;

    if (container['doExpand']){
        $('#'+container.id+'_select_box_val').addClass('currentValue');
        $('#'+container.id+'_select_box_ulc').removeClass('custom_select_uld');
        $('#'+container.id+'_select_box_ulc').addClass('custom_select_ule');
        container['doExpand'] = false;
    }
    else{
        $('#'+container.id+'_select_box_val').removeClass('currentValue');
        $('#'+container.id+'_select_box_ulc').removeClass('custom_select_ule');
        $('#'+container.id+'_select_box_ulc').addClass('custom_select_uld');
        container['doExpand'] = true;
    }
}