/*
sermondata.js - used to load and manipulate the sermon data objects
					 This includes loading and creating any necessary
					 index Sorts.
					 
*/ 
// global Variables
var eventHashTable = null;
var speakerSermonsArray = null;
var sermonsArray = null;
var sermonHashTable = null;
var themeHashTable = null;
var sermonId = 0;
var baseAudioLink = "http://rs.eonstreams.com:8080/ramgen/netmedia/content/worthingtoncc/sounds/"

function loadSermons(){
/*
   loadSermons used to maintain the list of sermons.
	a. use the addSermon(date,desc,scripture,speaker, link,audioLind) method to create the link.
		   Here are the details:
		   1. date - '04/30/2000' - date of the event.
		   2. desc - "GOOD FOR EVIL" - Title or subtitle for the Sermon 
		   	  {Note: keep small ~20-30 chars use tip for longer description.} 
		   3. scripture - "Matthew 5:38-42" - scripture the sermon is based on.
		   4. speaker - "Marshall Hayden" - who presented the sermon.
		   5. link - baseHTTP + "/sermons/sn09162001.htm" - link to a more 
		      detailed description or the outline of the sermon.
		   6. audioLink - baseHTTP + "/sermons/sn09162001.wav" - link to the audio file
		      for this sermon.
		      	{Note: this link only appears in the new sermons/sermons.html list.
		   
		Hints:
			- If you don't have a value enter null in the slot
*/				
	if (sermonHashTable==null){
		sermonsArray    = new Array();
		sermonHashTable = new Array();
		themeHashTable  = new Array();
	}else{
		return;
	}
      /*  addSermon('mm/dd/yyyy'  ,
      				"Sermon Title"					,
      				"Scripture"				,
      				"Speaker"		,
      				"NoteLink URL",
      				"AudioLinkURL");
		*/      				
      //addSermon('mm/dd/yyyy'  ,"Sermon Title"	,"Scripture"	,"Marshall Hayden"	,null,null);
   
  
 /* 	addSermon('mm/dd/yyyy'  ,
					"Sermon_Title",
					"Scripture",
					"Marshall Hayden",
					null,null);
 */
 	addTheme('01/01/2009',"month","God’s Kingdom");
 	addTheme('02/01/2009',"month","God’s Righteousness");
 	addTheme('03/01/2009',"month","Getting Ready for the Best");
 	addTheme('04/01/2009',"month","The Greatest Story Ever Told");
 	addTheme('05/01/2009',"month","First Class Giving");
 	addTheme('06/01/2009',"month","Number One Homes");
 	addTheme('07/01/2009',"month","A First-Class Second Family");
 	addTheme('08/01/2009',"month","First-Class Blessings");
 	addTheme('09/01/2009',"month","A Surprising Priority");
	//addTheme('09/01/2009',"month","Joy");
 	addTheme('10/01/2009',"month","Our First Word");
 	addTheme('11/01/2009',"month","Bowing Before #1");
 	addTheme('12/01/2009',"month","Our First Response - Trust, not Worry");
 	addTheme('01/01/2010',"month","The Will of God"); // Theme for the month of January 2002
 	
 	
				
	addSermon('12/30/2008',
				"Handling Correction"				,
				"II Samuel 12:1-7, Psalm 51:1-13, 17"	,
				"Marshall Hayden"	,
				baseHTTP + "sermons/sn12302001.htm",
				"12302001.945.rm",
				"Our First Response - Trust, not Worry #5 ");
				

	addSermon('01/04/2009'  ,
				"What God Really Wants",
				"John 14:21, Matthew 18:10-14, John 3:16-21",
				"Marshall Hayden",
				baseHTTP + "sermons/sn01062002.htm",
				"01062002.945.rm",
				"God’s Intention");
					
//	addSermon('01/05/2009'  ,
//		"Giving of Your Talents",
//		"",
//		"John Canon",
//		null,
//		null,
//		null);

	addSermon('01/11/2009'  ,
		"Giving of Your Tithe",
		"",
		"Randy Timblin",
		null,
		null,
		null);

	addSermon('01/18/2009'  ,
		"Sharing Your Testimony",
		"Colossians 1:1-6",
		"Tim Clark",
		"sermons/sn030119Frame.html",
		null,
		"?????????");
		
	addSermon('01/25/2009'  ,
		"Giving Your Time",
		"Luke 12:13-38",
		"Tim Clark",
		"sermons/sn030126Frame.html",
		null,
		"?????????");

	addSermon('02/01/2009'  ,
		"Giving Your Time",
		"Luke 12:13-38",
		"Tim Clark",
		"sermons/sn030202Frame.html",
		null,
		"?????????");

	addSermon('02/08/2009'  ,
		"I Love You",
		"Matthew 22:37-40",
		"Tim Clark",
		"sermons/sn030223Frame.html",
		null,
		"?????????");
 
 	addSermon('02/15/2009'  ,
		"Teachers",
		"James 3:1-18",
		"Tim Clark",
		"sermons/sn021103Frame.html",
		null,
		"?????????");

	addSermon('09/01/2009'  ,
		"The Servant and the Savior",
		"James 1:1",
		"Tim Clark",
		"sermons/sn020901Frame.html",
		null,
		"Joy");

	addSermon('09/08/2009'  ,
		"The 'Pure Joy' of 9/11",
		"James 1:2-4",
		"Tim Clark",
		"sermons/sn020908Frame.html",
		null,
		"Joy");
	
	addSermon('09/15/2009'  ,
		"Why?",
		"James 1:5-8",
		"Tim Clark",
		"sermons/sn020915Frame.html",
		null,
		"brotherhood");
	
	addSermon('09/22/2009'  ,
		"Ruth - the Persister",
		"Ruth 1:11-18, 2:5-7",
		"Tim Clark",
		"sermons/sn020922Frame.html",
		null,
		"");
	
	addSermon('09/29/2009'  ,
		"A Heart For Kids",
		"Mark 10:13-16",
		"Tim Clark",
		"sermons/sn020929Frame.html",
		null,
		"");
	
	addTheme('10/04/2009',"month","Giving");

	
	addSermon('10/11/2009'  ,
		"A Heart For Kids ",
		"Mark 10:13-16",
		"Tim Clark",
		"sermons/sn021006Frame.html",
		null,
		"");
	
	
	addSermon('10/18/2009'  ,
		"The Marks of a Christian",
		"James 1:19-27",
		"Tim Clark",
		"sermons/sn021013Frame.html",
		null,
		"");
	
	addSermon('10/25/2009'  ,
		"Judgment",
		"James 2:1-13",
		"Tim Clark",
		"sermons/sn021020Frame.html",
		null,
		"");
	
	addSermon('11/01/2009'  ,
		"????????",
		"James ?:??-??",
		"Tim Clark",
		"sermons/sn888888Frame.html",
		null,
		"");
	
	addTheme('11/08/2009',"month","Difficult Arenas for Doing the Will of God");


	addSermon('11/15/2009'  ,
		"",
		"",
		"",
		"",
		null,
		"?????????");
	
	addSermon('11/22/2009'  ,
		"PSU's Buzz Roberts",
		"9:45 & 10:45",
		"Guest Preacher",
		"sermons/sn888888Frame.html",
		null,
		"?????????");
	
	addSermon('11/29/2009'  ,
		"Count Your Blessings",
		"Psalm 84",
		"Tim Clark",
		"sermons/sn021124Frame.html",
		null,
		"?????????");
	

	addSermon('12/06/2009'  ,
		"The Pitfalls Of Pride",
		"James 4:7- 5:6",
		"Tim Clark",
		"sermons/sn021201Frame.html",
		null,
		null);	

	addSermon('12/13/2009'  ,
		"Patience",
		"James 5:7-12",
		"Tim Clark",
		"sermons/sn021208Frame.html",
		null,
		null);
	
	addSermon('12/20/2009'  ,
		"Let's Pray",
		"James 5:13-20",
		"Tim Clark",
		"sermons/sn021215Frame.html",
		null,
		null);
	
	addSermon('12/27/2009'  ,
		"When God Put On Flesh",
		"Hebrews 1",
		"Tim Clark",
		"sermons/sn021222Frame.html",
		null,
		null);
	
//	addSermon('01/03/2010'  ,
//		"Truths For The New Year",
//		"Psalm 39",
//		"Tim Clark",
//		"sermons/sn021229Frame.html",
//		null,
//		null);
//


				
}
// --------------------------------Theme Functions
// 
// dateStr = 'mm/dd/yyyy'
// type = {month, year}
// theme = the text of the theme
//
function addTheme(dateStr,type,theme){
	key = getThemeKey(dateStr,type);
	themeHashTable[key] = theme;
}

