
///
///   DSCovery Search Clients
///
DScript.DScovery.Search = 
{
    DScoverySearch : new Class
    ({
        initialize: function()
        {
            var context = 
            {
                'id':'',
                'search':'',
                'issearching':0,
                'loopcounter':0,
                'dopostback':0,
                'datasources':''
            }
            
            this.context = context;
            this.context.datasources = DScript.DScovery.DataGroups.context;
        },
        
        Search : function()
        {
            this.doSearch(this.context.search,complete);
        },
        
        doCallBack : function()
        {
            if (this.context.issearching == 1)
            {
                this.context.loopcounter = this.context.loopcounter + 1;
                WebForm_DoCallback(this.context.id,Json.toString(this.context),this.onCallBack,this,null,true);
            }
        },
        
        onCommand : function(delegate)
        {
            return delegate.bindWithEvent(this);
        },
        
        onSearchCommand : function()
        {
            return DScript.DScovery.Search.search.bindWithEvent(this);
        },
        
        bindResultSet : function(resultsetElement)
        {
            if (typeof(createOverview) != "undefined")
            {
                callbacks = $(resultsetElement).getElements('li.callback');
            
                for (index = 0; index < callbacks.length; index++)
                {
                    callbacks[index].onclick = createOverview.bindWithEvent(callbacks[index]);
                }
            }
            else if (this.ResultsTemplateContainer.onBindWithEvent)
            {
                callbacks = $(resultsetElement).getElements('li.callback');
            
                for (index = 0; index < callbacks.length; index++)
                {
                    eval('datasource = DScript.DScovery.DataGroups.'+callbacks[index].getProperty('target'));
                    
                    if (typeof(datasource) != "undefined")
                    {
                        datasource.context.datasource.hitcount = callbacks[index].getNext().innerHTML.toInt();
                        var doBindWithEvent = this.ResultsTemplateContainer.onBindWithEvent.bind(datasource,callbacks[index]);
                        doBindWithEvent();
                    }
                    else
                    {
                        var doBindWithEvent = this.ResultsTemplateContainer.onBindWithEvent.bind(callbacks[index]);
                        doBindWithEvent();
                    }
                }
            }
        },
        
        onError : function(message)
        {
            if (DScript.DScovery.DScoveryManager.logging != 0)
            {
                console.log(message);
            }
        },
        
        onStartSearch : function (sender) 
        {
        
        },
        
        onFinishSearch : function (sender) 
        {
        
        },
        
        onCallBack : function (arg)
        {
            response = Json.evaluate(arg);
          
            if (response.issearching == 1 && response.loopcounter < 10)  // true - Seaching
            {
                this.context.context = response;
            }
            else if (response.issearching == 0 || response.loopcounter >= 10)  // false - Finished Searching
            {
               $clear(this.context.periodical); // Stop Query loop
               response.issearching = 0;
               response.loopcounter = 0;
               this.context.context = response;
               if (this.context.ThrobberTemplateContainer.Image) this.context.ThrobberTemplateContainer.Image.HTMLControl().style.visibility = 'hidden';
               this.context.periodical = null;
               DScript.DScovery.DataGroups.Add(response.datasources); // Reset our datsources
               if (response.loopcounter >= 10)
                this.context.onError('Search Response has timed out');
                
               this.context.onFinishSearch(this.context.context);
               
                if (this.context.ResultsTemplateContainer.onRender)
                { 
                    SearchResults1 = new DScript.HTML.HTMLControls.Control();
                    SearchResults1.context.id = (this.context.ResultsTemplateContainer.context.id.replace(/\$/g ,'_'));
                    this.context.ResultsTemplateContainer.onRender(SearchResults1.HTMLControl(), response);
                }
            }
        }
    }),
    
    search : function(event)
    {     
        if (this.SearchTemplateContainer.SearchText) this.context.search = this.SearchTemplateContainer.SearchText.HTMLControl().value;
        
        if (this.context.search.length > 2)
        {
            if (this.ThrobberTemplateContainer.Image) this.ThrobberTemplateContainer.Image.HTMLControl().style.visibility = 'visible';
            this.context.issearching = 1;  
            this.context.datasources = null;
            this.periodical = this.doCallBack.periodical(3000,this);
            this.onStartSearch(this.context);
        }
        else
        {
            this.context.issearching = 0;  
            this.context.loopcounter=0;
            if (this.ThrobberTemplateContainer.Image) this.ThrobberTemplateContainer.Image.HTMLControl().style.visibility = 'hidden';
            $clear(this.periodical); // Stop Query loop
            this.onError('Invalid Search Request: \'' + this.context.search + '\'');
        }
    },

    implement : function(properties)
    {
        Object.extend(DScript.DScovery.Search,properties);
    }
}