










































































if(typeof(Control) == 'undefined')
	var Control = {};

Control.Expandable = Class.create();
Object.extend(Control.Expandable,{
	instances: [],
});
Object.extend(Control.Expandable.prototype,{
	initialize: function(nodeContainer,options){
		this.active = 0;
		this.effect = '';
		this.nodeContainer = $(nodeContainer);
		if (!this.nodeContainer) {
			console.debug("couldn't find list container '"+this.nodeContainer+"'");
			return;
		}
		this.options = {
			controlSelector: 'title',
			contentSelector: 'content',
			controlClassName: 'expandableControl',
			feedbackClassName: 'expandableFeedback',
			contentClassName: 'expandableContent',
			showClassName: 'expandableShow',
			hideClassName: 'expandableHide',
			hideOnLoadClassName: 'hide',
			showText: 'Expand',
			hideText: 'Hide',
			onEvent: 'click',
			hideOnLoadDefault: 0,
			displayControlLink: 1,
		};
		Object.extend(this.options,options || {});
		
		this.nodeControl = false;
		this.nodeContent = false;
		this.nodeFeedback = false;
		
		this.nodeControl = this.nodeContainer.getElementsBySelector(this.options.controlSelector).first();
		if (!this.nodeControl) {
			console.debug("couldn't get expandableControl using selector '" + this.options.controlSelector+"'");
			return;
		}
		//console.log("expandableControl("+this.options.controlSelector+")", this.nodeControl);
		
		// check that we haven't already initialised this as an expandable section
		if (this.nodeControl.hasClassName(this.options.controlClassName)) {
			console.debug("expandableControl has already been initialised");
			return;
		}
		
		this.nodeContent = this.nodeContainer.getElementsBySelector(this.options.contentSelector).first();
		if (!this.nodeContent) {
			console.debug("couldn't get expandableContent using selector '" + this.options.contentSelector+"'");
			return;
		}
		//console.log("expandableContent("+this.options.contentSelector+")", this.nodeContent);
		
		// add the button
		if (this.options.displayControlLink) 
		{
			var strNodeFeedback = "<div class='smallButtonsContainer'><a class='smallButton " + this.options.feedbackClassName + "'></a></div>";
			new Insertion.Top(this.nodeControl, strNodeFeedback);
			this.nodeFeedback = this.nodeControl.getElementsBySelector("." + this.options.feedbackClassName).first();
			if (!this.nodeFeedback) {
				console.debug("couldn't get expandableFeedback using selector '" + "." + this.options.feedbackClassName+"'");
				return;
			}
		}
				
		// allow the hide on load to be switched on if we see a given class name (e.g. 'hide') 
		// in the content node
		this.hideOnLoad = this.options.hideOnLoadDefault;
		if (this.nodeContent.hasClassName(this.options.hideOnLoadClassName)) {
			this.hideOnLoad = 1;
		}
		
		// add the required event behaviour
		if (this.options.onEvent == 'click') {
			this.nodeControl.observe('click', function(event){
				this.toggle();
			}.bindAsEventListener(this));
		}
		else if (this.options.onEvent == 'mouseover') {
			console.debug("adding mouseover event to nodeContainer", nodeContainer);
			this.nodeContainer.observe('mouseover', function(event){
				this.show();
			}.bindAsEventListener(this));
			
			console.debug("adding mouseout event to nodeContainer", nodeContainer);
			this.nodeContainer.observe('mouseout', function(event){
				this.hide();
			}.bindAsEventListener(this));
		}
		else {
			console.debug("couldn't recognise onEvent: " + this.options.onEvent);
			return;
		}
		
		// if all is well so far then we can add this instance to the global array
		Control.Expandable.instances.push(this);
		
		this.nodeControl.addClassName(this.options.controlClassName);
		this.nodeContent.addClassName(this.options.contentClassName);
		
		if (this.hideOnLoad) {
			console.debug("hideOnLoad");
			this.hide();
		}
		else {
			this.show();
		}
	},
	show: function() {
		this.nodeFeedback.removeClassName(this.options.showClassName).addClassName(this.options.hideClassName);
		this.nodeFeedback.update(this.options.hideText);
		
		// if the effect is already running then find out where it is so we can start 
		// going back from the same point
/*		var myFrom = 0;
		if (this.effect && this.effect.state == 'running') {
			myFrom = this.effect.currentFrame;
			console.debug("show: already an effect running - current frame is: ", myFrom);
		}
		
		console.debug("show: creating effect on node", this.nodeContent);
		this.effect = new Effect.BlindDown(this.nodeContent, 
						{
							duration: 0.5,
							queue: { position:'start', scope:('global'), limit: 1 },
							from: myFrom
						});
		console.debug("show: created effect", this.effect);*/
		
		this.active = 1;
		this.nodeContent.show();
	},
	hide: function() {
		this.nodeFeedback.removeClassName(this.options.hideClassName).addClassName(this.options.showClassName);
		this.nodeFeedback.update(this.options.showText);
		
/*		var myFrom = 0;
		if (this.effect && this.effect.state == 'running') {
			myFrom = this.effect.currentFrame;
			console.debug("hide: already an effect running - current frame is: ", myFrom);
		}
		
		console.debug("hide: creating effect on node", this.nodeContent);
		this.effect = new Effect.BlindUp(this.nodeContent, 
						{
							duration: 0.5, 
							queue: { position:'start', scope:('global'), limit: 1 },
							from: myFrom
						});
		console.debug("hide: created effect", this.effect);*/
		
		this.active = 0;
		this.nodeContent.hide();
	},
	toggle: function() {
		this.active ? this.hide() : this.show();
	}
});
if(typeof(Object.Event) != 'undefined')
	Object.Event.extend(Control.Expandable);
