function addLoadEvent( func ) 
{
    var oldonload = window.onload;
    if ( typeof window.onload != 'function' )
        window.onload = func;
    else 
    {
        window.onload = function() 
        {
            oldonload();
            func();
        }
    }
}

/**
 * Determine whether a node's text content is entirely whitespace.
 *
 * @param nod  A node implementing the |CharacterData| interface (i.e.,
 *             a |Text|, |Comment|, or |CDATASection| node
 * @return     True if all of the text content of |nod| is whitespace,
 *             otherwise false.
 */
function isAllWhiteSpace( nod )
{
  // Use ECMA-262 Edition 3 String and RegExp features
  return !(/[^\t\n\r ]/.test(nod.data));
}

/**
 * Determine if a node should be ignored by the iterator functions.
 *
 * @param nod  An object implementing the DOM1 |Node| interface.
 * @return     true if the node is:
 *                1) A |Text| node that is all whitespace
 *                2) A |Comment| node
 *             and otherwise false.
 */

function isIgnorable( nod )
{
	return ( nod.nodeType == 8) || // A comment node
			( (nod.nodeType == 3) && isAllWhiteSpace(nod) ); // a text node, all ws
}
/**
 * Version of |nextSibling| that skips nodes that are entirely
 * whitespace or comments.
 *
 * @param sib  The reference node.
 * @return     Either:
 *               1) The closest next sibling to |sib| that is not
 *                  ignorable according to |is_ignorable|, or
 *               2) null if no such node exists.
 */
function nodeAfter( sib )
{
	while ( ( sib = sib.nextSibling ) ) 
	{
		if ( !isIgnorable( sib ) ) 
			return sib;
	}
	return null;
}


/* Sets up the homepage Help text which is shown/hidden on mouse over and enhance the skip flash movie link */
function prepareHomePage()
{
    var skipLink = $( 'skip-link' );
    if ( skipLink )
    {
        skipLink.onclick = function() 
        { 
            clearFlash(); 
            this.hide(); 
            return false; 
        };
        
        var flash = $( 'flash' );
        if ( flash && flash.style.visible != 'hidden' )
            skipLink.show();
        else
            skipLink.hide();
    }
    
    // HelpText
	var cssClassName = 'hide';
    var sidebar = $( 'sidebar-right' );
    var helpText = $( 'helper-text' );
    
    if ( sidebar && helpText )
    {
        sidebar.onmouseover = function() { helpText.removeClassName( cssClassName ); };
	    sidebar.onmouseout = function() { helpText.addClassName( cssClassName ); };
	}	
}
addLoadEvent( prepareHomePage );


/* Make sure any previous answers are hidden to prevent confusing interface */
function closeAllAnswers()
{
    var divAnswers = $$( 'div.answer' ); //getElementsByClassName( 'answer', 'div' );
    for ( var i=0; i < divAnswers.length; i++ )
        divAnswers[i].addClassName( 'hide' );
        
    var spanAnswers = $$( 'span.answer' );
    for ( var i=0; i < spanAnswers.length; i++ )
        spanAnswers[i].addClassName( 'hide' );
}

function prepareAnswersDisplay()
{
    var cssClassName = 'hide';
    var answerLinks = $$( 'a.answer' );
    
    for ( var i=0; i < answerLinks.length; i++ )
    {	
		var answerDiv = nodeAfter( answerLinks[i] );
		if ( answerDiv )
		{
		    answerLinks[i].onmouseover = function() 
			{ 
			    closeAllAnswers();
			    var sidebarAnswer = nodeAfter(this);
			    sidebarAnswer.removeClassName( cssClassName ); // The sidebar answer
			}
			answerLinks[i].onmouseout = function() 
			{ 
			    closeAllAnswers();
			}
	    }
	}
}
addLoadEvent( prepareAnswersDisplay );

/* Takes in an Element and either applies the BlinUp or Blindown Prototype effect */
function toggleBlindEffect( /* Element */ element )
{
    // Note: Elements without a class of cotainerOpen/Closed defaults to Opening the element which could confuse the user by requiring two clicks to get the expected result
	if( element.hasClassName( 'elementOpen' ) ) // Close the element
	{
        new Effect.BlindUp( element );
        element.addClassName( 'elementClosed' );
        element.removeClassName( 'elementOpen' );
	}
	else // Open the element
	{
        new Effect.BlindDown( element );
        element.addClassName( 'elementOpen' );
        element.removeClassName( 'elementClosed' );
	}
}
			
function prepareSidebarMenu()
{
    var menuHeading = $( 'sidebar-menu-header' );
    var container = $( 'menu-list' );
    
    if ( menuHeading && container )
    {        
        container.hide(); // Hide the menu items list, intitially
        menuHeading.style.cursor = 'pointer'; // Highlight element is clickable
        menuHeading.onclick = function() { toggleBlindEffect( container ); return false; };  
    }
}
addLoadEvent( prepareSidebarMenu );

// After the Flash intro has finished playing, actionscript calls this method to hide the movie
function clearFlash()
{
    clearSkipLink();
    var intro = $( 'flash' ); // Would it improve this method by passing the ID?
    if(intro)
        intro.style.visibility = 'hidden'; //intro.style.display = "none"; // use addClassName method instead
}

function clearSkipLink()
{
    var skipLink = $( 'skip-link' );
    if ( skipLink )
        skipLink.hide();    
}

function toggleSummaryLinkText( link, fullContent )
{
    // If the full content is hidden
    var linkText = ( fullContent.style.display == 'none' ) ? 'Show Full Profile' : 'Show Summary';
    link.update( linkText );
}

/* Takes in the ID of the full content container then hides it, 
    creates and shows a summary  then sets up a link to toggle 
    between showing the full content or just the summary 
    
    @param fullContentID - the ID of the element containing the full content
    @param summaryText - the text to be used in the summary paragraph
*/
function createSummary( /*string*/ fullContentID, /*string*/ summaryText )
{
    var fullContent = $( fullContentID );
    
    if ( fullContent )
    {
        // Hide full content
        fullContent.hide();
        
        // Create summary 
        var summaryContent = new Element( 'p'/*, { id : 'summary-cc' }*/ );
        summaryContent.update( summaryText );
        
        // Add more/less link and set up to display full content
        var link = new Element( 'p', { 'class': 'summary-link' } );
        toggleSummaryLinkText( link, fullContent );
        link.onclick = function() 
        { 
            fullContent.toggle();
            summaryContent.toggle();
            toggleSummaryLinkText( this, fullContent );
        }
        
        // Add summary elements
        fullContent.insert( { before : link } );
        fullContent.insert( { before : summaryContent } );
    }
}

function fundamentalSummaries()
{
    // Full content element, summary element
    createSummary( 'full-content-cc', 'Carolyn qualified as a chartered accountant in 1981 and moved to Hampshire shortly afterwards. She commenced her professional career with accountants Rothman Pantall &amp; Co. and became an equity partner in 1995...' );
    createSummary( 'full-content-tj', 'Tracey\'s career started as a senior tax officer with the Inland Revenue followed by a move into private practice as a Tax Manager with Rothman Pantall & Co...' );
}

addLoadEvent( fundamentalSummaries );

document.observe("dom:loaded", function()
{
  if ( document.body.id == "you-talk-we-listen" )
	{ 
		var qs = new Querystring();
		var answer_id = qs.get("a");
		
		if ( answer_id )
		{
			var answer = $( 'answer_' + answer_id );
			if ( answer )
				answer.removeClassName('invisible');
		}
  }
});
