var inventory = newStack (wt, invmax, golfgrabber);
var icon;
var currentInvItem = NULL;
var isInvOn = 0;

var stickySam = 0;

objectType inventoryIcon ("inventory") {
	event oneCursor = arrowTarget;
	event onlyAction {
		var rowsNeeded = (stackSize (inventory) - 1) / 9;
		var x = 1000, y = 190 - rowsNeeded * 60;
		freeze ();

		darkBackground ();
		mixOverlay ('iface/invback.tga', CENTRE, CENTRE);

		var invItems = copyStack (inventory);

		# Big black crosshair showing the centre of the screen
		# blankArea (0, 239, 639, 240);
		# blankArea (319, 0, 320, 479);

		while (invItems) {
			if (x > 550) {
				if (stackSize (invItems) < 9) {
					x = 430 - stackSize (invItems) * 25;
				} else {
					x = 200;
				}
				y += 90;
			}
			var thisItem = popFromStack (invItems);
			callEvent (getIcon, thisItem);
			pasteImage (icon, x, y);
			addScreenRegion (thisItem, x - 18, y - 18, x + 18, y + 18, 0, 0, 0);
			x += 50;
		}
		invGo ();
                isInvOn = 1;
		while (howFrozen ()) pause (1);
	}
}

objectType getIcon ("") {
}

objectType getActiveIcon ("") {}

objectType inventoryClick ("") {
}

objectType money ("money") {
       event getIcon {
            icon = anim ('iface/mushroom.duc', 0);
       }
       event lookAt {
          if (samMoney == 1) {
            say (sam, "We only have one dollar.");
          }
          else {
            say (sam, "We have " + samMoney + " dollars.");
          }
       }
       event default {
            if (isInvOn == 1) say (sam, "No, we need to spend our cash more wisely than that.");
            else Say (sam, "No, we need to spend our cash more wisely than that.");
       }
}

objectType invmax ("Max") {
	event getIcon {
		icon = anim ('iface/act1-1.duc', 38);
	}
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 61);
       }
        event lookAt {
             say (sam, "It's my li'l buddy, ready for action.");
        }
        event default {
           if (isInvOn == 1) say (sam, "That thing is having enough trouble as it is.");
           else Say (sam, "That thing is having enough trouble as it is.");
}
        event invGrease {
         if (slippery == 0) {
           if (samsAdvice == 1) {
              say (max, "I don't think you should take your own advice literally, Sam.");
           }
           else {
              say (sam, "Okay, Max is all greased up now.");
              say (sam, "Wow, Max.  You look like some sort of freakish, floppy-eared bowling ball.");
              say (max, "You better have a pretty good reason for covering me in grease, Sam.");
	      say (sam, "I'm sure I did at one point in time...");
	      say (max, "Sure, sure...");
           }
           slippery = 1;
         }
         else {
           say (max, "Never again!");
         }
        }
        event max {
             Say (sam, "I can't use Max on himself!  That's insane!");
	     Say (max, "And you should know.");
	     Say (sam, "Shut up, Max.");
        }
}


objectType wt ("walkie-talkie") {
	event getIcon {
		icon = anim ('iface/mushroom.duc', 0);
	}
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 70);
       }
        event lookAt {
               say (sam, "It's out of batteries, and it feels about as heavy as a brick.");
}
        event default {
              if (isInvOn == 1) say (sam, "I can't use a walkie-talkie with that!");
              else Say (sam, "I can't use a walkie-talkie with that!");
}
       event invmax {
               say (sam, "Nah!  The little guy doesn't deserve it.");
}
       event max {
               Say (sam, "Nah!  The little guy doesn't deserve it.");
}
}

objectType golfgrabber ("Gator Golf grabber") {
	event getIcon {
		icon = anim ('iface/act1-1.duc', 50);
	}
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 60);
       }
        event lookAt {
               say (sam, "It's from our last quest of absurdity.");
}
        event default {
            if (isInvOn == 1) say (sam, "I don't want to grab that with an extendable arm!");
            else Say (sam, "I don't want to grab that with an extendable arm!");
}
        event invmax {
              say (sam, "Maybe later on if he tries to do something annoying.");
}
        event max {
              Say (sam, "Maybe later on if he tries to do something annoying.");
}
}

