/*
	The Mindlab
	For Morevsego.co.uk
*/

function open(src)
{
	src.style.display = '';
}

function close(src)
{
	src.style.display = 'none';
}

function swap(e, src)
{
	if(!e)
		var e = window.event;
	var targ;
	if(e.target)
		targ = e.target;
	else if(e.srcElement)
		targ = e.srcElement;
	if(src != targ)
		return false;

	var x = src;

	// Go up one level
	x = x.parentNode;

	var last_clicked = null;

	// Find table child
	for(var i=0; i<x.childNodes.length; i++)
	{
		if(x.childNodes[i].nodeName == 'TABLE')
		{
			// Collapse the table
			if(x.childNodes[i].style.display == 'none')
			{
				open(x.childNodes[i]);
				createCookie(x.childNodes[i].getAttribute("name"), 365);
			}
			else
			{
				close(x.childNodes[i]);
				eraseCookie();
			}
			last_clicked = x.childNodes[i];
		}
	}

	// Close all except the one we just modified
	var x = document.getElementsByTagName("table");
	for(var i=0; i<x.length; i++)
	{
		if(x[i].getAttribute("id") == "link_group")
		{
			if(x[i] != last_clicked)
			{
				// Hide
				close(x[i]);
			}
		}
	}

	return false;
}

function close_all()
{
	// Close all folder groups
	var x = document.getElementsByTagName("table");
	for(var i=0; i<x.length; i++)
	{
		if(x[i].getAttribute("id") == "link_group")
		{
			// Hide
			close(x[i]);
		}
	}

	// Delete the cookie
	eraseCookie();
}

function eat_cookies()
{
	var opened_name = readCookie();

	var e = document.getElementsByTagName("td");
	for(var i=0; i<e.length; i++)
	{
		if(e[i].getAttribute("id") == "folder")
		{
			for(var j=0; j<e[i].childNodes.length; j++)
			{
				if(e[i].childNodes[j].nodeName == 'TABLE')
				{
					var group = e[i].childNodes[j];
					if(group.getAttribute("id") == "link_group")
					{
						if(opened_name == group.getAttribute("name"))
						{
							// Show it
							open(group);
						}
						else
						{
							// Hide by default
							close(group);
						}
					}
				}
			}
		}
	}
}

function createCookie(opened_name, days)
{
	if(days)
	{
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = "nav_sel=" + opened_name + expires + "; path=/";
}

function readCookie()
{
	var nameEQ = "nav_sel=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while(c.charAt(0) == ' ')
			c = c.substring(1,c.length);
		if(c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie()
{
	createCookie("", -1);
}

