// Show / hide vars
var dynTableRowArray = new Array();
var dynTableRowsVisible = new Array();
var dynAnimationSpeed = 250;

function toggleTableRow (evt)
{
	var requestLink;
	var baseRowID;
	var theRowID;
	
	// Stop the default event trigger from happening (e.g., loading a new page)
	evt.preventDefault();
	
	// Assume we're being called from jQuery, so grab the REL attribute from the link
	baseRowID = $(this).attr('rel');
	theRowID = baseRowID +'-dynamic';
	
	if (typeof(dynTableRowArray[theRowID]) == 'undefined')
	{
		requestLink = $(this).attr('href');
		
		// Insert the HTML for the row and begin an AJAX load
		$('tr#'+ baseRowID).after('<tr id="'+ theRowID +'" class="dynamic"><td colspan="9"><div class="dynamic"><img src="/images/ui/toggle-spinner-blue-bg.gif" alt="Loading..." width="18" height="18" align="top" />&nbsp; Loading information...</div></td></tr>');
		$('tr#'+ theRowID +' td div').load('/members/'+ requestLink, parseNewContent);
		
		// Do an initial grab of the height of the contents within, only once, if we haven't used this element before
		$('tr#'+ theRowID).show();
		dynTableRowArray[theRowID] = new Array();
		dynTableRowArray[theRowID].height = $('#'+ theRowID +' div.dynamic').height();	
		dynTableRowArray[theRowID].visible = false;
		dynTableRowArray[theRowID].animating = false;
		$('tr#'+ theRowID +' div.dynamic').height(0);
		$('tr#'+ theRowID).hide();
	}
	else if (dynTableRowArray[theRowID].animating)
	{
		return false;
	}
	
	dynTableRowArray[theRowID].animating = true;
	
	if (!dynTableRowArray[theRowID].visible)
	{
		dynTableRowsVisible[theRowID] = true;
		dynTableRowArray[theRowID].visible = true;
		
		$('tr#'+ theRowID).show();
		$('tr#'+ theRowID +' div.dynamic').animate({ height: dynTableRowArray[theRowID].height }, dynAnimationSpeed, function () {
			$('a[rel='+ baseRowID +']').addClass('active');
			dynTableRowArray[theRowID].animating = false;
			
			// This is a really hackish solution, as IE7 does not seem to recalculate the #content div height when it dynamically resizes
			if ($.browser.msie && $.browser.version == '7.0')
			{
				$('span.block-bottom').hide();
			}
		});
	}
	else
	{
		dynTableRowsVisible[theRowID] = false;
		dynTableRowArray[theRowID].visible = false;
		$('tr#'+ theRowID +' div.dynamic').animate({ height: 0 }, dynAnimationSpeed, function () {
			$('a[rel='+ baseRowID +']').removeClass('active');
			$('tr#'+ theRowID).hide();
			dynTableRowArray[theRowID].animating = false;
			
			if ($.browser.msie && $.browser.version == '7.0')
			{
				$('span.block-bottom').show();
			}
		});
	}
	
	event.returnValue = false;
	return false;
}

function parseNewContent ()
{
	// Code should ideally be refactored considering the code above; this is some copypasta to get it to at least a working state
	var theRowID = $(this).parent().parent('tr.dynamic').attr('id');
	
	if (!dynTableRowArray[theRowID].visible)
	{
		$('tr#'+ theRowID).show();
		$('tr#'+ theRowID +' div.dynamic').height('auto');
		dynTableRowArray[theRowID].height = $('#'+ theRowID +' div.dynamic').height();
		$('tr#'+ theRowID +' div.dynamic').height(0);
		$('tr#'+ theRowID).hide();
	}
	else
	{
		if (dynTableRowArray[theRowID].animating)
		{
			//$('tr#'+ theRowID +' div').stop();	// for future tidying, doing this will allow the "true" height anim to start earlier in some cases
			// The problem atm is that the callback to change the + / - indicator is too linked into the button, so it won't update if it loads really quickly
		}
		
		var startHeight = $('tr#'+ theRowID +' div.dynamic').height();
		$('tr#'+ theRowID +' div.dynamic').height('auto');
		dynTableRowArray[theRowID].height = $('#'+ theRowID +' div.dynamic').height();
		$('tr#'+ theRowID +' div.dynamic').height(startHeight);
		
		// Note this isn't blocked... it probably should be
		$('tr#'+ theRowID +' div.dynamic').animate({ height: dynTableRowArray[theRowID].height }, dynAnimationSpeed);
	}
}

function toggleTableDetail ()
{
	var theContainer = $(this).attr('rel');
	var containerHeight;
	
	// Check if we're opening or closing ^^
	if ($('.'+ theContainer).is(':visible'))
	{
		$('.'+ theContainer).animate({ height: 0 }, dynAnimationSpeed, function () {
			$('.'+ theContainer).hide();
		});
	}
	else
	{
		$('.'+ theContainer).show();
		$('.'+ theContainer).height('auto');
		containerHeight = $('.'+ theContainer).height();
		$('.'+ theContainer).height(0);
		$('.'+ theContainer).animate({ height: containerHeight }, dynAnimationSpeed);
	}
	
	
}

/* ======================================================================= */
function jsDebug (message)
{
	var EOL = false;
	var curDate = new Date();
	var curTimeText = '';
	
    if ($.support.rnNewLine)
        EOL = '\r\n';
    else if ($.support.rNewLine)
        EOL = '\r';
    else if ($.support.nNewLine)
        EOL = '\n';
    else
        EOL = ' ';
	
	curTimeText = padTime(curDate.getHours()) +':'+ padTime(curDate.getMinutes()) +':'+ padTime(curDate.getSeconds());
	
	if (!$('#debuggy').is(':visible'))
	{
		$('#debuggy-text').html('<strong>----- DEBUG CONSOLE -----</strong><br />'+ EOL);
		$('#debuggy').fadeIn('fast');
	}
	
	$('#debuggy-text').html($('#debuggy-text').html() +'<span style="font-size: .75em"><strong>['+ curTimeText +']</strong></span> '+ message +'<br />'+ EOL);
	$('#debuggy').scrollTop($('#debuggy-text').height());
}

function jsDebugHide ()
{
	$('#debuggy').fadeOut('fast', function(){ $('#debuggy-text').html(''); });
}

function padTime (timeText)
{
	if (timeText < 10)
	{
		return '0'+ timeText;
	}
	
	return timeText;
}