objectType invrubberducky ("rubber ducky") {
    event getIcon {
           icon = anim ('iface/act1-1.duc', 37);
    }
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 65);
       }
        event lookAt {
               say (sam, "It's a rubber ducky of mass destruction.  On the back is a compartment that stores an emergency pulley in it.");
}
        event default {
               if (isInvOn == 1) say (sam, "Nope.");
               else Say (sam, "Nope.");
}
        event invmax {
               say (max, "Sam!  Let ME play with the rubber ducky!");
}
        event max {
               Say (max, "Sam!  Let ME play with the rubber ducky!");
               Say (sam, "Sorry.  Innocent pedestrians and pidgeons would be at stake.");
}
}

objectType beerbottle ("beer bottle") {
	event getIcon {
		icon = anim ('iface/act1-1.duc', 0);
	}
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 52);
       }
        event lookAt {
               say (sam, "It's a watery brown liquid that makes people act strange and fall over.  Max tried this stuff once and it didn't seem to have an effect on him.");
}
        event default {
               if (isInvOn == 1) say (sam, "I don't want to make that get drunk!!");
               else Say (sam, "I don't want to make that get drunk!!");
}
        event invmax {
               say (sam, "Max tried this stuff once and it didn't seem to have an effect on him.");
}
        event max {
               Say (sam, "Max tried this stuff once and it didn't seem to have an effect on him.");
}
}

objectType scissors ("scissors") {
	event getIcon {
		icon = anim ('iface/act1-1.duc', 47);
	}
	event getActiveIcon {
		icon = anim ('iface/act1-1.duc', 68);
	}
        event lookAt {
               say (sam, "Wow!  I bet these could cut through anything!  Anything paper that is.");
}
        event default {
               if (isInvOn == 1) say (sam, "I don't wanna cut that up.");
               else Say (sam, "I don't wanna cut that up.");
}
        event invmax {
               say (sam, "Max is already a cut-up.");
}
        event max {
               Say (sam, "Max is already a cut-up.");
}
        event invflintpic {
              say (sam, "I'll just cut my face out of this picture.");
               deleteFromStack (inventory, invflintpic);
               pushToStack (inventory, invflintpichole);
               pushToStack (inventory, samhead);
               unfreeze ();
               handleFocus (NULL);             
}
}

objectType pen ("pen") {
	event getIcon {
		icon = anim ('iface/act1-1.duc', 43);
	}
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 62);
       }
        event lookAt {
               say (sam, "A writing stick!");
}
        event default {
               if (isInvOn == 1) say (sam, "I'm afraid it wouldn't wash off!");
               else Say (sam, "I'm afraid it wouldn't wash off!");
}
        event invmax {
               say (sam, "I'm afraid it wouldn't wash off that rubbery head of his.");
}
        event max {
               Say (sam, "I'm afraid it wouldn't wash off that rubbery head of his.");
}
        event entryformnosign {
               say (sam, "Dum-de-dum.");
               say (sam, "Now it's filled out.");
               deleteFromStack (inventory, entryformnosign);
               pushToStack (inventory, entryformsign);
               unfreeze ();
               handleFocus (NULL);
}
}

objectType entryformnosign ("entry form") {
	event getIcon {
		icon = anim ('iface/act1-1.duc', 39);
	}
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 56);
       }
        event lookAt {
               say (sam, "As soon as I find a pen and sign this, we can enter the gameshow!");
}
        event default {
              if (isInvOn == 1) say (sam, "No, no, and no.");
              else Say (sam, "No, no, and no.");
        }
        event invmax {
               say (sam, "Max would just use it as a coloring page.");
}
        event max {
               Say (sam, "Max would just use it as a coloring page.");
}
        event pen {
               entryForm = 1;
               say (sam, "Finally, we have a chance to enter ourselves in a completely useless, luck-of-the-draw game-show!");
               say (max, "That really is amazing, Sam.");
               say (sam, "We just have to mail this...");
               deleteFromStack (inventory, entryformnosign);
               pushToStack (inventory, entryformsign);
               unfreeze ();
               handleFocus (NULL);
}
}