function getTheme(str,type){
	if (str.indexOf("/",0)>0){
		key = getThemeKey(str,type);
	}else{
		key = str;
	}
	//postDebugMsg('sermondata.js::key=' + key);
	rtnStr = themeHashTable[key];
	if ((rtnStr==null) || (rtnStr=='undefined')){
		rtnStr = '';
	}
	return rtnStr;
}

function getThemeKey(dateStr,type){
	sDate = dateStr.split('/');
	if (type.toLowerCase() == 'month'){
		monthNum = '00' + sDate[0];
		monthNum = monthNum.substring(monthNum.length - 2);		
		yearNum = sDate[2];
		key = monthNum + '-' + yearNum;
		//postDebugMsg("sermondata:getThemeKey:monthNum="+monthNum);
	}else{
		key = sDate[2];
	}
	return key;
}

///////////////////////////////////////////////////////////////////////////////////
// Sermon functionality.
///////////////////////////////////////////////////////////////////////////////////
// 
// General Method: getSermonObjectByDay(sermonKey)
// 
// purpose to return the first sermon object for the sermonKey
// use this function if only one sermon object exists for the
// date requested. If more than one sermon, use getSermonObjByDayAndTitle(...)
// 
function addSermon(date,title,scripture,speaker,hlink,audioLink,theme){
	sermonKey = getSermonKey(date);
	
	//if loadCalendar was called then add the sermon as an event
	if (eventHashTable!=null){
		if (speaker!=null)
			tip='Speaker: ' + speaker;
		if (hlink==null)
//			hlink = baseHTTP + 'sermons/sermons.html?jumpto='+escape(date.substring(0,3) + date.substring(6));
			hlink = 'sermons/sermons.html?jumpto='+escape(date.substring(0,3) + date.substring(6));
		addEvent(date,title + '<br>' + scripture,tip,null,hlink,null);
	}
	sermonId = (sermonsArray.length >0) ? sermonsArray.length :0 ;
	sermonsArray[sermonId] = new Sermon(sermonId,date,title,scripture,speaker,hlink,audioLink,theme);
	
	sermons = sermonHashTable[sermonKey];
	if (sermons == null){
		sermonHashTable[sermonKey] = new sermonArray();
	}
	sermonHashTable[sermonKey].appendSermon(sermonId);
}


