
Namespace.register('aegoncz.funds');

/* FundRatioManager Class
------------------------------------------------------------*/
aegoncz.funds.FundRatioManager = new Class({
	
	Implements:Events,
	
	slider:null,
	values:[],
	timer:null,
	
	// constructor
	initialize:function(slider) {
		
		// set slider reference
		this.slider = slider;
		this.slider.addEvent(mirabeau.events.SliderEvent.SLIDE_STOP,this.onRatioUpdatedQueue.bind(this));
	}
});

aegoncz.funds.FundRatioManager.implement({
	
	onRatioUpdatedQueue:function(e) {
		
		// clear any previous delays
		clearTimeout(this.timer);
		
		// prevent to much changing of ratio, add delay before update event !
		this.timer = this.onRatioUpdated.delay(500,this);
		
		// fire changed event
		this.fireEvent(mirabeau.events.Event.CHANGED);
	},
	
	onRatioUpdated:function() {
		
		// update values array
		this.values = this.slider.getValues();
		
		// fire updated event
		this.fireEvent(mirabeau.events.Event.UPDATED);
	},
	
	setDefaultRatioForFunds:function(funds) {
		
		// update values array
		this.values = this.slider.getValues();
		
		// set ratio for funds
		this.setRatioForFunds(funds);
	},
	
	setRatioForFunds:function(funds) {
		for(var i=funds.length;i--;) {
			this.setRatioForFund(funds[i]);
		}
	},
	
	setRatioForFund:function(fund) {
		for (var i=this.values.length;i--;) {
			if (this.values[i].id.indexOf(fund.id)!=-1) {
				fund.ratio = Math.round(this.values[i].value);
				return true;
			}
		}
		return false;
	}
});


