﻿var ExchangePrice = Class.create();
Object.extend(ExchangePrice.prototype, {
	"initialize": function(_arg){
		this.setSrc(_arg.src);
		this.setDes(_arg.des);
		this.nRate = _arg.rate;
		this.sSymbol = _arg.symbol;
		if (_arg.convert) this.convert = _arg.convert;

		return this;
	},
	"setSrc" : function (_e) {
		var that = this;
		this.eSrc = _e;
		if (!_e) return;
		if (!_e.__epInitialized) {
			Event.observe(_e, 'focus',this.focusSrc.bindAsEventListener(this), false);
			Event.observe(_e, 'blur',this.blurSrc.bindAsEventListener(this), false);
			Event.observe(_e, 'keydown', function() {
				setTimeout(function() { that.eSrc.fire("maybe:changed", {}); }, 10);
			}, false);
			Event.observe(_e, 'maybe:changed',this.updateDes.bindAsEventListener(this), false);

			_e.setValue = function () {
				var fSetValue = _e.setValue;
				return function(_s) { fSetValue.apply(this, [_s]); this.fire("maybe:changed", {}); };
			}();
			_e.getValue = function () {
				var fGetValue = _e.getValue;
				return function() {
					return fGetValue.apply(this).replace(/[^\d\.]/g,"");
				};
			}();
			_e.__epInitialized = true;
		}
		return _e;
	},
	"setDes" : function (_e) {
		return this.eDes = _e;
	},
	"focusSrc" : function (_event) {
		var e = Event.element(_event);
		e.setValue(this.getNumericValue(e));
		e.select();
	},
	"blurSrc" : function (_event) {
		var e = Event.element(_event);
		var v = this.getNumericValue(e);
		e.setValue(Number.InsertToken(v));
		if (e != this.eSrc) this.eSrc.setValue(e.getValue());
	},
	"convert" : function (_n) {
		return _n;
	},
	"updateDes" : function (_v) {
		if (!this.eDes) return;
		if (typeof(_v) === "object" && "memo" in _v) _v = _v.memo;
		if (!Number.isNumber(_v)) _v = this.getNumericValue(this.eSrc);
		_v = this.convert(_v);
		(this.eDes.setValue || this.eDes.updateText)(isNaN(_v)? "-": this.sSymbol + Number.InsertToken(_v));
	},
	"getNumericValue" : function (_e) {
		return (_e.getValue? _e.getValue(): ("textContent" in e? _e.textContent: _e.innerText)).replace(/[^\d\.]/g,"") || 0;
	},
	"validateSrcValue" : function (_e) {
		var v = this.getNumericValue(_e);
		v = (parseInt(v) == (v-0))? parseInt(v): NaN; // to integer
		if (isNaN(v)) {
			return 0;
		}
		return v;
	}
});
var BuyNowManager = Class.create();
Object.extend(BuyNowManager.prototype, {
	"initialize": function(_arg){
		this.eNow = _arg.now;
		this.eBid = _arg.bid;
		this.BidElements = _arg.bidelems;
		this.DefaultBaseName = _arg.defname;
		this.buyNowPrice = 0;
		this.originalBidValue = "";

		var that = this;
		$A([this.eNow, this.eBid]).each(function(_e) {
			Event.observe(_e, "click", that.updateBuyingForm.bindAsEventListener(that));
		});

		return this;
	},
	"updateBuyingForm" : function () {
		var bl = this.eNow.checked;
		$A(this.BidElements).each(function(_e) {
			if(_e) {
				_e[bl? "hide": "show"]();
			}
		});
		if (bl) {
			this.originalBidValue = this.BidElements[0].getValue();
			if (g_currencySwitchManager) {
				g_currencySwitchManager.switchCurrency(this.DefaultBaseName, true);
			}
			$(this.DefaultBaseName+"Static").updateText(Number.InsertToken(this.buyNowPrice));
			this.BidElements[0].setValue(Number.InsertToken(this.buyNowPrice));

		} else {
			if (g_currencySwitchManager) {
				g_currencySwitchManager.switchCurrency(this.DefaultBaseName, false);
			}
			this.BidElements[0].setValue(this.originalBidValue);
		}
		if (this.BidElements[1]) {
			this.BidElements[1].checked = true;
		}
		$("PriceTitle").updateText(g_LocalText.Login.PriceCaption + g_LocalText.Other[bl? "OriginalPrice": "CounterPrice"]);
		$(this.DefaultBaseName+"Static")[bl? "show":"hide"]();
	}
});
var CurrencySwitchManager = Class.create();
Object.extend(CurrencySwitchManager.prototype, {
	"initialize": function(_arg){
		this.switchNames = _arg.names;
		this.converters = _arg.converters;
		var that = this;
		$A(this.switchNames).each(function(_name) {
			// when changed static text always synchronize value to edit control
			$(_name+"Static").setValue = (function (__name, _v) {
				this.updateText(_v);
				$(__name+"Text").value = _v;
			}).bind($(_name+"Static"), _name);

			// swap source and destination
			Event.observe($(_name), "click",
				(function (__name) {
					this.switchCurrency(__name);
					$(__name+"Text").select();
				}).bind(that, _name)
			);
		});
		return this;
	},
	"getPair" : function(_name){
		return (this.switchNames[0] == _name)? this.switchNames[1]: this.switchNames[0];
	},
	"isLocalCurrency" : function(){
		return !$(this.switchNames[0]).checked;
	},
	"switchCurrency" : function(_name, _noedit){
		var sSrcName = _name;
		var sDesName = this.getPair(_name);
		if (!sDesName.length) return;
		g_exchangePrice.setDes($(sDesName+"Static")).show();
		g_exchangePrice.setSrc($(sSrcName+"Text"))[_noedit? "hide": "show"]();
		$(sSrcName+"Static")[_noedit? "show": "hide"]();
		$(sDesName+"Text").hide();
		g_exchangePrice.convert = this.converters[_name];
	}
});
