//****************************************************************************
// Copyright (c) 2005, Coveo Solutions Inc.
//****************************************************************************

function CES_SearchBox() {

//****************************************************************************
// Event handler for the onkeypress event of the TXTQuery control.
//****************************************************************************
this.TXTQuery_OnKeyPress = function(p_Event)
{
    if (p_Event.keyCode == 13 || p_Event.which == 13) {
        if (this.m_SearchAsYouType) {
            this.CancelEverything();
        }
        this.SubmitSearch();
        CNL_CancelEvent(p_Event);
        res = false;
    } else if (this.m_SearchAsYouType) {
        this.ScheduleUpdate();
    }
}

//****************************************************************************
// Event handler for the onfocus event of the TXTQuery control.
//****************************************************************************
this.TXTQuery_OnFocus = function()
{
    this.m_QueryLock = true;
    this.UpdateResults();
}

//****************************************************************************
// Event handler for the onblur event of the TXTQuery control.
//****************************************************************************
this.TXTQuery_OnBlur = function()
{
    this.m_QueryLock = false;
    this.ShouldMaybeHideResults();
}

//****************************************************************************
// Event handler for the onload event of the results iframe.
//****************************************************************************
this.IFrame_OnLoad = function()
{
    if (this.m_IFrame != null) {
        CNL_ResizeIFrame(this.m_IFrame);
        CNL_PositionObject(this.m_IFrame, this, this.m_Position);
        this.m_IFrame.style.visibility = 'visible';
    }
}

//****************************************************************************
// Event handler for the onmouseover event of the quick search results.
//****************************************************************************
this.IFrame_OnMouseOver = function()
{
    this.m_ResultsLock = true;
}

//****************************************************************************
// Event handler for the onmouseout event of the quick search results.
//****************************************************************************
this.IFrame_OnMouseOut = function()
{
    this.m_ResultsLock = false;
    this.ShouldMaybeHideResults();
}

//****************************************************************************
// Event handler for the onclick event of the BTNSearch control.
//****************************************************************************
this.BTNSearch_OnClick = function()
{
    this.SubmitSearch();
}

//****************************************************************************
// Event handler for the onclick event of the BTNLink control.
//****************************************************************************
this.BTNLink_OnClick = function()
{
    this.TXTQuery.value = '';
    this.SubmitSearch();
}

//****************************************************************************
// Submits the search to the search page.
//****************************************************************************
this.SubmitSearch = function() 
{
    window.location = this.ComputeUri();
}

//****************************************************************************
// Returns the uri of the search page with the current query.
//****************************************************************************
this.ComputeUri = function()
{
    var uri = this.m_Target;
    uri += '&' + this.m_QueryParameter + '=' + encodeURIComponent(this.TXTQuery.value);
    uri += '&' + this.m_TimeZoneOffsetParameter + '=' + -new Date().getTimezoneOffset();
    uri += this.m_CustomData;

    return uri;
}

//****************************************************************************
// Schedules an update of the quick search results.
//****************************************************************************
this.ScheduleUpdate = function()
{
    var myself = this;
    clearTimeout(this.m_Timer);
    this.m_Timer = setTimeout(function() { myself.UpdateResults(); }, this.m_ShowDelay);
}

//****************************************************************************
// Called when the results should maybe be hidden.
//****************************************************************************
this.ShouldMaybeHideResults = function()
{
    if (!this.m_QueryLock && !this.m_ResultsLock) {
        this.HideResults();
    }
}

//****************************************************************************
// Updates the quick search results.
//****************************************************************************
this.UpdateResults = function()
{
    if (this.TXTQuery.value != '') {
        // Compute the uri for the search results
        var uri = this.m_QuickSearch;
        uri += '&' + this.m_QueryParameter + '=' + encodeURIComponent(this.TXTQuery.value);
        uri += '&' + this.m_TargetFrameParameter + '=_top';
        uri += '&' + this.m_TimeZoneOffsetParameter + '=' + -new Date().getTimezoneOffset();
        uri += '&' + this.m_SearchPageParameter + '=' + encodeURIComponent(this.m_Target);
        uri += '&' + this.m_LcidParameter + '=' + this.m_LCID;
        uri += this.m_CustomData;

        // Either create a new IFrame or reuse existing one
        if (this.m_IFrame == null) {        
            var myself = this;
            this.m_IFrame = document.createElement('iframe');
            this.m_IFrame.m_Owner = this;
            this.m_IFrame.src = uri;
            this.m_IFrame.frameBorder = false;
            this.m_IFrame.onmouseover = function() { myself.IFrame_OnMouseOver(); };
            this.m_IFrame.onmouseout = function() { myself.IFrame_OnMouseOut(); };
            this.m_IFrame.style.border = '1px solid gray';
            this.m_IFrame.style.background = 'white';
            this.m_IFrame.style.position = 'absolute';
            this.m_IFrame.style.visibility = 'hidden';
            this.m_IFrame.style.left = '0px';
            this.m_IFrame.style.top = '0px';
            document.body.appendChild(this.m_IFrame);
        } else {
            this.m_IFrame.src = uri;
        }  
    } else {
        this.HideResults();
    }
}

//****************************************************************************
// Hides the quick search results.
//****************************************************************************
this.HideResults = function()
{
    // Release the iframe if any
    if (this.m_IFrame != null) {
        document.body.removeChild(this.m_IFrame);
        this.m_IFrame = null;
    }
}

//****************************************************************************
// Hides the quick search results, and cancels all pending operations.
//****************************************************************************
this.CancelEverything = function()
{
    clearTimeout(this.m_Timer);
    this.HideResults();
}

} // Constructor

