// Horizontal menu system for www.dianedoble.com
// Ver 1.0

// Set this to 1 for useful debug alerts
var debug=0;
var menuXOffset = 0;
var menuYOffset = 14;

//This function used courtesy of http://www.howtocreate.co.uk/tutorials/javascript/
function getRefToDiv(divID,oDoc) {
	if( document.getElementById ) {
		if(debug){window.alert('getRefToDiv is returning ' + document.getElementById(divID) + ' with divID being ' + divID); }
		return document.getElementById(divID);
	}
	if( document.all ) {return document.all[divID]; }
	if( !oDoc ) { oDoc = document; }
	if( document.layers ) {
		if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
		//repeatedly run through all child layers
			for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
			//on success, return that layer, else return nothing
			y = getRefToDiv(divID,oDoc.layers[x].document); }
			return y; }
		}
	return false;
}

//This function used courtesy of Scl software's Javascript Menu.  Original copyright info below
/*======================================================================
  Javascript Menu 1.0
  Copyright (c) 2008 SCL Software (http://www.sclsoftware.com)
  You can use it freely as long as all copyright messages are intact.
======================================================================*/
function getPosition(element){
	var posx = 0;
	var posy = 0;
	if (element.offsetParent){
		do{
			posx += element.offsetLeft;
            posy += element.offsetTop;
			if (!element.offsetParent){break;}}
		while (element = element.offsetParent)
	}
	else if (element.x)
	{
	posx += element.x;
	posy += element.y;
	}
	if(debug){window.alert('getPosition got ' + element + ' as the element and is returning x=' + posx + ' and y=' + posy);};
	return {x:posx, y:posy};
};

//This function used courtesy of Scl software's Javascript Menu.  Original copyright info below
/*======================================================================
  Javascript Menu 1.0
  Copyright (c) 2008 SCL Software (http://www.sclsoftware.com)
  You can use it freely as long as all copyright messages are intact.
======================================================================*/
function addEvent(element, evType, functionName, useCapture){
	if(element.addEventListener){
		element.addEventListener(evType, functionName, useCapture);
	}
	else if(element.attachEvent){
		element.attachEvent('on' + evType , functionName);
	}
	else
	{element['on' + evType] = functionName;}
};
	
// Main function to create menu items next ID'd elements
function createLocatedDiv(element,content,menuCssClass,visibilityStatus){
	var position = getPosition(getRefToDiv(element));
	menuDiv = document.createElement('div');
	menuDiv.id = element + 'div';
	menuDiv.style.left = (position.x + menuXOffset) + 'px';
	menuDiv.style.top = (position.y + menuYOffset) + 'px';
	if(visibilityStatus){menuDiv.style.visibility = 'visible';}
		else{menuDiv.style.visibility = 'hidden';}
	if(menuCssClass){menuDiv.className = menuCssClass;}
    addEvent(menuDiv, 'mouseover',function(){ showDiv(element, true); }, false);
	addEvent(menuDiv, 'mouseout', function(){ showDiv(element, false); } , false);
	menuDiv.innerHTML = content;
    if(debug){window.alert('The ID of this Div is ' + menuDiv.getAttribute('id'));};	
	document.body.appendChild(menuDiv);
}

//Show hide Div elements on mouseover/out events
function showDiv(element,state){
	if(debug){	window.alert('showDiv got ' + element + ' for element and ' + state + ' for state');};
	elementReference = getRefToDiv(element + "div");
	if(state){elementReference.style.visibility = 'visible'}
	else{	elementReference.style.visibility = 'hidden';}
	
	menuElement=getRefToDiv(element);
//	if(state){menuElement.style.background="#F8ECD9";
//	}else{menuElement.style.background="#fcf4e9";}
	if(state){menuElement.style.fontWeight="bold";
	}else{menuElement.style.fontWeight="normal";}
	
}

//Move div element on a resize event
function resize(elementList){
	if(debug){window.alert("Got resize event for element " + element);}
	for(var n=0; n<elementList.length; n++ ) {
		var newMenuPosition = getPosition(getRefToDiv(elementList[n])); //Get new position of the menu heading
		var menuElement = getRefToDiv(elementList[n]+'div'); //Get reference of menu item we created earlier
		//Now move the menu item
		menuElement.style.left = (newMenuPosition.x + menuXOffset) + 'px';
		menuElement.style.top = (newMenuPosition.y + menuYOffset) + 'px';
	}
}