addEvent( window, 'load', initNotes );

var notes = [];
notes.change = function() 
{
	var keep = false;
	for ( var i = 0; i < this.length; i++ )
	{
		var note = this[i];
		if ( note.movement == 'shrink' && note.length > 10 )
		{
			note.length--;
			keep = true;
		}
		else if ( note.movement == 'grow' && note.length < note.textList.length )
		{
			note.length++;
			keep = true;
		}
		else 
		{
			note.movement = '';
		}
		note.speak();
	}
	if ( ! keep ) { clearInterval( this.interval ); this.interval = null; }
}
notes.interval = null;
function initNotes()
{
    var list = document.getElementsByTagName( 'P' );
	for ( var x in list )
	{
		if ( sci.hasClass( list[x], 'note' ) && list[x].firstChild.nodeName == '#text' )
		{
			var note = list[x];
			var textList = note.firstChild.nodeValue.split( / / );
			if ( textList.length > 10 )
			{
			    note.index = notes.length;
				notes[notes.length] = note;
				note.notes = notes;
				note.textList = textList;
				note.length = 10;
				note.movement = '';
				note.speak = function() 
				{ 
					var end = this.length == this.textList.length ? '' : '...';
					this.innerHTML = ( this.textList.slice(0,this.length).join( ' ' ) + end );
				}
				
				note.mIn = function() 
				{ 
					this.movement = 'grow'; 
					if ( ! this.notes.interval ) 
					{ 
						this.notes.interval = setInterval( 'notes.change()', 100 )
					}
				}
				note.mOut = function() 
				{ 
					this.movement = 'shrink'; 
					if ( ! this.notes.interval ) 
					{ 
						this.notes.interval = setInterval( 'notes.change()', 100 )
					}
				}
				addEvent( note, 'mouseover', function() { this.mIn() } );
				addEvent( note, 'mouseout', function() { this.mOut() } );
				note.speak();
			}	
		}
	}
}
