# Which object to manipulate when displaying the verb coin...
var thisObject = NULL;

# What do we do when we left click the mouse?
sub leftClick () {
	var overObject = getOverObject ();

	# In case we're walking already...
	stopCharacter (sam);

	# Did we click an object?
	if (overObject) {
		# Yep!

		# Are we trying to combine objects?
		if (currentInvItem && (overObject != inventoryIcon)) {
			# Yep!

			# Can we call a walkToPerson event for the object?
			if (! callEvent (walkToPerson, overObject)) {
				# Nope? OK, let's assume it's a fixed region then,
				# and use moveCharacter instead
				moveCharacter (sam, overObject);
			}

                        cancelSub (maxIdle);

			# Only take control away once the ego's finished walking
			stop ();

			# Just in case the event sets currentInvItem to something
			# else... we don't want to set it to NULL afterwards
			var r = currentInvItem;
			currentInvItem = NULL;

			# Now let's do the action...
			findEvent (r, overObject);

			# And give control back to the player
			go ();

		} else if (callEvent (oneCursor, overObject)) {
			# It has a special cursor - and therefore only one action

			# Can we call a walkToPerson event for the object?
			if (! callEvent (walkToPerson, overObject)) {
				# Nope? OK, let's assume it's a fixed region then,
				# and use moveCharacter instead
				moveCharacter (sam, overObject);
			}

                        cancelSub (maxIdle);

			# Only take control away once the ego's finished walking
			stop ();

			# Now let's do the only action...
			callEvent (onlyAction, overObject);

			# And give control back to the player
			go ();

		} else {
			# The object has more than one action, so let's bring up the coin

                        # But first let's rename the verbs back to their proper forms...
                        rename (lookAt, "Look at ");
                        rename (pickUp, "Pick up ");
                        rename (talkTo, "Talk to ");
                        rename (use, "Use ");

			# Remember which object we're going to manipulate
			thisObject = overObject;

                        #Call the verb-rename event.
                        callEvent (verbRename, thisObject);

                        # Figure out what the longest string is.
                        var stringLength1 = stringLength (lookAt + thisObject);
                        var stringLength2 = stringLength (pickUp + thisObject);
                        var stringLength3 = stringLength (talkTo + thisObject);
                        var stringLength4 = stringLength (use + thisObject);
                        var maxString1 = 0;
                        var maxStringLength = 0;
                        if (stringLength1 > stringLength2) maxString1 = stringLength1;
                        else maxString1 = stringLength2;

                        if (maxString1 > stringLength3) maxString1 = maxString1;
                        else maxString1 = stringLength3;

                        if (maxString1 > stringLength4) maxStringLength = maxString1;
                        else maxStringLength = stringLength4;

			# Work out the top-left corner for the coin image...
			var x = getMouseScreenX ();
			var y = getMouseScreenY ();
			if (x < 0) x = 0;
			if (y < 0) y = 0;
                        if (maxStringLength <= 15) {
             			if (x > 800 - 256) x = 800 - 256;
                        }
                        else if (maxStringLength <= 20) {
             			if (x > 800 - 316) x = 800 - 316;
                        } 
                        else if (maxStringLength <= 25) {
                                if (x > 800 - 393) x = 800 - 393;
                        }
                        else {
             			if (x > 800 - 483) x = 800 - 483;
                        }
			if (y > 600 - 120) y = 600 - 120;

			# Freeze the screen
			freeze ();

                        # Set the string color.
                        setPasteColor (255, 255, 255);

                        #Paste on the blue rectangles.
                        if (maxStringLength <= 15) addOverlay ('iface/blue1.tga', x, y);
                        else if (maxStringLength <= 20) addOverlay ('iface/blue2.tga', x, y);
                        else if (maxStringLength <= 25) addOverlay ('iface/blue4.tga', x, y);
                        else addOverlay ('iface/blue3.tga', x, y);

                        #Paste the strings.
                        pasteString (x+6, y+26, lookAt + thisObject);
                        pasteString (x+6, y+54, pickUp + thisObject);
                        pasteString (x+6, y+81, talkTo + thisObject);
                        pasteString (x+6, y+110, use + thisObject);


			# Add a screen region for each action...
			addScreenRegion (lookAt,	x,	y,	x + 300,  y + 27, 0, 0, NORTH);
			addScreenRegion (pickUp,	x,	y+30,	x + 300,  y + 56, 0, 0, NORTH);
			addScreenRegion (talkTo,	x,	y+60,	x + 300,  y + 85, 0, 0, NORTH);
			addScreenRegion (use,		x,	y+90,	x + 300,  y + 113, 0, 0, NORTH);

			#addScreenRegion (inventory,	x + 0,	 y + 65,  x + 115, y + 150, 0, 0, NORTH);

			# Pop!
			playSound ('iface/pop.wav');
			# Same goes for the event handlers
			onLeftMouseUp (pickVerb);
			onFocusChange (handleFocus);

			# Call handleFocus once ourselves, with the mouse pointing at NULL (nothing)
			handleFocus (NULL);
		}
	} else {
		if (currentInvItem) {
			currentInvItem = NULL;
			handleFocus (NULL);
		} else {
			# We didn't click an object. Just walk to the location of the mouse.
                        spawnSub (maxIdle);
			moveCharacter (sam, getMouseX (), getMouseY ());
                        maxMove ();
		}
	}
}

# This is the code for clicking the left mouse button when the verb coin's up
sub pickVerb () {
	var gotVerb = getOverObject ();
	var gotObject = thisObject;
	thisObject = NULL;
	unfreeze ();
	handleFocus (NULL);
	if (gotVerb) {
		if (! callEvent (walkToPerson, gotObject)) {
			moveCharacter (sam, gotObject);
		}
                stopCharacter (max);
                cancelSub (maxIdle);
		stop ();
		findEvent (gotVerb, gotObject);
		go ();
	}
}