/**************************************************************************
*
*  @@@BUILDINFO@@@ 87findResult-2.jsx 2.0.1.61  08-Feb-2007
*  Copyright 2006-2007 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains the property of
* Adobe Systems Incorporated  and its suppliers,  if any.  The intellectual 
* and technical concepts contained herein are proprietary to  Adobe Systems 
* Incorporated  and its suppliers  and may be  covered by U.S.  and Foreign 
* Patents,patents in process,and are protected by trade secret or copyright 
* law.  Dissemination of this  information or reproduction of this material
* is strictly  forbidden  unless prior written permission is  obtained from 
* Adobe Systems Incorporated.
**************************************************************************/

app.createFResult = function( where )
{
    //
    // Find in files result palette
    //
	var w = this.findResult = new Window( 
		"dockpalette {													                \
			text            : '$$$/ESToolkit/Panes/FindResult/Title=Find results',      \
			id              : 'findresults',											\
			margins         : 2,                                                        \
			spacing         : 2,                                                        \
			properties      :                                                           \
			{											                                \
			    flyoutmenu          : true,                                             \
			    defaultVisibility   : false,								            \
			    icon                : '#PFindResults_N',	                            \
			    rollover            : '#PFindResults_R',                                \
				name                : 'findresults'," + where + ",                      \
				after               : 'findAndReplace'                                  \
			},													                        \
			output          : ListBox                                                   \
			{									                                        \
				alignment           : ['fill', 'fill' ]                                 \
            },                                                                          \
            statusGroup          : Group                                                \
            {                                                                           \
                alignment       : ['fill', 'bottom' ],                                  \
                orientation     : 'row',                                                \
                margins         : 0,                                                    \
                spacing         : 0,                                                    \
                statustext      : StaticText                                            \
                {                                                                       \
                    properties  : { truncate : 'middle' },                              \
                    alignment   : ['fill','center'],                                    \
                    characters  : 40                                                    \
                },                                                                      \
                progress        : Progressbar                                           \
                {                                                                       \
					visible			: false,											\
                    alignment       : ['right','center']                                \
                },                                                                      \
                cancelBtn       : Button	                                            \
                {                                                                       \
					text			: '$$$/CT/ExtendScript/UI/Cancel=&Cancel',			\
					enabled			: false,											\
					properties		: { name:'cancel' },								\
                    alignment       : ['right','center']                                \
                },                                                                      \
                sizeBoxSpacer    : Group                                                \
                {                                                                       \
                    preferredSize   : [20,1],                                           \
                    alignment       : ['right','center']                                \
                }                                                                       \
            }                                                                           \
		}");
	
	w.menuText = localize ("$$$/ESToolkit/Menu/Window/FindResult/Title=&Find Results");

	w.cancelBtn		= w.statusGroup.cancelBtn;
	w.statustext	= w.statusGroup.statustext;
	w.progress		= w.statusGroup.progress;

    w.initialized	= false;	// true if initialized
	w.inSearch		= false;	// flags an active search
	w.searchCount	= 0;		// the number of entries
	w.cancelSearch	= false;	// true if user clicked Cancel
	w.findText		= "";		// the text to find
	w.filePrefix	= "";		// the file prefix to be ignored when creating output lines

	///////////////////////////////////////////////////////////////////////////////
	//
	// fly-out menu
	//
	
	var item = new MenuElement( 'command', '$$$/ESToolkit/Panes/FindResult/Flyout/Clear=Clear', "at the end of findresults/flyout", "findresults/flyout/clear" )
	item.output = this.findResult.output;
	item.onSelect = function()
	{
	    this.output.removeAll();
	}

    w.onResize = 
    w.onResizing = function ()
    {
	    this.layout.resize();
    }

    w.cancelBtn.onClick = function ()
    {
	    this.window.cancelSearch = true;
    }

    ///////////////////////////////////////////////////////////////////////////////
    //
    // results
    //
    w.startSearch = function( what, count, clear, filePrefix )
    {
        if( !this.inSearch )
        {
			this.filePrefix	  = filePrefix;
			this.cancelSearch = false;
            if( clear )
                this.output.removeAll();
            else
                this.output.add( 'item', '------------------------------' );
            var text = "$$$/ESToolkit/Panes/FindResult/FindText=Find \"%1\"";
			this.findText			= what;
            this.statustext.text    = localize( text, what);
            this.progress.maxvalue  = count;
            this.progress.value     = 0;
            this.progress.show();
            
            this.inSearch			= true;
            this.searchCount		= 0;
			this.fileCount			= 0;
			this.lastFile			= '';
			this.cancelBtn.enabled	= true;

            this.show();
            this.active = true;
        }
    }
    
	// add a result, and return false if the user closed the ESTK or aborted the search.
    this.findResult.addResult = function( lineObj, incProgress )
    {
		var aborted = this.cancelSearch;
        if( this.inSearch && !aborted )
        {
			aborted = !app.pumpEventLoop (true);
			var text = lineObj.title + '(' + (lineObj.line + 1) + "): " + lineObj.text.replace(/\t/g, " ");
            var item = this.output.add( 'item', text );
            item.info = lineObj;
			this.output.selection = item;
            this.searchCount++;
			
			if( lineObj.scriptID != this.lastFile )
				this.fileCount++;
			this.lastFile = lineObj.scriptID;
                
            if( incProgress )
                this.statusGroup.progress.value++;
        }
		return !aborted;
    }
    
	// end a search, and return true if anything was found.
    w.endSearch = function()
    {
        if( this.inSearch )
        {
			var text = "$$$/ESToolkit/Panes/FindResult/FindResult=%1, %2 matches in %3 files.";
            this.progress.hide();
            this.statustext.text    = localize (text, this.findText, 
                                                  this.searchCount,
                                                  this.fileCount);

			this.cancelSearch					= false;
			this.cancelBtn.enabled				= false;
            this.inSearch                       = false;
			this.output.selection				= null;
            this.progress.hide();
		}
		return (this.searchCount > 0);
    }
    
    w.incProgress = function()
    {
        this.progress.value++;
    }
    
    ///////////////////////////////////////////////////////////////////////////////
    //
    // user interaction
    //
    
    w.docAvailable = function()
    {
        return ( null != Document.find( this.awaitedScriptID ) );
    }
    
    w.output.onDoubleClick = function()
    {
        if( this.selection )
        {
	        var doc = Document.find( this.selection.info.scriptID );
	        
	        if( !doc )
	        {
	            if( this.selection.info.target )
	            {
                    this.awaitedDoc         = null;
                    this.awaitedScriptID    = this.selection.info.scriptID;

	                scripts.loadScript( this.selection.info.scriptID, this.selection.info.title, this.selection.info.target, this.selection.info.engine );
	                
                    if( wait( this.window.docAvailable ) )
                    {
                        doc = this.awaitedDoc;
                        
                        this.awaitedDoc         = null;
                        this.awaitedScriptID    = null;
                    }
	            }
	            else
	            {
	                var file = new File( this.selection.info.scriptID )
    	            
	                if( file.exists )
	                {
	                    var docwin = Document.load( file );
    	                
	                    if( docwin )
	                        doc = docwin.document
	                }
	            }
	        }
	         
	        if( doc )
	        {       
		        doc.activate();
		        doc.setSelection( this.selection.info.line, this.selection.info.pos, 
		                          this.selection.info.line, this.selection.info.pos + this.selection.info.searchStr.length );
                doc.scrollSelection();		                          
            }
	    }
    }
    
    this.findResult.onShow = function()
    {
        if( !this.initialized )
            this.layout.layout();
    }
    
	panes.push(this.findResult);
    this.findResult.show();
    //this.findResult.layout.layout();
}
