/* A List with tooltip based on the example of map from AList Apart*/	
var listWithTooltip = {
	offsetX: -16, // tooltip X offset
	offsetY: 16,  // tooltip Y offset
	element: false,
	DLs:     false,
	DTs:     false,
	DDs:     false,
	on:      false,
	/* constructor - sets events */
	init: function(){
		var i=0;
		var ii=0;
		var currentLocation = 0;
		listWithTooltip.DLs = document.getElementsByTagName('dl');
		listWithTooltip.DTs = document.getElementsByTagName('dt');
    listWithTooltip.DDs = document.getElementsByTagName('dd'); 
		// only loop thru items once
		if( listWithTooltip.on == false ){
			//loop through each DL on page
			while (listWithTooltip.DLs.length > i) {
				//only affect DLs with a class of 'map'
				if (listWithTooltip.DLs[i].className == 'liste_avec_tooltip'){
					//change map DL class, this way map is text only without javascript enabled
					//listWithTooltip.DLs[i].className = 'list_with_tooltip on';
					//remove any white space
					listWithTooltip.stripWhitespace(listWithTooltip.DLs[i]);
					listWithTooltip.stripWhitespace(listWithTooltip.DTs[i]);
          listWithTooltip.stripWhitespace(listWithTooltip.DDs[i]); 
					// loop thru all DT elements
					while (listWithTooltip.DTs.length > ii){
						//current Location
						currentLocation = listWithTooltip.DTs[ii].firstChild;
						// add events to links
						listWithTooltip.addEvt(currentLocation,'mouseover',listWithTooltip.showTooltip);//displays tooltip on mouse over
						listWithTooltip.addEvt(currentLocation,'mouseout',listWithTooltip.hideTooltip);//hide tooltip on mouse out
						listWithTooltip.addEvt(currentLocation,'focus',listWithTooltip.showTooltip);//display tooltip on focus, for added keyboard accessibility
						listWithTooltip.addEvt(currentLocation,'blur',listWithTooltip.hideTooltip);//hide tooltip on focus, for added keyboard accessibility
						ii++;
					};
					ii=0;
				};
				i++;
			};
			listWithTooltip.on = true;
		};
	},
	/* SHOW TOOLTIP */
	showTooltip: function() {
		var evt = this;
		var i = 0;
		//Find DD to display - based on currently hovered anchor move to parent DT then next sibling DD
		var objid = evt.parentNode.nextSibling;
		if(objid!=null) {
		  listWithTooltip.element = objid;//set for the hideTooltip
		  //get width and height of background map
		  var mapWidth  = objid.parentNode.offsetWidth;
		  var mapHeight = objid.parentNode.offsetHeight;
		  //get width and height of the DD
		  var toopTipWidth  = objid.offsetWidth;
		  var toopTipHeight = objid.offsetHeight;
		  //figure out where tooltip should be places based on point location
		  var newX = listWithTooltip.offsetX ;
      if(window.event) {
        newX+=window.event.x ;
      } else {
        newX+=evt.offsetLeft;
      };
      var newY = listWithTooltip.offsetY ;
      if(window.event) {
        newY+=window.event.y ;
      } else {
        newY+=evt.offsetTop;
      };
		  //check if tooltip fits map width 
		  if ((newX + toopTipWidth) > mapWidth) {
			 objid.style.left = newX-toopTipWidth-24 + 'px';
		  } else {
			 objid.style.left = newX + 'px';
		  };
		  //check if tooltip fits map height 
		  if ((newY + toopTipHeight) > mapHeight) {
			 objid.style.top = newY-toopTipHeight-14 + 'px';
		  } else {
			 objid.style.top = newY + 'px';
		  };
    }
	},
	/* HIDE TOOLTIP */
	hideTooltip: function() {
	 if(listWithTooltip.element!=null) {
		  listWithTooltip.element.style.left = '-9999px';
		}
	},
	addEvt: function(element, type, handler) {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = listWithTooltip.addEvt.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			};
		};
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = listWithTooltip.handleEvent;
	},
	handleEvent: function(event) {
		var returnValue = true;
		// grab the event object (IE uses a global event object)
		event = event || listWithTooltip.fixEvent(window.event);
		// get a reference to the hash table of event handlers
		var handlers = this.events[event.type];
		// execute each event handler
		for (var i in handlers) {
			this.$$handleEvent = handlers[i];
			if (this.$$handleEvent(event) === false) {
				returnValue = false;
			};
		};
		return returnValue;
	},
	fixEvent: function(event) {
		// add W3C standard event methods
		event.preventDefault = listWithTooltip.fixEvent.preventDefault;
		event.stopPropagation = listWithTooltip.fixEvent.stopPropagation;
		return event;
	},
	stripWhitespace: function( el ){
		for(var i = 0; i < el.childNodes.length; i++){
			var node = el.childNodes[i];
			if( node.nodeType == 3 && !/\S/.test(node.nodeValue)) {
				node.parentNode.removeChild(node);
			};
		};
	}
};
listWithTooltip.fixEvent.preventDefault = function() {this.returnValue = false;};
listWithTooltip.fixEvent.stopPropagation = function() {this.cancelBubble = true;};
listWithTooltip.addEvt.guid = 1;

function hover(obj){
  if(document.all){
    UL = obj.getElementsByTagName('ul');
    if(UL.length > 0){
      sousMenu = UL[0].style;
      if(sousMenu .display == 'none' || sousMenu.display == ''){
        sousMenu.display = 'block';
      }else{
        sousMenu.display = 'none';
      }
    }
  }
}

function setHover(){
  ul = document.getElementById('navigation')
  if(ul!=null) {
    LI = ul.getElementsByTagName('li');
    nLI = LI.length;
    for(i=0; i < nLI; i++){
      LI[i].onmouseover = function(){
        hover(this);
      }
      LI[i].onmouseout = function(){
        hover(this);
      }
    }
  }
}

/**
 * This method change the class of the element whose the id value is equal to <code>id</code>
 * @param id        the identifier of a element in the page
 * @param newClass  the new value of the class attribute of the element
 */ 
function changeClass(id, newClass) {
  var identity=document.getElementById(id);
  identity.className=newClass;
}

// ouverture d'une fen?e pour afficher une image
function fenetre(image, largeur, hauteur, titre) {
    window.open("album_image.php?img=" + image + "&larg=" + largeur + "&haut=" + hauteur + "&titre=" + escape(titre), "", "width=" + largeur + ",height=" + hauteur);
}

/****        Scripts to manage text ***/
function replaceText(el, text) {
   if(el!=null) {
        // Clear all the nodes
        clearNodes(el) ;
        // create a new text node
        var textNode=document.createTextNode(text);
        // Append the new node
        el.appendChild(textNode);
    }
}

function clearNodes(el) {
    if(el!=null){
        if(el.childNodes) {
            for(var i=0;i<el.childNodes.length;i++) {
                var childNode=el.childNodes[i];
                el.removeChild(childNode);
            }
        }
    }
}

function getText(el) {
    var text="";
    if(el!=null) {
        if(el.childNodes) {
            for(var i=0;i<el.childNodes.length;i++) {
                var childNode=el.childNodes[i];
                // Element nodes and other non-text nodes have a null node value
                if(childNode.nodeValue!=null) {
                    text=text+childNode.nodeValue;
                }
            }
        }
    }
    // Return the text value
    return text;
}
