/*
 * filter.js
 * Handling of dynamic filtering
 *
 * Part of the dan pearlman theme
 *
 * @author Jack Weinert (lieblinx/CodeMonkz)
 */
function DP_Insert_MoreEntries(moreEntry)
{
	var parentNode = moreEntry.parentNode;
	if ((parentNode) && (HTML.HasElementClass(parentNode,'inAnimation') === false))
	{
		var moreContainer = HTML.GetSubElementByClass(parentNode,'moreEntries');
		if (moreContainer)
		{
			var newEntries = HTML.GetSubElementsByClass(moreContainer,'entry');
			
			for (var i in newEntries)
			{
				var newEntry = newEntries[i];
				HTML.AppendElementClass(newEntry,'hidden');
				parentNode.insertBefore(newEntry,moreEntry);
				
				var entryAni = new DNAnimation(newEntry);
				entryAni.AppendFrame(new DNWaitFrame(i * 50));
				entryAni.AppendFrame(new DNClassFrame(0,true,'textbottom'));
				entryAni.AppendFrame(new DNRemoveClassFrame('hidden'));
				entryAni.AppendFrame(new DNStyleValueFrame(250,true,'fontSize',0,100,'%','LINEAR'));
				entryAni.AppendFrame(new DNRemoveClassFrame('textbottom'));
				entryAni.StartAnimation();
				DNAniServer.AppendAnimation(entryAni);
			}
			
			// remove 'moreEntries' element
			parentNode.removeChild(moreContainer);
			parentNode.removeChild(moreEntry);
			
			HTML.AppendElementClass(parentNode,'inAnimation');
			
			var overallAni = new DNAnimation(parentNode);
			overallAni.AppendFrame(new DNWaitFrame((newEntries.length - 1) * 50 + 250));
			overallAni.AppendFrame(new DNRemoveClassFrame('inAnimation'));
			overallAni.StartAnimation();
			DNAniServer.AppendAnimation(overallAni);
		}
	}
}

