// ddm - DROP DOWN MENU

	// Requires a call to ddmBar.timer.unload(); on unload

//TODO: .all - check to make sure object exists - degrade gracefully if not

function DdmTimer()
{
	this.id = 0;
	this.title = "";
	this.update = ddmTimerUpdate;
	this.tickNow = ddmTimerTickNow;
	this.state = 0;				// 0 = out/waiting, 1 = overDelay, 2=over, 3 = outDelay
	this.unload = ddmTimerUnload;
}

function ddmTimerUpdate(state,title)
{
	if(this.id)
	{
		clearTimeout(this.id);
		this.id = 0;
	}
	
	switch(state)
	{
		case 0:
			if(title != "")
			{
				document.all["ddmMenu" + title].style.visibility = "hidden";
				title = "";
			}
			break;
		
		case 1:	
			if(this.state == 0)
			{
				this.id = setTimeout("ddmOverDelayComplete('" + title + "')",25); 
				break;
			}
			if(this.title != title)
				document.all["ddmMenu" + this.title].style.visibility = "hidden";
			state = 2;
			
		case 2:
			var menu = document.all["ddmMenu" + title];
			
			if(menu.style.visibility != "visible")
			{
				var filter = menu.filters["revealTrans"]
				filter.apply();
				menu.style.visibility = "visible";
				filter.play();
			}
			break;
		
		case 3: 
			if(this.state == 2)
			{
				this.id = setTimeout("ddmOutDelayComplete('" + title + "')",250);
			}
			else
				state = 0;
			break;
	}

	this.title = title;
	this.state = state;
}

function ddmTimerUnload()
{
	ddmTimerUpdate(0,"");
}

function ddmOverDelayComplete(title)
{
	ddmBar.timer.update(2,title);
}

function ddmOutDelayComplete(title)
{
	ddmBar.timer.update(0,title);
}

function ddmBarLinkOver()
{
	var barLink = document.all["ddmBarLink" + this.title];
	barLink.className += "over";
	
	this.position();
	
	ddmBar.timer.update(1,this.title);
}

function ddmBarLinkOut()
{
	var barLink = document.all["ddmBarLink" + this.title];
	if(barLink.className.length >= 4)
		barLink.className = barLink.className.substring(0,barLink.className.length-4);

	ddmBar.timer.update(3, this.title);
}

function ddmMenuOver()
{
	ddmBar.timer.update(2, this.title);
}

function ddmMenuOut()
{
	ddmBar.timer.update(3, this.title);
}

function ddmTimerTickNow()
{
	clearTimeout(this.id);
	if(this.title != "")
		ddmOutDelayComplete(this.title);
}

function DdmBar()
{
	this.menus = new Array();
	this.menuExtended = null;
	this.menuExtended2 = null;
	this.baseURL = '';
	this.add = ddmMenuAdd;
	this.write = ddmBarWrite;
	this.menuActivate = ddmMenuActivate;
	this.menusWrite = ddmMenusWrite;
	this.headerWrite = ddmHeaderWrite;
	this.footerWrite = ddmFooterWrite;
	this.sideMenuWrite = ddmSideMenuWrite;
	this.timer = new DdmTimer("ddmTimerEvent",250);
}

function DdmMenu(ddmBar, title, url, target)
{
	this.ddmBar = ddmBar;
	this.index = ddmBar.menus.length;
	this.url = url;
	this.title = title;
	this.barLinkOver = ddmBarLinkOver;
	this.barLinkOut = ddmBarLinkOut;
	this.items = new Array();
	this.add = ddmItemAdd;
	this.write = ddmMenuWrite;
	this.sideWrite = ddmSideWrite;
	this.over = ddmMenuOver;
	this.out = ddmMenuOut;
	this.position = ddmMenuPosition;
}

function DdmItem(title, url, absolute, emphasize, target)
{
	this.menu = this;
	this.title = title;
	
	if(url)
		this.url = url;
		
	if(absolute)
		this.absolute = absolute;
		
	if(emphasize)
		this.emphasize = emphasize;
	else
		this.emphasize = false;

	if(target)
		this.target = target;
		
	this.write = ddmItemWrite;
	this.sideWrite = ddmSideItemWrite;
	
}

function ddmMenuAdd(title,url,target)
{
	this.menus[this.menus.length] = new DdmMenu(this,title,url,target);
	return this.menus[this.menus.length - 1];
}

function ddmMenuActivate(menuTitle)
{
	document.all["ddmBarLink" + menuTitle].className = "active";
}

function ddmItemAdd(title, url, absolute, emphasize, target)
{
	this.items[this.items.length] = new DdmItem(title, url, absolute, emphasize, target);
	return this.items[this.items.length - 1];
}

/*function ddmHeaderWrite()
{
	document.writeln('<table class="ddmHeader" align="center" cellpadding="0" cellspacing="0"><tr>');
	document.writeln('<td class="ddmHeader"><img src="' + this.baseURL + 'siteWide/logo.gif"></td>');
	document.writeln('<td><strong>Effective E-Learning Tools</strong></td>'); // <span class="seperator">&#149</span> Simple Solutions'); // <span class="seperator">&#149</span> Guaranteed Results</strong></td>');
	document.writeln('<td><strong>Sales: 1.800.959.2522</strong></td>');
	document.writeln('</tr></table>');
}*/
function ddmHeaderWrite()
{
	document.writeln('<table class="ddmHeader" align="center" cellpadding="0" cellspacing="0"><tr>');
	document.writeln('<td class="ddmHeader"><img src="' + this.baseURL + 'siteWide/logo.gif"></td>');
	document.writeln('<td><strong>Effective E-Learning Tools</strong></td>'); // <span class="seperator">&#149</span> Simple Solutions'); // <span class="seperator">&#149</span> Guaranteed Results</strong></td>');
	//document.writeln('<td><img src="' + this.baseURL + 'siteWide/FIPS.png"></td>'); 
	document.writeln('<td><strong>Sales: 1.800.959.2522</strong></td>');
	document.writeln('</tr></table>');
}

