function init() {
/* Run all the onLoad commands */

    /* Originally the plan was to select a random program to highlight
      each time the index was reloaded.  In the end, BBF decided to have
      a static, general image/message instead.  The code below can be turned
      back on to re-create the randomization.  displayThumbnails() is only
      needed if chooseProgram() is turned off.
    */
    // chooseProgram();     // In "Load Program-Specific Content" code
    displayThumbnails();    // In "Load Program-Specific Content" code
    hpMessages();           // In "Rotating messages on the homepage" code

}

// *****************************************************************************
// Load Program-Specific Content
// *****************************************************************************
// Cache hidden graphics
var cachedImages = new Array();
if (document.images) {

    // Program Icons
    cachedImages["programs_icon1_on"] = new Image(98,132);
    cachedImages["programs_icon1_on"].src = "images/program_icon1_bk.jpg";
    cachedImages["programs_icon1_off"] = "images/program_icon1.jpg";

    cachedImages["programs_icon2_on"] = new Image(98,132);
    cachedImages["programs_icon2_on"].src = "images/program_icon2_bk.jpg";
    cachedImages["programs_icon2_off"] = "images/program_icon2.jpg";

    cachedImages["programs_icon3_on"] = new Image(98,132);
    cachedImages["programs_icon3_on"].src = "images/program_icon3_bk.jpg";
    cachedImages["programs_icon3_off"] = "images/program_icon3.jpg";

    cachedImages["programs_icon4_on"] = new Image(98,132);
    cachedImages["programs_icon4_on"].src = "images/program_icon4_bk.jpg";
    cachedImages["programs_icon4_off"] = "images/program_icon4.jpg";

    cachedImages["programs_icon5_on"] = new Image(98,132);
    cachedImages["programs_icon5_on"].src = "images/program_icon5_bk.jpg";
    cachedImages["programs_icon5_off"] = "images/program_icon5.jpg";

    cachedImages["programs_icon6_on"] = new Image(98,132);
    cachedImages["programs_icon6_on"].src = "images/program_icon6_bk.jpg";
    cachedImages["programs_icon6_off"] = "images/program_icon6.jpg";

    cachedImages["programs_icon7_on"] = new Image(98,132);
    cachedImages["programs_icon7_on"].src = "images/program_icon7_bk.jpg";
    cachedImages["programs_icon7_off"] = "images/program_icon7.jpg";

    // Program Headers
    cachedImages["programs_header1"] = new Image(487,276);
    cachedImages["programs_header1"].src = "images/program_header1.jpg";

    cachedImages["programs_header2"] = new Image(487,276);
    cachedImages["programs_header2"].src = "images/program_header2.jpg";

    cachedImages["programs_header3"] = new Image(487,276);
    cachedImages["programs_header3"].src = "images/program_header3.jpg";

    cachedImages["programs_header4"] = new Image(487,276);
    cachedImages["programs_header4"].src = "images/program_header4.jpg";

    cachedImages["programs_header5"] = new Image(487,276);
    cachedImages["programs_header5"].src = "images/program_header5.jpg";

    cachedImages["programs_header6"] = new Image(487,276);
    cachedImages["programs_header6"].src = "images/program_header6.jpg";
    
    cachedImages["programs_header7"] = new Image(487,276);
    cachedImages["programs_header7"].src = "images/program_header7.jpg";

}

