function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

// create Javascript enabled cookie, for sufficiently advanced js install
if (document.getElementById)
	createCookie('js', '1', 365);


//
// help tooltips
// enable by creating a link and a div like this:
// <a href="#" class="help-link"><div class="help-text">
//		Help text
// </div></a>
//

// global variable - reflects state of textdiv
var textdiv_active = false;

// global variable that stores current help-text div
var textdiv = null;
var textdiv_link = null;

// global variable that stores shadow div
var shadowdiv = null;

function create_shadowdiv()
{
	var objBody = document.getElementsByTagName("body").item(0);
	
	shadowdiv = document.createElement("div");
	shadowdiv.className = 'help-shadow';
	objBody.appendChild(shadowdiv);
}

function show_helptext(event)
{
	if (event == null)
		event = window.event;
		
	var target = event.target || event.srcElement;

	// if we wind up on an image contained within the link, we need to get the link itself
	while (target && target.tagName != 'A')
		target = target.parentNode;
		
	if (!target)
	    return;

	// document.body.scrollTop does not work in IE
	var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
	var scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;

	if (Element.hasClassName(target, "help-link"))
	{
	    var replace = false;
	    
		// do drop shadow
		if (!shadowdiv)
			create_shadowdiv();

        // note this should be null if textdiv is active
		var new_textdiv = document.getElementsByClassName('help-text', target)[0];
		
		if (!textdiv || !textdiv_active)
		{
		    textdiv = new_textdiv;
		    textdiv_link = target;
		    replace = true;
		}
		// did we somehow miss an opportunity to hide the textdiv?
		else if (textdiv_active && new_textdiv)
		{
		    event.target = textdiv_link;
		    hide_helptext(event);
		    
		    textdiv = new_textdiv;
		    textdiv_link = target;
		    replace = true;
		}

		if (textdiv)
		{
    		// move textdiv to the body element so the absolute positioning works
		    if (replace)
		    {
			    target.removeChild(textdiv);
			    document.body.appendChild(textdiv);
    			
			    shadowdiv.innerHTML = textdiv.innerHTML;
			    textdiv_active = true;
		    }
			
			// hide the div first to avoid an "up-then-over" visual effect
			textdiv.style.display = 'none';
			textdiv.style.left = event.clientX + scrollLeft + 20 + 'px';
			textdiv.style.top = event.clientY + scrollTop - 5 + 'px';
			textdiv.style.display = 'block';
		
			shadowdiv.style.display = 'none';
			shadowdiv.style.left = event.clientX + scrollLeft + 25 + 'px';
			shadowdiv.style.top = event.clientY + scrollTop + 'px';
			shadowdiv.style.display = 'block';
		}
	}

	return false;
}

function hide_helptext(event)
{
	if (textdiv)
	{
		textdiv.style.display = 'none';
	
		// re-attach textdiv to the link
		if (textdiv.parentNode == document.body)
		{
			document.body.removeChild(textdiv);
			textdiv_link.appendChild(textdiv);
		}
			
		// do drop shadow
		if (!shadowdiv)
			create_shadowdiv();

		shadowdiv.style.display = 'none';
	}
	
	textdiv_active = false;

	return false;
}


function init_helplinks()
{
	// sometimes init_helplinks will be called in response to an AJAX update
	// when this happens we lose our handle on the current textdivs, so hide them
	if (textdiv)
		textdiv.style.display = 'none';
	if (shadowdiv)
		shadowdiv.style.display = 'none';
		
	helplinks = document.getElementsByClassName('help-link');
	helplinks.each(function(helplink, index) {
		helplink.onmouseover = show_helptext;
		helplink.onmousemove = show_helptext;
		helplink.onmouseout = hide_helptext;
	});
}

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



