﻿function allPagesLoaded(evt) {
    var b = document.getElementById("titleSwitch");
    if (b) {
        EventUtils.addEventListener(b, "click", titleFontSwitch, false);
    }
    var b = document.getElementById("bodySwitch");
    if (b) {
        EventUtils.addEventListener(b, "click", bodyFontSwitch, false);
    }
    var b = document.getElementById("bgSwitch");
    if (b) {
        EventUtils.addEventListener(b, "click", titleBackgroundSwitch, false);
    }
}

var bgState = 0;
var titleState = 0;
var bodyState = 0;

function titleBackgroundSwitch(evt) {
    var b = document.getElementById("header");
    switch (bgState) {
        case 0:
            b.style.backgroundImage = "url('./images/header-back2.gif')";
            bgState++;
            break;
        case 1:
            b.style.backgroundImage = "url('./images/header-back.gif')";
            bgState = 0;
            break;
    }
    alert(b.style.backgroundImage);
}

function bodyFontSwitch(evt) {
    var b = document.getElementsByTagName("BODY")[0];
    switch (bodyState) {
        case 0:
            b.style.fontFamily = "Klavika Regular";
            bodyState++;
            break;
        case 1:
            b.style.fontFamily = "Trebuchet MS";
            bodyState++;
            break;
        case 2:
            b.style.fontFamily = "Tahoma";
            bodyState++;
            break;
        case 3:
            b.style.fontFamily = "Arial";
            bodyState++;
            break;
        case 4:
            b.style.fontFamily = "sans-serif";
            bodyState = 0;
            break;
    }
    alert(b.style.fontFamily);
}

function titleFontSwitch(evt) {
    var b = document.getElementById('mainTitle');
    switch (titleState) {
        case 0:
            b.style.fontFamily = "Edmunds";
            titleState++;
            break;
        case 1:
            b.style.fontFamily = "Times New Roman";
            titleState++;
            break;
        case 2:
            b.style.fontFamily = "Serif";
            titleState = 0;
            break;
    }
    alert(b.style.fontFamily);
}
    

// Constructor
function EventUtils() {
    throw 'RuntimeException: EventUtils is a static utility class' +
            ' and may not be instantiated';
}

EventUtils.addEventListener = function(target, type, callback, captures) {
    if (target.addEventListener) {
        // EOMB
        target.addEventListener(type, callback, captures);
    } else if (target.attachEvent) {
        // IE
        target.attachEvent('on' + type, callback, captures);
    } else {
        // IE 5 Mac and some others
        target['on' + type] = callback;
    }
}

EventUtils.getEventTarget = function(evt) {
    if (evt.target) return evt.target;
    if (evt.srcElement) return evt.srcElement;
}

// Constructor
function DOMUtils() {
    throw 'RuntimeException: DOMUtils is a static utility class' +
            ' and may not be instantiated';
}

/**
 * Gets the next sibling element with the specified tag name. If no such
 * sibling exists, returns null.
 * @param target - The element whose siblings should be searched.
 * @param tagString - The tag to search for, case-insensitive.
 * @param incl - True if the target itself should be included in the search,
 *               false otherwise.
 */ 
DOMUtils.getNextElementByTagName = function(target, tagString, incl) {
    if (incl) {
        element = target;
    } else {
        element = target.nextSibling;
    }
    
    while (element) {
        if (element.tagName == tagString.toUpperCase()) { break; }
        element = element.nextSibling;
    }
    return element;
}

/**
 * Gets the previous sibling element with the specified tag name. If no such
 * sibling exists, returns null.
 * @param target - The element whose siblings should be searched.
 * @param tagString - The tag to search for, case-insensitive.
 * @param incl - True if the target itself should be included in the search,
 *               false otherwise.
 */
DOMUtils.getPreviousElementByTagName = function(target, tagString, incl) {
    if (incl) {
        element = target;
    } else {
        element = target.previousSibling;
    }

    while (element) {
        if (element.tagName == tagString.toUpperCase()) { break; }
        element = element.previousSibling;
    }
    return element;
}

/**
 * Gets the next sibling element with the specified string as part of it's
 * class attribute. If no such sibling exists, returns null.
 * @param target - The element whose siblings should be searched.
 * @param classString - The string to search class attributes for,
 *                      case-sensitive.
 * @param incl - True if the target itself should be included in the search,
 *               false otherwise.
 * @returns Any match in the class string, regardless of spacing, will cause an
 *          element to be returned. Thus, for a search on "bar", elements with
 *          class attributes "foo bar baz" and "foobar baz" would be potential
 *          matches, while "foo baz", or "foob arbaz" would not.
 */
DOMUtils.getNextElementByClass = function(target, classString, incl) {
    if (incl) {
        var element = target;
    } else {
        var element = target.nextSibling;
    }

    while (element) {
        if (!element.className || element.className.search(classString) < 0) {
            element = element.nextSibling;
        } else {
            break;
        }
    }
    return element;
}

/**
 * Gets the previous sibling element with the specified string as part of it's
 * class attribute. If no such sibling exists, returns null.
 * @param target - The element whose siblings should be searched.
 * @param classString - The string to search class attributes for,
 *                      case-sensitive.
 * @param incl - True if the target itself should be included in the search,
 *               false otherwise.
 * @returns Any match in the class string, regardless of spacing, will cause an
 *          element to be returned. Thus, for a search on "bar", elements with
 *          class attributes "foo bar baz" and "foobar baz" would be potential
 *          matches, while "foo baz", or "foob arbaz" would not.
 */
DOMUtils.getPreviousElementByClass = function(target, classString, incl) {
    if (incl) {
        var element = target;
    } else {
        var element = target.previousSibling;
    }

    while (element) {
        if (!element.className || element.className.search(classString) < 0) {
            element = element.previousSibling;
        } else {
            break;
        }
    }
    return element;
}