objectType entryformsign ("signed entry form") {
	event getIcon {
		icon = anim ('iface/act1-1.duc', 40);
	}
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 57);
       }
        event lookAt {
               say (sam, "Let's mail this thing!");
}
        event default {
              if (isInvOn == 1) say (sam, "That won't work!");
              else Say (sam, "That won't work!");
}
        event invmax {
               say (sam, "Not now that it's signed!");
}
        event max {
               Say (sam, "Not now that it's signed!");
}
        event envelope {
               entryForm = 2;
               say (sam, "Okay.  I put the entry form in the envelope.");
               say (sam, "I'll just change the mailing address and return address with this pen.");
               say (sam, "There.  But now the pen is out of ink.  I'll toss it.");
               deleteFromStack (inventory, envelope);
               deleteFromStack (inventory, pen);
               deleteFromStack (inventory, entryformsign);
               pushToStack (inventory, envelopein);
               unfreeze ();
               handleFocus (NULL);
}
}

objectType envelope ("envelope") {
	event getIcon {
		icon = anim ('iface/act1-1.duc', 41);
	}
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 58);
       }
        event lookAt {
               say (sam, "It's an empty envelope.");
}
        event default {
              if (isInvOn == 1) say (sam, "I can't do that!");
              else Say (sam, "I can't do that!");
        }
        event entryformsign {
               say (sam, "Okay.  I put the entry form in the envelope.");
               say (sam, "I'll just change the mailing address and return address with this pen.");
               say (sam, "There.  But now the pen is out of ink.  I'll toss it.");
               deleteFromStack (inventory, envelope);
               deleteFromStack (inventory, pen);
               deleteFromStack (inventory, entryformsign);
               pushToStack (inventory, envelopein);
               unfreeze ();
               handleFocus (NULL);
}
}

objectType envelopein ("envelope with entry") {
	event getIcon {
		icon = anim ('iface/act1-1.duc', 42);
	}
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 59);
       }
        event lookAt {
               say (sam, "It's an envelope with a signed entry form in it.");
}
        event default {
              if (isInvOn == 1) say (sam, "No way!");
              else Say (sam, "No way!");
        }
}

objectType invflintpic ("beach picture") {
       event getIcon {
          icon = anim ('iface/act1-1.duc', 44);
       }
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 63);
       }
        event lookAt {
               say (sam, "It's me, Max, and Flint at the beach.");
}
        event invmax {
               say (sam, "Max, you look constipated in this picture.");
}
        event max {
               Say (sam, "Max, you look constipated in this picture.");
}
        event default {
              if (isInvOn == 1) say (sam, "I can't do that!");
              else Say (sam, "I can't do that!");
        }
        event scissors {
               say (sam, "It seems a shame to destroy this lasting token of our friendship with Flint, but...");
               say (sam, "Ah, what the hey, cutting stuff is fun!");
               say (max, "Sam, for once in my brief life, I agree.");
               deleteFromStack (inventory, invflintpic);
               pushToStack (inventory, invflintpichole);
               pushToStack (inventory, samhead);
               unfreeze ();
               handleFocus (NULL);             
}
}

objectType samhead ("Sam's face") {
     event getIcon {
         icon = anim ('iface/act1-1.duc', 26);
     }
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 66);
       }
     event lookAt {
         say (sam, "Who's that handsome dog?");
     }
     event invmax {
         say (sam, "I just don't see any similarities.");
     }
     event default {
         if (isInvOn == 1) say (sam, "That won't work!");
         else Say (sam, "That won't work!");
     }
     event max {
         Say (sam, "I just don't see any similarities.");
     }
     event invStickyMop {
         if (stickySam == 0) { 
               say (sam, "Okay, now it's a sticky picture of me!");
               say (max, "Your perverted imagination never ceases to amaze me, Sam.");
               stickySam = 1;
         }
         else {
               say (sam, "It's sticky enough already.");
         }
     }
     event invThugCard {
         if (stickySam == 0) {
            say (sam, "It won't stick to the card!");
            return;
         }
         else {
            say (sam, "Okay, I've stuck my face on the thug card.");
            deleteFromStack (inventory, samhead);
            deleteFromStack (inventory, invThugCard);
            pushToStack (inventory, invThugCardSam);
            unfreeze ();
            handleFocus (NULL);  
         }
     }
}

objectType invflintpichole ("beach picture without Sam") {
       event getIcon {
          icon = anim ('iface/act1-1.duc', 46);
       }
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 64);
       }
        event lookAt {
           say (sam, "It's the same old picture, but without me in it.");
}
        event default {
              if (isInvOn == 1) say (sam, "No way!");
              else Say (sam, "No way!");
        }
}

