/* --------- */
/* UTILITIES */
/* --------- */
// Function taken from http://onlinetools.org/articles/unobtrusivejavascript/cssjsseparation.html
function jscss(a,o,c1,c2)
{
	switch (a)
	{
		case 'swap':
		  o.className = !jscss('check',o,c1) ? o.className.replace(c2,c1) : o.className.replace(c1,c2);
		break;
		case 'add':
		  if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
		break;
		case 'remove':
		  var rep = o.className.match(' '+c1) ? ' '+c1 : c1;
		  o.className = o.className.replace(rep,'');
		break;
		case 'check':
		  return new RegExp('\\b'+c1+'\\b').test(o.className)
		break;
	}
}

function maximize( element, maxsize, step, onfinish )
{
	if ( !element )
		return;
	
	var newHeight = element.clientHeight + step;
	if ( newHeight >= maxsize )
	{
		element.style.height = maxsize + "px";
		if ( onfinish )
			onfinish();
	}
	else
		element.style.height = newHeight + "px";
}

function minimize( element, minsize, step, onfinish )
{
	if ( !element )
		return;
	
	var newHeight = element.clientHeight - step;
	if ( newHeight <= minsize )
	{
		element.style.height = minsize + "px";
		if ( onfinish )
			onfinish();
	}
	else
		element.style.height = newHeight + "px";
}

function closeElement( element )
{
	if ( element )
		element.style.display = "none";
}

function noEnter(e,o)
{
	var keynum = 0;
	if ( !o ) return;
	
	if( window.event ) // IE
		keynum = e.keyCode;
	else if( e.which ) // Netscape/Firefox/Opera
		keynum = e.which;

	if ( keynum == 13 ) // 13 == Enter Key
	{
		o.blur();
		o.focus();
	}
		
	return (keynum != 13);
}

var fileBoxes = new Array();
function createFileEntry( container )
{
	if ( !container )
		return;
		
	var num = fileBoxes.length + 1;
	var newEntry = document.createElement('input');
	newEntry.setAttribute('type', 'file');
	newEntry.setAttribute('name', 'file'+num);
	newEntry.setAttribute('size', '30');
	newEntry.setAttribute('class', 'fileinput');
	
	container.appendChild( newEntry );
	fileBoxes.push( newEntry );
}

function removeLastFileEntry( container )
{
	if ( !container )
		return;
	container.removeChild( fileBoxes.shift() );
}

/* ---------- */
/* Admin Menu */
/* ---------- */
var elemMenu = null;
var elemMenuBtn = null;
var menuState = "notset";
var interID = -1;
var menuHeightDown = 0;
var menuHeightStep = 1;
var menuHeight = 0;

var menuMaximizeCallback = function()
{
	menuState = "down";
	setMenuBtnText();
	clearInterval(interID);
	interID = -1;
}

var menuMinimizeCallback = function()
{
	menuState = "up";
	setMenuBtnText();
	clearInterval(interID);
	interID = -1;
}

function setMenuBtnText()
{
	if ( menuState == "up" )
		elemMenuBtn.firstChild.nodeValue = "\u02C5 Click to show the Admin Menu \u02C5";
	else
		elemMenuBtn.firstChild.nodeValue = "\u02C4 Click to hide the Admin Menu \u02C4";
}

function toggleMenu()
{
	if ( !elemMenu || !elemMenuBtn )
		return;
	
	if ( menuState == "notset" ) {
		alert("You must call setupMenuState(state) prior to using this function!");
		return;
	}
	
	// Don't do anything if we are moving the menu
	if ( menuState == "trans" || interID != -1 )
		return;
	
	menuHeightStep = menuHeightDown / 12;
		
	if ( menuState == "up" )
		interID = setInterval( "maximize(elemMenu, menuHeightDown, menuHeightStep, menuMaximizeCallback)", 50 );
	else
		interID = setInterval( "minimize(elemMenu, 0, menuHeightStep, menuMinimizeCallback)", 50 );
		
	menuState = "trans";
}

function setupMenuState( state )
{
	// Figure out our maximum height by our children
	var i, maxHeight = 0, menuItems = elemMenu.getElementsByTagName("div");
	for ( i=0; i < menuItems.length; i++ )
	{
		if ( menuItems[i].clientHeight > maxHeight )
			maxHeight = menuItems[i].clientHeight;
	}
	
	// Load in our variables
	menuHeightDown = maxHeight;
	menuState = state;

	if ( state == "down" )
		menuHeight = menuHeightDown;
	else if ( state == "up" )
		menuHeight = 0;
	else
	{
		elemMenuBtn.style.display = "none";
		return;
	}
		
	elemMenu.style.height = menuHeight + "px";
	setMenuBtnText();
}

/* ------------------ */
/* Dynamic File Boxes */
/* ------------------ */
var elemContainer = null;
var elemCount = null;

function createFileEntries()
{
	if ( !elemContainer || !elemCount )
		return;
	
	resetFileEntries();
	
	var i, num = elemCount.value;
	for ( i=0; i < num; i++ )
		createFileEntry( elemContainer );
		
	resizeNav();
	resizeNav(); // Call it twice to account for special cases
}

function resetFileEntries()
{
	if ( !elemContainer )
		return;
		
	var i, num = fileBoxes.length;
	for ( i=0; i < num; i++ )
		removeLastFileEntry( elemContainer );
}