function loadProgram(programID) {
// Display correct feature icon, header graphic, and text when users
// click on the thumbnails at the bottom of the page.
    if (programID) {

        /* Highlight the right program icon */
        for (var i=1; i < 8; i++) {
            if (programID == i) {
                document.getElementById('icon' + i).src = cachedImages['programs_icon' + i + '_on'].src;
            }
            if (programID != i) {
                document.getElementById('icon' + i).src = cachedImages['programs_icon' + i + '_off'];
            }
        }

        /* Load the right header graphic */
        document.getElementById('hp_header_image').innerHTML = '<img src="' + cachedImages['programs_header' + programID].src + '" width="487" height="276" />';

        /* Load the right header text */
        var programTexts = new Array();
        programTexts[1] =
            "<p class='hp_header_title' align='right'>Dairy Goats...</p>\n\n" +
            "<p class='hp_header_text' align='right'>BBF's Goat Dairy Program provide a group " +
            "of families with a herd of dairy goats, training by a " +
            "veterinarian,and a unique business model that protects " +
            "each family's property while efficiently sharing " +
            "resources. Project Greenhands and BBF partnered to " +
            "design this Goat Dairy Program to be environmentally " +
            "sustainable.  With the proper training, a goat dairy " +
            "can provide income and sustenance for generations to come.</p>\n\n" +
            "<p align='right'><img src='images/program_arrows.gif' height='8' width='18' border='0' valign='bottom'><a class='program_link' href='dairy_goats.html'>Learn more here</a></p>";

        programTexts[2] =
            "<p class='hp_header_title' align='right'>Mushrooms...</p>\n\n" +
            "<p class='hp_header_text' align='right'>Because mushrooms command " +
            "a high price at markets in Aceh, they are a valuable commodity. We " +
            "developed a program that turns mushroom farming into a manageable " +
            "process for enterprising women. Like all of our programs, this is " +
            "a self-sustaining business using locally-available materials.</p>\n\n" +
            "<p align='right'><img src='images/program_arrows.gif' height='8' width='18' border='0' valign='bottom'><a class='program_link' href='mushrooms.html'>Learn more here</a></p>";

        programTexts[3] =
            "<p class='hp_header_title' align='right'>Community Infrastructure...</p>\n\n" +
            "<p class='hp_header_text' align='right'>After the tsunami hit Aceh in " +
            "December 2004 and took almost 170,000 lives, BBF leveraged its small " +
            "size to move in and help remote villages. BBF filled-in some of the " +
            "gaps while large relief agencies tackled recovery issues and " +
            "infrastructure repairs in major population centers.</p>\n\n" +
            "<p align='right'><img src='images/program_arrows.gif' height='8' width='18' border='0' valign='bottom'><a class='program_link' href='infrastructure.html'>Learn more here</a></p>";

        programTexts[4] =
            "<p class='hp_header_title' align='right'>Honey...</p>\n\n" +
            "<p class='hp_header_text' align='right'>BBF’s Honey Bee Program taps " +
            "into the expertise of nearby beekeepers to set-up bee colonies and " +
            "provide training so that local communities can maintain them in a " +
            "way that ensures longevity of the bee colonies, providing honey that " +
            "may be used in villages and sold.</p>\n\n" +
            "<p align='right'><img src='images/program_arrows.gif' height='8' width='18' border='0' valign='bottom'><a class='program_link' href='honey.html'>Learn more here</a></p>";

        programTexts[5] =
            "<p class='hp_header_title' align='right'>Education...</p>\n\n" +
            "<p class='hp_header_text' align='right'>In areas of Indonesia that " +
            "are now recovering from years of fighting, long-lasting peace is " +
            "dependent upon the adults and younger generations developing new " +
            "skills and livelihoods. BBF sees great opportunity to seize on the " +
            "new hope brought about by peace and provide tremendous impact with " +
            "basic education, adult education, literacy, and small business " +
            "management programs.</p>\n\n" +
            "<p align='right'><img src='images/program_arrows.gif' height='8' width='18' border='0' valign='bottom'><a class='program_link' href='education.html'>Learn more here</a></p>";

        programTexts[6] =
            "<p class='hp_header_title' align='right'>Building Professional<br />Social Work...</p>\n\n" +
            "<p class='hp_header_text' align='right'>In developing countries, " +
            "professional social workers possess the ideal skills for planning and " +
            "implementing much needed social welfare services and programs. When local " +
            "social workers are available, they help to establish social welfare " +
            "systems, provide vital helping resources for long-term needs and deliver " +
            "services in a culturally sensitive manner in languages used by the " +
            "community</p>\n\n" +
            "<p align='right'><img src='images/program_arrows.gif' height='8' width='18' border='0' valign='bottom'><a class='program_link' href='http://www.bpsw.org' target='_blank'>Learn more here</a></p>";

        programTexts[7] =
            "<p class='hp_header_title' align='right'>Goat Breeding and Fattening...</p>\n\n" +
            "<p class='hp_header_text' align='right'>In July 2008, BBF " +
            "opened the largest goat breeding and fattening center " +
            "in Aceh, funded by an Indonesian gov- ernment agency, the BRR Trust " +
            "Fund. The Lamno Livestock & Agriculture Learning Center is designed " +
            "to provide economic, environmental, and social benefits to the region.</p>\n\n" +
            "<p class='hp_header_text' align='right'>We are in the process of " +
            "updating the Web page for this program. Visit again in the next " +
            "few weeks to learn more.</p>\n\n" +
            "<p align='right'><img src='images/program_arrows.gif' height='8' width='18' border='0' valign='bottom'><a class='program_link' href='pr_aug1908.html'>Learn more here</a></p>";

        document.getElementById('hp_header').innerHTML = programTexts[programID];

    }
}


function chooseProgram() {
// Randomly select a program to start with

    // Display the icons
    displayThumbnails();

    // Select a random program (1-7) and call loadProgram with it
    var programID = (Math.floor(Math.random() * 7)) + 1;
    loadProgram(programID);

}

function displayThumbnails() {

    // Display the icons
    document.getElementById('icons').style.display = 'block';

}


