/*
events.js - is used to navigate the wccCalendar.php selecting a day will
display all the events for that day.

selecting an event will display the details for the event.

Two arrays will be kept one by event id the next by event date. The event
date will roll up the events for the date selected and display a list.

Three object areas are expected: calDailyList, calEventDetail, calEventFull;

The data is being stored in the events.data.js script.
addEvent(date,id,desc,starttime,endtime,url,event_text,wcc_cal_indic,contact_name, contact_phone,event_type
				   
*/

// global Variables
// all the events for a day
var eventHashTable = new Array();
// array of event objects by event_id.
var eventsHashTable = new Array();
var cureventId = "";

function listSmallDaysEvents(edate){
	events = 	eventHashTable[edate];
	htmlStr = events.listEvents();
	calIdKey = "tdday_" + edate;
	if (cureventId !=""){
		document.getElementById(cureventId).bgColor = '';
	}
	cureventId=calIdKey;
	document.getElementById(calIdKey).bgColor='#C0C0C0';
	document.getElementById("calDailyList").innerHTML = htmlStr;
	document.getElementById("calEventDetail").innerHTML = '';
	return true;
}

function listDaysEvents(edate){
	events = 	eventHashTable[edate];
	htmlStr = '&nbsp;';
	if (events != null){
		htmlStr = events.listEvents();
		calIdKey = "tdday_" + edate;
	}
	
	return htmlStr;
}


function eventDetail(eid){
	calEvent  = 	eventsHashTable[eid];
	htmlStr = calEvent.buildDetail();
	document.getElementById("calEventDetail").innerHTML = htmlStr;
	return true;
}

function getEventDetail(eid){
	calEvent  = 	eventsHashTable[eid];
  return calEvent.buildDetail();	 
}

function getEventsObjects(eventKey){
	if (eventHashTable[eventKey] != null){
		return eventHashTable[eventKey];
	}
	return null;
}

function getDBDateKey(pdate){
	rtnStr='';
	tDate = new Date(pdate);
	mm=tDate.getMonth() + 1;
	if (mm<10){ mm = '0' + mm;}
	dd = tDate.getDate();
	if (dd<10){dd='0' + dd;}
	tKey =  tDate.getFullYear()+ '-' + mm + '-' + dd  ;
	return tKey;
}	
// Calendar and Event Logic
function getNumEvents(eventKey){
	rtn=0;
	if (eventHashTable[eventKey] != null){
		rtn   = eventHashTable[eventKey].getEventsLen();
	}
	return rtn;
}
// list the events for an overlib bubble
function getEvents(eventKey,maxNum){
	rtnStr = "";
	if (eventHashTable[eventKey] != null){
		rtnStr += eventHashTable[eventKey].listBubbleEvents(maxNum);
		rtnStr = rtnStr.replace('eventKey_',eventKey + '_');
	}
	return rtnStr;
}


function addEvent(edate,eid,desc,starttime,endtime,url,event_text,wcc_cal_indic,contact_name, contact_phone,event_type){
	
	eventsHashTable[eid] = new Event(edate,eid,desc,starttime,endtime,url,event_text,wcc_cal_indic,contact_name, contact_phone,event_type);
	
	daysEvents = eventHashTable[edate];
	if (daysEvents == null){
		eventHashTable[edate] = new daysEvent();
	}
	eventHashTable[edate].appendEvent(eid);
	
}
//
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> daysEvent object
function daysEvent(){
	this.events = new Array();
	//methods
	this.appendEvent = appendEvent;
	this.listEvents = listEvents;
	this.listBubbleEvents = listBubbleEvents;
	this.getEventsLen = getEventsLen;
}

function appendEvent(eid){
	len = this.events.length;
	this.events[len] = eventsHashTable[eid];
}

function getEventsLen(){
	len =  this.events.length;
	return len;
}
function listSmallEvents(pMaxNum){
rtnStr = "Date: " + this.events[0].edate + "<br>";
rtnStr += listEvents(pMaxNum);
if (this.events.length < 10 ){
		for (i=this.events.length;i<11;i++){
			rtnStr += '<br>';
		}
	}
}
function listBubbleEvents(pMaxNum){
	rtnStr='';
	maxNum = pMaxNum < this.events.length ? pMaxNum : this.events.length;
	for(i=0;i<maxNum;i++){
		rtnStr += '<br>' ;
		rtnStr += '<b>' +  this.events[i].desc + ' - '  + this.events[i].starttime + '</b><br>';
		rtnStr += '<br>';
	}
	return rtnStr;
}

