/**
 *
 *	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>
 *
 *	Start page with one div already opened
 *	<script type="text/javascript">new W.Toggle('div', 'toggle_div').start();</script>
 *
 *	@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,
	min			: 47,
	max			: 488,
	players		: null,		// array of flash players
	duration	: 25,
	hbx			: false,	// enable or disable hbx tracking
	hbx_category: null,		// hbx_category from xml file
	
	
	init : function(node, link)
	{
		var obj			= this;
		this.node 		= W.$(node);
		this.link		= W.$(link);
		this.players	= [];
	},
	
	
	
	activate : function(e)
	{
		W.Event.stop(e);
		
		if (W.Dom.getStyle(this.node, 'height') != this.min + 'px') {
			this.state = 1;
			this.close();
		} else {
			this.state = 0;
			this.close_all();
			this.control_player('stop_video');
			this.open();
		}
	},
	
	
	
	start : function()
	{
		if (W.Dom.getStyle(this.node, 'height') == this.min + 'px') {
			this.state = 0;
			this.close_all();
			this.open();
		}
	},
	
	
	
	open : function()
	{
		var node				= this.node;
		
		node.style.height 		= this.min + 'px';
		node.style.overflow 	= 'hidden';
		
		//var maxHeight 			= node.scrollHeight;	// ?? in IE7 the first time scrollHeight is accessed the value is wrong, but the next time it will be right

		if (this.hbx) {
			var id	= 'locations';
			_hbLink(this.hbx_category + id);	
			_hbPageView(id, this.hbx_category + id);
		}
		
		new W.Tween({start: this.min, change: this.max - this.min, duration: this.duration, obj:this, bind: 'update', method: 'easeInOutQuad', callback: 'finish'});
	},
	
	
	
	close : function()
	{
		var node		= this.node;
		var height		= parseInt(node.offsetHeight, 10);
		var minHeight	= this.min;
		
		new W.Tween({start: height, change: minHeight - height, duration: this.duration, 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; --i >= 0; ) {
			id 	= toggles[i].id;
			num	= id.substr(4);
			if (W.Dom.getStyle(toggles[i], 'height') != this.min + 'px') {
				new W.Toggle(id, 'toggle' + num).activate();
			}
		}
	},
	
	
	
	finish : function()
	{
		if (this.state) {
			this.control_player('play_video');
			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();
			}
		}
	},
	
	
	
	add_player : function(id)
	{
		this.players.push(id);
	},
	
	
	
	control_player : function(fn)
	{
		if (!this.players.length) { return; }
		
		var id;
		
		for (var i = this.players.length; --i >= 0; ) {
			id	= this.players[i];
			if (W.$(id) && W.$(id)[fn]) { W.$(id)[fn](); }
		}
	},
	
	
	
	enable_hbx : function(hbx_category, lang)
	{
		this.hbx			= true;
		
		if (hbx_category.charAt(hbx_category.length - 1) == '/') hbx_category += lang; else hbx_category += '/' + lang;
		
		this.hbx_category = hbx_category + '/';
	}
};
