<!--

/*  ************************************************************  */
/*  For inclusion in the head of any document page that wants it  */
/*  ************************************************************  */

/*  This script eases the inclusion of expandable examples in a document. 
    Each example is some arbitrary amount of script which is not wanted in 
    the ordinary flow of text but which the reader is invited to reveal and 
    run, if in the mind for diversion. 

    A DIV with the class name "Demonstration" or "Example" may have an ID 
    that describes the example. 

    The DIV is to contain a SCRIPT block, plus explanatory text for readers 
    who do not have scripting enabled. The ID of the script block names the 
    function to run for the example.  */

/*  *************  */
/*  Configuration  */

var sDemoClass = "Demonstration";

var sSourceClass = "source";

/*  **************  */
/*  Implementation  */

function CreateButton (Text, ShowOrHide)
{
    var document = window.document;
    var button = document.createElement ("BUTTON");
    button.style.display = ShowOrHide ? "block" : "none";
    button.appendChild (window.document.createTextNode (Text));
    return button;
}

function CreateExample (Div)
{
    var scripts = Div.getElementsByTagName ("SCRIPT");
    var numscripts = scripts.length;
    if (numscripts == 0) return false;

    var scriptfunc = new Function (scripts [numscripts - 1].id + " ();");
    if (scriptfunc == null) return false;

    var code = "";
    for (var n = 0; n < numscripts; n ++) {
        if (n != 0) code += "\n\n";
        code += scripts [n].text;
    }
    code = code.replace ("\r\n", "\n");         // better for Internet Explorer 
    /^([\s\S]*\S)\s*$/m.exec (code);            // extract without trailing white space 
    code = RegExp.$1;

    var document = window.document;
    var oscript = document.createElement ("PRE");
    oscript.className = sSourceClass;
    oscript.appendChild (document.createTextNode (code));

    var name = Div.className;
    var id = Div.id;
    if (id != null && id != "") name += " (" + id + ")";

    var oshow = CreateButton ("Show " + name, true);
    var ohide = CreateButton ("Hide " + name, false);
    var orun = CreateButton ("Run " + name, false);

    Div.style.display = "none";
    Div.appendChild (oscript);

    var parent = Div.parentNode;
    parent.insertBefore (oshow, Div);
    parent.insertBefore (ohide, Div);
    parent.insertBefore (orun, Div);
    
    oshow.onclick = function ()
    {
        oshow.style.display = "none";
        ohide.style.display = "inline";
        ohide.focus ();
        orun.style.display = "inline";
        Div.style.display = "block";
    };

    ohide.onclick = function ()
    {
        oshow.style.display = "block";
        oshow.focus ();
        ohide.style.display = "none";
        orun.style.display = "none";
        Div.style.display = "none";
    };

    orun.onclick = scriptfunc;
}

function ConfigureExamples (Event)              // onload 
{
    var divs = window.document.getElementsByTagName ("DIV");
    var numdivs = divs.length;
    for (var n = 0; n < numdivs; n ++) {
        var div = divs [n];
        if (div.className == sDemoClass) CreateExample (div);
    }
}

RegisterEventHandler (window, "load", ConfigureExamples);

/*  Copyright © 2007-2009. Geoff Chappell. All rights reserved.  */

//-->