(function(AG) {
	AG.OnloadObserver = new Class({
		
		extend: {
			handlerQueue: [],
			queueLength: 0,
			
			queueHandler: function(handler) {
				AG.OnloadObserver.handlerQueue.push(handler);
				if (AG.OnloadObserver.queueLength === 0) {
					AG.OnloadObserver.triggerHandlers();
				}
			},
			
			incrementQueue: function() {
				AG.OnloadObserver.queueLength++;
			},
			
			decrementQueue: function() {
				if (AG.OnloadObserver.queueLength > 0) {
					AG.OnloadObserver.queueLength--;
					
					if (AG.OnloadObserver.queueLength === 0 && AG.OnloadObserver.handlerQueue.length > 0) {
						AG.OnloadObserver.triggerHandlers();
					}
				}
			},
			
			triggerHandlers: function() {
				AG.OnloadObserver.handlerQueue.each(function(handler) {
					handler();
					AG.OnloadObserver.handlerQueue.remove(handler);
				});
			}
			
		}
		
	});
})(AnimalsGame);
