//****************************************************************************
// Copyright (c) 2005, Coveo Solutions Inc.
//****************************************************************************

var CES_QUICKSEARCHBOX_DELAY = 300;
var CES_QUICKSEARCHBOX_HIDE_DELAY = 100;

//****************************************************************************
/// <summary>
/// Signals that a key have been pressed in the quick search box.
/// </summary>
/// <param name="p_Key">The key that was pressed.</param>
/// <param name="p_Query">The query textbox object.</param>
/// <param name="p_Results">The iframe object that displays the results (if enabled).</param>
/// <returns>Whether the event should be further processed.</returns>
//****************************************************************************
function CES_QuickSearchBox_KeyDown(p_Key, p_Query, p_Results)
{
    var res = true;
    if (p_Key == 13) {
        CES_QuickSearchBox_SubmitSearch(p_Query);
        if (p_Results != null) {
            CES_QuickSearchBox_UpdateResults(p_Query, p_Results);
        }
        res = false;
    } else if (p_Results != null) {
        // Schedule the quick results for update
        clearTimeout(p_Results.m_Timer);
        p_Results.m_Timer = setTimeout('CES_QuickSearchBox_UpdateResults(document.getElementById(\'' + p_Query.id + '\'), document.getElementById(\'' + p_Results.id + '\'))', CES_QUICKSEARCHBOX_DELAY);
    }    

    return res;
}

//****************************************************************************
/// <summary>
/// Signals that the query textbox has gain focus.
/// </summary>
/// <param name="p_Query">The query textbox object.</param>
/// <param name="p_Results">The iframe object that displays the results (if enabled).</param>
//****************************************************************************
function CES_QuickSearchBox_GainFocus(p_Query, p_Results)
{
    if (p_Results != null) {
        clearTimeout(p_Results.m_Timer);
        p_Results.m_Timer = setTimeout('CES_QuickSearchBox_UpdateResults(document.getElementById(\'' + p_Query.id + '\'), document.getElementById(\'' + p_Results.id + '\'))', CES_QUICKSEARCHBOX_DELAY);
    }
}

//****************************************************************************
/// <summary>
/// Signals that the query textbox has lost focus.
/// </summary>
/// <param name="p_Query">The query textbox object.</param>
/// <param name="p_Results">The iframe object that displays the results (if enabled).</param>
//****************************************************************************
function CES_QuickSearchBox_LostFocus(p_Query, p_Results)
{
    if (p_Results != null) {
        clearTimeout(p_Results.m_Timer);
        p_Results.m_Timer = setTimeout('CES_QuickSearchBox_HideResults(document.getElementById(\'' + p_Query.id + '\'), document.getElementById(\'' + p_Results.id + '\'))', CES_QUICKSEARCHBOX_HIDE_DELAY);
    }
}

//****************************************************************************
/// <summary>
/// Submits the search to the search page.
/// </summary>
/// <param name="p_Key">The key that was pressed.</param>
/// <param name="p_Query">The query textbox object.</param>
//****************************************************************************
function CES_QuickSearchBox_SubmitSearch(p_Query)
{
    p_Query.m_Submitting = true;
    window.location = CES_QuickSearchBox_ComputeUri(p_Query);
}

//****************************************************************************
/// <summary>
/// Computes the uri of the search page.
/// </summary>
/// <param name="p_Query">The query textbox object.</param>
/// <returns>The computed uri.</returns>
//****************************************************************************
function CES_QuickSearchBox_ComputeUri(p_Query)
{
    var uri = p_Query.m_Target;
    uri += '?' + p_Query.m_QueryParameter + '=' + encodeURIComponent(p_Query.value);
    uri += '&' + p_Query.m_TimeZoneOffsetParameter + '=' + -new Date().getTimezoneOffset();
    uri += p_Query.m_CustomData;

    return uri;
}

//****************************************************************************
/// <summary>
/// Updates the results of a quick search.
/// </summary>
/// <param name="p_Query">The query textbox object.</param>
/// <param name="p_Results">The iframe object that displays the results.</param>
//****************************************************************************
function CES_QuickSearchBox_UpdateResults(p_Query, p_Results)
{
    if (p_Query.value != '' && !p_Query.m_Submitting) {
        // Reload the frame only if the query changed
        if (p_Query.value != p_Results.m_Query) {
            var uri = p_Results.m_Uri;
            uri += '&' + p_Query.m_QueryParameter + '=' + encodeURIComponent(p_Query.value);
            uri += '&' + p_Query.m_TargetFrameParameter + '=_top';
            uri += '&' + p_Query.m_TimeZoneOffsetParameter + '=' + -new Date().getTimezoneOffset();
            uri += '&' + p_Query.m_SearchPageParameter + '=' + encodeURIComponent(CES_QuickSearchBox_ComputeUri(p_Query));
            uri += p_Query.m_CustomData;
            p_Results.m_Query = p_Query.value;
            p_Results.src = uri;
        } else {
            // Else, just show the frame
            CES_QuickSearchBox_ShowResults(p_Query, p_Results);
        }
    } else {
        p_Results.m_Query = '';
        CES_QuickSearchBox_HideResults(p_Query, p_Results);
    }
}

//****************************************************************************
/// <summary>
/// Shows the results of the quick search.
/// </summary>
/// <param name="p_Query">The query textbox object.</param>
/// <param name="p_Results">The iframe object that displays the results.</param>
//****************************************************************************
function CES_QuickSearchBox_ShowResults(p_Query, p_Results)
{
    CNL_PositionObject(p_Results, p_Query, p_Results.m_Position);
    p_Results.style.visibility = 'visible';

    // IE seems to have a bug that causes the iframe to (sometime) look empty,
    // even if the document is loaded and all --- it is just not drawn. Doing
    // this seems to workaround the bug by forcing IE to redraw the content.
    p_Results.style.width = p_Results.offsetWidth + 1 + 'px';
}

//****************************************************************************
/// <summary>
/// Hides the results of a quick search.
/// </summary>
/// <param name="p_Query">The query textbox object.</param>
/// <param name="p_Results">The iframe object that displays the results.</param>
//****************************************************************************
function CES_QuickSearchBox_HideResults(p_Query, p_Results)
{
    p_Results.style.visibility = 'hidden';
}

