Function.prototype.bind = function(object) {
var method = this;
return function() {
return method.apply(object, arguments);
}
};
Number.prototype.toAutoFixed = function () {
var v = Math.abs(this);
if (v < 0.0001) {
v = v.toFixed(10).substr(2);
for (var i = 0, j = v.length; i < j && i < 8; i++) {
if (0 != v.charAt(i)) {
return this.toFixed(i + 1);
}
}
return 0;
}
return this.toFixed(4);
};
function financeUaCurrencyConverter (el) {
this.el = el;
this.fromCurrencyRate = 0;
this.toCurrencyRate = 0;
this.crossRate = 0;
this.sourceData = null;
}
financeUaCurrencyConverter.prototype = {
setSourceData : function (value) {
if (null == value) {
this.sourceData = null;
return false;
} else {
this.sourceData = eval('(' + value + ')');
return true;
}
},
calculate : function () {
this.fromCurrencyRate = 0;
this.toCurrencyRate = 0;
this.crossRate = 0;
var k = this.sourceData.key;
this.fromCurrencyRate = this.toFloat(this.sourceData.data[this.el.fromCurrency.value][k.official_rate])
/ this.toInt(this.sourceData.data[this.el.fromCurrency.value][k.official_amount]);
this.toCurrencyRate = this.toFloat(this.sourceData.data[this.el.toCurrency.value][k.official_rate])
/ this.toInt(this.sourceData.data[this.el.toCurrency.value][k.official_amount]);
if (0 != this.fromCurrencyRate) {
this.crossRate = this.toFloat(this.el.fromCurrencyAmount.value) * this.fromCurrencyRate / this.toCurrencyRate;
var percent = this.toFloat(this.el.percentValue.value) * this.crossRate / 100;
this.crossRate = ('+' == this.el.percentSign.value)
? this.crossRate + percent
: this.crossRate - percent;
}
},
toInt : function (value) {
var result = parseInt(value);
return (isNaN(result))
? 0
: result;
},
toFloat : function (value) {
var result = parseFloat(value);
return (isNaN(result))
? 0
: result;
}
};
function gadgetFinanceUaCurrencyConverter (moduleId, isIE) {
this.el = {
'container' : _gel('container' + moduleId),
'source' : _gel('source' + moduleId),
'fromCurrencyAmount' : _gel('fromCurrencyAmount' + moduleId),
'fromCurrency' : _gel('fromCurrency' + moduleId),
'toCurrency' : _gel('toCurrency' + moduleId),
'percentSign' : _gel('percentSign' + moduleId),
'percentValue' : _gel('percentValue' + moduleId),
'logPanel' : _gel('logPanel' + moduleId),
'infoPanel' : _gel('infoPanel' + moduleId),
'resultPanel' : _gel('resultPanel' + moduleId)
};
this.userPrefs = {
'source' : { 'type' : 'select' },
'fromCurrencyAmount' : { 'type' : 'text' },
'fromCurrency' : { 'type' : 'select' },
'toCurrency' : { 'type' : 'select' },
'percentSign' : { 'type' : 'select' },
'percentValue' : { 'type' : 'text' }
};
this.savePrefsDelayTimerId = null;
this.prefs = new _IG_Prefs(moduleId);
this.loadPrefs();
this.preferredCurrency = {};
this.loadPreferredCurrency();
this.shortCurrencyNames = false;
this.calcCount = 0;
this.sourceUrl = 'http://content.finance.ua/' + this.prefs.getMsg('language') + '/export/google-gadgets/currency-official/~';
this.converter = new financeUaCurrencyConverter(this.el);
this.event('source');
window.onresize = function () { this.onResize(); }.bind(this);
this.onResize();
}
gadgetFinanceUaCurrencyConverter.prototype = {
loadSource : function (sourceId) {
this.showMessage('loading');
_IG_FetchContent(this.sourceUrl + '/' + sourceId, function(response) {
if (this.converter.setSourceData(response)) {
this.showMessage('loadingSuccessful');
this.reloadCurrencySelector('fromCurrency');
this.reloadCurrencySelector('toCurrency');
this.event('toCurrency');
} else {
this.showMessage('loadingError', true);
}
}.bind(this), { refreshInterval: 3600 });
},
loadPreferredCurrency : function() {
var r = /^[A-Za-z]{3}$/;
var x = this.prefs.getArray('preferredCurrency');
for (var i in x) {
if (r.test(x[i])) {
this.preferredCurrency[x[i].toUpperCase()] = true;
}
}
},
loadPrefs : function () {
for (var p in this.userPrefs) {
value = this.prefs.getString(p);
switch (this.userPrefs[p].type) {
case 'text':
this.el[p].value = value;
break;
case 'select':
for (var i = 0, j = this.el[p].options.length; i < j; i++) {
var o = this.el[p].options[i];
if (o.value == value) {
o.selected = true;
break;
}
}
break;
}
}
},
savePrefs : function () {
this.savePrefsDelayTimerStop();
this.savePrefsDelayTimerId = setTimeout(function() {
for (var p in this.userPrefs) {
this.prefs.set(p, this.el[p].value);
}
this.savePrefsDelayTimerId = null;
}.bind(this), 5000);
},
savePrefsDelayTimerStop : function () {
if (this.savePrefsDelayTimerId) {
clearTimeout(this.savePrefsDelayTimerId);
}
},
event : function (el, event) {
if ('object' != typeof el) {
el = this.el[el];
}
switch(el.id) {
case this.el.source.id:
this.calcCount = 0;
this.loadSource(el.value);
break;
default:
if (null == this.converter.sourceData) {
this.showMessage('loadingError', true);
break;
}
if (!this.checkInput(this.el.fromCurrencyAmount)
|| !this.checkInput(this.el.percentValue)) {
this.savePrefsDelayTimerStop();
break;
}
if (event) {
var k = event.keyCode;
if (!((k > 47 && k < 58) || k == 8 || k == 46 || k == 190)) { // num, BS, DEL, .
break;
}
}
this.converter.calculate();
this.updateInfoPanel(this.converter.fromCurrencyRate, this.converter.toCurrencyRate);
this.updateResultPanel(this.converter.crossRate);
this.savePrefs();
this.calcCount++;
switch (this.calcCount) {
case 1:
_IG_AdjustIFrameHeight();
break;
case 2:
this.showMessage();
break;
}
break;
}
},
reloadCurrencySelector : function (elementName) {
var el = this.el[elementName];
var selected = (0 == el.options.length)
? this.prefs.getString(elementName)
: el.value;
if (!this.converter.sourceData.data[selected]) {
selected = ('fromCurrency' == elementName)
? 'USD'
: this.converter.sourceData.source_currency;
}
el.options.length = 0;
var k;
for (k in this.preferredCurrency) {
if (this.converter.sourceData.data[k]) {
el.options.add(this.createOption(k, this.converter.sourceData.data[k][this.converter.sourceData.key.currency_title], selected));
}
}
if (el.options.length) {
el.options.add(this.createOption(null));
}
for (k in this.converter.sourceData.data) {
if (' ' == k || this.preferredCurrency[k]) {
continue;
}
el.options.add(this.createOption(k, this.converter.sourceData.data[k][this.converter.sourceData.key.currency_title], selected));
}
},
createOption: function(k, v, s) {
var o = document.createElement('OPTION');
if (k) {
o.text = this.shortCurrencyNames ? k : '[' + k + '] ' + v;
o.value = k;
if (k == s) {
o.selected = true;
}
} else {
o.text = '\u2014';
o.disabled = true;
}
return o;
},
checkInput : function (el) {
var r = /^\d+(\.\d{0,2})?$/;
var d = /[,-]/;
if (d.test(el.value)) {
el.value = el.value.replace(d, '.');
}
if (!r.test(el.value)) {
el.style.backgroundColor = '#ffcccc';
return false;
} else {
el.style.backgroundColor = '#ffffff';
return true;
}
},
showMessage : function (messageId, clean) {
this.el.logPanel.innerHTML = messageId ? this.prefs.getMsg(messageId) : ' ';
if (clean) {
this.el.resultPanel.innerHTML = ' ';
this.el.infoPanel.innerHTML = ' ';
}
},
updateResultPanel : function (crossRate) {
var html = this.prefs.getMsg('lblCalculate') + ' ' + this.converter.sourceData.date + ':
'
+ parseFloat(this.el.fromCurrencyAmount.value) + ' ' + this.el.fromCurrency.value;
if (this.el.percentValue.value > 0) {
html += ' ' + this.el.percentSign.value + ' ' + parseFloat(this.el.percentValue.value) + '%';
}
html += ' = ' + crossRate.toAutoFixed() + ' ' + this.el.toCurrency.value + '';
this.el.resultPanel.innerHTML = html;
},
updateInfoPanel : function (fromCurrencyRate, toCurrencyRate) {
var html = this.prefs.getMsg('lblAdditional') + ':
';
if (this.el.fromCurrency.value != this.converter.sourceData.source_currency) {
html += '1 ' + this.el.fromCurrency.value + ' = '
+ fromCurrencyRate.toAutoFixed() + ' ' + this.converter.sourceData.source_currency;
}
if (this.el.fromCurrency.value != this.converter.sourceData.source_currency
&& this.el.toCurrency.value != this.converter.sourceData.source_currency) {
html += ', ';
}
if (this.el.toCurrency.value != this.converter.sourceData.source_currency) {
html += '1 ' + this.el.toCurrency.value + ' = '
+ toCurrencyRate.toAutoFixed() + ' ' + this.converter.sourceData.source_currency;
}
html += '';
this.el.infoPanel.innerHTML = html;
},
onResize : function () {
var lastState = this.shortCurrencyNames;
this.shortCurrencyNames = this.el.fromCurrency.offsetWidth < 100;
if (lastState != this.shortCurrencyNames) {
this.reloadCurrencySelector('fromCurrency');
this.reloadCurrencySelector('toCurrency');
}
this.el.fromCurrency.style.width = this.el.container.clientWidth > 280 ? '190px' : '100%';
this.el.toCurrency.style.width = this.el.container.clientWidth > 280 ? '245px' : '100%';
}
};