function $(id) {
  return document.getElementById(id);
}

function getPageOffsetLeft(el) {

  var x;

  // Return the y coordinate of an element relative to the page.

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {

  var y;

  // Return the y coordinate of an element relative to the page.

  y = el.offsetTop;
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}

function hasClassName(el, name) {

  var i, list;

  // Return true if the given element currently has the given class
  // name.

  list = el.className.split(" ");
  for (i = 0; i < list.length; i++)
    if (list[i] == name)
      return true;

  return false;
}

function removeClassName(el, name) {

  var i, curList, newList;

  if (el.className == null)
    return;

  // Remove the given class name from the element's className property.

  newList = new Array();
  curList = el.className.split(" ");
  for (i = 0; i < curList.length; i++)
    if (curList[i] != name)
      newList.push(curList[i]);
  el.className = newList.join(" ");
}

function addWindowEvent(fn, evt) {  
	if(window.addEventListener) {
		window.addEventListener(evt,fn,false);
	}
	else if(window.attachEvent) {
		window.attachEvent("on" + evt,fn);
	}
	else if(window.onload) {
		var oldHandler = window.onload;
		window.onload = function piggyback()
		{
			oldHandler();
			fn();
		};
	}
	else {
		window["on" + evt] = fn;
	}
}

function addLoadHandler(handler)
{
	if(window.addEventListener) {
		window.addEventListener("load",handler,false);
	}
	else if(window.attachEvent) {
		window.attachEvent("onload",handler);
	}
	else if(window.onload) {
		var oldHandler = window.onload;
		window.onload = function piggyback()
		{
			oldHandler();
			handler();
		};
	}
	else {
		window.onload = handler;
	}
}

function addEvent(el, evType, fn, useCapture) {
  if (el.addEventListener) {
    el.addEventListener(evType, fn, useCapture);
    return true;
  }
  
  if (el.attachEvent) {
    var ret = el.attachEvent('on' + evType, fn);
    EventCache.add(el, evType, fn);
    return ret;
  }
  
  if (typeof(el['on' + evType]) == "function") {
    var oldFn = el['on' + evType];
    el['on' + evType] = function() { oldFn(); return fn(); }
  }
  else {
    el['on' + evType] = fn;
  }
}

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
}

Function.prototype.bindAsEventListener = function(object) {
var __method = this;
	return function(event) {
		__method.call(object, event || window.event);
	}
}

function setOpacity(el,opacity) {
	if (window.ActiveXObject) el.style.filter = "alpha(opacity=" + opacity*100 + ")";
	if (opacity > 0.99) { opacity = 0.99; }
	if (opacity < 0.01) { opacity = 0.01; }
	el.style.opacity = opacity;
}

if (document.all) {
  addEvent(window, 'unload', EventCache.flush, false);
}
