//set up some global tab variables here
var nTabs=4; //n of tabs


/*-----------------------------------------------------------
    Toggles element's display value
    Input: any number of element id's
    Output: none 
    ---------------------------------------------------------*/
function toggleDisp() {
     var w=document.getElementById("wrapper");
     var h=w.style.height;
     w=w.style.width;
     w=w.substring(0,w.length-2); //cut off px
     h=h.substring(0,h.length-2);
      w=parseInt(w); //convert to integer
      h=parseInt(h);
    for (var i=0;i<arguments.length;i++){
        var d = $(arguments[i]);
        if (d.style.display == 'none'){
            d.style.display = 'block';
            d.style.overflow='auto';
	    d.style.height=Math.round(h*.94)+ 'px';
            d.style.width=Math.round(w*.98) + 'px';
	}
        else
            d.style.display = 'none';
    }
}

/*-----------------------------------------------------------
    Toggles tabs - Closes any open tabs, and then opens current tab
    Input:     1.The number of the current tab
                    2.The number of tabs
                    3.(optional)The number of the tab to leave open
                    4.(optional)Pass in true or false whether or not to animate the open/close of the tabs
    Output: none 
    ---------------------------------------------------------*/
function toggleTab(num,numelems,opennum,animate) {
    if ($('tabContent'+num).style.display == 'none'){
         var color=document.getElementById("wrapper").style.backgroundColor;
        for (var i=1;i<=numelems;i++){
            if ((opennum == null) || (opennum != i)){
                var temph = 'tabHeader'+i;
                var h = $(temph);
                if (!h){
                    var h = $('tabHeaderActive');
                    h.id = temph;
                }
                var tempc = 'tabContent'+i;
                var c = $(tempc);
                 c.style.border='none';
                 c.style.backgroundColor=color; //same as wrapper color
                if(c.style.display != 'none'){
                    if (animate || typeof animate == 'undefined')
                        Effect.toggle(tempc,'blind',{duration:0.5, queue:{scope:'menus', limit: 3}});
                    else
                        toggleDisp(tempc);
                }
            }
        }
        var h = $('tabHeader'+num);
        if (h)
            h.id = 'tabHeaderActive';
        //h.blur();
        var c = $('tabContent'+num);
        c.style.marginTop = '2px';
        if (animate || typeof animate == 'undefined'){
            Effect.toggle('tabContent'+num,'blind',{duration:0.5, queue:{scope:'menus', position:'end', limit: 3}});
        }else{
            toggleDisp('tabContent'+num);
        }
    }
}

//figure out what the current tab number is
function getCurrentTab(){
  for(var num=1;num<=nTabs;num++){
     var d = $('tabContent'+num);
     if (d.style.display == 'block'){
       return(num);         
     }
  }
}

//set up the Filters tab. This makes an ajax request to a perl script
//filters are derived from a configuration file
function initColeEzTabs(){
  try{
   url="/cgi-bin/1605b/tabs/EZFilters.pl";   
  var ajax1=new Ajax.Updater({success: 'EZFilters'},url);
  url="/cgi-bin/1605b/filterData/dumpFilterData.sh";
  var pars="";
   var myAjax = new Ajax.Request(url, 
				 {method: 'get', 
				  parameters: pars,
				  onComplete: getTabExtraData
				 });
  }catch(err){alert("initColeEzTabs: "+err);}
}

//filterCheckClass
//this holds data used to determine if a filter selection is valid for the
//chosen states
//data[i][0] is ith state code, [i][j] is state i and fVar typecd j
function FilterCheckClass(fVar,data){
  this.fVar=fVar; //the fVar that this pertains to
  this.data=data;
}


//global var
filterCheck=new Array(); //hold filterCheck instances

//handle transfer of data for making tabs 'aware of' available data
//filterFORTYPGRP[i][0] is ith state code, [i][j] is state i forest type code
function getTabExtraData(request){
  var vSep='@';
  var filterR=request.responseText.split('\n');
  var filterDump=new Array
    for(var i=0;i<filterR.length;i++){
      filterDump[i]=filterR[i].split(vSep); 
    }
 
  var nStates=parseInt(filterDump[0][1]); //should be n of states
  var start=1; //first row of each group is a header
  while(start<filterDump.length){
    var data=new Array();
    var end=start+nStates;
    var fVar=filterDump[start-1][0];
    for(var i=start;i<end;i++)
      data.push(filterDump[i]);
    start=end+1;
    filterCheck.push(new FilterCheckClass(fVar,data));
  }//end while
  //alert("filterCheck.length="+filterCheck.length);
}

