function donationextend_dohook($hookname, $args)
{
    global $session;
    switch ($hookname) {
        case "donation":
            require_once "lib/gamelog.php";
            gamelog("Donation registered by donationextend");
            $amt = $args['amt'];
            $id = $args['id'];
            $kitty = get_module_setting("kitty");
            $newkitty = $kitty + $amt;
            set_module_setting("kitty", $newkitty);
            //set last player
            if (!get_module_pref("user_shy", "donationextend", $id)) {
                $sql = "SELECT name FROM " . db_prefix("accounts") . " WHERE acctid='{$id}'";
                $result = db_query_cached($sql, "donationextend_lastdonator", 300);
                $row = db_fetch_assoc($result);
                $name = $row['name'] . "`0";
                set_module_setting("lastdonator", $name);
            } else {
                set_module_setting("lastdonator", "Anonymous");
            }
            break;
        case "everyfooter":
            //debug($args,true);
            // Set up and decrement the kitty
            require_once "lib/bars.php";
            $last = get_module_setting("lasttick");
            $ext1 = get_module_setting("extend1cost");
            $player = get_module_setting("lastdonator");
            $player = appoencode(stripslashes($player));
            $elapsed = time() - $last;
            $dropped = 0;
            $droppertenseconds = get_module_setting("extend1cost") / 8640;
            if ($elapsed > 10) {
                $dropped = $droppertenseconds;
            }
            $oldkitty = get_module_setting("kitty");
            $newkitty = $oldkitty - $dropped;
            if ($newkitty < 0) {
                $newkitty = 0;
            }
            //set up display output
            $out = "";
            $sql = "SELECT count(acctid) AS c FROM " . db_prefix("accounts") . " WHERE locked=0";
            $result = db_query_cached($sql, "donationextend_totalplayers", 1800);
            $row = db_fetch_assoc($result);
            $totalplayers = $row['c'];
            if ($newkitty > $ext1) {
                $ext2 = get_module_setting("extend2cost");
                if ($newkitty > $ext2) {
                    $over = $newkitty - $ext2;
                    $bar = simplebar($over, $ext2, 70, 5, "AA00AA", "FFFFFF");
                    $overdisp = number_format($over / 100, 2);
                    //represent as time left to end of Special Extend period
                    $droppersec = get_module_setting("extend1cost") / 86400;
                    $secsleft = round($over / $droppersec);
                    $expirationtime = time() + $secsleft;
                    require_once "lib/datetime.php";
                    $expirein = reltime($expirationtime, false);
                    $out .= "<span style='font-size:smaller'><strong>Special Extend active!</strong><br />" . $bar . "Thank you for your support!  Special Extend expires in " . $expirein . "<br /><a href='runmodule.php?module=donationextend' target='_blank' onclick=\"" . popup("runmodule.php?module=donationextend") . ";return false;\">(what's a Special Extend?)</span></a>";
                } else {
                    $moreneeded = $ext2 - $newkitty;
                    $bar = simplebar($newkitty - $ext1, $ext2 - $ext1, 70, 5, "00FF00", "AA00AA");
                    $moredisp = number_format($moreneeded / 100, 2);
                    $out .= "<span style='font-size:smaller'><strong>Extended Play active!</strong><br />" . $bar . "\$" . $moredisp . " more for <a href='runmodule.php?module=donationextend' target='_blank' onclick=\"" . popup("runmodule.php?module=donationextend") . ";return false;\">Special Extend</a>!</span>";
                }
            } else {
                $moreneeded = $ext1 - $newkitty;
                $moredisp = number_format($moreneeded / 100, 2);
                $bar = fadebar($newkitty, $ext1, 57);
                $perplayer = $moreneeded / 100 / $totalplayers;
                if ($perplayer > 1) {
                    $perplayerdisp = "about " . round($moreneeded / $totalplayers, 4) . " cents per player";
                } else {
                    $perplayerdisp = "about \$" . number_format($moreneeded / 100 / $totalplayers, 2) . " per player";
                }
                $out .= $bar;
                if ($newkitty > 0) {
                    $out .= "<span style='font-size:smaller'>\$" . $moredisp . " more (" . $perplayerdisp . ") for <a href='runmodule.php?module=donationextend' target='_blank' onclick=\"" . popup("runmodule.php?module=donationextend") . ";return false;\">Extended Play</a>!</span>";
                } else {
                    if ($session['user']['loggedin']) {
                        $out .= "<span class='colDkRed'><strong>The kitty is empty!</strong></span>  Improbable Island is entirely dependant on your donations to survive.  When the meter is empty, the Island and its creators are in trouble!  Someone please chuck a couple of bucks in the hat!";
                    }
                }
            }
            global $template;
            //debug($template);
            if (strpos($template['footer'], "{paypal_extras}")) {
                //if (isset($template['{paypal_extras}'])){
                //debug("Yes!");
                $rep = "paypal_extras";
            } else {
                //debug("No!");
                $rep = "paypal";
            }
            //$rep = "paypal_extras";
            //insert display output into page
            if (!array_key_exists($rep, $args) || !is_array($args[$rep])) {
                $args[$rep] = array();
            }
            array_push($args[$rep], $out);
            //write values back to database
            if ($elapsed > 10) {
                set_module_setting("lasttick", time());
                set_module_setting("kitty", $newkitty);
            }
            //debug($args,true);
            addcharstat("Misc");
            addcharstat("Misc", "testing testing woohoo!");
            break;
        case "stamina-newday":
            $kitty = get_module_setting("kitty");
            if ($kitty >= get_module_setting("extend1cost")) {
                require_once "modules/staminasystem/lib/lib.php";
                $bonus = get_module_setting("extend1amt");
                if ($kitty > get_module_setting("extend2cost")) {
                    $bonus += get_module_setting("extend2amt");
                    output("`0Because Improbable Island's players have been especially generous, today is a `b`5Special Extended Play`0`b day - you `2gain`0 some Stamina!`n`n");
                } else {
                    output("`0Because Improbable Island's server costs and advertising budget have been covered quite nicely by donations, today is an `b`2Extended Play`0`b day - you `2gain`0 some Stamina!`n`n");
                }
                if ($session['user']['donation'] > 0) {
                    $bonus = $bonus * 2;
                }
                addstamina($bonus);
            }
            break;
    }
    return $args;
}
function fadebar($cur, $max, $width = 70, $height = 5, $bgcol = "222222", $reverse = false)
{
    $pct = $cur / $max * 100;
    $nonpct = 100 - $pct;
    $px = $width / 100 * $pct;
    $nonpx = $width - $px;
    if ($cur < $max) {
        if ($pct < 50) {
            $red = 5 * $pct;
            $redhex = dechex($red);
            $greenhex = "FF";
        } else {
            $green = 5 * $nonpct;
            $greenhex = dechex($green);
            $redhex = "FF";
        }
        //check for missing characters
        if (strlen($greenhex) < 2) {
            $greenhex = "0" . $greenhex;
        } else {
            if (strlen($redhex) < 2) {
                $redhex = "0" . $redhex;
            }
        }
        if ($reverse) {
            $barcol = $redhex . $greenhex . "00";
        } else {
            $barcol = $greenhex . $redhex . "00";
        }
        $bar = simplebar($cur, $max, $width, $height, $bgcol, $barcol);
    } else {
        if ($reverse) {
            $barcol = "FF0000";
        } else {
            $barcol = "00FF00";
        }
        $bar = simplebar($cur, $max, $width, $height, $bgcol, $barcol);
    }
    return $bar;
}