var the451 = {
   Event : {
    attach: function(event_type, e, action) {
      if(e.addEventListener) {
        e.addEventListener(event_type, action, false);
      } else if(e.attachEvent) {
        e.attachEvent('on'+event_type, action);
      } else {
        eval('e.on'+event_type+'='+action);
      }
      return;
    },
    element: function(e) {
      var targ; //from quirksmode...
    	if (!e) var e = window.event;
    	if (e.target) targ = e.target;
    	else if (e.srcElement) targ = e.srcElement;
      if (targ.nodeType == 3) targ = targ.parentNode;
    	return targ;
    },
    findElement: function(e_tagName, e) {
      if(e && e.tagName) var el = e;
      else var el = the451.Event.element(e);
      if(!el.tagName || !el.tagName.match(new RegExp(e_tagName, 'i'))) return the451.Event.findElement(e_tagName, el.parentNode);
      return el;
    }
  },
  Element: {
    hasClassName: function(e, e_className) {
      if(!e) return false;
      if(!e.className) return false;
      var classNames = e.className.split(' ');
      for(var i = 0; i < classNames.length; i++) {
        if(classNames[i].match(new RegExp(e_className, 'i'))) return true;
      }
      return false;
    },
    getElementsByClassName: function(e_className, e) {
      if(!e) var e = document.body;
      var descendant_elements_in_class = [];
      if(e.childNodes && e.childNodes.length > 0) {
        for(var i = 0; i < e.childNodes.length; i++) {
          if(e.childNodes[i].hasChildNodes()) {
            var descendant_elements_in_class_temp = the451.Element.getElementsByClassName(e_className, e.childNodes[i]);
            if(descendant_elements_in_class_temp.length > 0) {
              var offset = descendant_elements_in_class.length;
              for(var k = 0; k < descendant_elements_in_class_temp.length; k++) {
                descendant_elements_in_class[offset+k] = descendant_elements_in_class_temp[k];
              }
            }
          } else if(the451.Element.hasClassName(e.childNodes[i], e_className)) descendant_elements_in_class[descendant_elements_in_class.length] = e.childNodes[i];
        }
      } else return [];
      var elements_in_class = descendant_elements_in_class;
      if(the451.Element.hasClassName(e, e_className)) elements_in_class[elements_in_class.length] = e;
      return elements_in_class;
    },
    addClassName: function(e_className, e) {
      if(!the451.Element.hasClassName(e, e_className)) e.className += ' '+e_className;
      return e;
    },
    removeClassName: function(e_className, e) {
      if(!the451.Element.hasClassName(e, e_className)) return e;
      e.className = e.className.replace(new RegExp(e_className, 'i'), '');
      e.className = e.className.replace(/^\s/, '');
      e.className = e.className.replace(/\s$/, '');
      return e;
    }
  }
}

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

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return {x: curleft, y: curtop};
}

var callLater = function(a, b, c) { return function() { a(b, c); } };

var focusOnSponsor = function(el, link, e) {
 var firedAt = new Date();
 document.sponsorQueue = { element: el, link: link, time: firedAt.getTime(), pos: {x: e.clientX, y: e.clientY} };
}

var removeFocusFromSponsor = function(el, link) {
 if(!document.sponsorQueue) return;
 if(document.sponsorQueue.link == link) document.sponsorQueue = false;
 return;
}

mustHoverFor = 800;
queueInterval = 50;

var doQueue = function() {
 if(!document.sponsorQueue) return;
 var time = new Date();
 if(time.getTime() >= document.sponsorQueue.time + mustHoverFor) {
  sponsorPopup.style.display = 'none';
  sponsorPopup.innerHTML = '<p>Would you like to download an overview of this firm?</p>';
  //sponsorPopup.innerHTML += '<p><a href="/whitepaper.php?s='+document.sponsorQueue.element.id+'">yes</a></p>'
	sponsorPopup.innerHTML += '<p><a href="whitepaper.php?s='+document.sponsorQueue.element.id+'">yes</a></p>'
                            +'<p><a href="javascript: void(0);" onclick="javascript: return hideSponsorPopup();">no</a></p>';
  var pos = findPos(document.sponsorQueue.element);
  //var pos = document.sponsorQueue.pos;
  sponsorPopup.style.left = (pos.x+document.sponsorQueue.element.width)+'px';
  sponsorPopup.style.top = (pos.y - document.sponsorQueue.element.height)+'px';
  sponsorPopup.style.display = 'block';
  removeFocusFromSponsor(document.sponsorQueue.el, document.sponsorQueue.link);
 }
}

var hideSponsorPopup = function() {
  sponsorPopup.style.display = 'none';
  return;
}

var init = function() {
  var sponsors = the451.Element.getElementsByClassName('has_whitepaper', document.body);
  for(var s = 0; s < sponsors.length; s++) {
    sponsors[s].style.zIndex = 999;
    sponsors[s].style.border = '2px solid #fff';
    the451.Event.attach('mouseover', sponsors[s], function(e) {
      focusOnSponsor(the451.Event.findElement('img', e), the451.Event.findElement('a', e), e);
      the451.Event.element(e).style.border = '2px solid #ccc';
    });
    the451.Event.attach('mouseout', sponsors[s], function(e) {
      removeFocusFromSponsor(the451.Event.findElement('img', e), the451.Event.findElement('a', e));
      the451.Event.element(e).style.border = '2px solid #fff';
    });
  }
  sponsorPopup = document.createElement('div');
  document.body.appendChild(sponsorPopup);
  sponsorPopup.id = 'sponsorPopup';
  sponsorPopup.style.height = '180px';
  sponsorPopup.style.width = '250px';
  //sponsorPopup.style.padding = '20px';
  sponsorPopup.style.overflow = 'hidden';
  sponsorPopup.style.backgroundImage = 'url(/images/bubble_w_shadow.png)';
  sponsorPopup.style.backgroundRepeat = 'no-repeat';
  sponsorPopup.style.fontSize = '12px';
  sponsorPopup.style.position = 'absolute';
  sponsorPopup.style.display = 'none';
  setInterval(doQueue, queueInterval);
}

the451.Event.attach('load', window, init);

