/**
* Create a clickable table which highlights when you hover over it and responds to non-mouse control
* Note - this uses and requires cwa.tablehover.js an adaptation of the jQuery tableHover plugin
* Note - JS function set up uses namespacing method from http://www.dustindiaz.com/namespace-your-javascript/
* @author Sunil Jolly
* @version 1
**/
var StrandTable = function() {
	
	var tableHover;
	var focusAttempts = 0;
	var currentlyFocusedCell;

	//if strand chart is in a thickbox (iframe) then try and set focus
	function ensureFocusIsSetIfRequired() {
		var inFrameMode = (top.location != location);
		if (inFrameMode) ensureFocusIsSet();		
	}
	
	//calls method to try and set focus
	function ensureFocusIsSet() {
		setTimeout("StrandTable.setElementFocusRedirect()", 100);
	}

	//try and focus on the first table element in the pop up
	//if successfull then stop, otherwise keep going
	function setElementFocus() {
		try{
			document.getElementById('first-table-element').focus();
		} catch(error) {
			if (this.focusAttempts++ < 10) {
				this.ensureFocusIsSet();
			}
		}
	}
	
	//create table with hover effects
	function initTableHighlights() {
		var tableHoverConfig = {colClass: 'hover', 
								cellClass: 'hovercell', 
								clickClass: 'click', 
								colClass: 'hovercol',
								bodyRows: true, 
								clickMethod: tableCellClicked, 
								ignoreCols: [0]}
		
		$('#strand').tableHover(tableHoverConfig); 
		createFocusableCells();
	}
	
	//use focus events to replicate mouse events
	function createFocusableCells() {
		
		//'fake' a mouse rollover when a field is given focus
		$("td, th").focus(function () {
			$(this).trigger("mouseover");
			$(this).addClass("has-focus");
			currentlyFocusedCell = $(this);
		});
		
		//'fake' a rollOut
		$("td, th").blur(function () {
			$(this).trigger("mouseout");
			$(this).removeClass("has-focus");
			currentlyFocusedCell = undefined;
		});
	
		//if 'enter' key is pressed while focus is given to a cell, then 'fake' a click
		$(document).keyup(
			function(e) {
				if(e.keyCode == 13 && currentlyFocusedCell) {
					currentlyFocusedCell.trigger("click");
				}
			}
		);
	}
	
	//remove read more links, user with js enabled clicks cells instead
	function hideReadMoreLinks() {
		$('.read-more').hide();
	}
	
	//callback from tablehover js. Called when user clicks on a cell
	//@param x is a table row
	//@param y is a table column
	function tableCellClicked(x, y) {
		if(!$("#"+x+"-"+y).is('.no-clicking'))
			parent.document.location = $("#"+x+"-"+y+" .read-more").attr('href');
	}	

	return {

		//initialiser
		init : function() {
			initTableHighlights();
			hideReadMoreLinks();
			ensureFocusIsSetIfRequired();
		}, 
		
		//hack - setTimeOut loses the scope of this object, 
		//so need to redirect it back into the object scope by using a public method
		setElementFocusRedirect : function() {
			setElementFocus();
		}
		
	};
	
}();

