/*************************************/
// HELPER FUNCTIONS
function minMax( div, image )
{
	var minImg = "/todo/images/widgets/plain/minus.white.png";
	var maxImg = "/todo/images/widgets/plain/plus.white.png";

	$( "#" + image ).attr( "src", ( $( "#" + div ).css( "display" ) == "none" ? minImg : maxImg ) );
	$( "#" + div ).toggle( 'slow' );
}

/*************************************/

var menuList = new Array( );

function newMenu( thisID, name )
{
	if ( menuList == null )
	{
		menuList = new Array( );
	}
	
	var temp = new Object( );
	
	temp.thisID = thisID;
	temp.name = name;
	
	menuList[menuList.length] = temp;
	
	return temp;
}

function makeMenuRow( temp )
{
	newRow = $( document.createElement( "tr" ) );
	newRow.attr( "id", "menu_" + temp.thisID );
	newRow.object = temp;
	
	td = $( document.createElement( "td" ) );
	td.attr( "id", "menuList_" + temp.thisID );
	td.html( "<a href=\"todo.cfm?listID="+temp.thisID+"\" target=\"infoFrame\">"+temp.name+"</a>" );
	
	newRow.append( td );
	
	return newRow;
}

function showMenuLists( )
{
	table = $( document.createElement( "table" ) );
	table.attr( "id", "menuListTABLE" );
	table.css( "width", "100%" );
	table.css( "border", "0" );
	table.attr( "cellPadding", 4 );
	table.attr( "cellSpacing", 0 );
	
	newRow = $( document.createElement( "tr" ) );
	td = $( document.createElement( "td" ) );
	td.html( "<a href=\"todo.cfm\" target=\"infoFrame\">My Todo List</a>" );
	newRow.append( td );
	table.append( newRow );
	
	if ( menuList != null )
	{
		for ( i = 0; i < menuList.length; i++ )
		{
			temp = menuList[i];
			
			newRow = makeMenuListRow( temp );
			
			table.append( newRow );
		}
	}
	
	area = $( '#minMenuLists' );
	area.append( table );
}

function addMenuList( name )
{
	var pars = '&action=add&listName='+name;
	errMsg( pars, true );
	$.ajax( {
		type: "POST",
		url: 'templates/fctList.cfm',
		data: pars,
		success: function( t )
		{
			try
			{
				errMsg( "success " + t, true  );
				newID = escape($trim(t.responseText));
				var temp = newMenuList( newID, name );
				showMenuLists( );
			}
			catch( e )
			{
				errMsg( "thrown on success", true );
				errMsg( e, true );
			}
		},
		error: function( t, status, theErr )
		{
			try
			{
				errMsg( "failure " + t.responseText, true  );
			}
			catch( e )
			{
				errMsg( "thrown on failure", true );
				errMsg( e, true );
			}
		}
	} );
	
	$( '#newListBox' ).remove( );
}

function deleteMenuList( obj )
{
	newList = new Array( );
	for ( i = 0; i < menuList.length; i++ )
	{
		tmp = menuList[i];
		if ( tmp.name != obj.name )
		{
			var temp = new Object( );
		
			temp.thisID = tmp.thisID;
			temp.name = tmp.name;
			newList[newList.length] = temp;
		}
	}
	
	menuList = newList;
	hideMenuLists( );
	sortMenuLists( );
	showMenuLists( );
}

function hideMenuLists( )
{
	$( "#menuListTABLE" ).remove( );
}

function sortMenuLists( )
{
	if ( menuList != null )
	{
		for ( i = menuList.length; --i>=0; )
		{
			for ( j = 0; j < i; j++ )
			{
				if ( menuList[j].label > menuList[j+1].label )
				{
					T = menuList[j];
					menuList[j] = menuList[j+1];
					menuList[j+1] = T;
				}
			}
		}
	}
}
