// Global CSS Tweaks
document.observe('dom:loaded', function() {
  $$('input[type=text]').each(function(e) {
    $(e).addClassName('text');
  });
});


// JavaScript Object Overrides
Function.prototype.method = function (name, func) {
  if (!this.prototype[name]) {
    this.prototype[name] = func;
  }
};

Array.method('find_by_id', function(id) {
	return this.find(function(el) {
		return el.id == id;
	});
});

// Ensure console.log is available
if (typeof(console) == 'undefined') {
  console = { log: Prototype.emptyFunction, debug: Prototype.emptyFunction }
}

// Ajax Activity Indicator
Ajax.Responders.register({
  onCreate: function() { if ($('ajax-activity')) { $('ajax-activity').show(); } },
  onComplete: function() {
    if (Ajax.activeRequestCount == 0) {
      if ($('ajax-activity')) { $('ajax-activity').hide(); }
    }
  }
});


// Various Helper Methods
function number_to_currency(num, currency_symbol) {
  //TODO figure out why price calc sometimes returns infinity and fix
  if (isNaN(num) || num > 100000) return "0.00";
  num = Math.round(num * 100) / 100;

	if (num - Math.floor(num) == 0) {
		num = num + ".00";
		
	} else {
		string = num.toString();
		parts = string.split(".");
		cents = parts[1];
		
		if (cents.length == 1) {
			num = num + "0";
		}
	}
  
  num = currency_symbol + num;
  return num;
}
