/*javascript functions to handle tabs, linked to the ajax stuff. 
*/
var sideTab_selectedTab = "";//globals
var tabBrowseData = new Object();
var scrollHelpText = "Use arrows to scroll ";
function sideTab_selectTab(tableID,tabID,params){/*
	Handle the click event. 
        */
        if(sideTab_selectedTab!=""){
                setDivIDClass(sideTab_selectedTab,"sideTab_backgroundedTab");
        }
        setDivIDClass(document.getElementById(tabID).parentNode.id,"sideTab_foregroundedTab");
        sideTab_selectedTab=document.getElementById(tabID).parentNode.id;
        
        params=params+"&maxDivHeight="+(15*tabBrowseData[tableID+"_maxRows"]+32+tabBrowseData[tableID+"_maxRows"]);//pass into server in case it's interested in the div height to use..
        ajax_getUrl(tabBrowseData[tableID+"_targetURL"],params,tabBrowseData[tableID+"_contentDivID"],'get',-1);//NOTE: overridding whatever passed in and setting for no history on tab changes.  If re-enabling you need to fix the tab select part which no workie when going backwards :(
}
function sideTab_scroll(direction,tableID){
    /*scroll the tabs. */
    scrollHelpText="";//stop showing the help text once they've clicked an arrow once...
    tbl=document.getElementById(tableID);
    if(tbl){
	var namesArrayName=tableID+"_names";
        var paramsArrayName=tableID+"_params";
        var mapsArrayName=tableID+"_mappings";
	var displayedRows=tabBrowseData[tableID+"_maxRows"];

        var datalen=tabBrowseData[namesArrayName].length;
        var currStart=tabBrowseData[mapsArrayName][0];
        //var currEnd=currStart+displayedRows-1;//0 offest.
        var currEnd=currStart+(tbl.rows.length-3);
       	//alert(currStart+" "+currEnd+" "+currEnd);

	if((direction=="up" && currStart>0) || (direction=="down" && currEnd<datalen-1)){//anywhere to browse to?  shouldn't be called if not, but doesn't hurt to check.

	        if(direction=="down"){
        	   	var newEnd=currEnd+displayedRows;//+1 let the lastrow start out the next scroll set so it has a little continuity.
           		if(newEnd>datalen)newEnd=datalen;
            		var newStart=newEnd-displayedRows;
                        if(newStart<0)newStart=0;
        	}else{//going up...
            		var newStart=currStart-displayedRows+1;
            		if(newStart<0)newStart=0;
            		var newEnd=newStart+displayedRows;
                        if(newEnd>datalen)newEnd=datalen;
        	}
		
		//Remove all existing tabs, then add new ones back in.  
		for(var i=tbl.rows.length-2;i>0;i--){//leave the first and last rows.
	                tbl.deleteRow(i);
        	}
		sideTab_addTabs(tableID,newStart,newEnd);

		sideTab_setScrollRows(tableID,datalen,newStart,newEnd);
       
		//Select the first tab..
        	tabID="sideTab_"+tableID+"_"+newStart;//The addTabs function sets the id of 1st data row to be the start index.
		sideTab_selectTab(tableID,tabID,tabBrowseData[paramsArrayName][tabBrowseData[mapsArrayName][0]]);
	
	}
    }
}


function sideTab_getSelectedTab(){//returns the currently selected tab
	return sideTab_selectedTab;
}


/*util*/
function setDivIDClass(divID,newClass){
    div=document.getElementById(divID);
    if(div){
            div.className=newClass;
    }
}

