var appArray = null;

function addApp( appID, appName, appLocation )
{
	if ( appArray == null ) appArray = new Array( );
	var temp = new Object( );
	
	temp._appID = appID;
	temp._appName = appName;
	temp._appLocation = appLocation;
	
	appArray[ appArray.length ] = temp;
}

function goApp( where, location )
{
	$( "#content" ).attr( "src", location );
}

var thisMenuArray = null;
function newMenu( category )
{
	if ( thisMenuArray == null ) thisMenuArray = new Array( );
	
	var temp = new Object( );
	temp._category = category;
	
	thisMenuArray[ thisMenuArray.length ] = temp;
}

function newMenuItem( category, itemName, itemLocation, itemKey )
{
	if ( findCategory( category ) < 0 ) newMenu( category );
	
	var temp = new Object( );
	temp._category = category;
	temp._itemName = itemName;
	temp._itemLocation = itemLocation;
	temp._itemKey = itemKey;
	
	var index = findCategory( category );
	
	if ( thisMenuArray[ index ]._menuItems == null )
		thisMenuArray[ index ]._menuItems = new Array( );
	
	el = thisMenuArray[ index ]._menuItems;
	
	el[ el.length ] = temp;
}

function findCategory( category )
{
	if ( thisMenuArray != null )
	{
		for ( k = 0; k < thisMenuArray.length; k++ )
		{
			if ( thisMenuArray[ k ]._category == category ) return k;
		}
	}
	
	return -1;
}

function toggleVisible( where, forced )
{
	//alert( "forced"+forced );

	if ( $( where ).attr( "_stayVisible" ) == null )
		$( where ).attr( "_stayVisible", forced!=undefined?forced:true );
	else if ( $( where ).attr( "_stayVisible" ).toString( ) == "true" )
		$( where ).attr( "_stayVisible", forced!=undefined?forced:false );
	else
		$( where ).attr( "_stayVisible", forced!=undefined?forced:true );
}

function popUpApp( where )
{
	if ( $( where ).attr( "_stayVisible" ) == null )
		$( where ).attr( "_stayVisible", true );
	
	table = $( document.createElement( "div" ) );
	i = 0;
	for ( i = 0; i < appArray.length; i++ )
	{
		temp = appArray[ i ];
		temp2 = where;
		
		tempDiv = $( document.createElement( "div" ) );
		tempDiv.addClass( "appMenu" );
		tempDiv.attr( { "_appName":temp._appName, "_appLocation":temp._appLocation } );
		tempDiv.click( function( ) {
			$( "#curApp" ).html( $( this ).attr( "_appName" ) );
			goApp( "#content", $( this ).attr( "_appLocation" ) );
			toggleVisible( $( "#appMenuPlatform" ) );
			hideMenu( $( "#appMenuPlatform" ) );
		} );
		tempDiv.css( "white-space", "nowrap" );
		tempDiv.append( temp._appName );
		
		table.append( tempDiv );
	}
	
	//alert( $( where ).attr( "_stayVisible" ) );
	if ( $( where ).attr( "_stayVisible" ).toString( ) == "true" )
	{
		newDiv = $( document.createElement( "div" ) );
		newDiv.append( table );
		
		newDiv.css( { "display":"block", "position":"absolute", "paddingLeft":"5px", "backgroundColor":"white" } );
		var b = $( "#spawner" );
		b.css( { "display":"inline", "backgroundColor":"green" } );
		b.html( newDiv );
		
		newDiv.attr( "id", "appMenu" );
		$( where ).attr( "_menu", newDiv );		
		
		var top = ( parseInt( $( where ).offset( ).top ) - parseInt( newDiv.css( "height" ).replace( "px", "" ) ) ) + "px";
		var left = $( where ).offset( ).left;
		
		//alert( "left:"+left+"top:"+top );
		
		b.css( { "top":top } );

		//alert( getWidth( newDiv ) + " " + getWidth( where ) );
		//alert( newDiv.css( "width" ).replace( "px", "" ) + " " + $( where ).css( "width" ).replace( "px", "" ) );
		if ( parseInt( newDiv.css( "width" ).replace( "px", "" ) ) < parseInt( $( where ).css( "width" ).replace( "px", "" ) ) )
		{
			//alert( ":))" );
			b.css( { "left":left-10 + "px" } );
			//newDiv.css( { "width":( $( where ).css( "width" ).replace( "px", "" ) - 5 ) + 'px', "left":left + 'px' } );
		}
		else if ( parseInt( newDiv.css( "width" ).replace( "px", "" ) ) > parseInt( $( where ).css( "width" ).replace( "px", "" ) ) )
		{
			//alert( ":)" );
			newLeft = $( where ).offset( ).left - ( parseInt( newDiv.css( "width" ).replace( "px", "" ) ) ) + parseInt( $( where ).css( "width" ).replace( "px", "" ) );
			b.css( { "left":newLeft-10 + "px" } );
		}
		else
		{
			alert( ":(" );
		}
	}
	else
	{
		$( "#spawner" ).html( "" );
	}
}

