
String.prototype.repeat = function(l){
	return new Array(l+1).join(this);
};

Array.prototype.inArray = function(value) {
	for (var i = 0; i < this.length; i++) {
	  if (this[i] == value) return true;
	}
	return false;
}
Array.prototype.unique = function() {
	var u = [];
	for (var i = 0; i < this.length; i++) {
	  if (!u.inArray(this[i])) u.push(this[i]);
	}
	return u;
}

function amount_out(v, decimals, thousand) {
	if (typeof v == 'undefined' || v === '') return '';
	if (typeof decimals != 'number') decimals = 2;
	var roundFactor = Math.pow(10, decimals);
  v = (Math.round((v-0)*roundFactor))/roundFactor;
  v = String(v);
  var ps = v.split('.');
  var whole = ps[0];
  var sub = decimals > 0 ? TGSystem.decSep+(ps[1] ? ps[1]+("0").repeat(decimals-ps[1].length) : ("0").repeat(decimals)): '';
  if (thousand !== false && TGSystem.thousandSep) {
	  var r = /(\d+)(\d{3})/;
  	while (r.test(whole)) {
    	  whole = whole.replace(r, '$1' + TGSystem.thousandSep + '$2');
	  }
	}
  v = whole + sub;
  return v;
}
function amount_in(amount) {
  amount = amount + '';
  var re = new RegExp('\\' + TGSystem.thousandSep, 'g');
  amount = amount.replace(re, '');
  var re = new RegExp('\\' + TGSystem.decSep, 'g');
  amount = amount.replace(re, '.');
  amount = amount * 1;
  return amount;
}

handleJSONResponse = function(success, response, config) {
	config = config || {};
  if (success == false) {
    return false;
  }
  try {
	  var json = response.responseText;
	  result = Ext.util.JSON.decode(json);
  } catch(e) {
    return false;
  }
  if (result.success == false && result.errDesc) {
    if (config.silent !== true) {
			alert(result.errDesc);
	  }
    return false;
  }
  return result;
}

CurrencySelector = function() {
	return {
		init: function() {
			this.el = Ext.get('currency-ct');
			if (!this.el) return;
			this.list = Ext.get('currency-selector-list');
			this.el.addClassOnOver('currency-ct-hover');
			this.el.on('click', this.showList, this);
			this.el.unselectable();
		},
		showList: function(e) {
			this.onListShow.defer(1, this);
			this.list.setStyle('width', '');
			this.list.setWidth(Math.max(this.list.getWidth(), this.el.getWidth()));
			this.list.alignTo(this.el, 'tl-bl', [0,-1]);
			this.list.show();
		},
		onListShow: function() {
			Ext.get(document.body).on('click', this.onDocClick, this);
		},
		hideList: function() {
			Ext.get(document.body).un('click', this.onDocClick, this);
			this.list.hide();
		},
		onDocClick: function(e) {
			var el = Ext.fly(e.getTarget());
			if (this.list.id == el.id || this.list.contains(el)) return;
			this.hideList();
		}
	}
}();

MainSearch = function() {
	return {
		init: function() {
			this.emptyText = t('Search - e.g. cubes, arrows...');
			this.field = Ext.get('main-search-field');
			if (!this.field) return;
			this.onBlur();
			Ext.EventManager.addListener(this.field.dom, 'focus', this.onFocus, this);
			Ext.EventManager.addListener(this.field.dom, 'blur', this.onBlur, this);
		},
		applyEmptyText: function() {
			if (this.emptyTextApplied === true) return;
			this.emptyTextApplied = true;
			this.field.addClass('empty-text');
			this.field.dom.value = this.emptyText;
		},
		removeEmptyText: function() {
			if (this.emptyTextApplied !== true) return;
			this.emptyTextApplied = false;
			this.field.removeClass('empty-text');
			this.field.dom.value = '';
		},
		onFocus: function() {
			this.removeEmptyText();
		},
		onBlur: function() {
			if (this.field.dom.value == '') {
				this.applyEmptyText();
			}
		}
	}
}();

ClientLogos = function() {
	return {
		init: function() {
			this.ct = Ext.get('client-logos');
			if (!this.ct) return;
			this.activeIndex = 1;
			this.startTimer();
		},
		startTimer: function() {
			window.setTimeout(this.next.createDelegate(this), 3*1000);
		},
		next: function() {
			var nextIndex = this.activeIndex+1;
			if (nextIndex >= this.ct.dom.childNodes.length) {
				nextIndex = 1;
			}
			Ext.get(this.ct.dom.childNodes[this.activeIndex]).hide(true);
			this.activeIndex = nextIndex;
			Ext.get(this.ct.dom.childNodes[this.activeIndex]).show(true);
			this.startTimer();
		}
	}
}();

Ext.onReady(function() {
	CurrencySelector.init();
	MainSearch.init();
	ClientLogos.init();
});