// Look for product/fund tables - handle the "add to favourites" and "display products/tree" button events
Event.observe(window,'load',function(){
	$$('.productdata table').each(function(t){new FundTable(t)});
});

// A fund table contains rows of funds, and subrows of series - allow the fund row to hide/show its series
var FundTable = Class.create({
	active_row:undefined,
	initialize:function(table){
		this.personalized_fund_ids = Cookie.get('personalized_fund_ids').split(' ');
		this.table = table;
		this.funds = this.table.getElementsBySelector('tr.fundrow').map(function(tr){
			var fund_row = tr;
			var series_rows = [];
			while (tr.next() && tr.next().hasClassName('seriesrow')) {
				series_rows.push(tr.next());
				tr = tr.next();
			}
			return {
				fund_row:fund_row,
				series_rows:series_rows,
				tree_button:fund_row.getElementsBySelector('a.treebutton-off').shift()
			};
		});
		this.tree_buttons = this.funds.pluck('tree_button');
		this.favorite_buttons = this.table.getElementsBySelector('a.favorite-add');

		// add events for each tree button
		this.funds.each(function(fund){ 
			if (fund.series_rows.length>1)
				Event.observe(fund.tree_button,'click',this.displayProductRows.bind(this,fund.tree_button));
		},this);

		// add events for each "add to favourites" button - set the status to del if it is in the favourites list
		this.favorite_buttons.each(function(a){
			// add event to toggle it on/off
			Event.observe(a,'click',this.toggleFavourite.bind(this,a));

			// change add buttons to delete buttons if the fund is a favourite
			if (this.personalized_fund_ids.include(a.readAttribute('rel')))
				this.swap(a,'favorite',['del','add']);
		},this);

		// display the active fund
		this.active_fund_id = QueryField('fund_id');
		if (/^\d+$/.test(this.active_fund_id)){
			this.displayProductRows(
				this.tree_buttons.find(function(tb){
					return tb.readAttribute('rel')==this.active_fund_id;
				},this)
			);
		} else {
			this.hideProductRows(true);
			// display the first fund
			//if (this.tree_buttons.length>0) this.tree_buttons.shift().onclick();
		}
	}, 	
	toggleFavourite:function(a){
		var fund_id = $(a).readAttribute('rel');
		if (a.hasClassName('favorite-add')) this.personalized_fund_ids.push(fund_id);
		else this.personalized_fund_ids = this.personalized_fund_ids.without(fund_id);
		Cookie.set('personalized_fund_ids',this.personalized_fund_ids.uniq().join(' '),365);
		this.swap(a,'favorite',['del','add']);
		
		// Remove the fund and its series - for the news.aspx page
		if (/news.aspx/.test(document.location)){
			var fund_row = a.up(1);
			while (fund_row.next() && fund_row.next().hasClassName('seriesrow')) fund_row.next().remove();
			fund_row.remove();
		}

		// return false - so the href is not redirected to
		return false;
	},	
	swap:function(a,prefix,offon){
		if (!prefix) prefix = /favorite/.test(a.className) ? 'favorite' : 'treebutton';
		if (!offon) offon = prefix=='favorite' ? ['del','add'] : ['off','on'];
		var on = a.hasClassName(prefix+'-'+offon[1]);
		offon.each(function(s){ a.removeClassName(prefix+'-'+s); });
		a.addClassName(prefix+'-'+offon[on ? 0 : 1]);
		return !on;
	},
	hideProductRows:function(initializing){
		// hide product rows (where theres more than 1 product for the fund)
		this.funds.each(function(fund){
			if (fund.series_rows.length>1)
				fund.series_rows.invoke('hide');
			else if (initializing) this.swap(fund.fund_row.getElementsBySelector('a').shift(),'treebutton');
		},this);
	},
	displayProductRows:function(a){
		this.hideProductRows();
		if (this.active_row && this.active_row!=a && this.active_row.hasClassName('treebutton-on')) this.swap(this.active_row);
		if (this.swap(a)){
			var fund_row = a.up(1);
			while (fund_row && fund_row.next() && fund_row.next().hasClassName('seriesrow')){
				fund_row.next().show();
				fund_row = fund_row.next();
			}
		}
		this.active_row = a;
		this.active_fund_id = this.active_row.readAttribute('rel');

		// modify the links in the sidebar to indicate the product_id to initially open
		$$('.subnav ul ul ul a').each(function(a){
			a.href=a.href.replace(/fund_id=(\d+)?$/,'fund_id='+this.active_fund_id);
		},this);

		return false;
	}
});

function QueryField(sName) {
	var qf = document.location.search.substring(1).split('&').map(function(p){
		return p.split('=');
	}).findAll(function(p){
		return p[0]==sName;
	});
	return qf.length>0 ? qf[0][1] : "";
}