﻿// Scrolls the given images.
// images is a list of image urls seperated by a pipe.
// links is an optional list of urls the image will link to when clicked.
// delay is the amount of time to show a given image.
function ScrollImages(element, images, links, delay, imageId, initialize) {
    // Array lists.    
    imageArray = images.split("|");
    linksArray = links.split("|");

    // Initialize/reset index.
    if (imageId == undefined || imageId >= imageArray.length) imageId = 0;

    // Preload image list if requested (for smoother scrolling)
    if (initialize == true) {
        for (var i = 0; i < imageArray.length; i++) {
            var img = new Image();
            img.src = imageArray[i];
        }
    }

    // Load image (optionally with link)...
    if (linksArray[imageId] != undefined) { document.getElementById(element).innerHTML = "<a href=\"" + linksArray[imageId] + "\"><img alt=\"" + imageArray[imageId] + "\" src=\"" + imageArray[imageId] + "\" /></a><br />" + imageArray[imageId]; }
    else { document.getElementById(element).innerHTML = "<img alt=\"\" src=\"" + imageArray[imageId] + "\" />"; }

    // Fade in the image.
    FadeIn(element, 1);

    // Scroll to next image after delay.          
    window.setTimeout("ScrollImages('" + element + "', '" + images + "', '" + links + "', " + delay + ", " + (imageId + 1) + ")", delay - (1000));
}

// Scrolls the messages by fading each one in.
// messages is a string of messages, seperated by the pipe character.
// links is a string of urls corresponding to the messages, also seperated by a pipe.
// delay is the time to leave the message up.
// If messageId is not passed it will initialize. Otherwise, it will start from that message.
function ScrollMessages(element, messages, links, delay, messageId) {
    // Array lists.
    messageArray = messages.split("|");
    linksArray = links.split("|");

    // Initialize/reset index.
    if (messageId == undefined || messageId >= messageArray.length) messageId = 0;

    // Load text (optionally with link)...
    if (linksArray[messageId] != undefined) { document.getElementById(element).innerHTML = "<a href=\"" + linksArray[messageId] + "\">" + messageArray[messageId] + "</a>"; }
    else { document.getElementById(element).innerHTML = messageArray[messageId]; }

    // Fade in text.
    FadeIn(element, 1);

    // Scroll to next image after delay.
    window.setTimeout("ScrollMessages('" + element + "', '" + messages + "', '" + links + "', " + delay + ", " + (messageId + 1) + ")", delay - (1000));
}

// Fades in the given element.
function FadeIn(element, step) {
    if (step <= 20) {
        SetOpacity(element, (step / 20));
        step++;
        window.setTimeout("FadeIn('" + document.getElementById(element).id + "', " + step + ")", 50);
    }
}

// Sets the opacity of an element.
function SetOpacity(element, opacity) {
    var el = document.getElementById(element);
    if (el.style.opacity != undefined) { el.style.opacity = opacity; }
    else if (el.style.MozOpacity != undefined) { el.style.MozOpacity = opacity; }
    else if (el.style.filter != undefined) { el.style.filter = "alpha(opacity=" + Math.round(opacity * 100) + ")"; }
}

// Hides/unhides the answer section of the FAQ page.
// This requires each q and a to be in their own div, nested inside another div.
// i.e. <div id="parent">
//          <div id="question">this is the questions</div>
//          <div id="answer">this is the answer</div>
//      </div>
function ToggleAnswer(element) {     
    var questionContainer = element.parentNode;
    while (questionContainer.tagName != "DIV" && questionContainer != null) {
        questionContainer = questionContainer.parentNode;
    }
    var parentContainer;
    if (questionContainer != null) { parentContainer = questionContainer.parentNode; }
    var children = parentContainer.childNodes;                       
    for (var i = 0; i < children.length; i++) {
        if (children[i].tagName == "DIV" && children[i] != questionContainer) {
            if (children[i].style.display == 'none') { children[i].style.display = 'inline'; }
            else { children[i].style.display = 'none'; }
            break;                    
        }
    }
    return false; // This keeps the href action from happening, causing the click to essentially not happen.
}

// Hides/unhides the given element.
function ToggleElementDisplay(element) {
    element = document.getElementById(element);
    if (element.style.display == 'none')
        element.style.display = 'inline';
    else
        element.style.display = 'none';
} 

// confirm dialogue
function Confirm(message) {
    if (confirm(message)) {
        return true; 
    } else {
        return false;
    } 
} 