/*
 *  This handles clicks from the selection pane. (the left one)
 */
function ClickEventBot(deepHList,centerpane){
    var dhListID;
    var cpaneID;
    var Buff;
    var Cnt;
    var limitsChanged;
    
    this.Cnt = 0;
    this.limitsChanged = false;    
    this.dhListID = deepHList;
    this.cpaneID = centerpane;
    this.Toggle = Toggle;
    this.Explode = Explode;
    this.SetMembers = SetMembers;
    this.DoPlot = DoPlot;
    this.ReToggleAll = ReToggleAll;
    this.ResetAll = ResetAll;
    this.Init = Init;
    this.DoLimit = DoLimit;
    this.DontLimit = DontLimit;
    
    function DoLimit(){this.limitsChanged = true;}
    function DontLimit(){this.limitsChanged = false;}
    
    function Init(){
       document.getElementById('reset').value = "true";
       updateMDX(this.cpaneID,false);
    }
    function ResetAll(){
        this.Init();
        window.open('default.aspx','_top','');
    }
    
    function Toggle(prefix,uid){
        var n = window.document.getElementById(prefix+'s_'+uid);
        var isSS = isInSingleSelect(n)
        if(n.getAttribute('sel') == 0){   

                document.getElementById(prefix+'_title').innerHTML = uid;
                if(isSS){
                    clearPane(n);
                    document.getElementById('rval').value = "__EVERYTHING__";
                }
                document.getElementById('targ').value = prefix;
                document.getElementById('val').value = uid;

            n.setAttribute('sel',1);
            n.className = 'selected';
        }else{        

            document.getElementById(prefix+'_title').innerHTML = 'No selection';
                document.getElementById('targ').value = prefix;
                document.getElementById('rval').value = uid;

            n.setAttribute('sel',0);
            //n.style.backgroundColor=this.USEL_BG;
            n.className = null;
            
        }
        this.DontLimit();
        updateMDX(this.cpaneID,false);  
    }
    function ReToggleAll(){
          //not implemented
    }
    /**
     *  Will request children from server if not yet added.  Otherwise just expands.
     */
    function Explode(uid,prefix){
        var n = document.getElementById(prefix+'p_'+uid);
        if(n.getElementsByTagName('ul').length == 0){
            this.Buff = window.bf.NewBuffer('load_members.aspx?uid='+escape(uid)+'&prefix='+prefix)  
        }            
        else
            toggle_children(n.getElementsByTagName('input')[0].id);
    }
    /**
     *  Companion to Explode.  Called by the server once members have completed loading.
     *  Updates the parent with the fresh children.
     */
    function SetMembers(parentid){
        n= frames[this.Buff.name].document.getElementsByTagName('ul')[0];
        p = document.getElementById(parentid);
        n = document.importNode(n,true);
        p.appendChild(n);
        toggle_children(p.getElementsByTagName('input')[0].id);    
        window.bf.DisposeBuffer(this.Buff.name);
    }
    function DoPlot(){
        document.getElementById('doplot').value = 'true';
        s_plot();
        //updateMDX(this.cpaneID,this.limitsChanged);
    }
    
    function toggle_children(id){
        if(branchIsOpen(id)) cBranch(id); 
        else oBranch(id);
    }
    function branchIsOpen(id){
       var element = document.getElementById(id).parentNode;
       if(element.getElementsByTagName("ul").length > 0)
            return (element.getElementsByTagName("ul")[0].style.display == 'block');  
       else return false;
    }
    function oBranch(id){
        document.getElementById(id).src= 'img/open.gif';
        document.getElementById(id).parentNode.getElementsByTagName("ul")[0].style.display = 'block'; 
    }
    function cBranch(id){
        document.getElementById(id).value= 'img/closed.gif';
        document.getElementById(id).parentNode.getElementsByTagName("ul")[0].style.display = 'none'; 
    }
    function updateMDX(cpaneID,lchanged){
        //peb.DisposeCanvas();
        if(lchanged){
            document.getElementById('xmin').value = document.getElementById('uxmin').value;
            document.getElementById('xmax').value = document.getElementById('uxmax').value;
            document.getElementById('ymin').value = document.getElementById('uymin').value;
            document.getElementById('ymax').value = document.getElementById('uymax').value;
        }else resetLimits();
        document.getElementById('width').value = document.getElementById(cpaneID).offsetWidth;
        document.getElementById('height').value = document.getElementById('outerCenter').offsetHeight -4;
        
        var aggDDL = document.getElementById('aggDDL');
        document.getElementById('agg').value = aggDDL[aggDDL.selectedIndex].value;
        var stat = document.getElementById('statusThingy');
        //stat.style.background = 'blue';
        //stat.style.color = 'white';
        //stat.innerHTML = 'Busy';
        document.pform.submit();
        document.getElementById('targ').value = '';
        document.getElementById('val').value = '';
        document.getElementById('rval').value = '';
        document.getElementById('doplot').value = '';
        document.getElementById('reset').value = '';
        resetLimits();
    }
    
    function resetLimits(){
        document.getElementById('xmin').value = '';
        document.getElementById('xmax').value = '';
        document.getElementById('ymin').value = '';
        document.getElementById('ymax').value = '';
    }
    
    function isInSingleSelect(element){
        element = getParentPane(element);
        return element.getAttribute('sselect') != null && element.getAttribute('sselect') == '1';
    }
    function getParentPane(element){
       while(element.nodeName != 'DIV')
            element = element.parentNode;
       return element;
    }
    function clearPane(elementItem){
        var parent = getParentPane(elementItem);
        var buttons = parent.getElementsByTagName('INPUT');
        for(i=0;i<buttons.length;i++){
            if(buttons[i].getAttribute('sel') == 1){
                buttons[i].setAttribute('sel',0);
                buttons[i].className = null;
            }
        }        
        var esels = false;
        var titles = document.getElementById('dim_browser_title').getElementsByTagName('td');
        for(i=0;i<titles.length;i++)
            if(titles[i].id && titles[i].id.indexOf('_title') > -1)
                if(titles[i].innerHTML == 'No selection'){
                    esels = true;
                    break;
                 }
        if(!esels)
            window.parent.sm.NewSeries();
    }
}
