// helps
//
// 		thanks to 
//		- http://simon.incutio.com/archive/2004/05/26/addLoadEvent

var ht; // will contains some timeout
var is_help_active = false; // used to know wether helpo is visible or not - only for mouseover helps. When click helps, we rely on the classname

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
/*
AddEvent Manager (c) 2005-2006 Angus Turnbull http://www.twinhelix.com
Free usage permitted as long as this credit notice remains intact.
*/

if (typeof addEvent != 'function')
{
 var addEvent = function(o, t, f, l)
 {
  var d = 'addEventListener', n = 'on' + t, rO = o, rT = t, rF = f, rL = l;
  if (o[d] && !l) return o[d](t, f, false);
  if (!o._evts) o._evts = {};
  if (!o._evts[t])
  {
   o._evts[t] = o[n] ? { b: o[n] } : {};
   o[n] = new Function('e',
    'var r = true, o = this, a = o._evts["' + t + '"], i; for (i in a) {' +
     'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
     '} return r');
   if (t != 'unload') addEvent(window, 'unload', function() {
    removeEvent(rO, rT, rF, rL);
   });
  }
  if (!f._i) f._i = addEvent._i++;
  o._evts[t][f._i] = f;
 };
 addEvent._i = 1;
 var removeEvent = function(o, t, f, l)
 {
  var d = 'removeEventListener';
  if (o[d] && !l) return o[d](t, f, false);
  if (o._evts && o._evts[t] && f._i) delete o._evts[t][f._i];
 };
}

function cancelEvent(e, c)
{
 e.returnValue = false;
 if (e.preventDefault) e.preventDefault();
 if (c)
 {
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
 }
};
addLoadEvent(activate_js);

function activate_js() {
	var zebody = document.getElementsByTagName('body')[0];
	zebody.className =  zebody.className+' javascript-on';
	activate_help();
}

function activate_help() {
	var helps = $$('.help'); // the help (fieldset)
	// adds drag & drop
	$$('#content .help').each(function(drag){
		new Drag.Move(drag);
	});
	
	for(var i=0; i<helps.length; i++) { 
		// hides the help
		helps[i].className = helps[i].className+' hidden';
		// help vars
		var helpid = helps[i].id;
 		var helplink = document.getElementById('to-'+helpid);
		
		if ($$('map').length>0 && $$('map')[0].className == 'click') {
			// case of the click help (specified on the <map> tag as a class
			// adds event to show up the help
			if (helplink.addEventListener) helplink.addEventListener("click", toggle_help, false);
			else addEvent(helplink, 'click', toggle_help);
			// add the close button
			var closeNode = document.createElement('a');
			closeNode.className = 'close';
			if (helps[i].addEventListener) closeNode.addEventListener("click", direct_hide_help, false);
			else addEvent(closeNode, 'click', direct_hide_help);
			var closeTxt = document.createTextNode('X');
			closeNode.appendChild(closeTxt);
			helps[i].appendChild(closeNode);			
			
						
		} else {		
			// default case : mouseover
			// adds event to show up the help
			if (helplink.addEventListener) helplink.addEventListener("mouseover", toggle_help, false);
			else addEvent(helplink, 'mouseover', toggle_help);
			// adds visibility events on the help itself
			if (helps[i].addEventListener) {
			  helps[i].addEventListener("mouseout", hide_help, false);
			  helps[i].addEventListener("mouseover", keep_help, false);
			} else {
				addEvent(helps[i], 'mouseout', hide_help);
				addEvent(helps[i], 'mouseover', keep_help);		
			}
		}		
		
	}
}
function toggle_help(e) {
	// vars
	var helpid = this.id.substr(3);
	var help = document.getElementById(''+helpid);
	// first let's hide all helps (only for mouveover helps)
	if (help.className.indexOf('click')<0) clear_all_helps();
	// places the help zone
	var cursor = getPosition(e);
	var _x = cursor.x+'px';
	var _y = cursor.y+'px';
	help.style.left = _x;
 	help.style.top = _y;
	// toggles the help
	var origClass = help.className.substr(0, help.className.lastIndexOf(' '));
	if (help.className.indexOf('visible')>0) help.className = origClass + ' hidden';
	else help.className = origClass + ' visible';		
 	cancelEvent(e, false);
}
function hide_help(e) {
	is_help_active = false;
	ht = window.setTimeout("clear_help()",200);
}
function keep_help(e) {
	if (ht) window.clearTimeout(ht);
	is_help_active = true;
}
function clear_help() {
	if (is_help_active == false) {
		var helps = $$('.help');
  		for(var i=0; i<helps.length; i++) { 
			var origClass = helps[i].className.substr(0, helps[i].className.lastIndexOf(' '));
			helps[i].className = origClass+' hidden';
		}
	}
}
function clear_all_helps() {
	var helps = $$('.help');
	for(var i=0; i<helps.length; i++) { 
		var origClass = helps[i].className.substr(0, helps[i].className.lastIndexOf(' '));
		helps[i].className = origClass+' hidden';
	}
}

function direct_hide_help () {
	var help = this.parentNode;
	var origClass = help.className.substr(0, help.className.lastIndexOf(' '));
	help.className = origClass+' hidden';
} 


// cursor position - thanks to http://hartshorne.ca/2006/01/23/javascript_cursor_position/
function getPosition(e) {
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    return cursor;
}

