// extends EventController class
var LCO = null;

// class definition
function HomeLC()
{
    this.m_tocState = "site";
}

// member function prototypes
HomeLC.prototype = new EventController();
HomeLC.prototype.headerTile;
HomeLC.prototype.handleEvent   = HomeLC_handleEvent;
HomeLC.prototype.getTargetFrame = HomeLC_getTargetFrame;
HomeLC.prototype.getFrame       = HomeLC_getFrame;
HomeLC.prototype.setSearch      = HomeLC_setSearch;
HomeLC.prototype.setJump      = HomeLC_setJump;
HomeLC.prototype.setSavedSearchFrames = HomeLC_setSavedSearchFrames;
HomeLC.prototype.loadFrame      = HomeLC_loadFrame;

HomeLC.prototype.ssMouseBodyOver = function()
{
    var obj = document.getElementById('ssTable');
    if (this.timerID == 0 && obj.style.display == '')
    {
        this.timerID = window.setTimeout("var obj = document.getElementById('ssTable'); obj.style.display = 'none'", 1000);
    }
}

HomeLC.prototype.getTocState = function()
{
    return this.m_tocState;
}
HomeLC.prototype.setTocState = function(tocState)
{
    this.m_tocState = tocState;
}

function HomeLC_setSearch(tile)
{
    var e = tile.getElement();
    var srcFrameObj = document.getElementById("homeFrame");
    var docObj = null;

    // Document logic for Internet Explorer
    if (srcFrameObj.contentWindow)
    {
        docObj = srcFrameObj.contentWindow.document;
    }
    // Document logic for Mozilla
    else if (srcFrameObj.contentDocument)
    {
        docObj = srcFrameObj.contentDocument;
    }

    var targetFrameObj = docObj.getElementById("rightFrame");

    var rightFrameObj = null;

    if (targetFrameObj.contentWindow)
    {
        rightFrameObj = targetFrameObj.contentWindow.document;
    }
    // Document logic for Mozilla
    else if (targetFrameObj.contentDocument)
    {
        rightFrameObj = targetFrameObj.contentDocument;
    }

    var bottomObj = rightFrameObj.getElementById("bottomFrame");

    // check for top frame if so doc/results frameset
    if (bottomObj)
    {
        //bottomObj.contentWindow.location.href = "search.htm?view=results&s_action=newSearch&doc_action=setHitDoc&doc_hit=1&s_pageNum=1&s_method=applyFilter&s_filter=simple|first|true|" + e.value +  "|null|any|true";
        bottomObj.contentWindow.location.href = "search.htm?view=results&s_action=newSearch&s_type=all&s_terms=" + e.value;
        // get first document for top frame
        var topObj = rightFrameObj.getElementById("topFrame");
        topObj.contentWindow.location.href = "template.htm?view=document&doc_action=setHitDoc&doc_hit=1";

        var FrameObj = document.getElementById("homeFrame");
        if (FrameObj.contentWindow)
        {
            docObj = FrameObj.contentWindow.document;
        }
        // Document logic for Mozilla
        else if (FrameObj.contentDocument)
        {
            docObj = FrameObj.contentDocument;
        }
    }
    else
    {
        //targetFrameObj.contentWindow.location.href = "search.htm?view=hitlist&s_action=newSearch&s_pageNum=1&s_method=applyFilter&s_filter=simple|first|true|" + e.value +  "|null|any|true";
        targetFrameObj.contentWindow.location.href = "search.htm?view=hitlist&s_action=newSearch&s_type=all&s_terms=" + e.value;
        srcFrameObj = document.getElementById("homeFrame");
        docObj = null;

        // Document logic for Internet Explorer
        if (srcFrameObj.contentWindow)
        {
            docObj = srcFrameObj.contentWindow.document;
        }
        // Document logic for Mozilla
        else if (srcFrameObj.contentDocument)
        {
            docObj = srcFrameObj.contentDocument;
        }
    }
    return true;
}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function HomeLC_setJump(source)
{
    var srcFrameObj = document.getElementById("homeFrame");
    var docObj = null;

    // Document logic for Internet Explorer
    if (srcFrameObj.contentWindow)
    {
        docObj = srcFrameObj.contentWindow.document;
    }
    // Document logic for Mozilla
    else if (srcFrameObj.contentDocument)
    {
        docObj = srcFrameObj.contentDocument;
    }

    var targetFrameObj = docObj.getElementById("rightFrame");

    var rightFrameObj = null;

    if (targetFrameObj.contentWindow)
    {
        rightFrameObj = targetFrameObj.contentWindow.document;
    }
    // Document logic for Mozilla
    else if (targetFrameObj.contentDocument)
    {
        rightFrameObj = targetFrameObj.contentDocument;
    }

    var bottomObj = rightFrameObj.getElementById("bottomFrame");

    // check for top frame if so doc/results frameset
    if (bottomObj)
    {
        // get first document for top frame
        var topObj = rightFrameObj.getElementById("topFrame");
        topObj.contentWindow.location.href = source.getElement().href;
        var FrameObj = document.getElementById("homeFrame");
        if (FrameObj.contentWindow)
        {
            docObj = FrameObj.contentWindow.document;
        }
        // Document logic for Mozilla
        else if (FrameObj.contentDocument)
        {
            docObj = FrameObj.contentDocument;
        }
    }
    else
    {
        targetFrameObj.contentWindow.location.href = source.getElement().href;
        srcFrameObj = document.getElementById("homeFrame");
        docObj = null;

        // Document logic for Internet Explorer
        if (srcFrameObj.contentWindow)
        {
            docObj = srcFrameObj.contentWindow.document;
        }
        // Document logic for Mozilla
        else if (srcFrameObj.contentDocument)
        {
            docObj = srcFrameObj.contentDocument;
        }
    }
    return true;
}