/* This function will sort through an array of
	sermon id's and put them in ascending/descending order
*/
function sortSermonsByDate(sermonIdArray,ascend){
	var sortedArray = sermonIdArray.sort(function(a,b){
			sermonADate = new Date(sermonsArray[a].date);
			sermonBDate = new Date(sermonsArray[b].date);
			if (sermonADate.getTime() < sermonBDate.getTime()){
				return -1;
			}else{
				return 1;
			}
		})
		return sortedArray;
}

function createSpeakerIndex(){
	if (speakerSermonsArray != null) return;
	
	speakerSermonsArray  = new Array();  //each slot holds an array of sermon ids 
													 //for an individual speaker
	var speakerNames 	   = new Array();  // hashtable keyed by speaker name to 
													 //give which slot they occupy in the 
													 //speakerSermonsArray
   var tArray				= null;
   //debug("\n createspeakerindex::sermonsarray.length="+sermonsArray.length);
	for (i=0;i<sermonsArray.length;i++){
		//debug("createspeakerindex::i="+i);
	
		var msermonObj = sermonsArray[i];
		if (msermonObj==null) continue;
		//debug("createspeakerindex::i="+i+"title = " + msermonObj.title);
	
		var tspkr = msermonObj.speaker;
		var spkrId = speakerNames[tspkr];
	//	debug("csidx::spkrId="+spkrId);
		if ((spkrId == null)||(spkrId=='undefined')){
			spkrId = speakerSermonsArray.length;
			tArray = new Array();
			tArray[0] = i;
			speakerNames[tspkr]=spkrId;
		}else{
		   tArray = speakerSermonsArray[spkrId];
			var j = tArray.length;
			tArray[j] = i;
		}
		speakerSermonsArray[spkrId] = tArray;
	}
	// at this point speakerSermonsArray[j]=Array of sermon id's all of which
	// have the same speaker. Send back to calling function.
	return speakerSermonsArray;
}

