jQuery(function ($) {
    $('#acc-srch > p').click(function () {
        $('#acc-srch').hide();
        
        $('#kvAC-items').hide();
    });
    
    $('#rlt-srch > p').click(function () {
        $('#rlt-srch').hide();
        
        $('#kvAC-items').hide();
    });
    
    //
    
    $('#type_hlp').change(function () {
        $('#acc_type').val($(this).val());
    });
    
    //
    
    $('#city_hlp').kvAC({
        ajax_path: web_root + 'scripts/load_cities.xml',
        onSelect: function () {
            $('#acc_city').val($('#city_hlp').val());
        }
    });
    
    $('#city2_hlp').kvAC({
        ajax_path: web_root + 'scripts/load_cities.xml',
        onSelect: function () {
            $('#rlt_city').val($('#city2_hlp').val());
        }
    });
});

//

function SrchAreaChange()
{
    if (jQuery('#sarea').val() == 'accommodation')
    {
        jQuery('#rlt-srch').hide();
    
        ShowAccSrch();
        
        jQuery('#kvAC-items').html('').show();
    }//if (jQuery('#sarea').val() == 'accommodation')
    else if (jQuery('#sarea').val() == 'realtys')
    {
        jQuery('#acc-srch').hide();
    
        ShowRltSrch();
        
        jQuery('#kvAC-items').html('').show();
    }//else if ...
    else
    {
        jQuery('#acc-srch').hide();
        jQuery('#rlt-srch').hide();
        
        jQuery('#kvAC-items').hide();
    }//else
}//SrchAreaChange

function ShowAccSrch()
{
    //var jq = jQuery;

    var sfpos = GetAbsPos(jQuery('#sf-cont').get(0));
    
    jQuery('#acc-srch').css({
        'top': sfpos.Top + 'px',
        'left': sfpos.Left - 202 + 'px'
    }).show();

    //
    
    jQuery('#acc_type').val(jQuery('#type_hlp').val());

    //
    
    //jQuery('#city_hlp').kvAC.ResetKvAcPos();
    
    var inputpos = GetAbsPos(jQuery('#city_hlp').get(0));
    
    var bb       = parseInt(jQuery('#city_hlp').css('borderBottomWidth'),10);
    
    var top      = inputpos.Top + jQuery('#city_hlp').outerHeight() - bb;
    
    jQuery('#kvAC-items').css({
        'top': top + 'px',
        'left': inputpos.Left + 'px'
    });
}//ShowAccSrch

function ShowRltSrch()
{
    //var jq = jQuery;

    var sfpos = GetAbsPos(jQuery('#sf-cont').get(0));
    
    jQuery('#rlt-srch').css({
        'top': sfpos.Top + 'px',
        'left': sfpos.Left - 202 + 'px'
    }).show();

    //
    
    //jQuery('#city2_hlp').kvAC.ResetKvAcPos();
    
    var inputpos = GetAbsPos(jQuery('#city2_hlp').get(0));
    
    var bb       = parseInt(jQuery('#city2_hlp').css('borderBottomWidth'),10);
    
    var top      = inputpos.Top + jQuery('#city2_hlp').outerHeight() - bb;
    
    jQuery('#kvAC-items').css({
        'top': top + 'px',
        'left': inputpos.Left + 'px'
    });
}//ShowRltSrch

//

function GetAbsPos(elem)
{
    var end  = false;
    var prnt = elem;
    var x    = 0;
    var y    = 0;
    
    while (!end)
    {
        if (prnt != null)
        {
            if (prnt.offsetLeft)
            {
                x += prnt.offsetLeft;
            }
            
            if (prnt.offsetTop)
            {
                y  += prnt.offsetTop;
            }
        
            prnt = prnt.offsetParent;
        }
        else
        {
            break;
        }
    }//while
    
    return {Left: x, Top: y};
}//GetAbsPos

