function _(id)
{
	return document.getElementById(id)
}

function details( what )
{
	var node = _("applist");
	if (node)
	{
		var nodes = node.getElementsByTagName("LI");	
		
		for (var i=0; i<nodes.length; i++)
		{
			node = nodes[i]
			node.className= node.className.replace("sel","");
		}
	}
	
	node = _("list-"+what);
	if (node)
		node.className += " sel";

	var node = _("appdetails");
	if (node)
	{
		var nodes = node.getElementsByTagName("DIV");	
		
		for (var i=0; i<nodes.length; i++)
		{
			node = nodes[i]
			node.className= node.className.replace("sel","");
		}
	}
	
	node = _("detail-"+what);
	if (node)
		node.className += " sel";
}









function updateUsage()
{
	var used = 
	{

		connect : { } ,
		listen : {} ,
		udp : {} ,
		memory : 0 ,
		LuaVMs : 0
	}
	function use( list, uses )
	{
		if (!list) return;
		
		for (var j=0; j<list.length; j++)
		{
			uses[ list[j] ] = 1;
		}
	}
	
	function blocked( R )
	{
		if ( 80e6 - used.memory < ( R.memory || 0x40000) )  return MemoryReason;
		if ( 10 - used.LuaVMs < ( R.LuaVMs || 1 ) ) return LuaVMReason;
		if ( R.listen  ) for (var i=0; i<R.listen.length; i++) { if ( used.listen[ R.listen[i] ] ) return ListeReason; }
		if ( R.connect ) for (var i=0; i<R.connect.length; i++) { if ( used.connect[ R.connect[i] ] ) return ConnectReason; }
		if ( R.udp     ) for (var i=0; i<R.udp.length; i++) { if ( used.udp[ R.udp[i] ] ) return UDPReason; }
	}
	

	for (var i=0; i<AppNames.length; i++)
	{
		var name = AppNames[i];
		var R = Resources[ name ];

		if (!R) continue;
		
		var div = _( 'detail-'+ name ); 
		if (!div) continue;
		var runs = div.getAttribute( 'data-state' ) == '3';
		if (!runs) continue;
		
		used.memory += R.memory || 0x40000;
		used.LuaVMs += R.LuaVMs || 1;
		
		use( R.listen, used.listen );
		use( R.connect, used.connect );
		use( R.udp, used.udp );
	}
	

	for (var i=0; i<AppNames.length; i++)
	{
		var name = AppNames[i];
		var R = Resources[ name ];

		if (!R) continue;
		
		var div = _( 'detail-'+ name ); 
		if (!div) continue;
		
		runs = div.getAttribute( 'data-state' ) == '3';
		if (runs) continue;
		
		var reason = blocked( R );
		if (reason)
			div.setAttribute( 'data-blocked', reason );
		else
			div.removeAttribute( 'data-blocked');
		
		div = _( name +'-reason' );
		if (!div) continue;
		
		div.innerHTML = reason || '';
	}
}

function show( obj )
{
	if (!obj) return;
	var c = obj.className || "";
	c = c.replace( " hide", "");
	c = c.replace(/^\s+|\s+$/g,"").replace(/\s{2,}/g,' ');
	obj.className = c;
	return  c;
}

function hide( obj )
{
	if (!obj) return;
	var c = obj.className  || ""
	c = c.replace( " hide", "");
	c = c +" hide";
	obj.className = c;
}

var pollTimeOut;
var pollCounter = 0;
function poll( ms )
{
	var url=window.location.origin + '/html/apps/poll.cgi';
	var objHTTP = new XMLHttpRequest();
	objHTTP.open( "GET", url);
	objHTTP.onreadystatechange = function ()
	{
		if (objHTTP.readyState == 4)
		{
			var s = objHTTP.responseText || "{}";
			var a;
			eval("a="+s); // object of [i]:{name:"name",state:2,mem:34567} 's
			var map = {};


			for (var i=1; i<100; i++)
			{
				var o = a[i];
				if (!o) break;

				var state = o.state % 5;
				map[ o.name ] = Math.max( state, map[ o.name ] || 0 );
			}
			

			var children = _('appdetails').childNodes;
			for( var i=0; i < children.length; i++)
			{
				var node = children[ i ];
				if (node.tagName != 'DIV') continue;
				
				var name = node.id.substr(7);
				node.setAttribute( 'data-state', map[ name ] || 0);
			}
			
			updateUsage();


			clearTimeout( pollTimeOut )
			if (pollCounter < 60)
			{
				pollCounter ++;
				pollTimeOut = setTimeout( poll, ms || 10000 )
			}
			else
			{
				show( _('refresh') );
			}
		}
	};
	objHTTP.send( null);
}

function doPoll( ms )
{
	clearTimeout( pollTimeOut )
	pollCounter = 0;
	pollTimeOut = setTimeout( poll, ms || 10000 )
	hide( _('refresh') );
}

function post( cmd, name )
{
	document.post.cmd.value = cmd+"="+name;
	document.post.submit();

	pollCounter = 0;
	return false;
}

function send( cmd, name )
{
	var url=window.location.origin + "/html/apps/manager.cgi";
	var s  = "cmd="+cmd+"%3D"+name;
	var objHTTP = new XMLHttpRequest();
	objHTTP.open( "POST", url, false);
	objHTTP.onreadystatechange = function (){if (objHTTP.readyState == 4){doPoll(500)}};
	objHTTP.send( s);
}

function doRemove( name )
{
	if ( confirm( confirm1 ) && confirm( confirm2 ) )
		post( "remove", name);
	else
		post( "nop", "");
}

function doCancel( name )
{
	hide( _(name+"-cancel"));
	hide( _(name+"-install"));
	return post( "cancel", name);
}

function doInstall( name )
{
	hide( _(name+"-cancel"));
	hide( _(name+"-install"));
	return post( "install", name);
}

function doRun( name )
{
	_('detail-' + name).setAttribute( 'data-state', 1 ) ;
	send( "run", name);
}

function doStop( name )
{ 
	_('detail-' + name).setAttribute( 'data-state', 4 ) ;
	send( "stop", name);
}
function doLock( name ) 	
{
	send( "lock", name);
	hide( _(name+"-lock"));
	show( _(name+"-unlock"));
}

function doUnlock( name )
{
	send( "unlock", name);
	hide( _(name+"-unlock"));
	show( _(name+"-lock"));
}

window.onload = function(){

	if (!window.location.origin)
	{
		window.location.origin = window.location.protocol 
		+ "//" + window.location.hostname 
		+ (window.location.port ? ':' + window.location.port : '');
	}

	document.getElementById('uploadfile').onchange = function(e) 
	{
		setTimeout(
			(function(form)
			{
				return function() {form.submit(); }
			})
			(this.form), 10);

	};
	if (MsgBox)
		alert( MsgBox );

	poll( 1000 )
};



var AppManVersion = '1.8.3'
