var as_offsetX = -10;
var as_offsetY = 40;
var as_altOffsetX = -37;
var as_tipOffsetX = 20;
var as_tipOffsetY = -28;
var as_curOffsetX = as_offsetX;

var as_owner = null, as_tipContainer = null, as_tipBody = null;
var as_pointer = null, as_altPointer = null, as_curPointer = null;
var as_originalWidth = 0, as_originalHeight = 0;
var as_initialized = false, as_showing = false;
var as_opacity, as_opacity_timer;

function doTip (owner, bodyString, mouseX, mouseY) {
  as_owner = owner;
	if ( as_tipContainer == null ) {
		as_tipContainer = document.createElement('div');
		document.getElementsByTagName('body')[0].appendChild(as_tipContainer);
    if (typeof(as_pointer) == 'undefined') return;
		as_tipContainer.className = 'as_tipContainer';
		as_pointer=document.createElement('img');
		as_altPointer=document.createElement('img');
		as_pointer.src = "http://"+window.location.hostname+"/images/bubble_up.gif";
    as_altPointer.src = "http://"+window.location.hostname+"/images/bubble_down.gif";
		as_pointer.className = as_altPointer.className = 'as_pointer';
    as_tipContainer.appendChild(as_pointer);
    as_tipContainer.appendChild(as_altPointer);
    as_tipBody = document.createElement('div');
    as_tipContainer.appendChild(as_tipBody);
	}
  if (typeof(as_opacity_timer) != 'undefined' && as_opacity_timer) clearTimeout(as_opacity_timer);
  as_tipBody.innerHTML = bodyString;
  if (typeof(as_pointer) == 'undefined') return;
	as_originalHeight = as_tipContainer.offsetHeight;
	as_originalWidth = as_tipContainer.offsetWidth;
  as_pointer.style.top = as_tipOffsetY+'px';
	as_altPointer.style.top = as_originalHeight - 4 + 'px';
	as_curPointer = as_pointer;
	as_showing = true;
  as_initialized = false;
  if (as_owner.addEventListener) {
    as_owner.addEventListener('mousemove',moveTip,false);
    as_owner.addEventListener('mouseout',hideTip,false);
  } else if (as_owner.attachEvent) {
    as_owner.attachEvent('onmousemove',moveTip);	
    as_owner.attachEvent('onmouseout',hideTip);	
  }
  if (typeof(is_ie) == 'undefined') {
    as_opacity = 0;
    as_tipContainer.style.opacity = 0;
    increaseOpaque();
  }
  if (typeof(mouseX) != 'undefined' && mouseX != null && mouseX != '') moveTip(null, mouseX, mouseY);
}

function increaseOpaque () {
  as_opacity += 10;
  if (as_opacity <= 100) {
    as_tipContainer.style.opacity = as_opacity/100;
    as_opacity_timer = setTimeout(increaseOpaque,65);
  }
}

function decreaseOpaque () {
  as_opacity -= 10;
  if (as_opacity > 0) {
    as_tipContainer.style.opacity = as_opacity/100;
    as_opacity_timer = setTimeout(decreaseOpaque,65);
  } else killTip();
}

function moveTip (e, Xpos, Ypos) {
  if (typeof(as_showing) == 'undefined' ) return;
	if ( !as_showing ) {
		hideTip();
    return;
  }
  var windowX, windowY;
  if (e == null) {
    windowX = Xpos;
    windowY = Ypos;
  } else {
    if (!e) e = window.event;
  	windowX = (typeof(e.pageX) != 'undefined') ? e.pageX : e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  	windowY = (typeof(e.pageY) != 'undefined') ? e.pageY : e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  }
	var winwidth = (typeof(window.innerWidth) != 'undefined') ? window.innerWidth + self.pageXOffset - 20 : document.documentElement.clientWidth + document.documentElement.scrollLeft;
	var winBottom = (typeof(window.innerHeight) != 'undefined') ? window.innerHeight + self.pageYOffset - 20 : document.documentElement.clientHeight + document.documentElement.scrollTop;
  var height =  as_tipContainer.offsetHeight;
  var width =  as_tipContainer.offsetWidth;

	if (windowX + width + as_curOffsetX > winwidth ) {
		as_tipContainer.style.left = winwidth - width + 'px';
		as_curPointer.style.left = as_tipOffsetX + windowX + as_curOffsetX - as_tipContainer.offsetLeft +'px';
	} else {
		as_curPointer.style.left = as_tipOffsetX + 'px';
		as_tipContainer.style.left = windowX + as_curOffsetX + 'px';
	}

	if (windowY + height + as_offsetY > winBottom ){
		as_curPointer = as_altPointer;
    if (as_initialized || typeof(Xpos) != 'undefined') {
  		as_pointer.style.visibility='hidden';
  		as_altPointer.style.visibility='visible';
    }
		as_tipContainer.style.top = windowY - height - as_offsetY + 10 + 'px';
		as_curOffsetX = as_altOffsetX;
	} else {
		as_curPointer = as_pointer;
    if (as_initialized || typeof(Xpos) != 'undefined') {
  		as_altPointer.style.visibility='hidden';
  		as_pointer.style.visibility='visible';
    }
		as_curOffsetX = as_offsetX;
		as_tipContainer.style.top = windowY + as_offsetY + 'px';
	}
  if (as_initialized || typeof(Xpos) != 'undefined') as_tipContainer.style.visibility = 'visible';
  as_initialized = true;
}
function hideTip () {
  bCancelled = true;
	as_showing = false;
  if (as_opacity_timer) clearTimeout(as_opacity_timer);
  if (typeof(is_ie) == 'undefined') decreaseOpaque();
  else killTip();
}
function killTip () {
  as_tipContainer.style.visibility = 'hidden';
  as_pointer.style.visibility = 'hidden';
  as_altPointer.style.visibility = 'hidden';
  if (as_owner.removeEventListener) {
    as_owner.removeEventListener('mousemove',moveTip,false);
    as_owner.removeEventListener('mouseout',hideTip,false);
  } else if (as_owner.detachEvent) {
    as_owner.detachEvent('onmousemove',moveTip);
    as_owner.detachEvent('onmouseout',hideTip);
  }
}