function getSermonObjById(id){
	return sermonsArray[id];
}

function getSermonObjByDay(sermonKey){
	var id = sermonHashTable[sermonKey].sermons[0]
	return sermonsArray[id];
}
/*  return a sermon object by looking up a title for a given day */
function getSermonObjByDayAndTitle(sermonKey,title){
	var idArray = sermonHashTable[sermonKey].sermons
	return findSermonByTitle(title,idArray);
}

function getSermonKey(date){
	rtnStr='';
	tDate = new Date(date);
	tKey = (tDate.getMonth() + 1) + '-' + (tDate.getDate()) + '-' + tDate.getFullYear();
	return tKey;
}		

function getSermonsListByDay(sermonKey){
	srtnStr = null;
	if (sermonHashTable[sermonKey] != null){
		srtnStr = sermonHashTable[sermonKey].listSermons();
	}
	return srtnStr;
}	

function getSermonAudioLink(sermonKey){
	srtnStr = null;
	if (sermonHashTable[sermonKey] != null){
		srtnStr = sermonHashTable[sermonKey].getFirstAudioLink();
	}
	return srtnStr;
}	

function findSermonByTitle(requestedTitle,idArray){
	if (theArray ==null) {
		for(i=0;i<sermonsArray.length;i++){
				if (sermonsArray[i].title.toLowerCase()==requestedTitle.toLowerCase()){
					return sermonsArray[i];
				}
		}
	}else {
		for(i=0;i<idArray.length;i++){
			var id = idArray[i];
			var sermonObj = sermonsArray[id];
			if (sermonObj.title.toLowerCase()==requestedTitle.toLowerCase()){
						return sermonObj;
			}
		}
	}
	return null;
}	



//////////////////////////////////////
// Sermon array object - actually an object that holds an array of 
// sermon id's.
//////////////////////////////////////
function sermonArray(){
	this.sermons = new Array();
	this.appendSermon = appendSermon;
	this.listSermons = listSermons;
	this.getAudioLink = getAudioLink;
	this.getFirstAudioLink = getFirstAudioLink;
}
/*  Although this handles more than one sermon
	 on a day. The getSermonAudioLink function(above) assumes
	 only one for a givin day.
*/
function appendSermon(id){
	len = this.sermons.length;
	this.sermons[len] = id
}
	
		
function listSermons(){
	rtnStr = '';
	for(i=0;i<this.sermons.length;i++){
		var id = this.sermons[i];
		var sermonObj = sermonsArray[id]; //global array of sermon objects by id#
		sDate = sermonObj.date.split('/');
		safeDate = sDate[0] + '_' + sDate[1] + '_' + sDate[2];
		rtnStr = '<tr><td bgcolor="'+DATE_BGCOLOR+'" align="center" >';
		rtnStr += '<font class="calevents">'+sermonObj.date +'</font>';
		rtnStr += '</td><td bgcolor="'+DATE_BGCOLOR+'" >';
		if (sermonObj.hlink!=null){
			//put the outline image and link
			rtnStr += '<img src="../images/note.gif" width="13" height="16" alt="Note" border="0" title="Click Here for Note" usemap="#NoteMap'+safeDate+'">';
			rtnStr += '<map name="NoteMap'+safeDate+'">';
			rtnStr += '<area alt="Click Here for Notes" shape="default" coords="0,0,13,16" href="'+ sermonObj.hlink+ '">';
			rtnStr += '</map>';
		}
		if (sermonObj.audioLink!=null){
			//put the audio image and link
			rtnStr += this.getAudioLink(i,safeDate);
		}
		rtnStr += '</td><td bgcolor="'+DATE_BGCOLOR+'" colspan="2">';
		rtnStr += '<font class="calevents">'+ sermonObj.buildSermonAnchor() + ' - Speaker: ' + sermonObj.speaker+'</font>';
		rtnStr += '</td></tr>';
		
	}
	return rtnStr;
}
/*
	this function will return the first sermon that has a non-null
	audio link.  That is if there are 2 different sermons on the 
	same day, this function will not function properly.
*/
function getFirstAudioLink(){
	rtnStr = '';
	for(i=0;i<this.sermons.length;i++){
		var id        = this.sermons[i]; //global array of sermon objects by id#
		var sermonObj = sermonsArray[id];
		
		sDate = sermonObj.date.split('/');
		safeDate = sDate[0] + '_' + sDate[1] + '_' + sDate[2];
		
		rtnStr = this.getAudioLink(i,safeDate);
		if (rtnStr != ''){
			return rtnStr;
		}
	}
	return rtnStr;
}
/* This function returns the formated image for the audio buttons
   it will examine the file extension to determine if the file is
   Real Media (.rm) or anything else
   
*/
function getAudioLink(idx,safeDate){
	rtnStr = '';
	var id = this.sermons[idx];
	var sermonObj = sermonsArray[id];  //global array of sermon objects by id#
	if (sermonObj.audioLink !=null){
		audioFile = sermonObj.audioLink;
		rtnStr    = generateAudioImageLink(audioFile,safeDate);			
	}
	return rtnStr;	
}
//////////////////////////////
// end sermonArray
///////////////////////////////
//////////////////////////////	
// Sermon object
/////////////////////////////
function buildSermonAnchor() {
	htmlStr = '';	
	
	if (this.hlink != null){
		htmlStr += '<A href="'+this.hlink + '"';
		if (this.speaker != null)
			htmlStr += ' Title="Speaker:' + this.speaker + '"';
		htmlStr += '>';
	}
	htmlStr += this.title + ' - ' + this.scripture;
	if (this.hlink != null){
		htmlStr += '</A>';
	} 
	return htmlStr;
}	