objectType sandwich ("sandwich") {
       event getIcon {
          icon = anim ('iface/act1-1.duc', 45);
       }
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 67);
       }
    event lookAt {
        say (sam, "Gross! This thing could kill somebody!");
    }
        event default {
              if (isInvOn == 1) say (sam, "I don't want to ruin that!");
              else Say (sam, "I don't want to ruin that!");
        }
    event max {
        Say (sam, "No, I'm not sure what noxious gases would result from feeding it to him.");
    }
    event invmax {
        say (sam, "No, I'm not sure what noxious gases would result from feeding it to him.");
    }
}

objectType doll ("blonde doll") {
     event getIcon {
         icon = anim ('iface/act1-1.duc', 27);
     }
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 54);
       }
     event lookAt {
         say (sam, "It's a cute little stuffed doll with blonde hair.");
     }
     event invmax {
         say (sam, "This is not a chew toy for Max.");
     }
     event max {
         Say (sam, "This is not a chew toy for Max.");
     }
     event spraypaint {
         say (sam, "Okay.  I've sprayed the doll's hair black.");
         deleteFromStack (inventory, spraypaint);
         deleteFromStack (inventory, doll);
         pushToStack (inventory, dollbh);
         littleGirl = 3;
         unfreeze ();
         handleFocus (NULL);   
     }
        event default {
              if (isInvOn == 1) say (sam, "That won't work!");
              else Say (sam, "That won't work!");
        }
}

objectType spraypaint ("black spray paint") {
     event getIcon {
         icon = anim ('iface/act1-1.duc', 48);
     }
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 69);
       }
     event lookAt {
         say (sam, "It's a bottle of black spray paint.");
     }
     event invmax {
         say (sam, "No, I like him white.");
     }
     event max {
         Say (sam, "No, I like him white.");
     }
     event doll {
         say (sam, "Okay.  I've sprayed the doll's hair black.");
         deleteFromStack (inventory, spraypaint);
         deleteFromStack (inventory, doll);
         pushToStack (inventory, dollbh);
         littleGirl = 3;
         unfreeze ();
         handleFocus (NULL);   
     }
        event default {
              if (isInvOn == 1) say (sam, "I can't do that!");
              else Say (sam, "I can't do that!");
        }
}

objectType dollbh ("black-haired doll") {
     event getIcon {
         icon = anim ('iface/act1-1.duc', 28);
     }
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 55);
       }
     event lookAt {
         say (sam, "It's a cute little stuffed doll with black hair.");
     }
     event invmax {
         say (sam, "This is not a chew toy for Max.");
     }
     event max {
         Say (sam, "This is not a chew toy for Max.");
     }
        event default {
              if (isInvOn == 1) say (sam, "No way!");
              else Say (sam, "No way!");
        }
}

objectType invcord ("cord") {
     event getIcon {
         icon = anim ('iface/act1-1.duc', 32);
     }
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 53);
       }
     event lookAt {
          say (sam, "It's some kind of electrical cable.  Possibly a phone line.");
     }
     event invmax {
         say (sam, "Zapping him for fun would be nice, but there's no source of electric current.");
     }
     event max {
         Say (sam, "Zapping him for fun would be nice, but there's no source of electric current.");
     }
        event default {
              if (isInvOn == 1) say (sam, "That won't work!");
              else Say (sam, "I can't do that!");
        }
}

objectType invJanitorKeys ("keys") {
     event getIcon {
         icon = anim ('iface/act1-1.duc', 82);
     }
       event getActiveIcon {
            icon = anim ('iface/act1-1.duc', 83);
       }
     event lookAt {
          say (sam, "It's Johnson's unusually shiny supply closet key.");
     }
     event default {
          if (isInvOn == 1) say (sam, "That won't work!");
          else Say (sam, "I can't do that!");
     }
}

objectType invGrease ("grease") {
     event getIcon {
         icon = anim ('iface/act1-1.duc', 76);
     }
     event getActiveIcon {
         icon = anim ('iface/act1-1.duc', 77);
     }
     event lookAt {
         say (sam, "It's a slippery tub of grease.");
     }
     event default {
          if (isInvOn == 1) say (sam, "Greasing that won't get us anywhere.");
          else Say (sam, "Greasing that won't get us anywhere.");
     }
     event invmax {
         if (slippery == 0) {
           if (samsAdvice == 1) {
              say (max, "I don't think you should take your own advice literally, Sam.");
           }
           else {
              say (sam, "Okay, Max is all greased up now.");
              say (sam, "Wow, Max.  You look like a floppy eared bowling ball.");
              say (max, "You better have a damn good reason for covering me in grease, Sam.");
           }
           slippery = 1;
         }
         else {
           say (max, "Never again!");
         }
     }
     event max {
        Say (max, "Why don't you grease me up in the inventory, Sam?");
     }
}

