// Copyright 2004 by Washington University,
//      School of Medicine,
//      Department of Pediatrics,
//      Pediatric Computing Facility
//      webmaster@kids.wustl.edu
//
//  This is free software.
//
//  Version: 2004070800
//
//  Mods:
//      2004070800 by Brian
//          Tweaked say() a bit
//
//      2004060200 by Brian
//          First published.
//
var lastOn  = "";
var agent   = navigator.userAgent.toLowerCase();
var onWebTV = false;
if (agent.indexOf('webtv') > 0) {
    onWebTV = true;
}

function say(car, cdr, text) {
    var a = new Array('o', 't', 'l', 'i', 'a', 'm');
    a.reverse(); car.reverse(); cdr.reverse();
    var a_joined = a.join('');
    var j = car.join('') + '&' + '#6' + '4;' + cdr.join('.');
    if ((text == null) || (text == '')) { text = j }
    document.write('<a href="' + a_joined + ':' + j + '">' + text + '</a>');
    return true;
}

function writeRandomImg(how_many, img_x, img_y, img_alt, img_type, img_name, img_pfx, img_at) {
    var idx       = Math.abs((new Date()).getSeconds() % how_many) + 1;
    if (img_at == null) {
        img_at = images_at; //  use default global value
    }
    var first_img = img_at + '/' + img_pfx + '-';
    if (idx < 10) {
        first_img = first_img + '0';   // force leading zero in filename
    }
    first_img = first_img + idx + '.' + img_type;

    var img_line = '<img src="' + first_img + '" width="' + img_x + '" height="' +
        img_y + '" alt="' + img_alt + '" name="' + img_name + '">';
    document.write(img_line);
    return true;
}

function imgOn(imgName, imgType) {
    if (onWebTV) { return false; }
    if (document[imgName] == null) { return false;}
    if (!imgType) { imgType = "gif" }
    if (!document[imgName].saveSrc) {
        document[imgName].saveSrc = document[imgName].src;
    }

    document[imgName].src = images_at + "/" + imgName + "-on." + imgType;

    lastOn = imgName;
    return false;
}

function imgOff(imgName) {
    if (onWebTV) { return false; }
    if (!imgName) {
        imgName = lastOn;
    }

    if (document[imgName] == null) { return false;}
    document[imgName].src = document[imgName].saveSrc;
    return false;
}

function startRotate(first, lb, ub, delay, img_name,
            img_pfx, img_type, img_at) {
    first = Math.abs(first);
    lb    = Math.abs(lb);
    ub    = Math.abs(ub);
    delay = Math.abs(delay);

    if (img_at == null) { img_at = images_at }
    nextImg(img_name, img_pfx, img_type, lb, ub, img_at);

    nextCommand = "startRotate(0, " + lb + ", " + ub + ", " + delay + ", '" +
        img_name + "', '" +
        img_pfx + "', '" +
        img_type + "', '" +
        img_at + "');";
    timeID = setTimeout(nextCommand, delay);
    return true;
}

function nextImg(img_name, img_pfx, imgType, lb, ub, img_at) {
    if (document[img_name] == null) { return true; }
    lb    = Math.abs(lb);
    ub    = Math.abs(ub);
    if (nextNames[img_name] == null) {
        nextNames[img_name] = (new Date()).getSeconds() % ub + 1;
    }
    myIndex = Math.abs(nextNames[img_name]);
    myIndex++;
    if (myIndex > ub) {
        myIndex = lb;
    }
    if (img_at == null) {
        img_at = images_at; //  default to the global variable if it's not passed
    }
    newSrc = img_at + '/' + img_pfx + '-' + myIndex + "." + imgType;
    if (myIndex < 10) {
       newSrc = img_at + '/' + img_pfx + "-0" + myIndex + "." + imgType;
    }
    if (nextNames[img_name] >= 0) {
        document[img_name].src = newSrc;
        nextNames[img_name] = myIndex;
    }
    else {
        nextNames[img_name] = myIndex * -1;
    }
    return true;
}