function Sermon(id,date,title,scripture, speaker, hlink,audioLink,theme){
	this.id = id;
	this.date = date;
	this.title = title;
	this.scripture = scripture;
	this.speaker = speaker;
	this.hlink=hlink;
	this.audioLink=audioLink;
	if(!(theme==''||theme=='undefined'||theme==null)){
		this.theme=theme;
	}else{
		this.theme='';
	}
	// sermon methods
	this.buildSermonAnchor = buildSermonAnchor;
}
// end Sermon object

// Utility Function
function generateAudioImageLink(audioFile,uidx){
		rtnStr = '';
	   link = baseAudioLink + audioFile;
		pidx = link.lastIndexOf(".");
		if (link.substring(pidx) == '.rm'){
			rtnStr += '&nbsp;<img src="../images/ra2.gif" width="16" height="13" alt="Audio" border="0" title="Click Here for Audio" usemap="#AudioMap'+uidx+'">';
			rtnStr += '<map name="AudioMap'+uidx+'">';
			rtnStr += '<area alt="Click Here for Audio" shape="default" coords="0,0,16,13" href="'+ link + '">';
			rtnStr += '</map>';
		}else if(link.substring(pidx)=='.htm'){
		    rtnStr += '&nbsp;<img src="../images/ra2.gif" width="16" height="13" alt="Audio" border="0" title="Click Here for Audio" usemap="#AudioMap'+uidx+'">';
			rtnStr += '<map name="AudioMap'+uidx+'">';
			rtnStr += '<area alt="Click Here for Audio" shape="default" coords="0,0,16,13" href="'+ audioFile + '">';
			rtnStr += '</map>';
		}else{
			rtnStr += '<img src="../images/audio.gif" width="20" height="18" alt="Audio" border="0" title="Click Here for Audio" usemap="#AudioMap'+uidx+'">';
			rtnStr += '<map name="AudioMap'+uidx+'">';
			rtnStr += '<area alt="Click Here for Audio" shape="default" coords="0,0,20,18" href="'+ link+ '">';
			rtnStr += '</map>';
		}
		return rtnStr;
}

function generateAudiohref(audioFile){
		rtnStr = '';
	   rtnStr = baseAudioLink + audioFile;
		return rtnStr;
}

