/*	HorizDynTable(id,viewSize,defaultCol,fixedCol)
	--------------------------------------------------------------------------------------------------
	makes a simple table a horizontaly dinamic one.
	viewSize= the number of columns to view at a time
	defaultCol= the default column from which we show viewSize columns initialy
	fixedCol=the column which should not be modified (this usualy contains horizontal labels)

	example of implementation:

	take special care not to name the object the same as the table id, this will cause an error in IE
	
	<table border="1" id="tableT1">
	<tr>
		<td>h</td>	<td>2</td>	<td>3</td>	<td>4</td>	<td>5</td>	<td>6</td>	<td>7</td>
	</tr>
	<tr>
		<td>e</td>	<td>2</td>	<td>3</td>	<td>4</td>	<td>5</td>	<td>6</td>	<td>7</td>
	</tr>
	<tr>
		<td>a</td>	<td>2</td>	<td>3</td>	<td>4</td>	<td>5</td>	<td>6</td>	<td>7</td>
	</tr>
	<tr>
		<td>d</td>	<td>2</td>	<td>3</td>	<td>4</td>	<td>5</td>	<td>6</td>	<td>7</td>
	</tr>
	</table>

	<script language="JavaScript">
	<!--
		t=new HorizDynTable('tableT1',3,3,1);	
	//-->
	</script>
	
	<a href="javascript:t.strafeLeft();">strafe left</a> 
	<a href="javascript:t.strafeRight();">strafe right</a>
*/
function HorizDynTable(id,viewSize,defaultCol,fixedCol){
		if (document.getElementById(id)){
			this.table=document.getElementById(id);
			
		}
		this.DOMLocations=new Array();
		this.DOMLocations['rows']=new Array();
		this.DOMLocations['cols']=new Array();
		this.DOMLocationsrows=new Array();
		this.colVis=new Array();
		
		this.regenDOMLocations=function (){
			//alert(this.table.firstChild.firstChild.nodeName);
			var curRow=1;
			for (bi=0;bi<this.table.childNodes.length;bi++ ){
				
				if (this.table.childNodes[bi].nodeName=='TBODY'){
					//alert(this.table.childNodes[bi].nodeName);
					this.tbody=this.table.childNodes[bi];
					
					for (ri=0; ri<this.tbody.childNodes.length; ri++ ){
					//cycles through the dom until it reaches the first row to index the column positions
						
						if (this.tbody.childNodes[ri].nodeName=='TR'){
							this.DOMLocations['rows'][curRow]=ri;
							curRow++;
							if (this.DOMLocations['cols'].length<1){
								curCol=1;
								for (ci=0; ci<this.tbody.childNodes[ri].childNodes.length; ci++ )	{
									
									if (this.tbody.childNodes[ri].childNodes[ci].nodeName=='TD' || this.tbody.childNodes[ri].childNodes[ci].nodeName=='TH'){

										this.DOMLocations['cols'][curCol]=ci;
										//alert('mapped column: ' + curCol + '=' +ci + this.tbody.childNodes[ri].childNodes[ci].innerHTML);
										curCol++

									}
									
								}
							}
							

						}
						
						
						//table.childNodes[1].childNodes[i].style.borderWidth=i;
					}
					//alert(this.DOMLocations['cols'])
					
					break;
				}
				
				
			}
			
		}
		
		this.getCell=function (row,col){
			//alert('test r: ' + row + ' c:' + col);
			if (this.DOMLocations['rows'][row]>=0 && this.DOMLocations['cols'][col]>=0 ){
			
				//alert('found mapping: r: ' + row + '=' + this.DOMLocations['rows'][row] + ' c: ' + col + '=' + this.DOMLocations['cols'][col]);
				return this.tbody.childNodes[ this.DOMLocations['rows'][row] ].childNodes[ this.DOMLocations['cols'][col] ];
			
			}else{
				//alert('error searching mapping: r: ' + row + '=' + this.DOMLocations['rows'][row] + ' c: ' + col + '=' + this.DOMLocations['cols'][col]);
				return false;
			}
			
		}

		this.getTotalRows=function(){
			return this.DOMLocations['rows'].length;
		}
		this.getTotalCols=function(){
			return this.DOMLocations['cols'].length;
		}

		this.setColVis=function(col,display){
			for (row=1;row<this.getTotalRows() ; row++){
				//alert('requesting: r:' + row+' c:'+col);
				if (cell=this.getCell(row,col))	{
					if (display==true){
						this.getCell(row,col).style.display='';
					}else{
						this.getCell(row,col).style.display='none';
					}
					this.colVis[col]=display;
				}
				
			}
									
		}

		this.getColVis=function(col){
			if (this.colVis[col]){
				return this.colVis[col];
			}else{
				return false;
			}
		}

		
		this.initTable=function(){
			
			if (!fixedCol){
				fixedCol=false;
			}
			if (!defaultCol){
				defaultCol=1;
			}
			if (!fixedCol){
				fixedCol=false;
			}
			if (!viewSize){
				viewSize=this.getTotalCols();
			}
			
			for (col=1;col<this.getTotalCols() ; col++){
					if ((col>=defaultCol && col<defaultCol+viewSize) || col==fixedCol){
						this.setColVis(col,true);
					}else{
						this.setColVis(col,false);
					}
			}
			

		}


		this.strafeLeft=function (){
			this.strafe('L');
			
		}
		this.strafeRight=function (){
			this.strafe('R');
			
		}

		this.strafe=function(LR){
	
				i=this.getFirstVisibleColumn();
					switch (LR)
					{
					case 'L': 
								for (ts=-1; ts<viewSize-1; ts++){
									//alert('found first visible col: '+i);
									this.setColVis(i+ts,true);
								}
								
								if (i-1>0 && i-1!=fixedCol){
									this.setColVis(i+(viewSize-1),false);
								}
						break;

					case 'R':
								if (i+viewSize < this.getTotalCols() && i!=fixedCol){
									this.setColVis(i,false);
								}
								
								for (ts=1; ts<=viewSize; ts++){
									//alert('found first visible col: '+i);
									if (i+ts < this.getTotalCols()){
									//	alert('set vis true: c'+ (i+ts) );
										this.setColVis(i+ts,true);
									}
									
								}
						break;
					}
					
		
		}

		this.getFirstVisibleColumn=function(){
			for (i=1;i<=this.getTotalCols();i++){
				if (this.getColVis(i) && i!=fixedCol){
					return i;
					break;
				}
			}
			return false;
		}

		
		this.getLastVisibleColumn=function(){
			for (i=this.getTotalCols();i>0;i--){
				if (this.getColVis(i) && i!=fixedCol){
					return i;
					break;
				}
			}
			return false;
		}

/*
		this.autoAttachControls=function(){
			lc=this.getLastVisibleColumn();
			//alert(lc);
			this.getCell(1,lc).style.color='blue';

			fc=this.getFirstVisibleColumn();
			//alert(fc);
			this.getCell(1,fc).style.color='red';

			strfleftctrl=document.createElement('a');
			strfleftctrl.appendChild(document.createTextNode('<'))
			//strfleftctrl.addEventListener('click',this.strafeLeft)


			this.getCell(1,fc).appendChild(strfleftctrl);
		}
*/
		this.regenDOMLocations();

		this.initTable();

		
}