//****************************************************************************
// Copyright (c) 2005, Coveo Solutions Inc.
//****************************************************************************

function CES_CachedDocument() {

// PUBLIC INTERFACE

//****************************************************************************
// Opens the currently displayed document in a new window.
//****************************************************************************
this.OpenInNewWindow = function()
{
    window.open(this.m_Uri, this.m_Uri.indexOf('outlook:') == 0 ? '_self' : '_blank');
}

//****************************************************************************
// Opens the currently displayed view as html in a new window.
//****************************************************************************
this.OpenViewAsHtmlInNewWindow = function()
{
    window.open(this.src, '_blank');
}

//****************************************************************************
// Highlights the next occurence of a term.
// p_Term  - The index of the term to highlight.
//****************************************************************************
this.HighlightNextOccurence = function(p_Term)
{
    if (!this.m_DoNotHighlight) {
        this.RemoveLastHighlight();
        
        var last = this.m_Positions[p_Term];
        if (last == undefined) {
            last = 0;
        }

        var current = last + 1;
        if (this.GetTermPart(p_Term, current, 1) == null) {
            current = 1;
        }

        this.HighlightTerm(p_Term, current);
        this.m_Positions[p_Term] = current;
    }
}

//****************************************************************************
// Highlights the previous occurence of a term.
// p_Term  - The index of the term to highlight.
//****************************************************************************
this.HighlightPreviousOccurence = function(p_Term)
{
    if (!this.m_DoNotHighlight) {
        this.RemoveLastHighlight();
        
        var last = this.m_Positions[p_Term];
        if (last == undefined) {
            last = 1;
        }

        var current = last - 1;
        if (this.GetTermPart(p_Term, current, 1) == null) {
            // Skip to the last one
            current = 1;
            while (this.GetTermPart(p_Term, current, 1) != null) {
                ++current;
            }
            --current;
        }

        this.HighlightTerm(p_Term, current);
        this.m_Positions[p_Term] = current;
    }
}

// PRIVATE INTERFACE

//****************************************************************************
// Resets the members used to track the current highlight.
//****************************************************************************
this.ResetMembers = function()
{
    this.m_Positions = new Array();
    this.m_LastTerm = 0;
    this.m_LastOccurence = 0;
    this.m_DoNotHighlight = false;
}

//****************************************************************************
// Retrieves the document object inside the frame.
//****************************************************************************
this.GetDocument = function()
{
    return frames[this.name].document;
}

//****************************************************************************
// Retrieves a specific highlight part.
// p_Term      - The index of the term.
// p_Occurence - The index of the occurence of the term.
// p_Part      - The index of the part of the term.
//****************************************************************************
this.GetTermPart = function(p_Term, p_Occurence, p_Part)
{
    return this.GetDocument().getElementById('CoveoHighlight:' + p_Term + '.' + p_Occurence + '.' + p_Part);
}

//****************************************************************************
// Highlights a term and scrolls it into view.
// p_Term      - The index of the term to highlight.
// p_Occurence - The index of the term occurence to highlight.
//****************************************************************************
this.HighlightTerm = function(p_Term, p_Occurence)
{
    var part = 1;
    var elem = this.GetTermPart(p_Term, p_Occurence, part);
    if (elem != null) {
        elem.scrollIntoView();
        while (elem != null) {
            elem.m_BackupColor = elem.style.color;
            elem.m_BackupBackground = elem.style.backgroundColor;
            elem.style.color = 'white';
            elem.style.backgroundColor = 'green';
            elem = this.GetTermPart(p_Term, p_Occurence, ++part);
        }

        this.m_LastTerm = p_Term;
        this.m_LastOccurence = p_Occurence;

        // FireFox 1.5.0.2 has a bug with scrollIntoView
        document.documentElement.scrollTop = 0;
    }
}

//****************************************************************************
// Unhighlights the last highlighted term.
//****************************************************************************
this.RemoveLastHighlight = function()
{
    if (this.m_LastTerm != 0) {
        var part = 1;
        var elem = this.GetTermPart(this.m_LastTerm, this.m_LastOccurence, part);
        while (elem != null) {
            elem.style.color = elem.m_BackupColor;
            elem.style.backgroundColor = elem.m_BackupBackground;
            elem = this.GetTermPart(this.m_LastTerm, this.m_LastOccurence, ++part);
        }
    }
}

} // Constructor