function sideTab_loadArrays(tableID,selectedTab){
    /*build a tab browse thingy, fill with array data from the tabBrowseObj (must exist already and is set from php)
        
	selectedTab is the index into the data arrays (ie- the first row is 0)

	see the php func for details and usage
    */
    destDivID=tabBrowseData[tableID+"_destDivID"];
    var dest=document.getElementById(destDivID);
    var tbl=document.getElementById(tableID);
    var contentDiv=tabBrowseData[tableID+"_contentDivID"];
    if(dest){
        if(tbl){//We can skip creating the table and 1st/last rows and just use what ever is here.  Remove all data rows first though
            for(var i=tbl.rows.length-2;i>0;i--){//leave the first and last rows.
                tbl.deleteRow(i);
            }
        }else{//create a new one
            //clear out anything that may be in the dest div
            dest.innerHTML="";
            var tbl=document.createElement("table");
            tbl.className="sideTab_table";
            tbl.cellSpacing="0";
	    tbl.id=tableID;
                                    
            tbl.insertRow(0);//This will either be a scroll row or a filler row along with the big multi row content div.
            //Insert the cells and define as appropriate.
            tbl.rows[0].insertCell(0);
            tbl.rows[0].insertCell(1);
            
            //set up the content area.
            tbl.rows[0].cells[1].className="sideTab_contentTD";
            //not sure I like setting the height in here like this...
            tbl.rows[0].cells[1].innerHTML="<div id='"+contentDiv+"' class='sideTab_contentArea'>&nbsp;</div>";
            
            //Add the last row which will also either be a scroll row or filler as needed.
            tbl.insertRow(1);
            tbl.rows[1].insertCell(0);
            tbl.rows[1].insertCell(1);
            
            tbl.rows[0].cells[0].align='right';
            tbl.rows[1].cells[0].align='right';
            dest.appendChild(tbl);//Set the table into the Dest Div
        }
	
	tbl.rows[0].cells[1].rowSpan=2;//This is temporary until we add the rows below..
        
        //Get the data from the tabBrowseObj.. assume success.
        var names=tabBrowseData[tableID+"_names"];
        var params=tabBrowseData[tableID+"_params"];
        
        var datalen=names.length;
        
        var start=selectedTab-1;//If a selected Tab was passed, make it the 2nd down.  Purely so it looks better.  No other reason.
        if(start<0)start=0;
        
        //var end=tabBrowseData[tableID+"_maxRows"];
        var end=start+tabBrowseData[tableID+"_maxRows"];
        
        if(end>datalen)end=datalen;
        
        sideTab_setScrollRows(tableID,datalen,start,end);

	//Now add in all the content rows.
	sideTab_addTabs(tableID,start,end);

	//Select a tab
	tabID="sideTab_"+tableID+"_"+(selectedTab);
	sideTab_selectTab(tableID,tabID,params[selectedTab]);
    }//..dest
}
function sideTab_addTabs(tableID,start,end){
	var tbl=document.getElementById(tableID);
	if(tbl){
		j=0;
		for(i=start;i<end;i++){
			row=tbl.insertRow(tbl.rows.length-1);
			index=row.rowIndex;
			row.insertCell(0);
			row.cells[0].className="sideTab_tableTD";
			title=tabBrowseData[tableID+"_names"][i];
			DisplayTabName=title;
                        
                        row.cells[0].id=tableID+"_td_"+index;//Note,the td needs an id so the select tab function can set it's class
			
			if(DisplayTabName.length>tabBrowseData[tableID+"_textCutOffLen"]){
			    DisplayTabName=DisplayTabName.substring(0,tabBrowseData[tableID+"_textCutOffLen"]-3)+"...";
			}
			row.cells[0].className='sideTab_backgroundedTab';

			//This used to use index insead of i to build the id, this should be correct now... need i so that the passed 'selectedTab' can reference the right row
                        row.cells[0].innerHTML="<div title='"+title+"' id='sideTab_"+tableID+"_"+i+"' onClick=\"sideTab_selectTab('"+tableID+"',this.id,'"+tabBrowseData[tableID+"_params"][i]+"');\">"+DisplayTabName+"</div>";

			tabBrowseData[tableID+"_mappings"][j]=i;//save off where in the data array we are...
			j++;
		}
		tbl.rows[0].cells[1].rowSpan=tbl.rows.length;
	}
}
function sideTab_setScrollRows(tableID,datalen,currStart,currEnd){
    /*this sets up the scrollRows appropriately for the passed tab table.  Assumes they already exists.*/

    var tbl=document.getElementById(tableID);
    tabWidth=tabBrowseData[tableID+"_tabWidth"];
    if(tbl){
        if(currStart>0 || currEnd<datalen){//need add scrolling stuff...
		
            if(currStart>0){//we're scrolled down a little to start out with..
                tbl.rows[0].cells[0].className="sideTab_scrollEnabled";
                tbl.rows[0].cells[0].innerHTML="<div class='smalItal' align='right' style='display:inline;vertical-align:bottom;'>"+scrollHelpText+"</div><div style='display:inline;' onClick=\"sideTab_scroll('up','"+tableID+"');\"><img src='images/NavArrowUp.gif' width='16' height='16' alt='up'></div>";
            }else{
                tbl.rows[0].cells[0].className="sideTab_scrollDisabled";
                tbl.rows[0].cells[0].innerHTML="<div>&nbsp;</div>";
            }

            if(currEnd<datalen){//we're scrolled up a little to start out with..
                tbl.rows[tbl.rows.length-1].cells[0].className="sideTab_scrollEnabled";
                tbl.rows[tbl.rows.length-1].cells[0].innerHTML="<div class='smalItal' align='right' style='display:inline;vertical-align:top;'>"+scrollHelpText+"</div><div style='display:inline;' onClick=\"sideTab_scroll('down','"+tableID+"');\" style='width:"+tabWidth+";'><img src='images/NavArrowDown.gif' width='16' height='16' alt='up'></div>";
            }else{
                tbl.rows[tbl.rows.length-1].cells[0].className="sideTab_fillerCell";
                tbl.rows[tbl.rows.length-1].cells[0].innerHTML="<div style='width:"+tabWidth+";height:100%;'>&nbsp;</div>";
            }
        }else{//just set the tabs for filler/pad
                tbl.rows[tbl.rows.length-1].cells[0].className="sideTab_fillerCell";
                tbl.rows[tbl.rows.length-1].cells[0].innerHTML="<div style='width:"+tabWidth+";height:100%;'>&nbsp;</div>";
		tbl.rows[0].cells[0].className="sideTab_fillerCellTop";
                tbl.rows[0].cells[0].innerHTML="<div style='width:"+tabWidth+";'>&nbsp;</div>";

        }
    }
}
