/**
 *
 *	Requires W.Dom
 *	Requires W.Event
 *	Requires W.Tween
 *
 *	Example:
 *
 *	<div id="headlines">
 *		<p><a href="#news2005-07-01">July 1, 2005 Press Release for July</a></p>
 *	</div>
 *
 *	<div id="news"></div>
 *
 *	<div id="news2005-07-01" class="swap">...</div>
 *
 *	<script type="text/javascript" language="javascript">
 *	// <![CDATA[
 *		var news = new W.Swap('headlines', 'news');
 *		news.select(news.find('news2005-07-01'));
 *	// ]]>
 *	</script>
 */
W.Swap = function()
{
	this.init();
};



W.Swap.prototype = {


	a			: null,		// links for populating container
	duration	: 8,		// length of the animation
	tween		: null,		// current tween
	zindex		: 1,		// keep incrementing the zindex
	current		: null,
	players		: null,		// array of flash players
	hbx			: false,	// enable or disable hbx tracking
	hbx_category: null,		// hbx_category from xml file
	timer		: null,		// reference to setTimeout


	init: function()
	{
		this.a			= document.getElementsByTagName('a');
		
		this.tween		= {};
		this.players	= [];
		
		this.initNav();
	},
	
	
	
	initNav : function()
	{
		var obj	= this;
		
		for (var i = this.a.length; --i >= 0; ) {
			// Safari always has a hash of at least #
			if (this.a[i].hash && this.a[i].hash != '#') { this.a[i].onclick = function(e) { obj.select(this, e); }; }
		}
	},
	
	
	
	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 + '/';
	},
	
	
	
	select : function(link, e)
	{
		new W.Toggle('more1', 'toggle1').close_all();
		
		if (!link) { return; }
		
		var id	= link.hash.substr(1);
		
		W.Event.stop(e);

		this.next	= W.$(id) || null;
		
		
		// checks
		if (!this.next) { return; }					// bail if link doesn't have a div
		if (this.tween.fade_out) { return; }		// bail if fade_out is already in progress
		if (this.next == this.current) { return; }	// bail if the link is already visible
		
				
		this.unselect();				// change all links to unselected state
		if (link) { W.Dom.addClassname(link, 'selected'); }
		
		if (this.current) { this.fade_out(); } else { this.fade_in(); }
		
		if (this.hbx && this.hbx_category) _hbLink(this.hbx_category + id);		// hbx_category is null when page first loads so don't trigger _hbLink so a link wasn't clicked
	},
	
	
	
	find : function(name)
	{
		name = '#' + name;
		
		for (var i = this.a.length; --i >= 0; ) {
			if (this.a[i].hash == name) { return this.a[i]; }
		}
		
		return false;
	},
	
	
	
	fade_out : function()
	{
		this.control_player('stop_video');
		
		var start 			= 100;
		var change			= -100;
		
		if (this.tween.fade_in) { this.tween.fade_in.stop(); }
		
		this.tween.fade_out	= new W.Tween({start: start, change: change, duration: this.duration, obj: this, bind: 'set_opacity', method: 'linear', callback: 'fade_in'});
	},
	
	
	
	fade_in : function()
	{
		var start			= 0;
		var change			= 100;

		this.current	= this.next;
		this.next		= null;
		this.current.style.display	= 'block';
		
		this.tween.fade_out = null;

		this.tween.fade_in	= new W.Tween({start: start, change: change, duration: this.duration, obj: this, bind: 'set_opacity', method: 'linear', callback: 'finish'});
	},
	
	
	
	set_opacity : function(num)
	{
		var elem	= this.current;
		
		elem.style.opacity	= num / 100;
		elem.style.filter	= 'alpha(opacity=' + num + ')';
		if (num <= 0) { elem.style.display = 'none'; }
		//alert(num);
	},
	
	
	
	finish : function()
	{
		this.tween.fade_in	= null;
		
		if (this.hbx) _hbPageView(this.current.id, this.hbx_category + this.current.id);
		
		
		// play video if it exists
		var id	= this.current.id + '_flash';
		var obj	= this;
		if (W.$(id)) { this.timer = setTimeout(function() { obj.wait_for_video(); }, 100); }	// wait until the function becomes available for Firefox
	},
	
	
	
	wait_for_video : function()
	{
		var fn	= 'play_video';
		var id	= this.current.id + '_flash';
		var obj	= this;
		
		if (W.$(id) && W.$(id)[fn]) {
			clearTimeout(this.timer);
			this.timer	= this.timer = setTimeout(function() { obj.play_video(); }, 800);	// 1 more delay for Mac Firefox
		}
	},
	
	
	
	play_video : function()
	{
		var fn	= 'play_video';
		var id	= this.current.id + '_flash';
		
		if (W.$(id) && W.$(id)[fn]) {
			clearTimeout(this.timer);
			W.$(id)[fn]();
		}
	},
	
	
	
	unselect : function()
	{
		for (var i = this.a.length; --i >= 0; ) {
			W.Dom.removeClassname(this.a[i], 'selected');
		}
	},
	
	
	
	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](); }
		}
	}

};