﻿/* Compares the links in the menu with the current location; any links that
 * match this location are set to selected.
 * This means the menu HTML can be copied and pasted between most pages
 * without any changes.
 */
function MenuControl() {
    var menu_div = document.getElementById("menu_div");
    var menu_links = menu_div.getElementsByTagName("a");

    var pageURL = location.href;  // Current location, including page name
    var lastSlash = pageURL.lastIndexOf('/');
    var path = pageURL.substring(0, lastSlash + 1);  // Get rid of page name

    // Check each menu item
    for (i = 0; i < menu_links.length; i++) {
        var a = menu_links[i];
        var link = a.href;  // The browsers resolve this URL

        // Make sure there's a slash on the end for consistency
        if (link.substring(link.length - 1) != '/') {
            link += "/";
        }

        if (link == path) {
            a.className = "selected";
        }
    }
}


/* Create links to BibleGateway.com's search - having this function saves 
 * URL maintenance in our pages, and if Bible Gateway change their URLs then
 * it's easily reflected here with just one change.
 */
 
// Use these variables to specify the Bible version to use
var NIV = "64";
var TNIV = "72";

//function BibleSearchLink(searchTerm, link_text) {
//    BibleSearchLink(searchTerm, link_text, NIV);
//}
 
function BibleSearchLink(searchTerm, link_text, version) {
    //TODO: make this URL safe (e.g. replace spaces with %20)
    

    document.write(
        "<a target=\"_Blank\" href=\"" +
            "http://www.biblegateway.com/passage/?search=" +
            searchTerm + ";&version=" + version + ";\">" +
        link_text + "</a>"
    );
}


/* Delay the loading of the Yahoo Translator because otherwise it holds up the
* loading of the rest of the page.
*/
function LoadTranslator() {
    var yahoo_div = document.getElementById("yahoo_translator");
    yahoo_div.innerHTML =
        "<script type=\"text/javascript\" charset=\"UTF-8\" language=\"JavaScript1.2\"" +
        "src=\"http://uk.babelfish.yahoo.com/free_trans_service/babelfish2.js?from_lang=en&region=us\">" +
        "</script>";
}


/* A simple way to make a Date object
 */
function mkdate(year, month, day, hour, minute) {
    date = new Date();
    date.setFullYear(year);
    date.setMonth(month - 1);  // Months are 0-11
    date.setDate(day);
    date.setHours(hour);
    date.setMinutes(minute);
    return date;
}

/* Checks if the given Date object represents a date and time after
 * the date and time right now 
 */
function after(date) {
    if ((new Date).getTime() > date.getTime()) {
        return true;
    }
    return false;
}

/* Checks if the given Date object represents a date and time before
 * the date and time right now 
 */
function before(date) {
    if ((new Date).getTime() < date.getTime()) {
        return true;
    }
    return false;
}

/* Takes the ID of an element to hide after a certain date.
 * Eg. HideAfter( "div6", 2009,03,22,10,30 )
 */
function HideAfter(id, year, month, day, hour, minute) {
    element = document.getElementById(id);

    date = new Date();
    date.setFullYear(year);
    date.setMonth(month - 1);  // Months are 0-11
    date.setDate(day);
    date.setHours(hour);
    date.setMinutes(minute);

    var now = new Date();

    if (now.getTime() > date.getTime()) {
        element.style.display = "none";
    }
}

/* Takes the ID of an element to hide before a certain date.
 * Eg. HideBefore( "div6", 2009,03,22,10,30 )
*/
function HideBefore(id, year, month, day, hour, minute) {
    element = document.getElementById(id);

    date = new Date();
    date.setFullYear(year);
    date.setMonth(month - 1);  // Months are 0-11
    date.setDate(day);
    date.setHours(hour);
    date.setMinutes(minute);

    now = new Date();

    if (now.getTime() < date.getTime()) {
        element.style.display = "none";
    }
}