// CONFIG

var show_popbox_after = 500;								// wait period before showing popbox in milliseconds
var hide_popbox_after = 400;								// wait period before hiding popbox in milliseconds
var id_main_content = 'maincontent';				// ID of the element containing all the popbox-links
var popbox_link_class = 'link';							// Class used to identify links that need the popbox
var id_popbox = 'popbox';										// ID of the popbox element
var id_popbox_main = 'popbox_main';					// ID of the popbox main element
var id_popbox_ad = 'popbox_ad';							// ID of the popbox ad element

// DO NOT EDIT BELOW THIS LINE

var popbox;
var popbox_main;
var popbox_ad;
var popbox_active = false;
var popbox_set_interval;
var popbox_unset_interval;
var current_object;

function beansPopbox() {
	// Set cookie
	var ms_epoch = new Date().getTime();
	var stamp = Math.round(ms_epoch / 1000);
	var last_visit = getCookie('last_visit');
	if (last_visit == null || last_visit == 0) {
		last_visit = stamp;
	}
	setCookie('last_visit', stamp);
	
	// Do some stuff
	if (document.getElementById(id_main_content) && document.getElementById(id_popbox) && document.getElementById(id_popbox_main) && document.getElementById(id_popbox_ad)) {
		popbox = document.getElementById(id_popbox);
		popbox_main = document.getElementById(id_popbox_main);
		popbox_ad = document.getElementById(id_popbox_ad);
		
		popbox.onmouseover = function() {
			popbox_active = true;
		}
		popbox.onmouseout = function() {
			popbox_active = false;
			popbox_unset_interval = setTimeout("hidePopbox()",hide_popbox_after);
		}
		
		linkRoot = document.getElementById(id_main_content);
		urls = linkRoot.getElementsByTagName('A');
		for (i = 0; i < urls.length; i++) {
			node = urls[i];
			if (node.className == popbox_link_class) {
				// Mark as new stuff
				if(node.rel > last_visit) {
					node.className = 'link newlink';
      	}
				
				// Popbox stuff
				node.onmouseover = function() {
					clearTimeout(popbox_unset_interval);
					hidePopbox(true);
					current_object = this;
					popbox_set_interval = setTimeout("showPopbox()",show_popbox_after);
				}
  			node.onmouseout = function() {
  				clearTimeout(popbox_set_interval);
  				popbox_unset_interval = setTimeout("hidePopbox()",hide_popbox_after);
				}
			}
		}
	}
}

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

function showPopbox() {
	var content_part;
	var nodes = [];
	var el = current_object.parentNode.parentNode.childNodes;
	for (i = 0; i < el.length; i++) {
		if (el[i].nodeName.toLowerCase() == 'dd') {
			content_part = el[i].firstChild.nodeValue;;
		}
	}
	
	// delete existing nodes
	if (title_element = document.getElementById('popbox_title')) {
		popbox_main.removeChild(title_element);
	}
	if (content_element = document.getElementById('popbox_content')) {
		popbox_main.removeChild(content_element);
	}
	
	// create nodes and set the text in the popbox
	// create title
	var popbox_title = document.createElement("h3");
	popbox_title.setAttribute("id", "popbox_title");
	var popbox_title_text = document.createTextNode(current_object.firstChild.nodeValue);
	popbox_title.appendChild(popbox_title_text);
	popbox_main.insertBefore(popbox_title, popbox_ad);
	
	// create content
	var popbox_content = document.createElement("div");
	popbox_content.setAttribute("id", "popbox_content");
	var popbox_content_text = document.createTextNode(content_part);
	popbox_content.appendChild(popbox_content_text);
	popbox_main.insertBefore(popbox_content, popbox_ad);
	
	// position and show the popbox
	var pos = findPos(current_object);
	popbox.style.left = (pos[0] + 50) + 'px';
	popbox.style.top = (pos[1] + 22) + 'px';
	popbox.style.display = 'block';
}

function hidePopbox(force) {
	if (popbox_active == false || force == true) {
		popbox.style.display = '';
	}
}

addLoadEvent(beansPopbox);