/*
 * Copyright (c) 2011 TWIMPACT UG (haftungsbeschraenkt). All rights reserved.
 */

(function($, w) {
  // ensure we have initialized the namespace for twimpact;
  w.Twimpact = w.Twimpact || {};

// helper function that either calls a function to retrieve the value or use k as index
  Twimpact.Util = function() {
  };

  Twimpact.Util.get = function(k, v) {
    return (typeof k == "function") ? k(v) : v[k];
  };

  var kilobyte = 1024;
  var megabyte = kilobyte * 1024;
  var gigabyte = megabyte * 1024;
  var terabyte = gigabyte * 1024;

  Twimpact.Util.byteSize = function(bytes, precision) {
    precision = precision || 0;

    if ((bytes >= 0) && (bytes < kilobyte)) {
      return bytes + 'B';
    } else if ((bytes >= kilobyte) && (bytes < megabyte)) {
      return (bytes / kilobyte).toFixed(precision) + 'KB';
    } else if ((bytes >= megabyte) && (bytes < gigabyte)) {
      return (bytes / megabyte).toFixed(precision) + 'MB';
    } else if ((bytes >= gigabyte) && (bytes < terabyte)) {
      return (bytes / gigabyte).toFixed(precision) + 'GB';
    } else if (bytes >= terabyte) {
      return (bytes / terabyte).toFixed(precision) + 'TB';
    } else {
      return bytes + 'B';
    }
  };

  Twimpact.Util.log = function(level, args) {
    if (window.console) {
      var logger = window.console[level];
      if (typeof logger == 'function') {
        logger.apply(window.console, args);
      }
    }
  };
})(jQuery, window);
