﻿window.onload = allfunctions;

var closeing;
var closeingSub;
var current;
var currentSub;
var href;

function allfunctions()
{
	document.onclick = closeMenu;
	
	var menus = document.getElementById('menu').getElementsByTagName('a');
	
	for(i = 0; i < menus.length; i++)
	{
		if(menus[i].className == 'dropMenuItem')
		{
			menus[i].onmouseover = openMenu;
			menus[i].onmouseout = delayedClose;
		}
		
		if(menus[i].className == 'secondMenu')
		{
			menus[i].onmouseover = openSub;
		}
		else if(menus[i].className != 'dropMenuItem' && menus[i].className != 'secondLinks')
		{
			menus[i].onmouseover = closeSub;
		}

		menus[i].onclick = workSession;
	}
	
	var submenus = document.getElementsByTagName('div');
	
	for(i = 0; i < submenus.length; i++)
	{
		if(submenus[i].className == 'dropSub' || submenus[i].className == 'secondSub')
		{
			submenus[i].onmouseover = cancelDelay;
			submenus[i].onmouseout = delayedClose;
		}
	}
}

function openMenu()
{	
	cancelDelay();
	closeSub();
	
	if(document.activeElement)
	{
		//document.activeElement.blur();
	}

	if(current) 
	{
		current.style.visibility = 'hidden';
	}
	
	if(document.getElementById('drop' + this.id))
	{
		current = document.getElementById('drop' + this.id);
		current.style.width = this.firstChild.width;
		current.style.visibility = 'visible';
	}
}

function openSub()
{	
	cancelDelay();
	
	if(document.activeElement)
	{
		//document.activeElement.blur();
	}

	if(currentSub) 
	{
		currentSub.style.visibility = 'hidden';
	}

	currentSub = document.getElementById('drop' + this.id);
	var currentWidth = current.style.width.split('px');
	currentWidth[0] -= 1;
	currentSub.style.marginLeft = currentWidth[0] + 'px';
	currentSub.style.marginTop = this.offsetTop;
	currentSub.style.visibility = 'visible';
}

function closeMenu()
{
	if(current) 
	{
		current.style.visibility = 'hidden';
	}
}

function closeSub()
{
	if(currentSub) 
	{
		currentSub.style.visibility = 'hidden';
	}
}

function delayedClose()
{
	closeing = window.setTimeout(closeMenu, 500);
	closeingSub = window.setTimeout(closeSub, 500);
}

function cancelDelay()
{
	if(closeing)
	{
		window.clearTimeout(closeing);
		closeing = null;
	}
	
	if(closeingSub)
	{
		window.clearTimeout(closeingSub);
		closeing = null;
	}
}

function workSession()
{ 
	href=this.href;
	xmlHttp=GetAXmlHttpObject();
	
	if(xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	var url = "processing/sessionAJAX.php";
	
	if(this.id != 'preserveEdit')
	{
		url += "?function=delete";
	}
	else
	{
		url += "?function=create";	
	}
	url += "&sid=" + Math.random();

	xmlHttp.onreadystatechange=stateHasChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}

function stateHasChanged()
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		//document.getElementById('rightContentDiv').innerHTML=xmlHttp.responseText;
		document.location = href;
	} 
}

function GetAXmlHttpObject()
{
	var xmlHttp=null;
	
	try
	{
		// Firefox, Opera 8.0+, Safari
		
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