_menuClick = false;

function dropDownMenu( category, where, clickable )
{
	if ( clickable == undefined ) { }
	else if ( clickable )
		_menuClick = clickable;
		
	if ( $( where ).attr( "_stayVisible" ) == null )
	{
		if ( _menuClick )
			toggleVisible( where, false );
		else
			toggleVisible( where );
	}

	if ( findCategory( category ) < 0 ) return;
	
	hideAll( );
	
	items = thisMenuArray[ findCategory( category ) ]._menuItems;
	//alert( $( where ).attr( "id" ) );
	$( where ).attr( "_before", "#7C7C7C" );
	$( where ).css( "backgroundColor", "blue" );
	table = "<table style=\"border: 0; border-spacing: 0; padding: 0;\">";
	i = 0;
	for ( i = 0; i < items.length; i++ )
	{
		temp = items[ i ];
		
		table = table + "<tr class=\"subMenuItem\">";
		table = table + "<td nowrap onClick=\"goApp( 'content', '"+temp._itemLocation+"' );\" class=\"clickable\" style=\" padding-left: 10px;\">" + temp._itemName + "</td>";
		table = table + "<td nowrap onClick=\"\" class=\"clickable\">" + temp._itemKey + "</td>";
		table = table + "</tr>";
	}
	table = table + "</table>";

	//alert( _dropMenuClick + " &i lick pie& " + ( $( where ).attr( "_stayVisible" ).toString( ) ) );

	if ( !_dropMenuClick || ( _dropMenuClick && $( where ).attr( "_stayVisible" ).toString( ) == "true" ) )
	{
		//alert( ":)" );
		newDiv = $( document.createElement( "div" ) );
		newDiv.attr( "id", "appMenu" );
		$( where ).attr( "_menu", newDiv );
		thisMenuArray[ findCategory( category ) ]._menu = newDiv;

		newDiv.html( table );
		newDiv.css( { "display":"block", "backgroundColor":"#7c7c7c", "color":"white", "position":"absolute", "border":"1px solid black" } );
		var b = $( "#spawner" );
		b.css( "display", "inline" );
		b.html( newDiv );
		
		var top = ( parseInt( $( where ).offset( ).top ) + parseInt( $( where ).css( "height" ).replace( "px", "" ) ) );
		var left = $( where ).offset( ).left;

		b.css( { "top":top+2+"px", "left":left-11+"px" } );
		//if ( newDiv.style.width < getWidth( where ) ) newDiv.style.width = ( getWidth( where ) ) + 'px';
	}
	else 
	{
		//alert( ":(" );
	}
}

function hideAll(  )
{
	for ( i = 0; i < thisMenuArray.length; i++ )
	{
		try
		{
			theDiv = thisMenuArray[ i ]._menu;
			theDiv.remove( );
		} catch ( e ) {
			//alert( "fdsalkjflsaj" );
		}
	}
}

function hideMenu( where )
{
	//alert( $( where ).attr( "_stayVisible" ) );
	//alert( $( where ).attr( "_menu" ) );
	$( where ).css( "backgroundColor", $( where ).attr( "_before" ) );
	
	if ( _menuClick && $( where ).attr( "_stayVisible" ).toString( ) == "true" ) return;
		
	//alert( "this far" );
	
	try
	{
		var b = $( "#spawner" );
		theDiv = $( where ).attr( "_menu" );
		b.html( "" );
		//alert( theDiv.attr( "id" ) );
		theDiv.remove( );
	} catch ( e ) {
		//alert( e );
	}
}