objectType invKeychain ("keychain") {
     event getIcon {
         icon = anim ('iface/act1-1.duc', 73);
     }
     event getActiveIcon {
         icon = anim ('iface/act1-1.duc', 75);
     }
     event lookAt {
         say (sam, "It's a dinky little keychain for some arcade game named 'Meteor Mess.'");
     }
     event invmax {
         say (sam, "Max hasn't developed the motor skills to do something like that.");
     }
     event max {
         Say (sam, "Max hasn't developed the motor skills to do something like that.");
     }
     event default {
          if (isInvOn == 1) say (sam, "That won't work!");
          else Say (sam, "I can't do that!");
     }
}

objectType invStickyMop ("sticky mop") {
     event getIcon {
         icon = anim ('iface/mouse.duc', 16);
     }
     event getActiveIcon {
         icon = anim ('iface/mouse.duc', 17);
     }
     event lookAt {
         say (sam, "It's the janitor's mop, covered in extremely sticky goo.");
     }
     event invmax {
         say (sam, "I don't wanna stick it to his head...yet.");
     }
     event max {
         Say (sam, "I don't wanna stick it to his head...yet.");
     }
     event samhead {
         if (stickySam == 0) { 
               say (sam, "Okay, now it's a sticky picture of me!");
               say (max, "Your perverted imagination amazes me every day, Sam.");
               stickySam = 1;
         }
         else {
               say (sam, "It's sticky enough already.");
         }
     }
     event default {
          if (isInvOn == 1) say (sam, "If I stick it to that, it might not come off!");
          else Say (sam, "If I stick it to that, it might not come off!");
     }
}

objectType invThugCard ("thug card") {
     event getIcon {
         icon = anim ('iface/act1-1.duc', 78);
     }
     event getActiveIcon {
         icon = anim ('iface/act1-1.duc', 79);
     }
     event lookAt {
         say (sam, "It's a thug membership card with a phone number on it.");
     }
     event default {
          if (isInvOn == 1) say (sam, "That won't work!");
          else Say (sam, "I can't do that!");
     }
     event samhead {
         if (stickySam == 0) {
            say (sam, "It won't stick to the card!");
            return;
         }
         else {
            say (sam, "Okay, I've stuck my face on the thug card.");
            deleteFromStack (inventory, samhead);
            deleteFromStack (inventory, invThugCard);
            pushToStack (inventory, invThugCardSam);
            unfreeze ();
            handleFocus (NULL);  
         }
     }
}

objectType invThugCardSam ("thug card with Sam") {
     event getIcon {
         icon = anim ('iface/act1-1.duc', 80);
     }
     event getActiveIcon {
         icon = anim ('iface/act1-1.duc', 81);
     }
     event lookAt {
         say (sam, "It's a thug membership card with my face on it.");
     }
     event default {
          if (isInvOn == 1) say (sam, "That won't work!");
          else Say (sam, "I can't do that!");
     }
}

sub invGo () {
	busy = FALSE;
	onLeftMouse (invClick);
	onRightMouse (invRight);
	onFocusChange (handleFocus);
	setCursor (anim ('iface/mouse.duc', 0));
	handleFocus (getOverObject ());
}

sub invRight () {
        var thisThing = getOverObject ();
        thisObject = thisThing;
        if (thisObject) {
           callEvent (lookAt, thisObject);
        }
        else {
	  currentInvItem = NULL;
	  invGo ();
        }
}

sub invClick () {
	var o = getOverObject ();
	if (! o) {
		unfreeze ();
                isInvOn = 0;
		handleFocus (NULL);
	} else if (currentInvItem && currentInvItem != o) {
		stop ();
		findEvent (currentInvItem, o);
		currentInvItem = NULL;
		invGo ();
	} else {
		stop ();

		# By default, clicking an inventory object selects it...
		# that's unless it's got an "inventoryClick" event
		if (! callEvent (inventoryClick, o)) currentInvItem = o;
		invGo ();
	}
}