
function Util() {
  throw new Error("Util is a static class. You can't create instances of it.");
}

Util.getEltDoc = function(anElt) {
  if (anElt.ownerDocument) {return anElt.ownerDocument;}
  if (anElt.document) {return anElt.document;}
  return document;
};

Util.newElement = function(tagName, cssClassName, attributesInJson, text, style, doc) {

  //if (!doc) {doc = document;}
  var newElement = doc.createElement(tagName);
  if (cssClassName) {
    newElement.className = cssClassName;
  }
  if (attributesInJson) {
    for (var key in attributesInJson) {
      if (key == 'id') {
        newElement.id = attributesInJson[key];
      } else {
        if (key == 'jfor') {
          newElement.setAttribute('for', attributesInJson[key]);
        }
        else {
          newElement.setAttribute(key, attributesInJson[key]);
        }
      }
    }
  }
  if (style) {
      for (key in style) {
        if (key == 'cssFloat' && Util.isIE()) {
          newElement.style['styleFloat'] = style[key];
        }
        else {
          newElement.style[key] = style[key];
        }
      }
  }
  if (text) {
    try {
      newElement.appendChild(doc.createTextNode(text));
    } catch (e) {
      newElement.text = text;
    }
  }
  return newElement;
};

Util.appendNewElement = function(parentElement, tagName, cssClassName, attributesInJson, text,style) {
  // IE does not like it
  //Util.assert(parentElement instanceof HTMLElement);
  var doc = Util.getEltDoc(parentElement);
  var newElement = Util.newElement(tagName, cssClassName, attributesInJson, text,style,doc);
  parentElement.appendChild(newElement);
  return newElement;
};


Util.appendNewTextNode = function(parentElement, textString) {
   // doesn't work on IE
  //Util.assert(parentElement instanceof HTMLElement);

  var doc = Util.getEltDoc(parentElement);
  var newTextNode = doc.createTextNode(textString);
  parentElement.appendChild(newTextNode);
  return newTextNode;
};

Util.now = function() {
  return (new Date()).getTime();
};

Util.relativeTime = function(origTime) {
  var diff = (Util.now() - origTime.getTime())/1000;
  if (diff > (86400*8)) {
    return origTime.toDateString()
  }
  else if (diff >= (6*86400 + 86400/2)) {
    return 'a week ago';
  }
  else if (diff >= (23*3600 + 3600/2)) {
    return Util.pluralize(Math.round(diff/86400),'day') + ' ago';
  }
  else if (diff >= (59*60 + 60/2)) {
    return Util.pluralize(Math.round(diff/3600),'hour') + ' ago';
  }
  else if (diff >= 30) {
    return Util.pluralize(Math.round(diff/60),'minute') + ' ago';
  }
  return 'mere moments ago';
};

Util.pluralize = function(num, singular, plural) {
  if (!plural) {plural = singular + 's';}
  return num + ' ' + (num == 1 ? singular : plural);
};

Util.unbold = function(str) {
  return  str.gsub(/<\/?b>/,'');
};

Util.isSafari = function () {
  return /Konqueror|Safari|KHTML/.test(navigator.userAgent);
};

function reportTrackingEvent(trackingStr) {
  if (window.urchinTracker) { urchinTracker(trackingStr); }
  return false;
}

function doOnLoadStuff() {
  if ($('search')) {$('search').getElementsByTagName('input')[0].focus();}
}

Event.observe(window, 'load', doOnLoadStuff, false);

