/**
 *
 *	Base class for for creating open and close animations for hiding and revealing a div
 *	@class Toggle class for creating hide and reveal effect
 *	Usage: 
 *	<div class="toggle"><a href="#" onclick="new W.Toggle('div', 'toggle_div').activate(); return false;"><img src="images/arrow_down.gif" alt="More" width="5" height="11" border="0" id="toggle_div" /></a></div></p>
 *	<div class="more" id="div"></div>
 *
 *	@param {String | HTMLElement}	node 	DOM node that will hide/reveal
 *	@param {String}					link	Name of <img>
 *
 *	@requires W
 *	@requires W.Event
 *	@requires W.Dom
 *	@requires W.Tween
 *	@constructor
 */
W.Toggle = function(node, link)
{
	this.init(node, link);
};



W.Toggle.prototype = {
	
	node	: null,
	state	: null,
	link	: null,
	
	
	init : function(node, link)
	{
		var obj		= this;
		this.node 	= W.$(node);
		this.link	= W.$(link);
	},
	
	
	
	activate : function()
	{
		if (W.Dom.getStyle(this.node, 'height') != '1px') {
			this.state = 1;
			this.close();
		} else {
			this.state = 0;
			this.close_all();
			this.open();
		}
	},
	
	
	
	open : function()
	{
		var node				= this.node;
		
		node.style.height 		= '1px';
		node.style.overflow 	= 'hidden';
		
		var maxHeight 			= node.scrollHeight;

		new W.Tween({start: 1, change: maxHeight, duration: 10, obj:this, bind: 'update', method: 'easeInOutQuad', callback: 'finish'});
	},
	
	
	
	close : function()
	{
		var node		= this.node;
		var height		= parseInt(node.offsetHeight, 10);
		var minHeight	= 1;
		
		new W.Tween({start: height, change: minHeight - height, duration: 10, obj:this, bind: 'update', method: 'easeInOutQuad', callback: 'finish'});
	},
	
	
	
	update : function(x)
	{
		this.node.style.height	= x + 'px';
	},
	
	
	
	close_all : function()
	{
		var toggles = W.Dom.getElementsByClassName('more', 'div');
		var id		= '';
		
		for (var i = (toggles.length - 1); i >= 0; --i) {
			id = toggles[i].id;
			if (W.Dom.getStyle(toggles[i], 'height') != '1px') {
				new W.Toggle(id, 'toggle_' + id).activate();
			}
		}
	},
	
	
	
	finish : function()
	{
		if (this.state) {
			if (this.link) {
				W.Dom.removeClassname(this.link, 'selected');
				this.link.blur();
			}
		} else {
			if (this.link) {
				W.Dom.addClassname(this.link, 'selected');
				this.link.blur();
			}
		}
	}
};