/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




function HomeLC_setSavedSearchFrames(id)
{
    var srcFrameObj = document.getElementById("homeFrame");
    var docObj = null;

    // Document logic for Internet Explorer
    if (srcFrameObj.contentWindow)
    {
        docObj = srcFrameObj.contentWindow.document;
    }
    // Document logic for Mozilla
    else if (srcFrameObj.contentDocument)
    {
        docObj = srcFrameObj.contentDocument;
    }

    var targetFrameObj = docObj.getElementById("rightFrame");

    var rightFrameObj = null;

    if (targetFrameObj.contentWindow)
    {
        rightFrameObj = targetFrameObj.contentWindow.document;
    }
    // Document logic for Mozilla
    else if (targetFrameObj.contentDocument)
    {
        rightFrameObj = targetFrameObj.contentDocument;
    }

    var bottomObj = rightFrameObj.getElementById("bottomFrame");

    // check for top frame if so doc/results frameset
    if (bottomObj)
    {
        bottomObj.contentWindow.location.href = "search.htm?view=results&sv_action=setSearch&sv_id=" + id;
        // get first document for top frame
        
        
        for (var i = 0; i< 5000000;i++)
        {
        	
       	}
        
        var topObj = rightFrameObj.getElementById("topFrame");
        topObj.contentWindow.location.href = "template.htm?view=document&doc_action=setHitDoc&doc_hit=1";

        var FrameObj = document.getElementById("homeFrame");
        if (FrameObj.contentWindow)
        {
            docObj = FrameObj.contentWindow.document;
        }
        // Document logic for Mozilla
        else if (FrameObj.contentDocument)
        {
            docObj = FrameObj.contentDocument;
        }
        var tocFrameObj = docObj.getElementById("leftFrame");
        tocFrameObj.contentWindow.location.href = "template.htm?view=toc";
    }
    else
    {
        targetFrameObj.contentWindow.location.href = "search.htm?view=hitlist&sv_action=setSearch&sv_id=" + id;

        srcFrameObj = document.getElementById("homeFrame");
        docObj = null;

        // Document logic for Internet Explorer
        if (srcFrameObj.contentWindow)
        {
            docObj = srcFrameObj.contentWindow.document;
        }
        // Document logic for Mozilla
        else if (srcFrameObj.contentDocument)
        {
            docObj = srcFrameObj.contentDocument;
        }

        var tocFrameObj = docObj.getElementById("leftFrame");
        tocFrameObj.contentWindow.location.href = "template.htm?view=toc";
    }
    return true;
}