function ddmFooterWrite()
{
	document.writeln('<table class="ddmFooter" align="center" cellpadding="0" cellspacing="0">');
	document.writeln('<tr><td class="ddmFooter">');
	
	for(var menu in this.menus)
		document.writeln('<a href="' + this.baseURL + this.menus[menu].url + '">' + this.menus[menu].title + '</a> | ');

	document.writeln('<a href="' + this.baseURL + 'Privacy.htm">Privacy Policy</a> | ');
	document.writeln('<a href="' + this.baseURL + 'SiteMap.htm">Site Map</a>');
	document.writeln('<br>Copyright 2004 Persistech, LLC - "Infrature" is a registered trademark of Persistech, LLC.');
	document.writeln('</td></tr></table>');
}

function ddmBarWrite(baseURL)
{
	if(baseURL)
		this.baseURL = baseURL;
	else
		this.baseURL = '';
		
	this.headerWrite();

	document.writeln('<table class="ddmBar" align="center" cellpadding="0" cellspacing="0">');
	document.writeln('<tr>');
	
	for(var menu in this.menus)
	{
		document.writeln('<a href="' + this.baseURL + this.menus[menu].url + '" onmouseover="ddmBar.menus[' + menu + '].barLinkOver();" onmouseout="ddmBar.menus[' + menu + '].barLinkOut();">');
		document.writeln('<td id="ddmBarLink' + this.menus[menu].title + '">');
		document.writeln('&nbsp;&nbsp;' + this.menus[menu].title + '&nbsp;&nbsp;');
		document.writeln('</td></a>');
	}

	document.writeln('<td width=100% style="cursor: auto;">&nbsp;</td>');
	document.writeln('</tr></table>');
}

function ddmMenusWrite()
{
	for(var menu in this.menus)
		this.menus[menu].write();
}

function ddmMenuWrite()
{
	document.writeln('<div id="ddmMenu' + this.title + '" class="ddmMenu" onmouseover="ddmBar.menus[' + this.index + '].over();" onmouseout="ddmBar.menus[' + this.index + '].out();">');
	for(var item in this.items)
		this.items[item].write(this.ddmBar.baseURL, item);
	document.writeln('</div>');
	//document.writeln('<script>document.all["ddmMenu' + this.title + '"].style.zIndex = 1000;</script>');
}

function ddmItemWrite(baseURL, itemIndex)
{
	if(this.url)
	{
		document.writeln('<a href="' + baseURL + this.url + '"');
		if(this.target)
			document.write(' target="' + this.target + '"');
		document.writeln('>' + this.title + '</a>');
	}
	else
	{
		var Heading = "h2";
		if(itemIndex == 0)
			Heading = "h1";
		document.writeln('<' + Heading + '>' + this.title + '</' + Heading + '>');
	}
}

function ddmSideItemWrite(baseURL, itemIndex, itemActiveTitle)
{
	if(this.url)
	{
		if(this.absolute)
			document.write('<a href="' + this.url + '"');
		else
			document.write('<a href="' + baseURL + this.url + '"');
		
		if(this.target)
			document.write(' target="' + this.target + '"');
		
		if(this.title == itemActiveTitle)
			document.write(' class="ddmSideMenuActive"');
		else
			if(this.emphasize)
				document.write(' class="ddmSideMenuEmphasize"');
		document.write('><li>' + this.title + '</li></a>');
		document.writeln('<br>');
	}
	else
		document.write('<p>' + this.title + '</p>');
}

function ddmSideWrite(itemActiveTitle)
{
	document.writeln('<div class="ddmSideMenu"><ul>');
	for(var item in this.items)
		this.items[item].sideWrite(this.ddmBar.baseURL, item, itemActiveTitle);
	document.writeln('</ul></div>');
}

function ddmSideMenuWrite(menuTitle,itemActiveTitle)
{
	if(this.menuExtended)
		this.menuExtended.sideWrite(itemActiveTitle);
		
	for(var menu in this.menus)
		if(this.menus[menu].title == menuTitle)
			this.menus[menu].sideWrite(itemActiveTitle);

	if(this.menuExtended2)
		this.menuExtended2.sideWrite(itemActiveTitle);
}

function ddmMenuPosition()
{
	var baseElement = document.all["ddmBarLink" + this.title];
	var offsetElement = baseElement;
	var left = 0;
	var top = offsetElement.offsetHeight + 1;

	// CALCULATE THE POSITION BY WALKING THE OFFSET TREE	
	while(offsetElement)
	{
		top += offsetElement.offsetTop;
		left += offsetElement.offsetLeft;
		offsetElement = offsetElement.offsetParent;
	}
	
	// FIXUP FOR IE BODY MARGINS
	if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined")
	{
		left += document.body.leftMargin;
		top += document.body.topMargin;
	}
	
	var positionElement = document.all["ddmMenu" + this.title];
	
	// FIXUP FOR MENU GOING OFF PAGE
	if(left + positionElement.offsetWidth > document.body.offsetWidth)
	{
		var canidateLeft = left + baseElement.offsetWidth - positionElement.offsetWidth;
		if(canidateLeft >= 0)
			left = canidateLeft;
	}	

	positionElement.style.top = top;
	positionElement.style.left = left;
}

var ddmBar = new DdmBar();


