
Namespace.register('aegoncz.funds');

/* Portfolio Performance Manager Class
------------------------------------------------------------*/
aegoncz.funds.PortfolioPerformanceManager = new Class({
	
	ratioManager:null,
	periodManager:null,
	fundProvider:null,
	
	colors:[],
	funds:[],
	request:null,
	view:null,
	
	initialize:function(domContainer,providerSource,fundRatioManager,fundPeriodManager) {
		
		// get funds data
		var funds = domContainer.getProperty('data-funds');
		if (funds) {
			fundsParts = funds.split(',');
			for (var i=0;fundsParts[i]!=undefined;i++) {
				this.funds.push(new aegoncz.funds.Fund(fundsParts[i]));
			}
		}
		
		// get color data
		var colors = domContainer.getProperty('data-colors');
		if (colors) {
			colorsParts = colors.split(',');
			for (var i=0;colorsParts[i]!=undefined;i++) {
				this.colors.push(colorsParts[i]);
			}
		}
		
		// set data source
		this.fundProvider = new aegoncz.funds.FundProvider(providerSource);
		this.fundProvider.addEvent(mirabeau.events.Event.FAILURE,this.onRequestFailure.bind(this));
		this.fundProvider.addEvent(mirabeau.events.Event.REQUEST,this.onRequest.bind(this));
		this.fundProvider.addEvent(mirabeau.events.Event.SUCCESS,this.onRequestSuccess.bind(this));
		
		// set period controller
		this.periodManager = fundPeriodManager;
		this.periodManager.addEvent(mirabeau.events.Event.UPDATED,this.onPeriodUpdated.bind(this));
		
		// define ratio manager
		this.ratioManager = fundRatioManager;
		this.ratioManager.addEvent(mirabeau.events.Event.CHANGED,this.onRatioChanged.bind(this));
		this.ratioManager.addEvent(mirabeau.events.Event.UPDATED,this.onRatioUpdated.bind(this));
		
		// set fund view type
		var viewType = domContainer.getProperty('data-view');
		if (viewType == 'chart') {
			this.view = new aegoncz.funds.FundViewChart(domContainer,this.colors);
		}
		else {
			this.view = new aegoncz.funds.FundViewTable(domContainer,this.colors);
		}
		
		//var self = this;
		this.view.addEvent(mirabeau.events.Event.CHANGED,this.onViewChanged.bind(this));
		
		// show loading
		this.view.showLoading();
		
		// get initial data set
		this.onRequestInitialData.delay(1000,this);
	}
});

aegoncz.funds.PortfolioPerformanceManager.implement({
	
	onRatioChanged:function(e) {
		this.view.showLoading();
	},
	
	onViewChanged:function(from,to) {
		this.periodManager.setPeriod(from,to);
	},
	
	onRequestInitialData:function() {
		
		// set default fund ratios
		this.ratioManager.setDefaultRatioForFunds(this.funds);
		
		// get new average data set for the current fund ratios
		this.request = this.fundProvider.getAverageDataByFunds(this.funds);
	},
	
	onRatioUpdated:function(e) {
		
		// set selected ratio for each fund
		this.ratioManager.setRatioForFunds(this.funds);
		
		// get new average data set for the current fund ratios
		this.request = this.fundProvider.getAverageDataByFunds(this.funds);
	},
	
	onPeriodUpdated:function(e) {
		
		// get period
		var period = this.periodManager.getPeriod();
		
		// get timestamps
		var from = period.from.getTime();
		var to = period.to.getTime();
		
		// set view period
		this.view.setPeriod(from,to);
	},
	
	onRequestFailure:function(e) {
		
		// request failed, stop showing loading indicators and show error
		this.view.hideLoading();
		
	},
	
	onRequest:function(e) {
		
	},
	
	onRequestSuccess:function(data) {
		
		// set data for view
		this.view.setData(data);
		
		// get period
		var period = this.periodManager.getPeriod();
		
		// get timestamps
		var from = period.from.getTime();
		var to = period.to.getTime();
		
		// set initial period
		this.view.setPeriod(from,to);
		
	}
	
	
});