function HomeLC_loadFrame(srcFrameId, targetFrameId, url)
{

    var srcFrameObj = document.getElementById(srcFrameId);

    var docObj = null;

    // Document logic for Internet Explorer
    if (srcFrameObj.contentWindow)
    {
        docObj = srcFrameObj.contentWindow.document;
    }
    // Document logic for Mozilla
    else if (srcFrameObj.contentDocument)
    {
        docObj = srcFrameObj.contentDocument;
    }

    var targetFrameObj = docObj.getElementById(targetFrameId);

    targetFrameObj.contentWindow.location.href = url;
}


function header_callback(tile)
{
    if (tile.getEventName() == "tab" )
    {
        LCO.loadFrame(tile.sourceFrame, tile.targetFrame, tile.URL);
        if (homeFrame.leftFrame.LCO.tocTile.getShowCheckBoxes() == 1)
        {
            var srcFrameObj = document.getElementById("homeFrame");
            var docObj = null;

            // Document logic for Internet Explorer
            if (srcFrameObj.contentWindow)
            {
                docObj = srcFrameObj.contentWindow.document;
            }
            // Document logic for Mozilla
            else if (srcFrameObj.contentDocument)
            {
                docObj = srcFrameObj.contentDocument;
            }

            var tocFrameObj = docObj.getElementById("leftFrame");
            tocFrameObj.contentWindow.location.href = "template.htm?view=toc";
        }
        return false;
    }
	else if (tile.getEventName() == "setSavedSearch")
	{
		LCO.setEvent("setSavedSearch");
		return LCO.handleEvent(LCO);
	}
	else if (tile.getEventName() == "removeSavedSearch")
	{
		LCO.setEvent("removeSavedSearch");
		return LCO.handleEvent(LCO);
	}
    else
    {
        return LCO.setSearch(tile);
    }
}

function save_callback(tile)
{
	if (tile.getEvent() == "E_SAVE_SEARCH_REMOVED")
	{
		LCO.setEvent(tile.getEvent());
		return LCO.handleEvent(LCO);
	}
	else if (tile.getEvent() == "E_SAVE_SEARCH_ADDED")
	{
		LCO.setEvent("E_SAVE_SEARCH_ADDED");
		return LCO.handleEvent(LCO);
	}
	else if (tile.getEvent() == "deleteSavedSearch")
	{
		LCO.setEvent("deleteSavedSearch");
		return LCO.handleEvent(LCO);
	}
}

function HomeLCInit()
{
	LCO = new HomeLC();
	LCO.headerTile = new HeaderTC(header_callback);
	LCO.saveTile = new SaveTC(save_callback);
}

function HomeLC_getTargetFrame()
{
	var srcFrameObj = document.getElementById("homeFrame");
	var docObj = null;

	// Document logic for Internet Explorer
	if (srcFrameObj.contentWindow)
	{
		docObj = srcFrameObj.contentWindow.document;
	}
	// Document logic for Mozilla
	else if (srcFrameObj.contentDocument)
	{
		docObj = srcFrameObj.contentDocument;
	}

	var targetFrameObj = docObj.getElementById("rightFrame");

	var topFrameObj = null;

	if (targetFrameObj.contentWindow)
	{
		topFrameObj = targetFrameObj.contentWindow.document;
	}
	// Document logic for Mozilla
	else if (targetFrameObj.contentDocument)
	{
		topFrameObj = targetFrameObj.contentDocument;
	}

	var topObj = topFrameObj.getElementById("topFrame");

	// check for top frame if so doc/results frameset
	if (topObj)
	{
		return topObj;
	}
	else
	{
		return targetFrameObj;
	}
}

