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

    resizeHeight();
    buildTabBar();
    // selfPromote();

}

// *****************************************************************************
// Re-size pages
// *****************************************************************************
function resizeHeight() {
/* In order to make sure that div that holds the page content is tall
  enough to fill up the whole height of the browser, we do a little test
  here and resize if necessary.
*/

    /* Find the current height of the content div */
    contentObj = document.getElementById('content');
    var contentObjHeight = contentObj.offsetHeight;

    /* Find the browswer window height */
    var screenHeight = screen.height;

    /* Resize div if necessary */
    if (contentObjHeight < screenHeight) {
        contentObj.style.height = screenHeight + 'px';
    }
}

// *****************************************************************************
// In-page tab bar navigation
// *****************************************************************************
var tabArray = new Array;
var selectedTab = '';
function buildTabBar() {

    if (document.getElementById('tab_bar')) {

        var tabData = document.getElementById('tab_bar').innerHTML;
        tabArray = tabData.split('|');

        var tabs_code = '<table cellpadding="0" cellspacing="0" width="100%"><tr><td class="blank_tab" style="width: 15px;">&nbsp;</td>';
        for (var i = 0; i < tabArray.length; i++) {

            // Chomp string
            tabArray[i] = tabArray[i].replace(/[^a-zA-Z0-9]+$/, '');

            // IDs
            var tabID = 'tab' + i;
            var contentID = (tabArray[i].split(':'))[1];

            // General vars
            var linkTarget = (tabArray[i].split(':'))[0] + '_' + (contentID.toLowerCase()).replace(/ /g, '_') + '.rhtml';
            linkTarget = linkTarget.replace(/^[^a-zA-Z0-9]+/, '');
            if (tabArray[i].match(':selected')) selectedTab = tabID;

            // Styles
            var tabStyle = (tabArray[i].match(':selected')) ? 'tab_selected' : 'tab';
            var linkStyle = (tabArray[i].match(':selected')) ? 'selected_tab' : 'unselected_tab';

            // Tab Cell
            tabs_code += '<td class="' + tabStyle + '" id="' + tabID + '" ' +
                'onMouseOver="highlightTab(this.id)" ' +
                'onMouseOut="unHighlightTab(this.id)">' +
                '<a class="' + linkStyle + '" href="' + linkTarget + '" onFocus="this.blur()">' + contentID + '</a></td>' +
                '<td class="blank_tab" style="width: 5px;">&nbsp;</td>';

        }
        tabs_code += '<td class="blank_tab" width="100%">&nbsp;</td></tr></table>';

        // Display everything
        document.getElementById('tab_bar').innerHTML = tabs_code;
        document.getElementById('tab_bar').style.display = 'block';

    }
}


function highlightTab(tabID) {

    if (document.getElementById(tabID) && selectedTab != tabID) {
        document.getElementById(tabID).style.background = '#888888';
        document.getElementById(tabID).style.color = '#111111';
    }
}


function unHighlightTab(tabID) {

    if (document.getElementById(tabID) && selectedTab != tabID) {
        document.getElementById(tabID).style.background = '#444444';
        document.getElementById(tabID).style.color = '#ffffff';
    }
}


// *****************************************************************************
// Self-promotion link box
// *****************************************************************************
// Set messages to scroll through
var messages = new Array;
messages[0] = 'For more great stuff visit the<br />rest of the Caretcake Network:<br /><a class="sidebar" href="http://apostrophe.caretcake.com" target="_blank">http://apostrophe.caretcake.com</a><br /><a class="sidebar" href="http://mine.caretcake.com" target="_blank">http://mine.caretcake.com</a>';
messages[1] = 'Visit the dave.caretcake bookmarks<br />on del.ici.ous at <a class="sidebar" href="http://del.icio.us/dave.caretcake" target="_blank">http://del.icio.us/dave.caretcake</a>.<br />Or, <a class="sidebar" href="http://del.icio.us/network?add=dave.caretcake" target="_blank">add me to your network</a>.';
messages[2] = 'Check out my photography on the<br />dave.caretcake flickr photo site at <a class="sidebar" href="http://www.flickr.com/photos/davecaretcake/" target="_blank">http://www.flickr.com</a>.';
messages[3] = 'For more great stuff visit the<br />rest of the Caretcake Network:<br /><a class="sidebar" href="http://apostrophe.caretcake.com" target="_blank">http://apostrophe.caretcake.com</a><br /><a class="sidebar" href="http://mine.caretcake.com" target="_blank">http://mine.caretcake.com</a>';
messages[4] = 'Cityscapes, landscapes, abstract,<br />and surreal paintings and sketches by Paul Abbey<br /><a class="sidebar" href="http://www.artofabbey.net" target="_blank">http://www.artofabbey.net</a>';
messages[5] = 'Illustrations and photography by<br />Seattle-based artist Gabrielle Fine.<br /><a class="sidebar" href="http://www.gabriellefine.com" target="_blank">http://www.gabriellefine.com</a>';
messages[6] = 'Improve your life! Cultivate<br />satisfaction!  Develop yourself! Or,<br />just visit Brian Tanaka\'s website, <a class="sidebar" href="http://www.devyou.com" target="_blank">http://www.devyou.com</a>';
// Init constants
var onScreen = 0;           // MSG currently displayed
var frame = 0;              // Where in the animation we are
var lastR = 255;  var lastB = 255;  var lastG = 255;

function selfPromote() {
/* Populate the social_networking_container div */

    var container = document.getElementById('social_networking_container');
    container.innerHTML = '<div id="msg">' + messages[0] + '</div>';
    mainPromoteLoop();
    container.style.display = 'block';

}

function mainPromoteLoop() {
// 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 = 68;  lastB = 68;  lastG =  68;
        var object = document.getElementById('msg');
        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 = 255;  lastB = 255;  lastG = 255;
        var object = document.getElementById('msg');
        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('msg').innerHTML = messages[onScreen];

}


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

    lastR -= 10;
    if (lastR < 68) lastR = 68;
    lastG -= 10;
    if (lastR < 68) lastG = 68;
    lastB -= 10;
    if (lastR < 68) lastR = 68;
    var object = document.getElementById('msg');
    object.style.color = 'rgb(' +  lastR + ', ' + lastB + ', ' + lastG + ')';

}
