jQuery(document).ready(function() {

	// Init Variables
	var newData, newHeader, row, col, type, length, numRows, rowName, width, fxOut, fxIn, info; 
	
	// Protocol Flowchart
	jQuery('#protocol > ul').not(':first').hide();
	jQuery('#protocol a.flow').live('click', function(){
		type = jQuery(this).attr('href');
		type = type.replace('http://www.cokidswithbraininjury.com/tbi-identification-protocol/','');
		type = type.replace('http://cokidswithbraininjury.com/tbi-identification-protocol/','');
		newData = jQuery('#protocol '+type).html();
		jQuery(this).removeClass('flow').addClass('flowed')
			.parent('li').append('<ul>'+newData+'</ul>').hide().slideDown('slow');
		return false;
		
	});
	jQuery('#protocol a.flowed').live('click', function(){
		jQuery(this).removeClass('flowed').addClass('flow')
			.parent().find('li').slideUp('slow', function(){ jQuery(this).remove(); });
		return false;
	});
	/*
	jQuery('#protocol a.doc').each(function(i, item){
		jQuery(this).parent().append('<span> &larr; Download PDF document</span>');
	});
	*/
	
	// The Matrix Scripting
	jQuery('#matrixdata').css('overflow','hidden');
	jQuery('#the-matrix table').hide();
		
	// Add links to matrixViewer
	jQuery('#matrixtitles').html('<ul></ul>');
	jQuery('#the-matrix td.heading').each(function(){
		jQuery('#matrixtitles ul').append('<li id="'+jQuery(this).parent().attr('id')+'"><h4>'+jQuery(this).find('h4').html()+'</h4></li>');
	});
	jQuery('#matrix-def li a').each(function(i,item){
		jQuery(this).attr('rel',parseInt(i+1));
	});
	// Add title
	jQuery('#matrixtitles').prepend('<h3>Domain</h3>');

	// Create on click links for matrixtitles
	jQuery('#matrixtitles li a').live('click', function(){
		rowName = jQuery(this).parent().parent().attr('id');
		row = jQuery('a[href$="'+rowName+'"]').attr('rel');
		col = jQuery('#mdata').attr('rel');
		
		// First link has no column set
		if (col == null) col = '2'; 
		
		updateMatrix(row,col);
		
		return false;
	});
	
	// Control clicks!
	jQuery('div.controls a').live('click', function() {
		type = jQuery(this).attr('id');
		row = jQuery('#mtitles').attr('rel');
		//col = jQuery('#mdata').attr('rel');
		length = jQuery('#the-matrix thead th').length;
		numRows = jQuery('#the-matrix tr').not('.titles').length;
		width = jQuery('#matrixdata').width();
		height = jQuery('#matrixdata').height();
		
		switch( type ) {
			case 'up':
				row--;
				if ( row < 1 ) {
					row = numRows;
				}
				break;
			
			case 'down':
				row++;
				if ( row > numRows ) {
					row = 1;
				}
				break;
			
			case 'prev':
				col--;
				if ( col < 2 ) {
					col = length;
				}
				break;
			
			case 'next':
				col++;
				if ( col > length ) {
					col = 2;
				}
				break;
		}
		
		updateMatrix(row,col);		
		return false;
	});
	
	// Create list of links for columns
	/*
	jQuery('#matrixviewer').prepend('<ul id="collinks"></ul>');
	jQuery('thead th').not(':first').each(function(i) {
		jQuery('ul#collinks').append('<li><a id="'+parseInt(i+2)+'"href="#">'+jQuery(this).html()+'</a></li>');
	});
	// Drop down and show links on click
	jQuery('ul#collinks a').live('click', function(){

		// Slide up links
		jQuery(this).parent().parent().slideUp();

		row = jQuery('#mtitles').attr('rel');
		col = jQuery(this).attr('id');
		updateMatrix(row,col);
		return false;
	});
	jQuery('h3 a#title')
		.live('click', function(){ jQuery('#collinks').slideToggle(); return false; });
	
	*/
	// Links from definition list
	jQuery('#matrix-def a').live('click', function(){
		row = jQuery(this).attr('rel');
		col = 2;
		jQuery.scrollTo('#the-matrix');
		updateMatrix(row,col);
		return false;
	});
	
}); 
function updateMatrix(row, col) {
	
	// Retrieve and add the new data
	//newData = jQuery('tr:nth-child('+row+')').not('.titles').find('td:nth-child('+col+')').html();
	//newHeader = jQuery('#the-matrix thead th:nth-child('+col+')').html();
	//jQuery('#matrixdata').html('<h3><a href="#" id="title">'+newHeader+'</a></h3>'+newData);
	newData = '';
	jQuery('#the-matrix thead th').each(function(i,item){
		// Skip the first column
		if ( i != 0 ) {
			newData += '<h3>'+jQuery(item).html()+'</h3>'
					+jQuery('tr:nth-child('+row+')').find('td:nth-child('+parseInt(i+1)+')').html();
		}
	});
	jQuery('#matrixdata').html(newData);
	
	
	// Update defnition info
	info = jQuery('a[rel$="'+row+'"]').parent().parent().html();
	jQuery('#matrixinfo').html(info);
	
	// Update navigation buttons
	// Don't show up/down buttons to the unable browsers
	jQuery('#mtitles').remove();
	jQuery('#matrixtitles').prepend('<div id="mtitles" class="controls" rel="'+row+'"><a href="#" id="down">Down</a> <a href="#" id="up">Up</a><div>');	
	//jQuery('#matrixdata').prepend('<div id="mdata" class="controls" rel="'+col+'"><a href="#" id="prev">Prev</a> <a href="#" id="next">Next</a><div>');
	
	// Update selected title
	jQuery('#matrixtitles li.selected').removeClass('selected');
	jQuery('#matrixtitles li:nth-child('+row+')').addClass('selected');
}