function xGetElementById2(e, window)
{
  if(window.getElementById) e=window.getElementById(e);
  else if(window.all) e=window.all[e];
  else e=null;
  return e;
}

function HomeLC_getFrame(frameName)
{
	var srcFrameObj = document.getElementById("homeFrame");
	var docObj = null;

	// Document logic for Internet Explorer
	if (srcFrameObj.contentWindow)
	{
		docObj = srcFrameObj.contentWindow.document;
	}
	// Document logic for Mozilla
	else if (srcFrameObj.contentDocument)
	{
		docObj = srcFrameObj.contentDocument;
	}

	return docObj.getElementById(frameName);
}

function HomeLC_handleEvent(source)
{
    if (source.getEvent() == "E_CANCEL_USERPREF")
    {
        var targetFrame = document.getElementById("mainFrame");
        targetFrame.contentWindow.location.href = "template.htm?view=home";
        return true;
    }
    else if (source.getEvent() == "E_ADVANCE_SEARCH_CANCEL")
    {
        var targetFrame = document.getElementById("mainFrame");
        targetFrame.contentWindow.location.href = "template.htm?view=home";
        return true;
    }
    else if (source.getEvent() == "E_BROWSE_VIEW")
    {
        // Since we are only storing state don't say we handled the event.
        this.m_tocState = "site";
        return false;
    }
    else if (source.getEvent() == "E_BROWSE_VIEW_CATEGORY")
    {
        // Since we are only storing state don't say we handled the event.
        this.m_tocState = "category";
        return false;
    }
    else if (source.getEvent() == "E_MULTI_PRINT")
    {
        var targetFrame = document.getElementById("homeFrame");
        targetFrame.contentWindow.location.href = "template.htm?view=print";
        return true;
    }
    else if (source.getEvent() == "E_CANCEL_EMAIL")
    {
        var targetFrame = document.getElementById("rightFrame");
        targetFrame.contentWindow.location.href = "template.htm?view=document";
        return true;
    }
    else if (source.getEvent() == "E_SELECT_HIT_DOCUMENT")
    {
        var targetFrameObj = this.getTargetFrame();
        targetFrameObj.contentWindow.location.href = source.resultsListTile.getElement();
        return true;
    }
    else if (source.getEvent() == "E_TOCNODE_CLICKED")
    {
        var targetFrameObj = this.getTargetFrame();
        var currentDoc = source.getSelectedDocumentId();
        var subDocLink = source.getSelectedSubDocLink();
        var docFrame;
        if (homeFrame.rightFrame.topFrame)
        {
        	docFrame = homeFrame.rightFrame.topFrame;
        }
        else
        {
        	docFrame = homeFrame.rightFrame;
        }
        
        if (targetFrameObj.contentWindow.location.href.indexOf(currentDoc) != -1)
        {
		var targetElement = xGetElementById2(subDocLink, docFrame.docFrame.document);
		xWinScrollTo(docFrame.docFrame, 0, xPageY(targetElement), 1000)
        }
        else
        {
	        targetFrameObj.contentWindow.location.href = "template.htm?view=document&doc_action=setDoc&doc_keytype=tocid&doc_key=" + currentDoc + "&hash=" + subDocLink;
	}
        return true;
    }
    else if (source.getEvent() == "E_SYNC_TO_TOC")
    {
        homeFrame.leftFrame.LCO.tocTile.syncToId(source.docoptionsTile.getElement());
        return true;
    }
    else if (source.getEvent() == "E_SET_HIT_DOC")
    {
        var targetFrameObj = this.getTargetFrame();
        targetFrameObj.contentWindow.location.href = "template.htm?view=document&doc_action=setHitDoc&doc_hit=" + source.docoptionsTile.getElement();
        return true;
    }
    else if (source.getEvent() == "setTocDoc")
    {
        var targetFrameObj = this.getTargetFrame();
        targetFrameObj.contentWindow.location.href = "template.htm?view=document&doc_action=setDoc&doc_keytype=tocid&doc_key=" + source.docoptionsTile.getElement();
        return true;
    }
    else if (source.getEvent() == "setSavedSearch")
	{
		this.setSavedSearchFrames(source.headerTile.getElement());
		return true;
	}
    else if (source.getEvent() == "removeSavedSearch")
	{
		this.saveTile.removeSearch(source.headerTile.getElement());
		return true;
	}
    else if (source.getEvent() == "E_SAVE_SEARCH")
	{
		var name = source.saveTile.getElement();
		if (this.headerTile.isSavedSearch(name))
		{
			alert("There is already a search named: " + name);
		}
		else
		{
			this.saveTile.saveSearch(source.saveTile.getElement());
		}
		return true;
	}
    else if (source.getEvent() == "setJumpLink")
    {
        return this.setJump(source);
    }
    else if (source.getEvent() == "E_SAVE_SEARCH_ADDED")
	{
		this.headerTile.insertSearchItem(source.saveTile.getElement(), source.saveTile.getID());
		return true;
	}
    else if (source.getEvent() == "E_SAVE_SEARCH_REMOVED")
	{
		this.headerTile.removeSearchItem(source.headerTile.getElement());
		return true;
	}
    else if (source.getEvent() == "E_ADVANCED_SEARCH")
    {
        var tocFrameObj = this.getFrame("leftFrame");
        source.setDomain(homeFrame.leftFrame.LCO.tocTile.getCheckedNodes());
        source.setFilterDisplayName("[TOC Domain]");
        //tocFrameObj.contentWindow.location.href = "template.htm?view=toc";
        homeFrame.leftFrame.LCO.tocTile.setShowCheckBoxes(0);
        LCO.headerTile.lastSearchForm = "advanced";
        return false;
    }
    else if (source.getEvent() == "E_BOOLEAN_SEARCH")
    {
        var tocFrameObj = this.getFrame("leftFrame");
        source.setBooleanDomain(homeFrame.leftFrame.LCO.tocTile.getCheckedNodes());
        source.setBooleanFilterDisplayName("[TOC Domain]");
        //tocFrameObj.contentWindow.location.href = "template.htm?view=toc";
        homeFrame.leftFrame.LCO.tocTile.setShowCheckBoxes(0);
        LCO.headerTile.lastSearchForm = "boolean";
        return false;
    }
    else if (source.getEvent() == "cancelQuery")
    {
        LCO.loadFrame("homeFrame", "rightFrame", "template.htm?view=document");
        if (homeFrame.leftFrame.LCO.tocTile.getShowCheckBoxes() == 1)
        {
            var srcFrameObj = document.getElementById("homeFrame");
            var docObj = null;

            // Document logic for Internet Explorer
            if (srcFrameObj.contentWindow)
            {
                docObj = srcFrameObj.contentWindow.document;
            }
            // Document logic for Mozilla
            else if (srcFrameObj.contentDocument)
            {
                docObj = srcFrameObj.contentDocument;
            }

            var tocFrameObj = docObj.getElementById("leftFrame");
            tocFrameObj.contentWindow.location.href = "template.htm?view=toc";
            //tocFrameObj.LCO.tocTile.setShowCheckBoxes(0);
        }
        return false;
    }
    else
    {
        var parentLC = this.getParent();
        if (parentLC != null && parentLC.handleEvent != null)
        {
            return parentLC.handleEvent(source);
        }
        else
        {
            return false;
        }
    }
}


