// IHM index.html image selection code
//-- setup random images w/ backgrounds and text descriptions
randpic = new Array(
{img:"images/marystatue3.gif", bg:"images/wheatbkg.jpg",					txt:"Mary Statue"},
{img:"images/ihm0.gif",        bg:"images/ricebk.jpg",						txt:"Mary Bronze"},
{img:"images/maryrays.gif",    bg:"images/clouds.gif",						txt:"Mary with rays"},
{img:"images/OLG-window.jpg",  bg:"images/blendembossed_tile10.jpg",		txt:"Our Lady of Guadalupe"}
)
randCnt  = randpic.length

//-- setup special images w/ backgrounds and text descriptions
specpic = new Array(
{img: "images/no_room.gif",         bg:"olbs_images/olbs-bg.jpg",			txt:"Advent"},					// 0
{img: "images/navidad-oval.gif",    bg:"images/blue-grn.gif",				txt:"Christmas"},				// 1
{img: "images/holy_fam.gif",        bg:"olbs_images/olbs-bg.jpg",			txt:"Holy Family"},				// 2
{img: "images/MaryMother.gif",      bg:"images/ricebk.jpg",					txt:"Mother of God"},			// 3
{img: "images/epiphany.gif",        bg:"olbs_images/olbs-bg.jpg",			txt:"Epiphany"},				// 4
{img: "images/presentation.jpg",    bg:"images/wheatbkg.jpg",				txt:"Presentation"},			// 5
{img: "images/stpats.jpg",          bg:"images/lgreen-bg.jpg",				txt:"St Patrick"},				// 6
{img: "images/calvary1.jpg",        bg:"images/blendembossed_tile10.jpg",	txt:"Good Friday"},				// 7
{img: "images/takendown.jpg",       bg:"images/blendembossed_tile10.jpg",	txt:"Holy Saturday"},			// 8
{img: "images/heisrisen.jpg",       bg:"images/marvel-white-soft-g.gif",	txt:"Easter"},					// 9
{img: "images/divine_mercy2.jpg",   bg:"olbs_images/olbs-bg.jpg",			txt:"Divine Mercy"},			// 10
{img: "images/stmark.gif",          bg:"images/ricebk.jpg",					txt:"St Mark"},					// 11
{img: "images/eucharist2.gif",      bg:"images/wheatbkg.jpg",				txt:"Corpus Christi"},			// 12
{img: "images/sacredheart2.jpg",    bg:"images/lgreen-bg.jpg",				txt:"Sacred Heart"},			// 13
{img: "images/OurLady.gif",         bg:"images/clouds.gif",					txt:"Immaculate Heart"},		// 14
{img: "images/sttekakwitha.gif",    bg:"images/wheatbkg.jpg",				txt:"Kateri Tekakwitha"},		// 15
{img: "images/transfig.gif",        bg:"images/lgreen-bg.jpg",				txt:"Transfigration"},			// 16
{img: "images/assumption.gif",      bg:"images/blendembossed_tile10.jpg",	txt:"Assumption"},				// 17
{img: "images/stephenpongracz.gif", bg:"images/blendembossed_tile10.jpg",	txt:"Pongracz"},				// 18
{img: "images/ascension.gif",       bg:"images/blendembossed_tile10.jpg",	txt:"Ascension"},				// 19
{img: "images/pentecost.gif",       bg:"images/blendembossed_tile10.jpg",	txt:"Pentecost"},				// 20
{img: "images/holy-trinity.gif",    bg:"images/blendembossed_tile10.jpg",	txt:"Holy Trinity"},			// 21
{img: "images/annunciation.gif",    bg:"images/blendembossed_tile10.jpg",	txt:"Annunciation"},			// 22
{img: "images/coronation.gif",      bg:"images//clouds.gif",				txt:"Coronation of Mary"},		// 23
{img: "images/OLG-window.jpg",      bg:"images/blendembossed_tile10.jpg",	txt:"Our Lady of Guadalupe"},	// 24
{img: "images/ChristTheKing.gif",   bg:"images/king-bg.jpg",				txt:"Christ the King"},			// 25
{img: "images/BaptismoftheLord.gif",bg:"images/blendembossed_tile10.jpg",	txt:"Baptism of the Lord"}		// 26
)
specCnt = specpic.length

