/* FBM Utilities */

Object.extend(Number.prototype, {

  byteToStr: function() {
    // human readable filesize
    var quantities = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
    var val = parseFloat(this);
    var t = 0;
    while(val >= 1000) {
      t++, val /= 1024;
    }

    if (typeof sprintf == 'undefined')
      return ((val<1) ? val : Math.round(val)) +' '+ quantities[t];

    // round value to get 2 most significant places
    if (val >= 10) {
      // no digits after decimal points
      val = Math.round(val);
    } else if (val >= 1) {
      // 1 digit after decimal point
      val = sprintf('%01.1f', val);
    } else {
      // 2 digits after decimal point
      val = sprintf('%01.2f', val);
    }
    
    return val +' '+ quantities[t];
  }

});
    
// just shortcut to create number object
function $N(object) {
  if (object && object.constructor == Number) return object;
    return new Number(object);
};

// append passed code to the body of current function
Function.prototype.appendCode = function(code) {
  var curr_code = this.toString();
  var last = curr_code.lastIndexOf('}');
  if (last > -1) {
    curr_code = curr_code.substring(0, last) + code + '}'; 
    eval(this.name + '=' + curr_code + ';');
  }
}

Date.prototype.toEUStr = function(withSec) {
  // format date like this: 2008.02.21, 08:03
  if (typeof withSec == 'undefined')
    withSec = 0;

  var Y = this.getFullYear();
  var d = this.getDate();
  if (d < 10) d = '0' + d;
  var m = this.getMonth() +1;
  if (m < 10) m = '0' + m;
  var H = this.getHours();
  if (H < 10) H = '0' + H;
  var i = this.getMinutes();
  if (i < 10) i = '0' + i;
  var s = this.getSeconds();
  if (s < 10) s = '0' + s;
  
  return Y + '.' + m + '.' + d + ', ' + H + ':' + i + ((withSec) ? ':' + s : '');
};  

addBookmark = function(url, title) {
  if (typeof title == 'undefined')
    title = document.title;

  if(document.all) {
    window.external.AddFavorite(url, title);
  } else if (window.sidebar) { 
    window.sidebar.addPanel(title, url, ''); 
  } else if(window.opera && window.print) {
    return true;
  }  
};

dialogbox = function(dialogboxUrl, width, height) {
  window.profileWin = new Window({
    className:      'dialogbox',
    draggable:      false,
    resizable:      false,
    minimizable:    false,
    maximizable:    false,
    hideEffect:     Element.hide,
    showEffect:     Element.show,
    width:          width,
    height:         height,
    destroyOnClose: true,
    zIndex:         1000,
    url:            dialogboxUrl
  });
  //contentWin.setContent('downloadbox', true, true);
  window.profileWin.showCenter(true);
};
