/* 
 * Cross-browser event handling, by Scott Andrew
 */
function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

/* 
 * Kills an event's propagation and default action
 */
function knackerEvent(eventObject) {
    if (eventObject && eventObject.stopPropagation) {
        eventObject.stopPropagation();
    }
    if (window.event && window.event.cancelBubble ) {
        window.event.cancelBubble = true;
    }
    
    if (eventObject && eventObject.preventDefault) {
        eventObject.preventDefault();
    }
    if (window.event) {
        window.event.returnValue = false;
    }
}

/* 
 * Safari doesn't support canceling events in the standard way, so we must
 * hard-code a return of false for it to work.
 */
function cancelEventSafari() {
    return false;        
}

/* 
 * Cross-browser style extraction, from the JavaScript & DHTML Cookbook
 * <http://www.oreillynet.com/pub/a/javascript/excerpt/JSDHTMLCkbk_chap5/index5.html>
 */
function getElementStyle(elementID, CssStyleProperty) {
    var element = document.getElementById(elementID);
    if (element.currentStyle) {
        return element.currentStyle[toCamelCase(CssStyleProperty)];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(element, '');
        return compStyle.getPropertyValue(CssStyleProperty);
    } else {
        return '';
    }
}

/* 
 * CamelCases CSS property names. Useful in conjunction with 'getElementStyle()'
 * From <http://dhtmlkitchen.com/learn/js/setstyle/index4.jsp>
 */
function toCamelCase(CssProperty) {
    var stringArray = CssProperty.toLowerCase().split('-');
    if (stringArray.length == 1) {
        return stringArray[0];
    }
    var ret = (CssProperty.indexOf("-") == 0)
              ? stringArray[0].charAt(0).toUpperCase() + stringArray[0].substring(1)
              : stringArray[0];
    for (var i = 1; i < stringArray.length; i++) {
        var s = stringArray[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1);
    }
    return ret;
}

/*
 * Disables all 'test' links, that point to the href '#', by Ross Shannon
 */
function disableTestLinks() {
  var pageLinks = document.getElementsByTagName('a');
  for (var i=0; i<pageLinks.length; i++) {
    if (pageLinks[i].href.match(/[^#]#$/)) {
      addEvent(pageLinks[i], 'click', knackerEvent, false);
    }
  }
}

/* 
 * Cookie functions
 */
function createCookie(name, value, days) {
    var expires = '';
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        var expires = '; expires=' + date.toGMTString();
    }
    document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
    var cookieCrumbs = document.cookie.split(';');
    var nameToFind = name + '=';
    for (var i = 0; i < cookieCrumbs.length; i++) {
        var crumb = cookieCrumbs[i];
        while (crumb.charAt(0) == ' ') {
            crumb = crumb.substring(1, crumb.length); /* delete spaces */
        }
        if (crumb.indexOf(nameToFind) == 0) {
            return crumb.substring(nameToFind.length, crumb.length);
        }
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, '', -1);
}
        
function pageLoaded()
{
    var how_many_photos = 9;
    var now = new Date();
    var sec = now.getSeconds();
    var photo = (sec % how_many_photos) + 1;
    var image = document.getElementById('rotatingImage');
    
    image.src = "images/people" + photo + ".jpg";
    image.width = 183;
    image.height = 127;
    
    updateMessage();
}

var currentMessageIndex = 0;
var currentMessageLetterIndex = 0;
var messages = new Array();
messages[0] = "The Depression Support Group & the Bipolar Support Group meet the 1st & 3rd Wednesdays of each month.";
messages[1] = "Ready for spring? Please join us on April 10th for our Homeward Bound 5K Walk. Join an MHA team today!";
messages[2] = "20% of the ailments for which Americans seek a doctor's care are related to anxiety disorders.";
messages[3] = "Help make a difference in someone's life. Please support your local MHA. Make an online donation today.";
messages[4] = "MHA\'s March Brown Bag: \"Hope and Help - Anorexia and Bulimia\"";
messages[5] = "Please visit our Joey A. Seaman Memorial Mental Health Library - our community's mental health resource.";
messages[6] = "The MHA Day Shelter serves an average of 62 different individuals per day who are without a home.";
messages[7] = "About 9.5% of US adults experience some form of clinical depression or bipolar during a given year.";

function updateMessage()
{
    var messageLabel = document.getElementById('lblMessage');
    
    // Do not use the label if it is null.
    if (messageLabel)
    {
        if (currentMessageIndex >= messages.length)
        {
            currentMessageIndex = 0;
            currentMessageLetterIndex = 0;
        }
        
        if (currentMessageLetterIndex > messages[currentMessageIndex].length)
        {
            currentMessageIndex++;
            currentMessageLetterIndex = 0;
            
            // Pause for 4.5 seconds.
            setTimeout("updateMessage()", 4500);
            return;
        }
        
        messageLabel.innerHTML = messages[currentMessageIndex].toString().substring(0, currentMessageLetterIndex) + '&nbsp;';
        currentMessageLetterIndex++;
    }
    
    setTimeout("updateMessage()", 40);
}