// -------------------------------------------------------------
// get_picture - sets document "picture" according
//               to the current date
// -------------------------------------------------------------
function get_picture()
{
	today = new Date()						// get the current date and time

//	today = new Date("1/9/2010")			// FOR TESTING

	Mon = today.getMonth() + 1
	Day = today.getDate()

	getEaster()								// Get date of Easter
	getNewLitYr()							// Get date of New Liturgical Year

//	document.write ("Today is:  " + today.toDateString() + "<br>" + "Easter is: " + easter.toDateString() + "<br>" + "New year is:  " + newyear.toDateString() + "<br>")	// FOR TESTING

	idx = -1

	if (IsAdvent()) {
  		idx = 0								// Advent
	}
	if (Mon == 12 && Day == 12) {
  		idx = 24							// Our Lady of Guadalupe
	}
	if (Mon == 12 && Day >= 25) {
  		idx = 1								// Navidad in December
	}
	if (IsNewYr(+27, 1)) {
  		idx = 2								// Holy Family
	}
	if (Mon == 1 && Day == 1) {
  		idx = 3								// Mary Mother of God
	}
	if (IsNewYr(+34, 1)) {
  		idx = 4								// Epiphany
	}
	if (IsNewYr(+41, 1)) {
  		idx = 26							// Baptism of the Lord
	}
	if (Mon == 2 && Day >= 1 && Day <= 2) {
  		idx = 5								// Presentation
	}
	if (Mon == 3 && Day >= 16 && Day <= 17) {
  		idx = 6								// St. Patrick
	}
	if (IsEaster(-2, 0)) {
  		idx = 7								// Good Friday
	}
	if (IsEaster(-1, 0)) {
  		idx = 8								// Holy Saturday Friday
	}
	if (IsEaster(0, 6)) {
  		idx = 9								// Easter
	}
	if (IsEaster(+6, 1)) {
  		idx = 10							// Divine Mercy
	}
	if (Mon == 3 && Day >= 24 && Day <= 25) {
  		idx = 22							// Annunciation
	}
	if (Mon == 4 && Day >= 24 && Day <= 25) {
  		idx = 11							// St. Mark
	}
	if (IsEaster(+41, 1)) {
  		idx = 19							// Ascension
	}
	if (IsEaster(+49, 1)) {
  		idx = 20							// Pentecost
	}
	if (IsEaster(+55, 1)) {
  		idx = 21							// Holy Trinity
	}
	if (IsEaster(+62, 1)) {
  		idx = 12							// Corpus Christi
	}
	if (IsEaster(+68, 0)) {
  		idx = 13							// Sacred Heart of Jesus
	}
	if (IsEaster(+69, 0)) {
  		idx = 14							// Immaculate Heart of Mary
	}
	if (Mon == 7 && Day == 14) {
  		idx = 15							// St. Kateri Tekakwitha
	}
	if (Mon == 8 && Day == 6) {
  		idx = 16							// Transfiguration
	}
	if (Mon == 8 && (Day == 14 || Day == 15)) {
  		idx = 17							// Assumption
	}
	if (Mon == 8 && (Day == 21 || Day == 22)) {
  		idx = 23							// Coronation of Mary
	}
	if (Mon == 9 && Day == 7 ) {
  		idx = 18							// Pongracz
	}
	if (IsNewYr(-8, 1)) {
  		idx = 25							// Christ the King
	}

	if (idx == -1) {						// no special day set
		ranNum = Math.floor(Math.random()*randCnt)
		document.picture.src = randpic[ranNum].img
		document.body.background = randpic[ranNum].bg

	} else {								// use special day pics
		document.picture.src = specpic[idx].img
		document.body.background = specpic[idx].bg
	}
}

// ----------------------------------------------------------------
// getEaster - calculate the month and day of easter for this year
//             sets global easter
// ----------------------------------------------------------------
function getEaster()
{
	year = today.getFullYear()
	century = Math.floor(year/100)
	G = year % 19
	K = Math.floor((century - 17)/25)
	I = (century - Math.floor(century/4) - Math.floor((century - K)/3) + 19*G + 15) % 30
	I = I - Math.floor(I/28)*(1 - Math.floor(I/28)*Math.floor(29/(I + 1))*Math.floor((21 - G)/11))
	J = (year + Math.floor(year/4) + I + 2 - century + Math.floor(century/4)) % 7
	L = I - J
	EMon = 3 + Math.floor((L + 40)/44)
	EDay = L + 28 - 31*Math.floor((EMon/4))

	easter = new Date()
	easter.setMonth(EMon-1)		// set Easter month
	easter.setDate(EDay)		// and the day
	easter.setHours(0,0,0,0)	// and the time
	easter.setYear(year)		// and the year
}

// -------------------------------------------------------------
// IsEaster - function for testing a day relative to Easter
//     input: offset => signed offset from Easter Day
//            range  => number of days from reference
//   returns: true if day is in the between Easter + offset
//            and Easter + offset + range, false otherwise
// -------------------------------------------------------------
function IsEaster(offset, range)
{
	msperday  = 1000 * 60 * 60 * 24

	// get the day of epoch for today and easter
	doe  = Math.floor (today.getTime()  / msperday)
	edoe = Math.floor (easter.getTime() / msperday)
	if (doe >= (edoe + offset) && doe <= (edoe + offset + range)) return 1
    return 0
}

