LibManager.require( 'scheduler' );
LibManager.require( 'frame.scriptaculous.builder' );
LibManager.require( 'frame.scriptaculous.effects' );

/**
 * Attach the changelog expander
 */
DEfusion.attachChangelog = function() {
	var changelog = $( 'changelog' );

	if( changelog != 'undefined' ) {
		// remove the 'last' classname from the row
		$( changelog ).removeClassName( 'last' );
		// get the changelog contents
		var contents = changelog.getElementsByTagName( 'dl' )[0];
		// create the link
		var link = Builder.node( 'a', { 'href': '#', 'id': 'toggleChangelog' }, 'View changelog' );
		contents.parentNode.replaceChild( link, contents );
		
		var par = changelog;
		while( par.nodeName != 'TABLE' ) {
			par = par.parentNode;
		}

		DEfusion.insertAfter( 
			par.parentNode, 
			Builder.node( 'div', { 'id':'changelogDetail', 'class': 'changelog' }, 
				[ 
					contents,
					Builder.node( 'div', { 'class' : 'footer' } )
				]
			), 
			par
		);
		
		$( 'changelogDetail' ).hide();
		
		Event.observe( link, 'click', DEfusion.toggleChangeLog, false );
	}
}

DEfusion.toggleChangeLog = function(e) {
	//Element.toggle( 'changelogDetail' );
	var el = 	$( 'changelogDetail' );
	var a = 	$( 'toggleChangelog' );
	var text = 	'Close';
	if( Element.visible( el ) ) {
		Effect.BlindUp( el );
		text = 'View';
	} else {
		Effect.BlindDown( el );
	}
	var txtNode = document.createTextNode( text + ' changelog' );
	a.replaceChild( txtNode, a.childNodes[0] );
	Event.stop( e );
} 

LibManager.isLoaded(
	['Scheduler','Builder'],
	function() {
		Scheduler.schedule(
			'changelog', 
			'DEfusion.attachChangelog()'
		);
	}
);