// *****************************************************************************
// Rotating messages on the homepage
// *****************************************************************************
// Set messages to scroll through
var messages = new Array;
var attributeSpacer = (window.ActiveXObject) ? '<br /><br /><br />' : '<br /><br />';
messages[0] = '<br />"Those hit hardest by disaster have the greatest desire to rebuild but are left with few resources to get it done.  Building Bridges to the Future provides basic resources and support after disaster so that vulnerable people can start again and rebuild their families\' lives."' + attributeSpacer + '<i>&#150; Sara Henderson</i>';
messages[1] = '<br />"I could never imagine that we would be receiving a small business as a donation from anyone. Building Bridges has helped the people in Lamtui to be productive and have a livelihood for many years to come with the Dairy Goat Program."' + attributeSpacer + '<i>&#150; Yusuf A. Rahman,<br />Chief of Lamtui Village</i>';
messages[2] = '<br />"Building Bridges was almost the first organization to reach Lamno in March 2005 and has shown its commitment and dedication to helping the tsunami affected people in Aceh."' + attributeSpacer + '<i>&#150; Jadal Husaini,<br />Head of Lamno Sub-District<br />in Aceh Jaya</i>';
messages[3] = '<br />"The medical clinic provided by Building Bridges and ThyssenKrupp in Janguet is the greatest contribution the village received after the tsunami. This clinic is in a good location to help not only our village, but many villages nearby."' + attributeSpacer + '<i>&#150; Chief of Janguet Village</i>';
messages[4] = '<br />"I thank Sara and Building Bridges for meeting the needs of our community to repair the farmland after the tsunami destroyed it. Now we can begin planting rice in the fields again."' + attributeSpacer + '<i>&#150; Secretary of Ujung<br />Seudhen Village</i>';
messages[5] = '<br />"Building Bridges was the only organization that came to help the Rumpet community and provide new homes for all of us. If Sara and Building Bridges never made it to Rumpet, we might now still be living in emergency tents. We are all very grateful."' + attributeSpacer + '<i>&#150; Rosie, Rumpet Village</i>';
messages[6] = '"We are very happy to see that the innovation of the Building Bridges to the Future Foundation and the hard work of the people of Lamno is creating new jobs and economic opportunity. This Learning Center created by Building Bridges and their partners will create more jobs for future generations."' + attributeSpacer + "<i>&#150; Nazmiyah Sayuti,<br />Indonesia Government's BRR Trust Fund (RecoveryAceh-Nias Trust Fund)</i>";
// Init constants
var onScreen = 0;                           // MSG currently displayed
var frame = 0;                              // Where in the animation we are
var lastR = 51;  var lastB = 51;  var lastG = 51;
var messageContainerID = 'hp_alert_text';   // Element holding the messages

function hpMessages() {
/* Populate the messages_container div */

    var container = document.getElementById(messageContainerID);
    container.innerHTML = messages[0];
    mainMessageLoop();
    container.style.display = 'block';

}

function mainMessageLoop() {
// The mainloop for the animation.  Calls loadMSG() 20 x's a second to set the frame rate
    setInterval("loadMSG()", 50)
}

function loadMSG() {
// Actually controls where in the animation we are.  Depending on the frame showing, the script does different things.

    if  (frame > 100 && frame < 116) {
    // If we're on frames 101 - 115, we do the color transition

        dimColor();
        frame++;
        return false;

    }
    else if (frame > 115 && frame < 130) {
    // If we're on frames 116 or 117, just show a blank message area

        lastR = 244;  lastB = 240;  lastG =  240;
        var object = document.getElementById(messageContainerID);
        object.style.color = 'rgb(' +  lastR + ', ' + lastB + ', ' + lastG + ')';
        frame++;
        return false;

    }
    else if (frame >= 130) {
    // If we're on frames 118 or higher, set everything back to the init values and prepare to switch messages

        lastR = 51;  lastB = 51;  lastG = 51;
        var object = document.getElementById(messageContainerID);
        object.style.color = 'rgb(' +  lastR + ', ' + lastB + ', ' + lastG + ')';
        frame = 0;
        onScreen++;
        if (onScreen >= messages.length) onScreen = 0;

    }
    else {
    // If we're on frames 0 - 100 (the first 5 seconds of every animation), just flip the frame and wait

        frame++;
        return false;

    }

    // Switch messages if we've reached frame 118
    document.getElementById(messageContainerID).innerHTML = messages[onScreen];

}


function dimColor() {
// Figure out the current color of the message text and dim it by one increment

    lastR += 10;
    if (lastR > 244) lastR = 244;
    lastG += 10;
    if (lastR > 240) lastG = 240;
    lastB += 10;
    if (lastR > 240) lastR = 240;
    var object = document.getElementById(messageContainerID);
    object.style.color = 'rgb(' +  lastR + ', ' + lastB + ', ' + lastG + ')';

}
