/**
JanYii Grid JavaScript

This is the code for the the Janyii table grid.
The main purpose of the code is to allow the whole grid to be scrolled horizontally,
but for the table header to remain fixed when the table is scrolled vertically.

This codes requires JQuery and the jy_grid.css file.

The grid is made up of two tables.
The first table is a header table, the second a data table
Each tables is nested in a div, and the whole thing in an outer div. 
The structure of the HTML is shown in jy_grid.htm.

The two tables are pulled together as follows:
1. The horizontal scroll bars of the two tables are linked, so that scrolling one scrolls the other.
2. The header table column widths are resized to match the data column widths.

Note
The header table has one additional 'filler' column at its end.
 
**/


var savedScrollLeft = 0;			//Save position of horiz scroll bar
var currentScrollLeft = 0;			//Current position of horiz scroll bar
var widths = new Array();			//Array of data column widths
var dataColumnCount = 0;			//Count of data columns
var selectedGridUpdateObj = null;	//Currently clicked (highlighted) row object

$(document).ready(function(){

	//Add zebra classes to band table
	if(document.getElementById('jy_grid_table')){
		$('#jy_grid_table > tbody > tr:even').addClass('jy_grid_even_row');
		$('#jy_grid_table > tbody > tr:odd').addClass('jy_grid_odd_row');	
		$('.jy_grid_row_error').each(function(index) {	
			//Set backround of error rows to standard error colour
			this.style.background = '#ffeeee';
		});
	}	  

	//Link the scroll bars together
  	$("#jy_grid_1_div").scroll(function() {
		currentScrollLeft = $("#jy_grid_1_div").scrollLeft();  
	  
	  	if(currentScrollLeft != savedScrollLeft){
	    	savedScrollLeft = currentScrollLeft;
	    	$("#jy_grid_2_div").scrollLeft(currentScrollLeft);
	    }	
  	}); 
  	
  	$("#jy_grid_2_div").scroll(function() {
		currentScrollLeft = $("#jy_grid_2_div").scrollLeft();  
	  	if(currentScrollLeft != savedScrollLeft){
	    	savedScrollLeft = currentScrollLeft;
	    	$("#jy_grid_1_div").scrollLeft(currentScrollLeft);
	    }	
  	}); 
 
 
  	//Opera reports a width of 70 (i.e. the min width) as expected, but is ignoring this when rendering the 
 	 //column. This fix resets the the width OK.
  	if($.browser.opera){
	  //Test opera fix  
	   $("#jy_grid_table_row_1 > td > div").each(function(index) {
	    	//alert(index + ' '+ $(this).width());
	    	var cellWidth =  $(this).width();
	    	if(cellWidth <= 70)
	    		$(this).width(70);
		})
  	}
  
  	//Get columen widths of first data row
  	$("#jy_grid_table_row_1 > td").each(function(index) {
    	 widths[index] = $(this).width();
    	 dataColumnCount = index;
	});
	
 	//Set widths of heading row to match widths of data row.
 	//Note the use of the dataColumnCount, this is needed because the heading row has one additional filler column.
  	$("#jy_grid_head_row > th > div").each(function(index) {
    	//alert(index + ' '+ $(this).width());
    	if(index <= dataColumnCount)
    	{
    	  	$(this).width(widths[index]);
    	 } 
	});
	
});

//Redirect to
function jyRedirectTo(url){
	window.location = url;	
}	

//Highlight the clicked data row
function jyRowClick(obj){
	if(selectedGridUpdateObj)
		selectedGridUpdateObj.className = '';
	selectedGridUpdateObj = obj;
	$('#jy_grid_table > tbody > tr:even').addClass('jy_grid_even_row');
	$('#jy_grid_table > tbody > tr:odd').addClass('jy_grid_odd_row');
	obj.className = 'jy_grid_highlight_row';
}	

//Handle request to delete onject
function jyDelete(obj, str){
	var agree=confirm(str);
	if (agree){
		var id = obj.id.replace('jy_del_id_', '');
	}
}	