function DPi_Animate_Menu(menu,timeOffset)
{
	var overallTime = 0;
	
	var parentNode = menu.parentNode;
	var menuEntries = HTML.NextSiblingTag(menu);
	if ((menuEntries) && (HTML.HasElementClass(menuEntries,'menuLevel1')))
	{
		if (HTML.HasElementClass(menuEntries,'hidden'))
		{
			// we have to show the entries
			//HTML.AppendElementClass(parentNode,'inAnimation');
			//HTML.AppendElementClass(parentNode,'hasOpenedEntry');
			//HTML.AppendElementClass(menuEntries,'openedEntry');
			
			var entries = HTML.GetSubElementsByClass(menuEntries,'entry',1);
			for (var i in entries)
			{
				var entry = entries[i];
				//HTML.AppendElementClass(entry,'hidden');
				
				var entryAni = new DNAnimation(entry);
				entryAni.AppendFrame(new DNWaitFrame(timeOffset));
				entryAni.AppendFrame(new DNClassFrame(0,true,'hidden'));
				entryAni.AppendFrame(new DNWaitFrame(i * 50 + timeOffset));
				entryAni.AppendFrame(new DNClassFrame(0,true,'textbottom'));
				entryAni.AppendFrame(new DNRemoveClassFrame('hidden'));
				entryAni.AppendFrame(new DNStyleValueFrame(250,true,'fontSize',0,100,'%','SQUARE'));
				entryAni.AppendFrame(new DNRemoveClassFrame('textbottom'));
				entryAni.StartAnimation();
				DNAniServer.AppendAnimation(entryAni);
			}
			
			overallTime = (entries.length - 1) * 50 + 250;
			//alert('overall time to show: ' + (entries.length - 1) + ' * 50 + 250 = ' + overallTime);
			var overallAni = new DNAnimation(parentNode);
			overallAni.AppendFrame(new DNWaitFrame(timeOffset));
			overallAni.AppendFrame(new DNClassFrame(0,true,'inAnimation'));
			overallAni.AppendFrame(new DNClassFrame(0,true,'hasOpenedEntry'));
			overallAni.AppendFrame(new DNWaitFrame(overallTime));
			overallAni.AppendFrame(new DNRemoveClassFrame('inAnimation'));
			overallAni.StartAnimation();
			DNAniServer.AppendAnimation(overallAni);
			
			var menuAni = new DNAnimation(menuEntries);
			menuAni.AppendFrame(new DNWaitFrame(timeOffset));
			menuAni.AppendFrame(new DNClassFrame(0,true,'openedEntry'));
			menuAni.AppendFrame(new DNRemoveClassFrame('hidden'));
			menuAni.StartAnimation();
			DNAniServer.AppendAnimation(menuAni);
			
			//HTML.RemoveElementClass(menuEntries,'hidden');
		}
		else
		{
			// we have to hide the entries
			//HTML.AppendElementClass(parentNode,'inAnimation');
			
			var entries = HTML.GetSubElementsByClass(menuEntries,'entry',1);
			var j=0;
			for (var i=entries.length - 1;i >= 0;i--)
			{
				var entry = entries[i];
				
				var entryAni = new DNAnimation(entry);
				entryAni.AppendFrame(new DNWaitFrame(j * 50 + timeOffset));
				entryAni.AppendFrame(new DNClassFrame(0,true,'textbottom'));
				entryAni.AppendFrame(new DNStyleValueFrame(250,true,'fontSize',100,0,'%','LINEAR'));
				entryAni.AppendFrame(new DNClassFrame(0,true,'hidden'));
				entryAni.AppendFrame(new DNRemoveClassFrame('textbottom'));
				entryAni.StartAnimation();
				DNAniServer.AppendAnimation(entryAni);
				
				j++;
			}
			
			var containerAni = new DNAnimation(menuEntries);
			containerAni.AppendFrame(new DNWaitFrame((j - 1) * 50 + 250 + timeOffset));
			containerAni.AppendFrame(new DNClassFrame(0,true,'hidden'));
			containerAni.AppendFrame(new DNRemoveClassFrame('openedEntry'));
			containerAni.StartAnimation();
			DNAniServer.AppendAnimation(containerAni);
			
			overallTime = (j - 1) * 50 + 250;
			//alert('overall time to hide: ' + (j - 1) + ' * 50 + 250 = ' + overallTime);
			var overallAni = new DNAnimation(parentNode);
			overallAni.AppendFrame(new DNWaitFrame(timeOffset));
			overallAni.AppendFrame(new DNClassFrame(0,true,'inAnimation'));
			overallAni.AppendFrame(new DNWaitFrame(overallTime));
			overallAni.AppendFrame(new DNRemoveClassFrame('inAnimation'));
			overallAni.AppendFrame(new DNRemoveClassFrame('hasOpenedEntry'));
			overallAni.StartAnimation();
			DNAniServer.AppendAnimation(overallAni);
		}
	}

	return overallTime;
}

function DP_Toggle_Menu(menu)
{
	var parentNode = menu.parentNode;
	
	if ((parentNode) && (HTML.HasElementClass(parentNode,'inAnimation') != true))
	{
		var handled = false;
		
		if (HTML.HasElementClass(parentNode,'hasOpenedEntry'))
		{
			// check if current menu is the opened one
			var menuEntries = HTML.NextSiblingTag(menu);
			if (!HTML.HasElementClass(menuEntries,'openedEntry'))
			{
				// another menu is opened, so close this first
				var openedMenuContainer = HTML.GetSubElementByClass(parentNode,'openedEntry');
				var openedMenu = HTML.PreviousSiblingTag(openedMenuContainer);
				var overallTime = DPi_Animate_Menu(openedMenu,0);
				DPi_Animate_Menu(menu,overallTime);
				handled = true;
			}
		}

		if (handled == false)
		{
			// no menu is opened, so just open this menu
			var overallTime = DPi_Animate_Menu(menu,0);
		}
	}
}
