/**
 * @author Roger
 */
Date.prototype.getWeek = function(dOffset){
	var onejan = new Date(this.getFullYear(), 0, 1);
	var weekNumber = Math.ceil((((this - onejan) / 86400000) + onejan.getDay()) / 7);
	var firstDay = new Date(this.getFullYear(),this.getMonth(),1);
	//console.log("First Day of Month "+firstDay.getDay());
	//console.log("This Month "+this.getMonth());
	if((firstDay.getDay() == 0) && (this.getMonth() > 0)){
	//	console.log("bumping");
		weekNumber++;
	}
	return weekNumber;
}

Date.prototype.getWeekOfMonth = function (dowOffset) {
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */

	dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
	var newYear = new Date(this.getFullYear(),0,1);
	var day = newYear.getDay() - dowOffset; //the day of week the year begins on
	day = (day >= 0 ? day : day + 7);
	var daynum = Math.floor((this.getTime() - newYear.getTime() -
	(this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
	var weeknum;
	if(!daynum){
		daynum = 0;
	}
	//if the year starts before the middle of a week
	if(day < 4) {
		weeknum = Math.floor((daynum+day-1)/7) + 1;
		if(weeknum > 52) {
		nYear = new Date(this.getFullYear() + 1,0,1);
		nday = nYear.getDay() - dowOffset;
		nday = nday >= 0 ? nday : nday + 7;
		/*if the next year starts before the middle of
		the week, it is week #1 of that year*/
		weeknum = nday < 4 ? 1 : 53;
	}
	}
	else {
		weeknum = Math.floor((daynum+day-1)/7);
	}
	return weeknum;
};
Date.prototype.getDOY = function() {
	var onejan = new Date(this.getFullYear(),0,1);
	return Math.ceil((this - onejan) / 86400000);
} 
Date.prototype.numWeeks = function(){
	// Will return the number of weeks in the month
	var offset = 0;
	var firstday_dow = new Date(this.getFullYear(),this.getMonth(),1).format("%w");
	var lastday_dow = new Date(this.getFullYear(),this.getMonth(),new Date(this.getFullYear(),this.getMonth(),1).get('lastdayofmonth')).format("%w");
	var firstday = new Date(this.getFullYear(),this.getMonth(),1).getDate();
	var lastday = new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
	//var lastday = new Date(this.getFullYear(),this.getMonth(),1).get('lastdayofmonth');
	if(firstday_dow >4){
		offset++;
	}
	if (firstday == 0 && this.getMonth() == 1){
		return (4+offset);
	}
	else if(firstday < lastday || this.getMonth() == 1){
		return (5+offset);
	}
	else{
		return 6;
	}
	
}
Date.prototype.lastDay = function(){
	var lastday = new Date(this.getFullYear(),this.getMonth()+1,0);
	return lastday.getDate();
}
Date.prototype.isLeapYear = function(){
		var intYearValue = this.getFullYear();
		var boolLeapYear = false;
		if((intYearValue % 4)==0){
			if((intYearValue % 100)==0){
				if((intYearValue % 400)==0){
					boolLeapYear = true;
				}
			}
			else{
				boolLeapYear = true;
			}
		}
		return(boolLeapYear);
}
Date.prototype.monthName = function(){
	var month=new Array(12);
	month[0]="January";
	month[1]="February";
	month[2]="March";
	month[3]="April";
	month[4]="May";
	month[5]="June";
	month[6]="July";
	month[7]="August";
	month[8]="September";
	month[9]="October";
	month[10]="November";
	month[11]="December";
	return month[this.getMonth()];
}
var tiCalendar = new Class({   
		Implements: Options,
		
		// Constants to allow for regionalization
		constants:{
			monthDisplayStyle:0,
			weekDisplayStyle:1,
			daysOfWeek : ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],
			tableDisplayFormat: 0
		},
		// here are variables to be used in this class
		openBoxes:[],
		displayStyle : 0, // 0 = monthview, 1=weekview, 2=dayview; 3=List
		currentStartDate : null,
		currentDisplayWeek : null,
		currentDisplayMonth : null,
		currentDisplayYear: null,
		preambleGUID: null,
		// anything that should be customizeable is in here
		options:{
			// containerEl is the container to draw the calendar inside
			containerEL:null,
			// calendarType is passed to the backend script to support multiple calendar types
			calendarType:'web',
			// calendarID is the identity column value for multiple calendar support
			calendarID: 0,
			// displayFormat: 0=list, 1=calendar
			displayFormat: 1,
			// if the display format is a list, displayTableID id the ID of the table
			displayTableID: null,
			// defaults for cell and column heights
			cellHeight: '60px',
			columnHeight: '400px',
			// default class values for the alternating columns
			onColumnClass: 'eventCalendarOnColumn',
			offColumnClass: 'eventCalendarOffColumn',
			// default colors for the alternating columns
			defaultColor: '#fff',
			alternateColor:'#85CB4F',
			// default classes for the links and titles
			standardEventClass:'eventItemDetailLink',
			externalEventClass:'eventItemExternalLink',
			allDayEventClass:'alldayevent',
			monthTitleClass:'EventCalendarTitle',
			// element objects for the next and previous buttons
			nextLinkCheckEl: null,  
			previousLinkCheckEl: null,
			nextLinkCheckImageEl: null,
			previousLinkCheckImageEl: null,
			weekviewLinkCheckEl: null,
			monthviewLinkCheckEl: null,
			weekviewLinkCheckImageEl: null,
			monthviewLinkCheckImageEl: null,
			// default images for the next and previous buttons and the 2 states
			previousLinkCheckImageSrc: 'images/play_back_16.png',
			previousLinkCheckImageOverSrc: 'images/hot/play_back_16_hot.png',
			nextLinkCheckImageSrc: 'images/play_16.png',
			nextLinkCheckImageOverSrc: 'images/hot/play_16_hot.png',
			weekviewLinkCheckImageSrc: 'images/colum_24.png',
			weekviewLinkCheckImageOverSrc: 'images/hot/colum_24_hot.png',
			monthviewLinkCheckImageSrc: 'images/table_24.png',
			monthviewLinkCheckImageOverSrc: 'images/hot/table_24_hot.png'
			
		},
		
		initialize: function(options){
			if(options){
				this.setOptions(options);
			}
			this.preambleGUID = this.generateGUID();
			this.currentStartDate = new Date();			
			this.currentDisplayWeek = this.currentStartDate.getWeek(0);
			this.currentDisplayMonth = this.currentStartDate.getMonth() + 1;
			this.currentDisplayYear = this.currentStartDate.getFullYear();
			this.setupEvents();
		},
		
		generateGUID: function(){
			var result, i, j;
			result = '';
			for(j=0; j<32; j++)
			{
				if (j == 8 || j == 12 || j == 16 || j == 20) {
					//result = result + '-';
				}
				i = Math.floor(Math.random()*16).toString(16).toUpperCase();
				result = result + i;
			}
			return result
		},
		drawCalendar :function(){
			var eventList = new Array();
			$('contentleft').setStyle('width','680px');
			new Request({
				url: 'cms/webToolsGetEvents.php?calendarType='+this.options.calendarType+'&calendarID='+this.options.calendarID+'&displayFormat='+this.options.displayFormat+'&displayMonth='+this.currentDisplayMonth+'&displayYear='+this.currentDisplayYear,
				method: 'get',
				onSuccess: function(responseText, responseXML){
					if (this.options.displayFormat == this.constants.tableDisplayFormat) {
						this.options.containerEL.innerHTML = responseText;
						var listHotBarTable = {};
						listHotBarTable = new sortableTable(this.options.displayTableID, {
							overCls: 'over',
							filterHide: false,
							clickEvent: function(){
								MochaUI.webToolsEditCalendarWindow(this.id);
							}
						});
					 }
				    else{
						var calDiv;
						var dataInsert = "";
						// load up what we need to parse the XML that was returned 
						try //Internet Explorer
						  {
							  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
							  xmlDoc.async="false";
							  xmlDoc.loadXML(responseText);
						  }
						catch(e)
						  {
						  try //Firefox, Mozilla, Opera, etc.
						  {
							  parser=new DOMParser();
							  xmlDoc=parser.parseFromString(responseText,"text/xml");
						  }
						  catch(e)
						  {
						 	 alert(e.message);
						  return;
						  }
						}
						x = xmlDoc.getElementsByTagName("event");
						var compareDate = this.currentStartDate;
						var elementDate = new Date(compareDate.getFullYear(), compareDate.getMonth(),1);
						var firstWeekNumber = elementDate.getWeek(0);
						if(this.displayStyle == this.constants.weekDisplayStyle || this.displayStyle==this.constants.monthDisplayStyle){
							// header column is always full-width
							dataInsert += '<div width="100%" style="width:100%; text-align:left;">';
							dataInsert += '<span class="'+this.options.monthTitleClass+'"><center>'+this.currentStartDate.monthName()+','+this.currentStartDate.getFullYear()+'</center></span><br/>';
							dataInsert += 'Events for week '+this.currentDisplayWeek;
							//dataInsert += '</div>';
							calDiv = new Element('div',{
							}).inject(this.options.containerEL);
							calDiv.innerHTML = dataInsert;
							//this.options.containerEL.innerHTML = calDiv.innerHTML;
							//alert(calDiv.innerHTML);
							this.options.containerEL.innerHTML = dataInsert;
							new Element('table',{
								id: this.preambleGUID+'webToolsEditCalendar'+this.options.CalendarID+'EventTable',
								styles: {
									'width': '100%'									
								},
								border: '0',
								width: '100%',
								cellspacing: '5px',
								cellpadding: '5px'
							}).inject(this.options.containerEL);
							if (this.displayStyle == this.constants.weekDisplayStyle){
								var cellDateCounter = 1;
								var classList=new Array(7);
								classList[0] = this.options.onColumnClass;
								classList[1] = this.options.offColumnClass;
								for (j=600;j<=1800;j=j+100)
								 {
									new Element('tr', {
										id: this.preambleGUID+'webToolsEditCalendar' + this.options.CalendarID + 'EventTableRow'+(firstWeekNumber+j),
										'class': 'eventCalendarWeekDisplay'
									}).inject($(this.preambleGUID+'webToolsEditCalendar' + this.options.CalendarID + 'EventTable'));
								 	for (i = 0; i <= 7; i++) {
										cellHeight = this.options.cellHeight;
										cellClass = classList[(i%2)];
										cellData = "";
										if ((i==0) && (j>600)){
											cellData = j;
										}
										if (j == 600) {
											cellDate = "";
											cellHeight="20px";
											cellClass='eventCalendarWeeks';
										}
										new Element('td', {
											id: this.preambleGUID+'EventCalendarDaysOfWeek' + i + '_' + j,
											width: '12%',
											valign: 'top',
											align: 'center',
											height: cellHeight,
											html: cellData,
											'class': cellClass
										}).inject($(this.preambleGUID+'webToolsEditCalendar' + this.options.CalendarID + 'EventTableRow' + (firstWeekNumber+j)));
										if((j==600) && (i>0)){												
											$(this.preambleGUID+'EventCalendarDaysOfWeek' + i+'_'+j).innerHTML = this.constants.daysOfWeek[i-1]+"<br/>"+cellDate;
										}
									}
								 }
							  }
							else if (this.displayStyle == this.constants.monthDisplayStyle) {
								var cellDateCounter = 1;
								if (firstWeekNumber != 0) {
								//	firstWeekNumber++;
								}
								var numWeeksInMonth = compareDate.numWeeks();
								
								for(j=0;j<compareDate.numWeeks()+1;j++)
								{
									new Element('tr', {
										id: this.preambleGUID+'webToolsEditCalendar' + this.options.CalendarID + 'EventTableRow'+(firstWeekNumber+(j-1)),
										'class': 'eventCalendarWeekDisplay'
									}).inject($(this.preambleGUID+'webToolsEditCalendar' + this.options.CalendarID + 'EventTable'));
									for (i = 0; i <= 6; i++) {
										cellHeight=this.options.cellHeight;
										cellClass='eventCalendarDaysOfWeek';
										cellDate = "&nbsp;";
										if(j==0)
										{
											cellDate = "&nbsp;";
										}
										else if(j==1 & elementDate.getDay() > i)
										{
											 cellDate = "&nbsp;";
										}
										else if(cellDateCounter <= compareDate.lastDay()){
											cellDate = '<div class="CalendarDate">'+(cellDateCounter++)+'</div>';
										}
										else{
											cellDate = '&nbsp;';
										}
										if(j==0){
											cellHeight="20px";
											cellClass='eventCalendarWeeks';
											}
										new Element('td', {
											id: this.preambleGUID+'EventCalendarDaysOfWeek' + i + '_' + (firstWeekNumber+(j-1)),
											width: '14%',
											valign: 'top',
											align: 'center',
											height: cellHeight,
											html: cellDate,
											'class': cellClass
										}).inject($(this.preambleGUID+'webToolsEditCalendar' + this.options.CalendarID + 'EventTableRow' + (firstWeekNumber+(j-1))));
										if(j==0){												
											$(this.preambleGUID+'EventCalendarDaysOfWeek' + i+'_'+(firstWeekNumber + (j-1))).innerHTML = this.constants.daysOfWeek[i];
										}else{
											if ($(this.preambleGUID + 'EventCalendarDaysOfWeek' + i + '_' + (firstWeekNumber + (j - 1))).innerHTML == "") {
												$(this.preambleGUID + 'EventCalendarDaysOfWeek' + i + '_' + (firstWeekNumber + (j - 1))).innerHTML += "<br/>";
											}
										}
									}
								}
							}
							else {
								new Element('tr', {
									id: this.preambleGUID+'webToolsEditCalendar' + this.options.CalendarID + 'EventTableRow'
								}).inject($(this.preambleGUID+'webToolsEditCalendar' + this.options.CalendarID + 'EventTable'));
								for (i = 0; i <= 6; i++) {
									new Element('td', {
										id: this.preambleGUID+'EventCalendarDaysOfWeek' + i,
										width: '14%',
										valign: 'top',
										align: 'left',
										height: this.options.columnHeight,
										styles: {
											'height': '400'
										},
										'class': 'eventCalendarDaysOfWeek'
									}).inject($(this.preambleGUID+'webToolsEditCalendar' + this.options.CalendarID + 'EventTableRow'));
									$(this.preambleGUID+'EventCalendarDaysOfWeek' + i).innerHTML = this.constants.daysOfWeek[i];
								}
							}
						}

						$$('.eventCalendarOnColumn').each(function(el,ndx){
							el.setStyle('background-color',this.options.defaultColor);
						},this);
						$$('.eventCalendarOffColumn').each(function(el,ndx){
							el.setStyle('background-color',this.options.alternateColor);
						},this);
						for(i=0;i<x.length;i++)
						 {
						 	eventDateFormatted = x[i].getElementsByTagName("event_date_formatted")[0].childNodes[0].nodeValue;

						 	eventDate = x[i].getElementsByTagName("event_date")[0].childNodes[0].nodeValue;
							try {
								eventTime = x[i].getElementsByTagName("event_time")[0].childNodes[0].nodeValue;
							}catch(ex){
								eventTime = 0;
							}
							try {
								eventName = x[i].getElementsByTagName("event_name")[0].childNodes[0].nodeValue;
							} 
							catch (ex) {
								eventName = "NA";
							}
							try {
								eventWeek = x[i].getElementsByTagName("event_week")[0].childNodes[0].nodeValue;
							} 
							catch (ex) {
								eventWeek = "";
							}
							try {
								eventDay = x[i].getElementsByTagName("event_day")[0].childNodes[0].nodeValue;
							}catch(ex){
								eventDay = 1;
							}
							try {
								eventHour = x[i].getElementsByTagName("event_hour")[0].childNodes[0].nodeValue;
							}catch(ex){
								eventHour = 0;
							}
							try {
								eventAllDay = x[i].getElementsByTagName("event_allday")[0].childNodes[0].nodeValue;
							}catch(ex){
								eventAllDay = 0;
							}
							eventID = x[i].getElementsByTagName("event_id")[0].childNodes[0].nodeValue;
							try {
								eventPrivacy = x[i].getElementsByTagName("event_privacy")[0].childNodes[0].nodeValue;
							}catch(ex){
								eventPrivacy = 0;
							}
							try {
								eventExternal = x[i].getElementsByTagName("event_external")[0].childNodes[0].nodeValue;
							}catch(ex){
								eventExternal = 0;
							}
							var EventClass = this.options.standardEventClass;
							if (eventExternal == "1") {
								EventClass = this.options.externalEventClass;
							}							
							if (this.displayStyle == this.constants.weekDisplayStyle) {
								if (eventWeek == this.currentDisplayWeek) {
										if (eventAllDay == -1) {
											new Element('a', {
												href: '',
												html: '<br/><span class="'+this.options.allDayEventClass+'">All Day Event<br/><b>' + eventName + '</b></span>',
												id: this.preambleGUID+'EventCalendarEventLinkCheck' + eventID,
												'events':{
													'mouseover': function(){
														this.setStyle('cursor','pointer');
														this.setStyle('background-color','#ccc');
													},
													'mouseout': function(){
														this.setStyle('cursor','auto');
														this.setStyle('background-color','');
													},
													'click': function(){
														var elpres = this.preambleGUID+'EventCalendarEventLinkCheck';
														if (!($(this.preambleGUID+'CalendarEventInfoDiv' + this.id.substring(elpres.length)))) {		
															new displayInfoBox(elGUID,elCalID, evID, $(elGUID+'EventCalendarEventLinkCheck' + evID).id.substring(elpres.length),elCalendarType);																}
													}
												}
											}).inject($(this.preambleGUID+'EventCalendarDaysOfWeek' + (eventDay.toInt()+1) + "_" + (7 * 100)));
											eventList.push(this.preambleGUID+'EventCalendarEventLinkCheck'+eventID);
										}
										else {
											if (eventHour == 0) {
												eventHour = 7;
											}
											new Element('a', {
												href: '',
												html: "<br/>" + eventName,
												'class': EventClass,
												id: this.preambleGUID+'EventCalendarEventLinkCheck' + eventID,
												name: this.preambleGUID+"_"+this.options.calendarID+"_"+eventID+"_"+this.options.calendarType,	
												'events':{
													'mouseover': function(){
														this.setStyle('cursor','pointer');
														this.setStyle('background-color','#ccc');
													},
													'mouseout': function(){
														this.setStyle('cursor','auto');
														this.setStyle('background-color','');
													},
													'click': function(e){
														new Event(e).stop();
														var thisName = this.name;
														var elGUID = thisName.substring(0,thisName.indexOf('_'));
														thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
														var elpres = elGUID+'EventCalendarEventLinkCheck';
														var elCalID = thisName.substring(0,Math.max(1,thisName.indexOf('_')-1));
														thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
														var evID = thisName.substring(0,Math.max(1,thisName.indexOf('_')));
														thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
														var elCalendarType = thisName.substring(0,thisName.length);
														if (!($(elGUID+'CalendarEventInfoDiv' + $(elGUID+'EventCalendarEventLinkCheck' + evID).id.substring(elpres.length)))) {
															new displayInfoBox(elGUID,elCalID, evID, $(elGUID+'EventCalendarEventLinkCheck' + evID).id.substring(elpres.length),elCalendarType);
														}
													}
												}
											}).inject($(this.preambleGUID+'EventCalendarDaysOfWeek' + (eventDay.toInt()+1) + "_" + (eventHour * 100)));
											eventList.push(this.preambleGUID+'EventCalendarEventLinkCheck'+eventID);	
										}
									//$('EventCalendarDaysOfWeek' + eventDay).innerHTML += "<br/>"+eventName ;
								}
							}
							if (this.displayStyle == this.constants.monthDisplayStyle){	
							   if (eventExternal == "1") {
							     	new Element('a', {
								   		html: "<br/>" + eventName,
								   		'class': EventClass,
										styles :{
											'display':'block'
										},
								   		id: this.preambleGUID+'EventCalendarEventLinkCheck' + eventID,
										name: this.preambleGUID+"_"+this.options.calendarID+"_"+eventID+"_"+this.options.calendarType,
										'events':{
											'mouseover': function(){
												this.setStyle('cursor','pointer');
												this.setStyle('background-color','#ccc');
											},
											'mouseout': function(){
												this.setStyle('cursor','auto');
												this.setStyle('background-color','');
											},
											'click': function(e){
												new Event(e).stop();
												var thisName = this.name;
												var elGUID = thisName.substring(0,thisName.indexOf('_'));
												thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
												var elpres = elGUID+'EventCalendarEventLinkCheck';
												var elCalID = thisName.substring(0,Math.max(1,thisName.indexOf('_')-1));
												thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
												var evID = thisName.substring(0,Math.max(1,thisName.indexOf('_')));
												thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
												var elCalendarType = thisName.substring(0,thisName.length);
												if (!($(elGUID+'CalendarEventInfoDiv' + $(elGUID+'EventCalendarEventLinkCheck' + evID).id.substring(elpres.length)))) {
													new displayInfoBox(elGUID,elCalID, evID, $(elGUID+'EventCalendarEventLinkCheck' + evID).id.substring(elpres.length),elCalendarType);
												}
											}
										}
								   	}).inject($(this.preambleGUID+'EventCalendarDaysOfWeek' + eventDay + '_' + eventWeek));
									eventList.push(this.preambleGUID+'EventCalendarEventLinkCheck'+eventID);
									$(this.preambleGUID+'EventCalendarEventLinkCheck' + eventID).addEvent('mouseover',function(){
										$(this.preambleGUID+'EventCalendarEventLinkCheck' + eventID).setStyle('cursor','pointer');
										//$(this.preambleGUID+'EventCalendarEventLinkCheck' + eventID).setStyle('background-color','#ccc');
									}.bind(this));
									$(this.preambleGUID+'EventCalendarEventLinkCheck' + eventID).addEvent('mouseout',function(){
										$(this.preambleGUID+'EventCalendarEventLinkCheck' + eventID).setStyle('cursor','auto');
										//$(this.preambleGUID+'EventCalendarEventLinkCheck' + eventID).setStyle('background-color','');
									}.bind(this));
							   }
							   else {
							   	new Element('a', {
							   		href: '',
							   		html: "<br/>" + eventName,
							   		'class': EventClass,
							   		id: this.preambleGUID+'EventCalendarEventLinkCheck' + eventID,
									name: this.preambleGUID+"_"+this.options.calendarID+"_"+eventID+"_"+this.options.calendarType,
									'events':{
										'mouseover': function(){
											this.setStyle('background-color','#ccc');
										},
										'mouseout': function(){
											this.setStyle('background-color','');
										},
										'click': function(e){
											new Event(e).stop();
											var thisName = this.name;
											var elGUID = thisName.substring(0,thisName.indexOf('_'));
											thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
											var elpres = elGUID+'EventCalendarEventLinkCheck';
											var elCalID = thisName.substring(0,Math.max(1,thisName.indexOf('_')-1));
											thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
											var evID = thisName.substring(0,Math.max(1,thisName.indexOf('_')));
											thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
											var elCalendarType = thisName.substring(0,thisName.length);
											if (!($(elGUID+'CalendarEventInfoDiv' + $(elGUID+'EventCalendarEventLinkCheck' + evID).id.substring(elpres.length)))) {
												new displayInfoBox(elGUID,elCalID, evID, $(elGUID+'EventCalendarEventLinkCheck' + evID).id.substring(elpres.length),elCalendarType);
											}
										}
									}
							   	}).inject($(this.preambleGUID+'EventCalendarDaysOfWeek' + eventDay + '_' + eventWeek));
								eventList.push(this.preambleGUID+'EventCalendarEventLinkCheck'+eventID);
								$(this.preambleGUID+'EventCalendarEventLinkCheck' + eventID).addEvent('mouseover',function(){
									$(this.preambleGUID+'EventCalendarEventLinkCheck' + eventID).setStyle('cursor','hand');
								}.bind(this));
								$(this.preambleGUID+'EventCalendarEventLinkCheck' + eventID).addEvent('mouseout',function(){
									$(this.preambleGUID+'EventCalendarEventLinkCheck' + eventID).setStyle('cursor','auto');
								}.bind(this));
							   }
							}
						}
						if (Browser.Engine.trident) {
							var mydiv = $('webToolsEditCalendarEventList').innerHTML;
							this.options.containerEL.innerHTML = mydiv;
							this.setupEventTriggers(eventList);
						}
							
					}
				}.bind(this)
			}).send();
					
		},
		setupEventTriggers: function(eventList){
			eventList.each(function(el1,ndx){
				var el = $(el1);
				el.addEvent('mouseover',function(){
					el.setStyle('cursor','pointer');
					el.setStyle('background-color','#ccc');
				}.bind(this));
				el.addEvent('mouseout',function(){
					el.setStyle('cursor','auto');
					el.setStyle('background-color','');
				}.bind(this))
				el.addEvent('click',function(e){
					new Event(e).stop();
					var thisName = el.name;
					var elGUID = thisName.substring(0,thisName.indexOf('_'));
					thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
					var elpres = elGUID+'EventCalendarEventLinkCheck';
					var elCalID = thisName.substring(0,Math.max(1,thisName.indexOf('_')-1));
					thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
					var evID = thisName.substring(0,Math.max(1,thisName.indexOf('_')));
					thisName = thisName.substring(thisName.indexOf('_')+1,thisName.length);
					var elCalendarType = thisName.substring(0,thisName.length);
					if (!($(elGUID+'CalendarEventInfoDiv' + $(elGUID+'EventCalendarEventLinkCheck' + evID).id.substring(elpres.length)))) {
						//alert('displaying info box');
						new displayInfoBox(elGUID,elCalID, evID, $(elGUID+'EventCalendarEventLinkCheck' + evID).id.substring(elpres.length),elCalendarType);
					}
				}.bind(this))
			},this);
		},
		// disposes quickly of any unopened boxes
		// this method should be called during the window onClose Event
		clearBoxes: function(){
			openBoxes.each(function(el){
				if ($(el)) {
					$(el).dispose();
				}
			});
		},
		// setup the next and previous links if they are on the page
		// 
		setupEvents: function(){
				var targetEl = this.options.nextLinkCheckEl;
				if (targetEl) {
					if(this.options.nextLinkCheckImageEl){
						targetEl.addEvent('mouseover', function(){
							this.options.nextLinkCheckImageEl.src = this.options.nextLinkCheckImageOverSrc;
						}.bind(this));
						targetEl.addEvent('mouseout', function(){
							this.options.nextLinkCheckImageEl.src = this.options.nextLinkCheckImageSrc;
						}.bind(this));
					}
					targetEl.addEvent('click', function(e){
						new Event(e).stop();
						this.forwardOne();
					}.bind(this))
				}
				var targetEl = this.options.previousLinkCheckEl;
				if (targetEl) {
					if(this.options.previousLinkCheckImageEl){
						targetEl.addEvent('mouseover', function(){
							this.options.previousLinkCheckImageEl.src = this.options.previousLinkCheckImageOverSrc;
						}.bind(this));
						targetEl.addEvent('mouseout', function(){
							this.options.previousLinkCheckImageEl.src = this.options.previousLinkCheckImageSrc;
						}.bind(this));
					}
					targetEl.addEvent('click', function(e){
						new Event(e).stop();
						this.backOne();
					}.bind(this))
				}
				var targetEl = this.options.weekviewLinkCheckEl;
				if (targetEl) {
					if(this.options.weekviewLinkCheckImageEl){
						targetEl.addEvent('mouseover', function(){
							this.options.weekviewLinkCheckImageEl.src = this.options.weekviewLinkCheckImageOverSrc;
						}.bind(this));
						targetEl.addEvent('mouseout', function(){
							this.options.weekviewLinkCheckImageEl.src = this.options.weekviewLinkCheckImageSrc;
						}.bind(this));
					}
					targetEl.addEvent('click', function(e){
						new Event(e).stop();
						this.displayStyle = this.constants.weekDisplayStyle;
						this.drawCalendar();
					}.bind(this))
				}
				var targetEl = this.options.monthviewLinkCheckEl;
				if (targetEl) {
					if(this.options.monthviewLinkCheckImageEl){
						targetEl.addEvent('mouseover', function(){
							this.options.monthviewLinkCheckImageEl.src = this.options.monthviewLinkCheckImageOverSrc;
						}.bind(this));
						targetEl.addEvent('mouseout', function(){
							this.options.monthviewLinkCheckImageEl.src = this.options.monthviewLinkCheckImageSrc;
						}.bind(this));
					}
					targetEl.addEvent('click', function(e){
						new Event(e).stop();
						this.displayStyle = this.constants.monthDisplayStyle;
						this.drawCalendar();
					}.bind(this))
				}
		 },
		 
		 forwardOne: function(){
			if (this.displayStyle == this.constants.monthDisplayStyle) {
				this.currentStartDate = new Date(this.currentStartDate.getFullYear(), this.currentStartDate.getMonth() + 1, this.currentStartDate.getDate());
				this.currentDisplayWeek = this.currentStartDate.getWeek(0);
				this.currentDisplayMonth = this.currentStartDate.getMonth() + 1;
				if(this.currentDisplayMonth == 1){
					this.currentDisplayYear++;
				}
			}
			if (this.displayStyle == this.constants.weekDisplayStyle) {
				this.currentStartDate = new Date(this.currentStartDate.getFullYear(), this.currentStartDate.getMonth(), this.currentStartDate.getDate() + 7);
				this.currentDisplayWeek = this.currentStartDate.getWeek(0);
				this.currentDisplayMonth = this.currentStartDate.getMonth()+1;
				this.currentDisplayYear = this.currentStartDate.getFullYear();
				if(this.currentDisplayMonth == 1){
					this.currentDisplayYear++;
				}
			}
			this.drawCalendar();
		 },
		 
		 backOne: function(){
			if (this.displayStyle == this.constants.monthDisplayStyle) {
				this.currentStartDate = new Date(this.currentStartDate.getFullYear(), this.currentStartDate.getMonth() - 1, this.currentStartDate.getDate());
				this.currentDisplayWeek = this.currentStartDate.getWeek(0);
				//this.currentDisplayMonth = this.currentStartDate.getMonth();
				this.currentDisplayMonth = this.currentDisplayMonth - 1;
				if (this.currentDisplayMonth == 0){
					this.currentDisplayMonth = 12;
					this.currentDisplayYear--;
				}
			}
			if (this.displayStyle == this.constants.weekDisplayStyle) {
				this.currentStartDate = new Date(this.currentStartDate.getFullYear(), this.currentStartDate.getMonth(), this.currentStartDate.getDate() - 7);
				this.currentDisplayWeek = this.currentStartDate.getWeek(0);
				this.currentDisplayMonth = this.currentStartDate.getMonth()+1;
				this.currentDisplayYear = this.currentStartDate.getFullYear();
				//this.currentDisplayMonth = this.currentDisplayMonth - 1;
				if (this.currentDisplayMonth == 12){
					//this.currentDisplayMonth = 12;
					this.currentDisplayYear--;
				}
				
			}
			this.drawCalendar();
		 }
});	

var openBoxes = [];

var displayInfoBox = new Class({
	initialize: function(preambleGUID, strCalendarID, strEventID, elLinkElement,calendarType){

		elLinkElement = $(preambleGUID+'EventCalendarEventLinkCheck'+strEventID);
		// get the coordinates of the link 
		var linkPosition = elLinkElement.getCoordinates();
		// get the coordinates of the document
		var docPosition = document.getSize();
		// set coordinates for info Div
		var leftPos = linkPosition.left;
		var topPos = linkPosition.top;
		if ((linkPosition.left+200)>docPosition.x)
		{
			leftPos = (linkPosition.left - 200);
		}
		if ((linkPosition.top+200)> docPosition.y)
		{
			topPos = (linkPosition.top - 200);
		}
		var infoDiv = new Element('div',{
			id: preambleGUID+'CalendarEventInfoDiv'+strEventID,
			'class': 'calendarEventInfoDiv',
			styles: {
				
				left: leftPos,
				top: topPos,
				width: 0,
				height: 0,
				opacity: '0',
				'z-index': '99',
				'display': 'block',
				'position': 'absolute',
				'padding-left': '2px',
				'padding-right': '2px'
			}
		}).inject(document.body,'top');
		// add the new id to the openBoxes array
		openBoxes.push(preambleGUID+'CalendarEventInfoDiv'+strEventID);
		var myFx = new Fx.Elements([infoDiv]).start({
			'0': {
				'width': [0,200],
				'height': [0,200],
				'opacity': [0,1]
			}
		});
		// get the details for the event and insert them in the div
		new Request.HTML({
			url: 'cms/getEventDetails.php?calendarType='+calendarType+'&eventID='+strEventID+'&GUID='+preambleGUID,
			update: preambleGUID+'CalendarEventInfoDiv'+strEventID,
			onComplete: function(a,b,c,d){
				$(preambleGUID+'EventCalendarInfoDiv_EditLink'+strEventID).addEvent('click',function(e){
					new Event(e).stop();
					if((calendarType == 'faculty') || (calendarType=='student')){
						//chaUI.eTeachEditEventWindow(strEventID);
						alert('display faculty or student window');
					}
					else{
						//alert('display Full Event');
						var myFx = new Fx.Elements([infoDiv]).start({
							'0': {
								'width': [200,400]
							}
						});
						var myFX = new Fx.Elements([$(preambleGUID+'DescriptionDiv'+strEventID)]).start({
							'0':{
								'display': ['none','block']
							}
						});
						//MochaUI.webToolsEditEventWindow(strEventID);
					}
					//$(preambleGUID+'EventCalendarInfoDiv_CloseLink'+strEventID).fireEvent('click',e);
				}.bind(this));
				// add an onclick event to the box so we can close it.
				$(preambleGUID+'EventCalendarInfoDiv_CloseLink'+strEventID).addEvent('click',function(e){
					new Event(e).stop();
					$(preambleGUID+'CalendarEventInfoDiv'+strEventID).innerHTML = "";
					var myFx = new Fx.Elements([$(preambleGUID+'CalendarEventInfoDiv'+strEventID)],{
						onComplete: function(){
							$(preambleGUID+'CalendarEventInfoDiv'+strEventID).dispose();
						}
					}).start({
						'0': {
							'width': [200,0],
							'height': [200,0],
							'opacity': [1,0]
						}
					});
					// remove the box from the list of openboxes
					openBoxes.erase($(preambleGUID+'CalendarEventInfoDiv'+strEventID).id);
				}.bind(this));
			}.bind(this)
		}).get();
		// provide mechanism to edit event

	}
})
	
