/**
 * Folding script for tiles
 *
 * This script collects all HTML elements with either one of the
 * classes 'folded' and 'unfolded' and folds/unfolds their
 * 'tile-content' element whenever their header element is clicked.
 *
 * @author: Johan Fredrik Varen
 */

document.observe("dom:loaded", function() {
	$$('#placeholder-left .folded').each( function(tile) {
		embedFolding(tile, $('placeholder-left'));
	});

/*
	$$('#placeholder-left .unfolded').each( function(tile) {
		embedFolding(tile, $('placeholder-left'));
	});

	$$('#placeholder-right .folded').each( function(tile) {
		embedFolding(tile, $('placeholder-right'));
	});

	$$('#placeholder-right .unfolded').each( function(tile) {
		embedFolding(tile, $('placeholder-right'));
	});
*/
}); 

function embedFolding(tile, scope) {

	// Subscribe to folding events in the scope if auto-folding is enabled (see site.js for the subscribe() function)
	if (tile.hasClassName('autofold')) {
		//tile.subscribe('folding:fold', scope);
		tile.subscribe('folding:unfold', scope);
	}
	
	// Get the 'tile-content' element from the tile
	var tileContent = tile.select('.tile-content')[0];

	// Get the header (h3) element from the tile
	var tileHeader = tile.select('h3')[0];

	if (tileHeader == null) {
		return;
	}

	tileHeader.observe('click', function(event) {
		// Do not react to clicks while effects are still running 
		if (Effect.Queues.get('global').effects.length > 0) {
			return;
		}					

		if (tile.hasClassName('folded')) {
			tile.fire('folding:unfold');
		} else {
			//tile.fire('folding:fold');
		}
	});

	tile.observe('broadcast:unfold', function(event) {
		// Unfold tile
		//tileContent.blindDown({duration: 0.3});
		//tile.addClassName('unfolded');
		//tile.removeClassName('folded');
		event.stop(); // No need to bubble this further up
	});
					
	tile.observe('broadcast:fold', function(event) {
		// Fold tile
		//tileContent.blindUp({duration: 0.3});
		//tile.addClassName('folded');
		//tile.removeClassName('unfolded');
		event.stop(); // No need to bubble this further up
	});
}