// ----------------------------------------------------------------
// getNewLitYr - calculate the epoch of the start of the
//               new liturgical year
//               sets global newyear
//               Since the liturgical year is in two calendar years
//               we will find Advent of the current calendar year if
//               today is after Easter of the current calendar year,
//               and Advent of the previous calendar year if today
//               is before Easter of the current calendar year,
//               this so we can calculate the Sundays of The Holy
//               Family and The Epiphany of the Lord correctly
// ----------------------------------------------------------------
function getNewLitYr()
{
	msperday  = 1000 * 60 * 60 * 24

	now  = today.getTime()
	estr = easter.getTime()

	if (now > estr) {	// get new year for current calendar year
		year = today.getFullYear()

	} else {			// get new year for previous calendar year
		year = today.getFullYear() - 1
	}

	// setup earliest possible date for the beginning
	// of the liturgical year less one day
	newyear = new Date("November 26, " + year)

	dow = newyear.getDay()			// get the day of the week
	epoch = newyear.getTime()		// get the day of the week
	offset = (7 - dow) * msperday	// make adjustment
	newyear.setTime(epoch + offset)	// set the newyear
}

// -------------------------------------------------------------
// IsAdvent - function for testing a day is in Advent
//     input: none
//   returns: true if day is in the between the beginning of the
//            Liturgical year and Christmas, false otherwise
// -------------------------------------------------------------
function IsAdvent()
{
	now  = today.getTime()

	// establish christmas epoch
	year = newyear.getFullYear()
	christmas = new Date("December 25, " + year)

	// establish advent epoch
	advent1 = newyear.getTime()

	if ((now >= advent1) && (now < christmas)) return 1
	return 0
}

// -------------------------------------------------------------
// IsNewYr - function for testing a day relative to the
//           beginning of the Liturgical year
//    input: offset => signed offset from start of Advent
//           range  => number of days from reference
//  returns: true if day is between the beginning of the
//           new year + offset, and the beginning of the
//           new year + offset + range, false otherwise
// -------------------------------------------------------------
function IsNewYr(offset, range)
{
	msperday  = 1000 * 60 * 60 * 24

	// get the day of epoch for today and 1st of Advent 
	doe  = Math.floor (today.getTime()  / msperday)
	adoe = Math.floor (newyear.getTime() / msperday)
	if (doe >= (adoe + offset) && doe <= (adoe + offset + range)) return 1
    return 0
}

// ================================================================
// Slide Show routines
// ================================================================

// ----------------------------------------------------------------
// slideShow - setup slide show of each image in sequence
//             for testing
// ----------------------------------------------------------------
function slideShow()
{
	interval = 2000
	idx = 0;
	array = randpic
	cnt   = randCnt

	// setup first image
	document.picture.src = array[idx].img
	document.body.background = array[idx].bg
	p = document.getElementById("caption")
	p.innerHTML = array[idx].txt + "<br><br>image: " + array[idx].img + "<br>bg: " + array[idx].bg
	window.status = "image index = " + idx
}

// ----------------------------------------------------------------
// play - timeout callback
// ----------------------------------------------------------------
function play()
{
	showNext()
    timerID = setTimeout("play()",  interval)
}

// ----------------------------------------------------------------
// togglePlay - switch from Play to Pause
// ----------------------------------------------------------------
function togglePlay(text)
{
	if (text == "Play") {
    	timerID = setTimeout("play()",  interval)
		document.buttonForm.playPause.value = "Pause"
	} else {
		clearTimeout(timerID)
		document.buttonForm.playPause.value = "Play"
	}
}

// ----------------------------------------------------------------
// showNext - cycle through all the pictures
// ----------------------------------------------------------------
function showNext()
{
	idx++
	if (idx >= cnt) {
		idx = 0
		if (array == randpic) {
			array = specpic
			cnt   = specCnt
		}else {
			array = randpic
			cnt   = randCnt
		}
	}

	document.picture.src = array[idx].img
	document.body.background = array[idx].bg
	p = document.getElementById("caption")
	p.innerHTML = array[idx].txt + "<br><br>image: " + array[idx].img + "<br>bg: " + array[idx].bg
	window.status = "image index = " + idx
}

// ----------------------------------------------------------------
// showPrev - show previous image
// ----------------------------------------------------------------
function showPrev()
{
	idx--
	if (idx < 0) {
		if (array == randpic) {
			array = specpic
			cnt   = specCnt
		}else {
			array = randpic
			cnt   = randCnt
		}
		idx = cnt - 1
	}

	document.picture.src = array[idx].img
	document.body.background = array[idx].bg
	p = document.getElementById("caption")
	p.innerHTML = array[idx].txt + "<br><br>image: " + array[idx].img + "<br>bg: " + array[idx].bg
	window.status = "image index = " + idx
}