function listEvents(pMaxNum){
	rtnStr='';
	maxNum = pMaxNum < this.events.length ? pMaxNum : this.events.length;
	for(i=0;i<maxNum;i++){
		rtnStr += '<br>' ;
		rtnStr += this.events[i].buildEventAnchor(1);
		rtnStr += '<br>';
	}
	return rtnStr;
}
//---------------------------- end days Events
	
//>>>>>>>>>>>>>>>>>>>>>>>>>>>> Event object

function Event(edate,eid,desc,starttime,endtime,eurl,event_text,wcc_cal_indic,contact_name, contact_phone,event_type){
	this.desc = desc;
	this.edate = edate;
	this.eid   = eid;
	this.starttime = null;
	this.endtime = null;
	if (this.endtime != ''){
		this.starttime = starttime;	
		this.endtime   = endtime;
	}
	this.hlink = null;
	if (eurl != '')
		this.hlink = eurl;
	this.etext = event_text;
	this.wcccal = wcc_cal_indic;
	this.contact = contact_name;
	this.phone   = contact_phone;
	this.etype   = event_type;
	//methods for this object
	this.buildEventAnchor = buildEventAnchor;
	this.buildDetail = buildDetail;
}

/*
<a href="javascript:void(0);" onmouseover="return overlib('This is what
  we call a sticky, since I stick around (it goes away if you move the mouse
  OVER and then OFF the overLIB popup--or mouseover another overLIB).',
  STICKY, MOUSEOFF);" onmouseout="return nd();">here</a>
  
  
<a href="javascript:void(0);" onclick="return overlib('Make this the buildDetail stuff
', STICKY, CAPTION,'Make this the event title again', CENTER);" onmouseout="nd();">Click here!</a>

*/
function buildEventAnchor(jsFlag) {
	htmlStr = '';	
	if(jsFlag > 0){	
    js = 'LANGUAGE="Javascript" onmouseover="return overlib(getEventDetail(' + this.eid +') ,STICKY, CAPTION,' +	"'Start:"	+ this.starttime + "'"+ ');" onmouseout="nd();" ';
	  ct = '' ;
	}else{
		js = '';
		ct = '';
	}
  if (this.etype=='SERMON'){
	  htmlStr +='<img src="/site2/images/icon-bks.gif">';
	}
	htmlStr += '<A ';
	if (this.hlink != null){
		htmlStr += ' href="'+this.hlink + '"' + js ;
	}else{
		htmlStr += ' href="javascript:void(0);" ' + js ;
	}
	
	/*if (this.starttime != null){
			htmlStr += ' Title="Start:' + this.starttime + ' ' + ct + '"';
	}else {
		if (jsFlag>0){
			htmlStr += ' Title="Click for more details."';
		}
	}*/
	htmlStr += '>';
	htmlStr += this.desc;
	if (jsFlag == 3) { htmlStr += ' - ' + this.starttime; }
	htmlStr += '</A>';
	
//	debug (htmlStr);
	
	return htmlStr;
}	

function buildDetail(){
	bdHtmlStr = '';
	/* this was a first attemp to move the notes 
	   down as user scrolled down the page. leave out for now.
	   
	a = this.edate.split('-');
	 eventDay = a[2];
	 startDate = new Date(a[0],a[1],1);
	if (eventDay >= 15 - startDate.getDay()){
		htmlStr += '<br><br><br><br><br><br><br><br><br><br>';
	} */
	bdHtmlStr += 'Desc: ' + this.desc + '<br>';
	if (this.endtime != null){
	   bdHtmlStr += 'Start Time: ' + this.starttime + '<br>';
		 bdHtmlStr += '  End Time: ' + this.endtime + '<br>';
	}

	if (this.contact !=null){
		bdHtmlStr += '<br><b>Contact/Phone:</b><br> ' + this.contact + '<br>';
		bdHtmlStr +=  this.phone + '<br>';
	}
	if (this.etext != null){
	  bdHtmlStr += '<b>Notes:<br>';
	  bdHtmlStr += this.etext + '<br>';
  }
		
	bdHtmlStr += '</b>-----------------------<br><br><br><font size="xx-small">';
	bdHtmlStr += 'Event Id: '   + this.eid + '<br>';
 	bdHtmlStr += 'Event Date: ' + this.edate + '<br>'; 
 	bdHtmlStr += 'Event Type: ' + this.etype + '<br>';
	bdHtmlStr += 'WCC Indic : ' + this.wcccal + '</font><br>';
	
	

	return bdHtmlStr;
}

function getFullText(){
	if (this.etext != null )
		return this.etext ;
		
	return '';
}

	
// --------------------------------end event object
