function createInventoryState(game){
	return {
		"game":game,
		"mode":"player",
		"indices":{
			"player":0,
			"ground":0
		},
		"scroll":{
			"player":0,
			"ground":0
		},
		"maximumScroll":{
			"player":0,
			"ground":0
		},
		"scrollers":{
			
		},
		"preload":function(){
			
		},
		"setup":function(){
			
		},
		"reset":function(){
			this.mode="player";
			this.indices.player=0;
			this.indices.ground=0;
			this.scrollers.player = null;
			this.scrollers.ground = null;
		},
		"currentItemName":null,
		"draw":function(){
			background(this.game.constants.inventory.background());
			
			//draw player inventory
			var cell = this.game.world.map[this.game.world.avatar.position.z][this.game.world.avatar.position.x][this.game.world.avatar.position.y];
			var creature = cell.creature;
			var items = cell.items;
			var creatureItems = creature.items;
			if(this.scrollers.player==null){
				this.scrollers.player=this.game.utilities.createScroller(creatureItems.length, this.game.constants.inventory.scroller.listItemCount());
				this.scrollers.player.index=creature.currentItem;
			}else{
				this.scrollers.player.updateItemCount(creatureItems.length);
			}
			if(this.scrollers.ground==null){
				this.scrollers.ground=this.game.utilities.createScroller(items.length, this.game.constants.inventory.scroller.listItemCount());
			}else{
				this.scrollers.ground.updateItemCount(items.length);
			}

			if(this.mode=="player" && creatureItems.length==0 && items.length>0){
				this.mode="ground";
			}else if(this.mode=="ground" && creatureItems.length>0 && items.length==0){
				this.mode="player";
			}
			
			var currentItemName=null;
			
			var offsetX = this.game.constants.inventory.player.offsetX();
			var offsetY = this.game.constants.inventory.player.offsetY();
			this.game.textManager.writeText("Player:",offsetX,offsetY,"silver","darksilver");
			if(creatureItems.length==0){
				this.game.textManager.writeText("((empty)",offsetX+this.game.constants.inventory.scroller.width(),offsetY+game.constants.text.cellHeight(),"silver","darksilver");
			}
			var indices = this.scrollers.player.itemIndices();
			for(var row in indices){
				var index = indices[row];
				var plotX=offsetX+this.game.constants.inventory.scroller.width();
				var plotY=offsetY + game.constants.text.cellHeight() + row*game.constants.board.cellHeight();
				var item=creatureItems[index];
				if(this.mode=="player" && index==this.scrollers.player.index){
					currentItemName=item.descriptor;
					image(
						this.game.patternManager.getGraphic("box","lightest","black"),
						plotX,
						plotY);
				}else{
					image(
						this.game.patternManager.getGraphic("box","black","black"),
						plotX,
						plotY);
				}
				var descriptor = this.game.itemDescriptors[item.descriptor];
				image(
					this.game.patternManager.getGraphic(descriptor.pattern(),descriptor.foreground(),descriptor.background()),
					plotX,
					plotY);
				var pinText = (item.pinned) ? ("(pin)") : ("");
				this.game.textManager.writeText("x"+item.count+pinText,plotX+game.constants.board.cellWidth(),plotY+game.constants.text.cellHeight()/2,"silver","darksilver");
			}
			var thumbTop = this.scrollers.player.thumbTop(offsetY + game.constants.text.cellHeight(), this.game.constants.inventory.scroller.listItemCount() * this.game.constants.board.cellHeight());
			var thumbBottom = this.scrollers.player.thumbBottom(offsetY + game.constants.text.cellHeight(), this.game.constants.inventory.scroller.listItemCount() * this.game.constants.board.cellHeight());
			fill(this.game.colors.superdark);
			noStroke();
			rect(offsetX,thumbTop, this.game.constants.inventory.scroller.width(), thumbBottom-thumbTop);
			
			offsetX = this.game.constants.inventory.ground.offsetX();
			offsetY = this.game.constants.inventory.ground.offsetY();
			this.game.textManager.writeText("Ground:",offsetX,offsetY,"silver","darksilver");
			if(items.length==0){
				this.game.textManager.writeText("((empty)",offsetX+this.game.constants.inventory.scroller.width(),offsetY+game.constants.text.cellHeight(),"silver","darksilver");
			}
			var indices = this.scrollers.ground.itemIndices();
			for(var row in indices){
				var index = indices[row];
				var plotX=offsetX+this.game.constants.inventory.scroller.width();
				var plotY=offsetY + game.constants.text.cellHeight() + row*game.constants.board.cellHeight();
				var item=items[index];
				if(this.mode=="ground" && index==this.scrollers.ground.index){
					currentItemName=item.descriptor;
					image(
						this.game.patternManager.getGraphic("box","lightest","black"),
						plotX,
						plotY);
				}else{
					image(
						this.game.patternManager.getGraphic("box","black","black"),
						plotX,
						plotY);
				}
				var descriptor = this.game.itemDescriptors[item.descriptor];
				image(
					this.game.patternManager.getGraphic(descriptor.pattern(),descriptor.foreground(),descriptor.background()),
					plotX,
					plotY);
				var pinText = (item.pinned) ? ("(pin)") : ("");
				this.game.textManager.writeText("x"+item.count+pinText,plotX+game.constants.board.cellWidth(),plotY+game.constants.text.cellHeight()/2,"silver","darksilver");
			}
			var thumbTop = this.scrollers.ground.thumbTop(offsetY + game.constants.text.cellHeight(), this.game.constants.inventory.scroller.listItemCount() * this.game.constants.board.cellHeight());
			var thumbBottom = this.scrollers.ground.thumbBottom(offsetY + game.constants.text.cellHeight(), this.game.constants.inventory.scroller.listItemCount() * this.game.constants.board.cellHeight());
			fill(this.game.colors.superdark);
			noStroke();
			rect(offsetX,thumbTop, this.game.constants.inventory.scroller.width(), thumbBottom-thumbTop);
			
			this.currentItemName=currentItemName;
			
			this.game.textManager.writeText("[darksilver]{silver}Esc[]{} back [darksilver]{silver}WS[]{} pick [darksilver]{silver}D[]{} drop [darksilver]{silver}A[]{} take [darksilver]{silver},.[]{} switch [darksilver]{silver}/[]{} examine [darksilver]{silver}P[]{} pin",0,this.game.constants.screen.height()-game.constants.text.cellHeight(),"silver","darksilver");
		},
		"keyPressed":function(){
			var cell = this.game.world.map[this.game.world.avatar.position.z][this.game.world.avatar.position.x][this.game.world.avatar.position.y];
			var creature = cell.creature;
			var items = cell.items;
			var creatureItems = creature.items;
			var terrain = cell.terrain;
			var terrainDescriptor = this.game.terrainDescriptors[terrain.descriptor];
			var self = this;
			var reorganizeInventories=function(){
				cell.items = self.game.utilities.restackItems(items);
				if(cell.items.length>0){
					self.indices.ground%=cell.items.length;
				}else{
					self.indices.ground=0;
				}
				creature.items = self.game.utilities.restackItems(creatureItems);
				if(creature.items.length>0){
					self.indices.player%=creature.items.length;
				}else{
					self.indices.player=0;
				}
			};
			var dropAll=function(){
				items.push(creatureItems[self.scrollers.player.index]);
				creatureItems[self.scrollers.player.index]=null;
				reorganizeInventories();
			};
			var dropHalf=function(){
				var item = creatureItems[self.scrollers.player.index];
				if(item.count==1){
					dropAll();
				} else {
					var clone = JSON.parse(JSON.stringify(item));
					clone.count = Math.floor(clone.count/2);
					item.count-=clone.count;
					items.push(clone);
					reorganizeInventories();
				}
			};
			var dropOne=function(){
				var item = creatureItems[self.scrollers.player.index];
				if(item.count==1){
					dropAll();
				} else {
					var clone = JSON.parse(JSON.stringify(item));
					clone.count = 1;
					item.count--;
					items.push(clone);
					reorganizeInventories();
				}
			};
			var pickUpAll=function(){
				creatureItems.push(items[self.scrollers.ground.index]);
				items[self.scrollers.ground.index]=null;
				reorganizeInventories();
			};
			var pickUpHalf=function(){
				var item = items[self.scrollers.ground.index];
				if(item.count==1){
					pickUpAll();
				} else {
					var clone = JSON.parse(JSON.stringify(item));
					clone.count = Math.floor(clone.count/2);
					item.count-=clone.count;
					creatureItems.push(clone);
					reorganizeInventories();
				}
			};
			var pickUpOne=function(){
				var item = items[self.scrollers.ground.index];
				if(item.count==1){
					pickUpAll();
				} else {
					var clone = JSON.parse(JSON.stringify(item));
					clone.count = 1;
					item.count--;
					creatureItems.push(clone);
					reorganizeInventories();
				}
			};
			var pinPlayerItem=function(){
				var item = creatureItems[self.scrollers.player.index];
				item.pinned=!item.pinned;
			};
			var pinGroundItem=function(){
				var item = items[self.scrollers.ground.index];
				item.pinned=!item.pinned;
			};
			if (keyCode==ESCAPE){
				var avatar = this.game.utilities.getAvatarInstance(game);
				avatar.currentItem = this.scrollers.player.index;
				this.game.state="play";
				return false;
			}else if(keyCode==83){
				if(this.mode=="player" && creatureItems.length > this.indices.player){
					this.scrollers.player.nextIndex();
				}else if(this.mode=="ground" && items.length > this.indices.ground){
					this.scrollers.ground.nextIndex();
				}
				return false;
			}else if(keyCode==87){
				if(this.mode=="player" && creatureItems.length < this.indices.player){
					this.scrollers.player.previousIndex();
				}else if(this.mode=="ground" && items.length < this.indices.ground){
					this.scrollers.ground.previousIndex();
				}
				return false;
			}else if(keyCode==65){
				if(this.mode=="ground" && items.length > this.indices.ground){
					if(keyIsDown(SHIFT)){
						pickUpOne();
					}else{
						pickUpAll();
					}
				}
				return false;
			}else if(keyCode==68){
				if(this.mode=="player" && 
					creatureItems.length > this.indices.player && 
					terrainDescriptor.canDrop(this.game, this.game.world.avatar.position.x, this.game.world.avatar.position.y)){
						
					if(keyIsDown(SHIFT)){
						dropOne();
					}else{
						dropAll();
					}
				}
				return false;
			}else if(keyCode==190){
				this.mode = "ground";
				return false;
			}else if(keyCode==188){
				this.mode = "player";
				return false;
			}else if(keyCode==80){
				//"pin" the item
				if(this.mode=="player" && creatureItems.length > this.indices.player){
					pinPlayerItem();
				}else if(this.mode=="ground" && items.length > this.indices.ground){
					pinGroundItem();
				}
				return false;
			}else if(keyCode==191){
				if(this.currentItemName!=null){
					game.states.itemExamine.itemName=this.currentItemName;
					game.states.itemExamine.previousState="inventory";
					this.game.state="itemExamine";
				}
				return false;
			}
			return true;
		},
		"keyReleased":function(){
			return true;
		}
	};
}