var navigations = {};

function animate (navName, module)
{
	var n = getNavigationClass (navName);

	if (n.cursor == null)
	{
		n.cursor = document.getElementById (navName + '_navcursor');
	}
	
	n.currx = getElementPosition (n.cursor);
	n.tox   = Math.round (getElementPosition (module) + (module.offsetWidth / 2) - (n.cursor.offsetWidth / 2));
	n.force = 1;	
}

function go (navName, module)
{
	var n = getNavigationClass (navName);
	
	if (typeof module == "string")
	{
		module = document.getElementById (module);
		n.currModule = module;
	}
	if (module != null)
	{
		animate (navName, module);
	}
}

function back (navName)
{
	var n = getNavigationClass (navName);
	
	if (n.currModule != null)
	{
		animate (navName, n.currModule);
	}
}

function move()
{
	if (navigations == null || typeof navigations == "undefined")
	{
		return;
	}
	
	for (var navName in navigations)
	{
		var n = navigations[navName];
		
		if (n.cursor == null || n.force == 0)
		{
			continue;
		}
		
		n.force = (n.tox - n.currx) / 7;
		
		if (n.force > -0.4 && n.force < 0.4)
		{
			n.currx = n.tox;
			n.force = 0;
		}
			
		n.currx += n.force;
	
		n.cursor.style.left = n.currx + 'px';
	}
}

function getElementPosition (e)
{
	var x = e.offsetLeft;	
    var c = e.offsetParent;
    
    while (c)
    {
        x += c.offsetLeft;
        c = c.offsetParent;
    }
    
    return x;
}

function getNavigationClass (navName)
{
	if (navigations == null || typeof navigations == "undefined")
	{
		return null;
	}
	
	var n = navigations[navName];
	
	if (n == null)
	{
		n = {};
		
		n.force      = 0;
		n.cursor     = null;
		n.currModule = null;
		n.tox        = 0;
		n.currx      = 0;

		navigations[navName] = n;
	}
	
	return n;
}

setInterval (move, 20);
