/* QwwJs_ListBoxPreselector
 * 
 * Part of the QlikWeb WorkBench Javascript Library (http://www.qlikwebworkbench.com)
 *
 * Copyright (c) 2007 Industrial CodeBox Ltd. All Rights Reserved.
 * http://www.industrialcodebox.com
 * Email support@industrialcodebox.com for licensing and support information.
 */
function QwwJs_ListBoxPreselector(cfgObj)
{     
    if(!cfgObj.Configurations && cfgObj.Configurations.length > 0)
    {
        alert("No Configurations supplied to QwwJs_ListBoxPreselector");
        return;
    };

    this.Cfg = cfgObj;
    this.lbms = new Array();
    
    var hasDocumentLoaded = false;
    
    this.OnListBoxMgrUpdated = function(data, extraInfo)
    {
        var haveAllLbmsUpdated = true;
        
        extraInfo.LBM.HasUpdatedAtLeastOnce = true;
        
        for(var i = 0; i < extraInfo.Preselector.lbms.length; i++)
        {
            var lbmgr = extraInfo.Preselector.lbms[i];

            if(lbmgr.HasUpdatedAtLeastOnce == false)
            {
                haveAllLbmsUpdated = false;
                break;
            };
        }   
        
        if(haveAllLbmsUpdated)
        {   
            extraInfo.Preselector.makeSelectionIfReady();
        } 
    };

    var hasRunOnce = false;
   
    this.makeSelectionIfReady = function()
    {
        if(hasRunOnce == true) return;
             
        var lbms = this.lbms;
        
        for(var i = 0; i < lbms.length; i++)
        {
            var lbmgr = lbms[i];

            if(lbmgr.HasUpdatedAtLeastOnce == false)
            {
                haveAllLbmsUpdated = false;
                return;
            };
        }
        
        if(hasDocumentLoaded)
        {
            this.MakeSelections();
            hasRunOnce = true;
        }
    };
    
    this.addOrGetListBoxMgr = function(objectId)
    {
        var isAdded = false;
        
        for(var i = 0; i < this.lbms.length; i++)
        {
            if(this.lbms[i].tag == objectId)
            {
                isAdded = true;
                return this.lbms[i];
            }
        }
        
        if(!isAdded)
        {
            var index = this.lbms.length;
            
            var newMgr = new QwwJs_ListBoxMgr(objectId);
            newMgr.tag = objectId;
            
            this.lbms[index] = newMgr;
            
            newMgr.HasUpdatedAtLeastOnce = false;
            newMgr.RegisterUpdatedCallBack(this.OnListBoxMgrUpdated, {"LBM" : newMgr, "Preselector" : this});
                
            return newMgr;
        }
    };
        
    for(var i = 0; i < this.Cfg.Configurations.length; i++)
    {
        var configuration = this.Cfg.Configurations[i];
        
        for(var j = 0; j < configuration.Selections.length; j++)
        {
            var selection = configuration.Selections[j];
            
            var listBoxId = selection.ListBoxID;
            
            this.addOrGetListBoxMgr(listBoxId);
        }        
    };


    this.summariseConfiguration = function()
    {
        var s = "";

        for(var i = 0; i < this.Cfg.Configurations.length; i++)
        {
            var configuration = this.Cfg.Configurations[i];
            
            
            s += "Configuration url match:" + configuration.UrlPattern + "\n";
            
            for(var j = 0; j < configuration.Selections.length; j++)
            {
                var selection = configuration.Selections[j];
                
                s += "Configuration for:" + selection.ListBoxID + "\n";
                
                s += "Selections:";
                
                for(k = 0; k < selection.Selections.length; k++)
                {
                    s += selection.Selections[k] + ",";
                }
                
                s += "\n";
                
            }        
            
            s += "\n\n";

        }    
        
        alert(s);
    
    };
    
    if(document.URL.indexOf("ListBoxPreselector=ShowConfig") > -1)
            this.summariseConfiguration();


   
    this.MakeSelections = function(forceRegardlessOfUrlPatternMatch)
    {        
        if(forceRegardlessOfUrlPatternMatch == null) forceRegardlessOfUrlPatternMatch = false;
        
        for(var i = 0; i < this.Cfg.Configurations.length; i++)
        {            
            var configuration = this.Cfg.Configurations[i];
            
            var urlPattern = configuration.UrlPattern;
            
            if(forceRegardlessOfUrlPatternMatch || 
                (document.URL.indexOf(urlPattern) > -1))
            {   
                for(var j = 0; j < configuration.Selections.length; j++)
                {
                    var selection = configuration.Selections[j];
                    
                    var listBoxId = selection.ListBoxID;
                    
                    var mgr = this.addOrGetListBoxMgr(listBoxId);
                    var all = mgr.GetAllItems();
                    
                    var arrayOfSelectionsAsValues = new Array();
                    
//                    for(var sel = 0; sel < selection.Selections.length; sel++)
//                    {
//                        var selectionAsText = selection.Selections[sel];
//                        
//                        //mgr.updateResults();
//                        
//                        for(var a = 0; a < all.length; a++)
//                        {
//                            var item = all[a];
//                            
//                            if(item.Text == selectionAsText)
//                            {
//                                //alert("match found");
//                                arrayOfSelectionsAsValues[arrayOfSelectionsAsValues.length] = item.Value;
//                                break;
//                            }
//                        }    
//                    }
                    
                    //mgr.MakeSelection(selection.Selections, true, true);
                    
                    //
                    // Using this because using a straight call to mgr.MakeSelection(selection.Selections, true, true)
                    // it seemed that a call by avq.js to pageoffset=20 was causing the listbox being tested on to 
                    // jumo to the 20th record without reflecting the preselection.
                    //
                    window.setTimeout(function(){mgr.MakeSelection(selection.Selections, true);},200);                   
                    //mgr.MakeSelection(arrayOfSelectionsAsValues, false);

                }
            }        
        }
        
    };
    

    this.OnDocumentLoaded = function()
    {   
        hasDocumentLoaded = true;
        this.makeSelectionIfReady();
    };

    if(qwwHub)
        qwwHub.Register(this);
};