
// Global variable for the Solution Selector's object
var solution_selector;

// Create an object for the Solution Selector
new solution_selector();

/* === Class: Solution Selector === */
function solution_selector() {
	// Store this object in its global
	solution_selector = this;
	
	// These variables keep track of scheduled timeouts and currently open selectors and folders
	this.current_timeout = null;
	this.current_selector = null;
	this.current_unfold = null;
	
	/* === Method: Show === */
	// Opens the specified selector and clean up any other open selectors
	this.show = function(id) {
		// Cancel any scheduled hide calls
		if(this.current_timeout)
			clearTimeout(this.current_timeout);
		
		// If another selector is currently opened, close it
		if(this.current_selector && this.current_selector !== id)
			this.hide(this.current_selector);
		
		// Open the specified selector
		document.getElementById(id).style.display = 'block';
		
		// Store the specified selector's id
		this.current_selector = id;
	}
	
	/* === Method: Leave === */
	// Starts a timeout to close the specified selector
	this.leave = function(id) {
		// Cancel any scheduled hide calls
		if(this.current_timeout)
			clearTimeout(this.current_timeout);
		
		this.current_timeout = setTimeout(
			function() {
				solution_selector.hide(id);
			}
		, 1000);
	}
	
	/* === Method: Hide === */
	// Hides and folds the specified selector
	this.hide = function(id) {
		// Fold any open folders
		if(this.current_unfold)
			document.getElementById(this.current_unfold).className = '';
		
		// Hide the specified selector
		document.getElementById(id).style.display = 'none';
	}
	
	/* === Method: Unfold === */
	// Unfolds the specified folder
	this.unfold = function(id) {
		// If another folder is currently opened, fold it
		if(this.current_unfold && this.current_unfold !== id)
			document.getElementById(this.current_unfold).className = '';
		
		// Open the specified folder
		document.getElementById(id).className = 'unfolded';
		
		// Store the specified folder's id
		this.current_unfold = id;
		
		document.getElementById(id).blur();
	}
}

