// add as many mesages as you want here - code to handle the
// message handling will not need to be changed
var messages = [
'<img src="images/ways_to_conserve/2.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/3.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/4.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/5.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/6.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/7.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/8.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/9.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/10.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/11.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/12.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/13.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/14.jpg" width=342 height=146 alt="Ways to Conserve">',
'<img src="images/ways_to_conserve/15.jpg" width=342 height=146 alt="Ways to Conserve">'
];
// set number of milliseconds for message cycling - change to
// suit (I used a short cycle for testing)
var msgCycleRate = 3000;

// convenience reference
function get( eid ) { return document.getElementById( eid ); }
;
// get a random integer in the range [min, max]
// note: min is optional with default of 0
function getRandomInt(max, min) {
if ('undefined' == typeof min) min = 0;
var range = max - min + 1;
var r = Math.min( range, Math.floor( range*Math.random( )));
return min + r;
}
;
// cookie management functions start here - - - -
// only need these, if you want the successive page loads to give
// the static message
function getCookie(key) {
// convenience reference
var c = document.cookie;
// if none, return null - exit immediately
if ( 0 == c.length ) return null;

// implicit else - test for cookie value for key
var c_start = c.indexOf(key + "=");
// if no value for key, return null - exit immediately
if (-1 == c_start) return null;
// implicit else - get value from cookie string
c = c.substring(c_start + (key + "=").length);
// find end of value for key
var c_end = c.indexOf( ";" );
// if no trailing semi-colon, this is last value, reset end
if ( -1 == c_end ) c_end = c.length;

// chop trailing parts, if any and return unescaped value
return unescape( c.substring( 0, c_end ) );
}
;
function setCookie( key, value, expiredays ) {
// if not given, set default number of days until expiration to one year
// in the future
if ('undefined' == typeof expiredays) expiredays = 365.25;
// get a new Date instance
var expires = new Date();
// convert requested (or default) number of days to milliseconds
expires.setTime( expires.getTime() + 1000*24*60*60 * expiredays );
// add new key/value pair to cookies for page
document.cookie = key + '=' + escape(value) + ((null == expiredays)?'':
(';expires=' + expires.toGMTString()));
}
;
// cookie management functions end here - - - -

// message management function start here - - - -
// check whether previous load has set cookie
function chkMsgIdx( ) {
var mIdx = getCookie( 'msgIdx' );
if ( null != mIdx ) mIdx = parseInt( mIdx ) + 1;
mIdx = mIdx % messages.length;
setCookie( 'msgIdx', mIdx);
setMsg( mIdx );
}
;
// set display value of single message element that remains static
// for each new load of the page
function setMsg( idx ) {
get( 'staticMsg' ).innerHTML = messages[ idx ];
}
;
// dynamically cycle through the collection of messages in serial
// order by index and update the target container
function cycleMsgs( idx ) {
if ('undefined' == typeof idx) idx = -1;
idx = (idx + 1)%messages.length;
get( 'dynamicMsg' ).innerHTML = messages[ idx ];
setTimeout( 'cycleMsgs(' + idx + ');', msgCycleRate);
}
;
function setRandomMsg() {
var idx = getRandomInt( messages.length -1 );
get( 'randomMsg' ).innerHTML = messages[ idx ];
}
function doMsgs() {
// need this on, if you're doing single message onload
chkMsgIdx();
// need this on, if your dynamcially cycling through
// all messages - you could randomly pick which message
// to use first and pass that in th following call to
// cycleMsgs(), but that wasn't part of your question
setTimeout( 'cycleMsgs();', 0);
// set a random message without regard to last value
setRandomMsg();
}
;
// message management functions end here - - - -

// in case other onload handling is required, add this
// to the list of handlers
if(window.addEventListener)
window.addEventListener('load', doMsgs, false); // non-IE
else window.attachEvent('onload', doMsgs); // IE
