/**
 *
 * Base class for for creating open and close animations for hiding and revealing a div
 * @class Toggle class for creating hide and reveal effect
 * <p>Usage: var trigger = new Trigger();</p>
 * @param {String | HTMLElement}	div 	DOM node that will hide/reveal
 * @requires W
 * @requires W.Event
 * @requires W.Anim
 * @requires W.Util
 * @constructor
 */
function Toggle(node, link)
{
	this.init(node, link);
}



Toggle.prototype = {
	
	node	: null,
	state	: null,
	link	: null,
	
	
	init : function(node, link)
	{
		var obj		= this;
		this.node 	= W.Util.$(node);
		this.link	= W.Util.$(link);
		
		this.node.onAnimEnd = obj.finish.bind(obj);
	},
	
	
	
	activate : function()
	{
		if (W.Util.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';
		//node.style.display		= 'block';
		
		var maxHeight 			= node.scrollHeight;

		new W.Anim(node).toggle(node, 1, maxHeight, 10);
	},
	
	
	
	close : function()
	{
		var node		= this.node;
		var height		= parseInt(W.Util.getStyle(node, 'height'));
		var minHeight	= 1;
		
		new W.Anim(node).toggle(node, height, minHeight, 10);
	},
	
	
	
	close_all : function()
	{
		var toggles = W.Util.getElementsByClassName('more', 'div');
		var id		= '';
		
		for (var i = (toggles.length - 1); i >= 0; --i) {
			id = toggles[i].id;
			if (W.Util.getStyle(toggles[i], 'height') != '1px') {
				new Toggle(id, 'toggle_' + id).activate();
			}
		}
	},
	
	
	
	finish : function()
	{
		if (this.state) {
			this.link.src = 'images/arrow_down.gif';
			this.link.blur();
		} else {
			if (this.link) this.link.src = 'images/arrow_up.gif';
			this.link.blur();
		}
	}
}
