/* File: /includes/js/CreditCounter.js (Modified: 3. september 2010 12:30:55) */

var CreditCounter = Class.create({
	initialize: function (container, info, options) {
		this.container = $(container);
		if (this.container) {
			this.info = $(info);
			this.numberElms = $A(this.container.getElementsByClassName('coinCounterNumber'));
			this.options = Object.extend({
				baseAmount: 0,
				timer: 2000,
				performVerify: true,
				setObservers: true
			}, options);
			this.currentAmount = this.options.baseAmount;
			this.goalAmount = this.currentAmount;
			this.amount = 0;
			this.lastDepositSerial = 0;
			this.lastVerifySerial = 0;
			if (this.options.setObservers) {
				this._setObservers();
			}
			if (this.options.performVerify) {
				this._performVerifyCall();
			}
		}
	},

	_performVerifyCall: function () {
		setTimeout(function () {
			new Ajax.Request('/section/credits/creditcounter.ashx?m=updatecoins&amount=' + this.options.baseAmount);
		} .bind(this), 500);
	},

	_setObservers: function () {
		MsgService.observe('CoinSystem', this._onCoinsEvent.bind(this));
	},

	_onCoinsEvent: function (obj) {
		switch (obj.subType) {
			case 'CoinCountUpdate':
				if (obj.serial > this.lastDepositSerial) {
					this.deposit(obj.coins);
					this.lastDepositSerial = obj.serial;
				}
				break;
			case 'CoinCountVerify':
				if (obj.serial > this.lastVerifySerial) {
					this.verify(obj.coins);
					this.lastVerifySerial = obj.serial;
				}
				break;
		}
	},

	deposit: function (amount) {
		this._showInfo(amount - this.goalAmount);
		this.goalAmount = amount;

		if (this._updateTimer) {
			clearTimeout(this._updateTimer);
			amount = this.amount + amount;
		}

		amount = amount - this.currentAmount - this.amount;

		this.amount = amount;
		var timer = Math.abs(this.options.timer / this.amount);

		var interval = amount > 0 ? 1 : -1;
		if (timer < 10) {
			timer = 10;
			interval = parseInt(this.amount / (this.options.timer / 10));
			if (this.amount % interval != 0) {
				this.amount -= interval;
			}
		}

		this._updateTimer = setTimeout(this._updateAmount.bind(this, interval, timer), timer);
	},

	_showInfo: function (amount) {
		if (amount != 0) {
			if (this.fadeEffect) {
				this.fadeEffect.element.remove();
				this.fadeEffect = null;
			}
			var op = amount > 0 ? '+' : '-';
			var color = amount > 0 ? '729d12' : 'ff0000';
			var elm = new Element('div').update(op + ' ' + Math.abs(amount)).setStyle({
				paddingLeft: '5px',
				fontWeight: 'bold',
				color: '#' + color
			});
			this.info.update(elm);

			this.fadeEffect = Effect.Fade(elm, { duration: (this.options.timer / 1000), afterFinish: function () {
				this.fadeEffect = null;
			} .bind(this)
			});
		}
		//		var strAmount = op + Math.abs(amount);
		//		var count = 0;
		//		for(var i = 0; i < this.numberElms.length; i++){
		//			if(this.numberElms.length - i > strAmount.length){
		//				this.numberElms[i].update(' ');
		//			}
		//			else{
		//				var num = new Element('span').update(strAmount[count]);
		//				this.fadeEffect = Effect.Fade(num, {duration: (this.options.timer / 1000), afterFinish:function(){
		//					this.fadeEffect = null;
		//				}.bind(this)});
		//				this.numberElms[i].update(num);
		//				count++;
		//			}
		//		}
	},

	_updateAmount: function (interval, timer) {
		if (interval > Math.abs(this.amount)) {
			this.amount = interval;
		}
		this.currentAmount += interval;

		var strAmount = Math.abs(this.currentAmount) + '';
		if (strAmount.length > this.numberElms.length) {
			this._adjustNumberElms(strAmount.length);
		}
		strAmount = $A(this.currentAmount.toPaddedString(this.numberElms.length));

		for (var i = strAmount.length - 1; i >= 0; i--) {
			this.numberElms[i].update(strAmount[i]);
		}
		var valid = this.currentAmount > this.goalAmount ? (this.amount - interval < 0) : (this.amount - interval > 0)
		if (valid) {//this.amount - interval != 0 && this.amount - interval != this.goalAmount){
			this.amount = this.amount - interval;
			this._updateTimer = setTimeout(this._updateAmount.bind(this, interval, timer), timer);
		}
		else {
			clearTimeout(this._updateTimer);
			this._updateTimer = null;
			this.amount = 0;
			if (this.goalAmount != this.currentAmount) {
				var strGAmount = $A(this.goalAmount.toPaddedString(this.numberElms.length));
				for (var i = strGAmount.length - 1; i >= 0; i--) {
					this.numberElms[i].update(strGAmount[i]);
				}
			}
		}
	},

	_adjustNumberElms: function (length) {
		var diff = length - this.numberElms.length;
		for (var i = 0; i < diff; i++) {
			var cNode = this.numberElms[0].cloneNode(true);
			cNode.update('0');
			this.container.insert({ top: cNode });
			this.numberElms.unshift(cNode);
		}
	},

	verify: function (amount) {
		if (this.baseAmount != amount) {
			this.baseAmount = this.currentAmount = this.goalAmount = amount;
			var strAmount = $A(this.baseAmount + '');
			this._adjustNumberElms(strAmount.length);
			for (var i = 0; i < this.numberElms.length; i++) {
				this.numberElms[i].update('0');
			}
			var diff = this.numberElms.length - strAmount.length;
			//			for(var i = 0; i < this.numberElms.length; i++){
			//				if(strAmount[i]){
			//					this.numberElms[i].update(strAmount[i]);
			//				}
			//				else{
			//					this.numberElms[i].update('0');
			//				}
			//			}
			for (var i = strAmount.length - 1; i >= 0; i--) {
				this.numberElms[i + diff].update(strAmount[i]);
			}
		}
	},

	getCurrentAmount: function () {
		return this.goalAmount;
	}
});


try {
} catch (e) {}
