﻿

function Calendar(outputObject)
{	
	this.output=outputObject;
	this.month_indicator=document.getElementById('month-indicator');
	this.grid=document.getElementById('cal-grid');
	this.weekheading=document.getElementById('week-heading');
	this.today=document.getElementById('today-indicator');
	
	this.prevMonth=document.getElementById('prev');
	this.nextMonth=document.getElementById('next');
	
	if(VARS['EVENT_REGISTRATION_PAGE'] != undefined) EVENT_REGISTRATION_PAGE = VARS['EVENT_REGISTRATION_PAGE'];
	
	if(VARS['STAGE_WIDTH'] != undefined) STAGE_WIDTH = VARS['STAGE_WIDTH'];
	if(VARS['STAGE_HEIGHT'] != undefined) STAGE_HEIGHT = VARS['STAGE_HEIGHT'];
	
	if(VARS['FONT_SIZE'] != undefined) FONT_SIZE = VARS['FONT_SIZE'];
	
	this.month_indicator.style.fontSize=FONT_SIZE+'pt';
	
	if(VARS['GRID_CELL_SIZE'] != undefined)
	{
		GRID_CELL_SIZE = Number(VARS['GRID_CELL_SIZE']);
		
		DAYS_SPACING = (GRID_CELL_SIZE + 2);
	}
	
	this.process_colours();
	
	if(colours_loaded)
	{
		today = new Date(); // new Date(year,month,date[,hour,minute,second,millisecond ]) < javascript date format
		active_date = new Date();
		
		current_day = DAYS[today.getDay()];
		current_date = today.getDate();
		current_month = MONTHS[today.getMonth()];
		current_year = today.getFullYear();
		
		this.today.innerHTML=current_day.substr(0, 3) + ' ' + current_date + ordinal(current_date) + ' ' + current_month.substr(0, 3) + ' ' + current_year;
		this.today.style.color=COLOURS['dateIndCol'];
		this.today.title = "Click to go to today's date.";
		this.today.style.fontSize=FONT_SIZE+'pt';
			
		if(navigator.userAgent.indexOf('IE') != -1)
			this.today.attachEvent("onclick",today_indicator_click);
		else
			this.today.addEventListener('click', today_indicator_click,false);
		
		
		
		this.nextMonth.style.backgroundColor=COLOURS['buttonCol'];
		this.prevMonth.style.backgroundColor=COLOURS['buttonCol'];
		
		var prev_month_index = (today.getMonth() - 1);
		if(prev_month_index < 0) prev_month_index = 11;
		this.prevMonth.title = MONTHS[prev_month_index];
		
		var next_month_index = (today.getMonth() + 1);
		if(next_month_index >= MONTHS.length) next_month_index = 0;
		this.nextMonth.title = MONTHS[next_month_index];
		
		if(navigator.userAgent.indexOf('IE') != -1)
			this.prevMonth.attachEvent("onclick",previous_button_click);
		else
			this.prevMonth.addEventListener('click', previous_button_click,false);
			
		if(navigator.userAgent.indexOf('IE') != -1)
			this.nextMonth.attachEvent("onclick",next_button_click);
		else
			this.nextMonth.addEventListener('click', next_button_click,false);
			
		date_clicked = this.process_VARS();
		
		//display day titles... S M T W T F S
		// only need to do this once
		var margin=8-Math.ceil(GRID_CELL_SIZE/4);
		margin=Math.max(2,margin);
		
		for(var day_counter = 0; day_counter < DAYS.length; day_counter++)
		{
			var cal_day=document.createElement('div'); // originally textfield
			cal_day.className='cal-day';
			cal_day.innerHTML = DAYS[day_counter].substr(0, 2);
			cal_day.style.color=COLOURS['daysCol'];
			cal_day.style.width=GRID_CELL_SIZE+'px';
			cal_day.style.height=GRID_CELL_SIZE+'px';
			cal_day.style.marginLeft=margin+'px';
			cal_day.style.marginRight=margin+'px';
			this.weekheading.appendChild(cal_day);
		}
		
		this.load_event_type_data();
		
		var infoBtn=document.getElementById('info');
		if(infoBtn)
			infoBtn.addEventListener('click',show_colour_legand,false);
			
		event_frame=document.getElementById('event-frame');
		event_frame.style.borderColor=COLOURS['openTextCol'];
		
		event_title=document.getElementById('event-frame-title');
		event_title.style.color = COLOURS['monthYearCol'];
		event_title.style.borderColor=COLOURS['openTextCol'];
		
		if(COLOURS['backgroundCol'] != undefined)
		{
			this.output.style.backgroundColor=COLOURS['backgroundCol'];
			event_frame.style.backgroundColor=COLOURS['backgroundCol'];
		}
		
		var cb=document.getElementById('close-btn');
		
		if(cb)
		{
			if(navigator.userAgent.indexOf('IE') != -1)
				cb.attachEvent("onclick",event_frame_close);
			else
				cb.addEventListener('click', event_frame_close,false);
		}
	}
};
	
Calendar.prototype.display_error_dialog=function (msg, cls)  // string, boolean
	{
		alert(msg);
	}
	
	function event_frame_close() // mouse event
	{
		$("#event-frame").fadeOut("fast");
	}
	
Calendar.prototype.process_colours=function ()
	{
		colours_loaded = true;
		
		if(COLOURS != undefined)
		{

		}
		else
		{
			colours_loaded = false;
			
			this.display_error_dialog('No colours were specified.');
		}
	}
	
Calendar.prototype.process_VARS=function () // date
	{
		if(VARS['EVENT_REGISTRATION_PAGE'] != undefined) EVENT_REGISTRATION_PAGE = Number(VARS['EVENT_REGISTRATION_PAGE']);
		
		if(CLOSED_DATES != undefined)
		{
			this.process_closed_dates(CLOSED_DATES);
		}
		else this.display_error_dialog('No closed dates were specified.');
		
		if(VARS['CMS_VERSION'] == undefined) this.display_error_dialog('No CMS_VERSION was specified.');
		
		if(VARS['CALENDAR_ID'] == undefined) this.display_error_dialog('A calendar ID was not specified.');
		
		if(VARS['date_clicked']!=''&&VARS['date_clicked']!= undefined)
		{
			active_date=new Date();
			var datesplit = VARS['date_clicked'].split('-'); // array	
			active_date=new Date(datesplit[0],datesplit[1]-1,datesplit[2]);
			
			return active_date;
		}	
			
		if(date_clicked != undefined)
		{
			current_day = DAYS[active_date.getDay()];
			current_date = active_date.getDate();
			current_month = MONTHS[active_date.getMonth()];
			current_year = active_date.getFullYear();
			
			this.month_indicator.innerHTML = current_month.substr(0, 3) + ' ' + current_year;
			this.month_indicator.style.color = COLOURS['monthYearCol'];
			
			
			return active_date;
		}
		
		return today;
	};
	
	Calendar.prototype.process_closed_dates=function (dates_array) // array
	{	
		var num_dates = dates_array.length;
		
		var counter = 0;
		
		for(; counter < num_dates; counter++)
		{
			if(dates_array[counter] != '' && typeof dates_array[counter] =='object')
			{
				var datesplit = dates_array[counter]['date'].split(':'); // array
				
				dates_array[counter]['date']=new Date(datesplit[0],datesplit[1]-1,datesplit[2]);
			}
		}
	}
	
	Calendar.prototype.is_closed_date=function(needle) // date, returns array
	{
		var closed_dates = new Array();
		
		if((CLOSED_DATES != null) && (CLOSED_DATES.length) && (CLOSED_DATES.length > 0))
		{
			var num_closed_dates = CLOSED_DATES.length;
			
			var counter = 0;
			
			for(; counter < num_closed_dates; counter++)
			{
				if(CLOSED_DATES[counter]['date'].toString() == needle.toString()) closed_dates.push(CLOSED_DATES[counter]);
			}
		}
		
		var return_value = null; // object
		var num_events = 0;
		
		if(closed_dates.length != 0)
		{
			if(closed_dates.length > 1) return_value = closed_dates;
			else if(closed_dates.length == 1) return_value = closed_dates[0];
			
			num_events = closed_dates.length;
		}
		
		return new Array(return_value, num_events);
	}
	
	function ordinal(val) // int, return string
	{
		var ordinal = '';
		
		var remainder = (val % 10);
		
		if((val >= 10) && (val < 21)) remainder = 4;	//all dates between and including  10 and 20 use ordinal th
		
		switch(remainder)
		{
			case 1:
				ordinal = 'st';
				break;
			
			case 2:
				ordinal = 'nd';
				break;
				
			case 3:
				ordinal = 'rd';
				break;
				
			default:
				ordinal = 'th';
				break;
		}
		
		return ordinal;
	}
	
	Calendar.prototype.draw_date_grid=function (date) // date format expected
	{
		this.month_indicator.innerHTML = MONTHS[date.getMonth()].substr(0, 3) + ' ' + date.getFullYear();
		this.remove_all_children_from_object(this.grid);
					
		//display date grid
		var days_in_month = DAYS_IN_MONTH[date.getMonth()];
		
		//increment the number of days in the month by one if the month is February and it is a leap year
		if(((date.getFullYear() % 4) == 0) && (date.getMonth() == 1)) days_in_month += 1;
		
		var start_date = new Date(date.getFullYear(), date.getMonth());
		
		var day_count = start_date.getDate();
		
		var x_counter = start_date.getDay();
		var y_pos = Math.round(GRID_CELL_START_Y); // not used
		
		
		var firstDay=start_date.getDay(); // get value of the first day of the month, 0 = Sunday
		
		var margin=8-Math.ceil(GRID_CELL_SIZE/4);
		margin=Math.max(2,margin);
		
		for(var bump=0;bump<firstDay;bump++)
		{
			var bumper = document.createElement('div');
			bumper.className='cal-day-space';
			bumper.style.backgroundColor=COLOURS['backgroundCol'];
			bumper.style.width=GRID_CELL_SIZE+'px';
			bumper.style.height=GRID_CELL_SIZE+'px';
			bumper.style.marginLeft=margin+'px';
			bumper.style.marginRight=margin+'px';
			this.grid.appendChild(bumper);
		}
		
		for(var grid_counter = start_date.getDay(); grid_counter < (days_in_month + start_date.getDay()); grid_counter++)
		{
			var new_date = new Date(active_date.getFullYear(), active_date.getMonth(), day_count);
			
			var date_name = new_date.getDate() + '-' + (new_date.getMonth()+1) + '-' + new_date.getFullYear(); // string
			
			var date_button = document.createElement('div');
			//date_button.title = date_name;
			date_button.className='cal-day';
			date_button.style.color=COLOURS['openTextCol'];
			//date_button.style.borderWidth='1px '
			//date_button.style.borderColor=COLOURS['backgroundCol'];
			//date_button.style.borderStyle='solid';
			date_button.innerHTML=String(day_count);
			date_button.style.backgroundColor=COLOURS['dayTextCol'];
			date_button.style.width=GRID_CELL_SIZE+'px';
			date_button.style.height=GRID_CELL_SIZE+'px';
			date_button.style.lineHeight=GRID_CELL_SIZE+'px';
			date_button.style.marginLeft=margin+'px';
			date_button.style.marginRight=margin+'px';
			
			if((new_date.getDate() == today.getDate()) &&
				(new_date.getMonth() == today.getMonth()) &&
				(new_date.getFullYear() == today.getFullYear()))
			{
				date_button.title = 'Today';
				date_button.style.fontWeight='bold';
				//date_button.style.borderColor=COLOURS['dateIndCol'];
				date_button.style.backgroundColor=COLOURS['daysCol'];
				date_button.style.color=COLOURS['dateIndCol'];
			}
			
			
			if(permanent_holidays[x_counter] == 'y')
			{
				date_button.title = unescape(permanent_holidays_messages[x_counter]);
				
				date_button.style.color=COLOURS['permHolsCol'];
			}
			
			var closed_array = this.is_closed_date(new_date); // array
			var closed_date = closed_array[0]; // object
			var num_closed = closed_array[1]; // int
			
			if((VARS['EVENT_POPUP'] == EVENT_POPUP_MODE_NEVER) || ((num_closed == 1) && (VARS['EVENT_POPUP'] == EVENT_POPUP_MODE_ON_MULTI)))
			{
				if(closed_date != null)
				{
					var col_to_use='';
					
					var cnt = 0;
					
					if (isArray(closed_date)==true)
					{
						var max = closed_date.length;
						date_button.title = '';
						
						for(; cnt < max; cnt++)
						{
							if (cnt == 0)
							{
								if(EVENT_TYPE_COLOURS[closed_date[cnt]['type']] != undefined)
									col_to_use = EVENT_TYPE_COLOURS[closed_date[cnt]['type']]['colour'];
								else col_to_use = COLOURS['holTextCol'];
							}
							
							date_button.title += unescape(unescape(closed_date[cnt]['title']));
							
							if (closed_date[cnt]['description'] != '') date_button.title +=  ': ' + unescape(unescape(closed_date[cnt]['description']));
							if (closed_date[cnt]['location'] != '')  date_button.title += "\n" + unescape(unescape(closed_date[cnt]['location']));
							if (closed_date[cnt]['startTime'] != '') date_button.title += "\n" + unescape(unescape(closed_date[cnt]['startTime']));
							if (closed_date[cnt]['endTime'] != '') date_button.title += ' - ' + unescape(unescape(closed_date[cnt]['endTime']));
							
							if(cnt != (max - 1)) date_button.title += "\n\n";
						}
					}
					else
					{
						date_button.title = '';
						
						if(EVENT_TYPE_COLOURS[closed_date['type']] != undefined)
							col_to_use = EVENT_TYPE_COLOURS[closed_date['type']]['colour'];
						else col_to_use = COLOURS['holTextCol'];
						
						var thisTitle='';
						
						thisTitle=unescape(unescape(closed_date['title']));
						
						if (closed_date['description'] != '') thisTitle +=  ': ' + unescape(unescape(closed_date['description']));
						if (closed_date['location'] != '')  thisTitle += "\n" + unescape(unescape(closed_date['location']));
						if (closed_date['startTime'] != '') thisTitle += "\n" + unescape(unescape(closed_date['startTime']));
						if (closed_date['endTime'] != '') thisTitle += ' - ' + unescape(unescape(closed_date['endTime']));
					}
					
					date_button.style.color='#'+col_to_use;
					date_button.title=thisTitle;
					date_button.style.cursor="pointer";
				}
			}
			
			//VARS['EVENT_POPUP'] == EVENT_POPUP_MODE_ALWAYS
			else
			{
				if(closed_date != null)
				{
					date_button.style.color=COLOURS['event'];
					
					date_button.id = this.indexesOfClosed(closed_date);
					date_button.title = 'Click to view events for this date.';
					date_button.style.cursor = 'pointer';
				}
			}
			
			if(VARS['admin_mode'])
			{
				date_button.id = String(new_date.getTime());
				date_button.title = 'Click to manage events for this date.';
			}
			
			if(navigator.userAgent.indexOf('IE') != -1)
				date_button.attachEvent("onclick", date_button_click);
			else
				date_button.addEventListener('click', date_button_click,false);
			
			if(((grid_counter + 1) % DAYS.length) == 0)
			{
				x_counter = 0;
				y_pos += Math.round(GRID_CELL_SIZE + 2);
			}
			else x_counter++;
			
			day_count++;
			
			this.weekheading.style.width=(GRID_CELL_SIZE+margin*2)*7+'px';
			this.grid.style.width=(GRID_CELL_SIZE+margin*2)*7+'px';
			this.grid.appendChild(date_button);
		}
		//$('#cal-grid .cal-day :hover').css(({'border-color':COLOURS['dayTextCol']}));
	}
	
	// create simple array of indexes to closed events for date buttons
	Calendar.prototype.indexesOfClosed=function(data)
	{
		var indexes='';  // return 'empty' by default
		var temp=new Array();
		if(isArray(data))
		{
			for(var i=0;i<data.length;i++)
			{
				var pos=CLOSED_DATES.indexOf(data[i]);
				if(pos!=-1)
					temp.push(pos);
			}
		}
		else
		{
			
		}
		if(temp.length>0)
			indexes=temp.toString();
		return indexes;
	}
	
	function today_indicator_click(mouse_event) // originally mouse event
	{
		active_date = new Date();
		
		cal.load_month_data(active_date.getFullYear(), (active_date.getMonth() + 1));
	}
	
	function previous_button_click(mouse_event) // originally mouse event
	{
		active_date.setMonth(active_date.getMonth() - 1);
		
		set_button_months();
		
		disable_buttons();
		
		cal.load_month_data(active_date.getFullYear(), active_date.getMonth() + 1);
	}
	
	function next_button_click(mouse_event) // originally mouse event
	{
		active_date.setMonth(active_date.getMonth() + 1);
		
		set_button_months();
		
		disable_buttons();
		
		cal.load_month_data(active_date.getFullYear(), active_date.getMonth() + 1);
	}
	
	function set_button_months()
	{
		var previous_month = (active_date.getMonth() - 1);
		
		if(previous_month < 0) previous_month = 11;
		cal.prevMonth.title = MONTHS[previous_month];
		
		var next_month = (active_date.getMonth() + 1);
		
		if(next_month > 11) next_month = 0;
		cal.nextMonth.title = MONTHS[next_month];
	}
	
	function disable_buttons()
	{
		if(navigator.userAgent.indexOf('IE') != -1)
			cal.prevMonth.detachEvent("onclick",previous_button_click);
		else
			cal.prevMonth.removeEventListener('click', previous_button_click,false);
			
		if(navigator.userAgent.indexOf('IE') != -1)
			cal.nextMonth.detachEvent("onclick",next_button_click);
		else
			cal.nextMonth.removeEventListener('click', next_button_click,false);
	}
	
	function enable_buttons()
	{
		if(navigator.userAgent.indexOf('IE') != -1)
			cal.prevMonth.attachEvent("onclick",previous_button_click);
		else
			cal.prevMonth.addEventListener('click', previous_button_click,false);
			
		if(navigator.userAgent.indexOf('IE') != -1)
			cal.nextMonth.attachEvent("onclick",next_button_click);
		else
			cal.nextMonth.addEventListener('click', next_button_click,false);
	}
	
	function date_button_click(event) // originally mouse event
	{
		var date_button=this; // capture the button they clicked on
		
		if(event.srcElement)
			date_button=event.srcElement;
			
		if(date_button.id)
		{
			var extra = date_button.id; // object
			
			if (extra&&extra.length>1)
			{
				var s=extra.split(',');
				extra = Number(s[0]);
			}
			else
				extra=Number(date_button.id);
			
			if(VARS['admin_mode'])
			{
				try
				{
					var clickedDate=new Date();
					clickedDate.setTime(extra);
					
					var month=clickedDate.getMonth()+1;
					
					var date_string = clickedDate.getFullYear() + '-' + (month<10?'0':'') +month+ '-' + (clickedDate.getDate()<10?'0':'') + clickedDate.getDate();
					window.location.assign('?cmd=editCalendar&id=' + VARS['CALENDAR_ID'] + '&date=' + date_string);
				}
				catch (e) // error
				{
					cal.display_error_dialog('Could not open the specified URL.');
				}
			}
			else
			{	
				
				var date_clicked = CLOSED_DATES[extra]['date']; // date
				
				var clicked_year = date_clicked.getFullYear();
				var clicked_month = date_clicked.getMonth();
				var clicked_date = date_clicked.getDate();
				var clicked_day = date_clicked.getDay();
				
				var date_counter = 0;
				var thisMonthClosedIndexes=date_button.id.split(',');
				var num_closed_dates = thisMonthClosedIndexes.length;
				
				var num_events = 0;
				var contents=document.getElementById('event-frame-content');
				
				cal.remove_all_children_from_object(contents);
				
				for(; date_counter < num_closed_dates; date_counter++)
				{
					var current_closed = CLOSED_DATES[thisMonthClosedIndexes[date_counter]]; // object
					current_closed['type'] = unescape(current_closed['type']);
					
					var newEvent=document.createElement('div');
					newEvent.title='Add this event to your calendar.';
					newEvent.className='cal-event';
					newEvent.id='event-'+String(thisMonthClosedIndexes[date_counter]);
					newEvent.style.color=COLOURS['openTextCol'];
					newEvent.style.textAlign='left';
					newEvent.style.fontSize='8pt';
					newEvent.innerHTML='<span>'+unescape(unescape(CLOSED_DATES[thisMonthClosedIndexes[date_counter]]['title']))+'</span>';
					if(navigator.userAgent.indexOf('IE') != -1)
						newEvent.attachEvent("onclick",event_item_click);
					else
						newEvent.addEventListener('click',event_item_click,false);
					contents.appendChild(newEvent);
						
					num_events++;
				}
				event_title.innerHTML=date_clicked.toDateString();
				$("#event-frame").fadeIn("slow");
				
			}
		}
	}
	
	Calendar.prototype.load_event_type_data=function ()
	{
		var ajax = new AjaxHandler(CALENDAR_EVENT_TYPES_SCRIPT+'&cms_version='+VARS["CMS_VERSION"], 'callback');
		ajax.callback = function(response)
		{
			var et_data = String(response).split("\r\n"); // array from string split
			var i = 0;
			var num_lines = (et_data.length - 1);
			
			for(; i < num_lines; i++)
			{
				var vals = et_data[i].split('='); // array
				var valName=String(vals[0]);
				EVENT_TYPE_COLOURS[valName]= {"title": vals[1], "colour": vals[2]};
			}
		 	cal.draw_date_grid(date_clicked);
		};
		try
		{
			ajax.request();
		}
		catch(security_error) // securoty error
		{
			this.display_error_dialog('Event Type could not be loaded.');
		}
	}
	
	Calendar.prototype.load_month_data=function (year, month) // int, int
	{
		var ajax = new AjaxHandler(CALENDAR_PROCESS_SCRIPT+'&year_to_load='+year+'&month_to_load='+(month)+'&calID='+VARS['CALENDAR_ID']+'&cms_version='+VARS["CMS_VERSION"]+'&format=json', 'callback');
		ajax.callback = function(response)
		{
			if(response.length>4)
			{
				CLOSED_DATES = JSON.parse(response); 
				cal.process_closed_dates(CLOSED_DATES); // referenced processing, converted : separated to date objects
			}
			else
				CLOSED_DATES=null;
		 
			cal.draw_date_grid(active_date);
			enable_buttons();
			requests_locked = false;
		};
		
		if(!requests_locked)
		{
			try
			{
				requests_locked = true;
				ajax.request();
			}
			catch(security_error) // security error
			{
				this.display_error_dialog('Data for the specified month could not be loaded.');
			}
		}
	}
	
	function show_colour_legand() // mainly for mobile devices, which dont have rollover display
	{
		if(event_frame)
		{
			event_title.innerHTML=MONTHS[active_date.getMonth()] + ' ' + active_date.getFullYear();
			
			var textColour=COLOURS['openTextCol'];
			
			if(textColour==COLOURS['backgroundCol'])
				textColour=COLOURS['dayTextCol'];
			
			var indexes=new Array("permHolsCol","openTextCol","dateIndCol","msgCol","event");
			var names=new Array('Weekends/Holidays','Open Days','Today','Meeting','Event');
			var samples='<div class="event-text" style="color:'+textColour+';width:60%;text-align:left;"><span>Legand:</span></div>';
			for(var i=0;i<indexes.length;i++)
			{
				samples+='<div class="sampleHolder" '+(i+1==indexes.length?'style="clear:right;"':'')+'><div class="colourSample" style="background-color:'+COLOURS[indexes[i]]+';'+(i==0?'clear:left;':'')+'"></div><div class="event-text" style="color:'+textColour+';">'+names[i]+'</div></div>';
			}
			
			var contents=document.getElementById('event-frame-content');
			cal.remove_all_children_from_object(contents);
			contents.innerHTML=samples;
			
			if(CLOSED_DATES)
			{
				var date_counter = 0;
				var num_closed_dates = CLOSED_DATES.length;
				
				var num_events = 0;
			
				for(; date_counter < num_closed_dates; date_counter++)
				{
					if(active_date.getMonth()==CLOSED_DATES[date_counter]['date'].getMonth()&&active_date.getFullYear()==CLOSED_DATES[date_counter]['date'].getFullYear())
					{
						var newEvent=document.createElement('div');
						newEvent.title='Add this event to your calendar.';
						newEvent.className='cal-event';
						newEvent.id='event-'+String(date_counter);
						newEvent.style.color=textColour;
						newEvent.style.textAlign='left';
						newEvent.style.fontSize='8pt';
						var thisTitle='';
							
						thisTitle=unescape(unescape(CLOSED_DATES[date_counter]['title']));
						
						if (CLOSED_DATES[date_counter]['description'] != '') thisTitle +=  ':<br/>' + unescape(unescape(CLOSED_DATES[date_counter]['description']));
						if (CLOSED_DATES[date_counter]['location'] != '')  thisTitle += "<br/>" + unescape(unescape(CLOSED_DATES[date_counter]['location']));
						thisTitle +="<br/>"+CLOSED_DATES[date_counter]['date'].toLocaleDateString()+((CLOSED_DATES[date_counter]['startTime'] != '')||(CLOSED_DATES[date_counter]['endTime'] != '')?':<br/>':"");
						if (CLOSED_DATES[date_counter]['startTime'] != '') thisTitle += unescape(unescape(CLOSED_DATES[date_counter]['startTime']));
						if (CLOSED_DATES[date_counter]['endTime'] != '') thisTitle += ' - ' + unescape(unescape(CLOSED_DATES[date_counter]['endTime']));
					
						newEvent.innerHTML='<span>'+thisTitle+'</span>';
						if(navigator.userAgent.indexOf('IE') != -1)
							newEvent.attachEvent("onclick",event_item_click);
						else
							newEvent.addEventListener('click',event_item_click,false);
						contents.appendChild(newEvent);
							
						num_events++;
					}
				}
			}
			
			$("#event-frame").fadeIn("slow");
		}
	}
	
	function event_item_click(event)
	{
		var me=this;
		
		try
		{
			if(this==window)
			{	
				if(event.srcElement)
					me=event.srcElement.parentElement;
			}
			var thisEvent=me.id.replace('event-','');
			if(thisEvent)
			{
				var link=unescape(unescape(CLOSED_DATES[thisEvent]['link']));
				
				//if(link.indexOf('export_calendar_event')!=-1)
				//	window.location.assign('http://'+window.location.host+'/'+link);
				//else
					window.location.assign(link);
			}
		}
		catch(error)
		{
			cal.display_error_dialog('Error occurred trying to load the event link.');
		}
	}
	
	
	function io_error(ioEvent) // IOErrorEvent
	{
		display_error_dialog('IO Error occurred when loading month data.');
	}
	
	Calendar.prototype.remove_all_children_from_object=function(movClip) // object
	{
		movClip.innerHTML='';
	}
	
	
	function isArray(obj) {
		return (obj.constructor == Array);
	}
	
	// if indexOf is missing
	
	if (!Array.prototype.indexOf)
	{
		  Array.prototype.indexOf = function(elt /*, from*/)
		  {
		    var len = this.length;
		
		    var from = Number(arguments[1]) || 0;
		    from = (from < 0)
		         ? Math.ceil(from)
		         : Math.floor(from);
		    if (from < 0)
		      from += len;
		
		    for (; from < len; from++)
		    {
		      if (from in this &&
		          this[from] === elt)
		        return from;
		    }
		    return -1;
	  };
}

function set_col_to_change(col)
{
	col_to_change=col;
}

function change_col(value)
{
	if (COLOURS[col_to_change] != undefined)
	{
		COLOURS[col_to_change] = value;
		if(gridColours.indexOf(col_to_change)!=-1)
			cal.draw_date_grid(date_clicked);
			
		if(topColours.indexOf(col_to_change)!=-1)
		{
			if(col_to_change=='backgroundCol')
			{
				cal.output.style.backgroundColor=COLOURS['backgroundCol'];
				event_frame.style.backgroundColor=COLOURS['backgroundCol'];
				$('.cal-day-space').css(({ 'background-color' : COLOURS['backgroundCol'] }));
			}
			if(col_to_change=='daysCol')
			{
				$('#week-heading .cal-day').css(({ 'color' : COLOURS['daysCol'] }));
			}
			else if(col_to_change=='monthYearCol')
			{
				cal.month_indicator.style.color = COLOURS['monthYearCol'];
			}
			else if(col_to_change=='dateIndCol')
			{
				cal.today.style.color=COLOURS['dateIndCol'];
			}
			else if(col_to_change=='buttonCol')
			{
				cal.nextMonth.style.backgroundColor=COLOURS['buttonCol'];
				cal.prevMonth.style.backgroundColor=COLOURS['buttonCol'];
			}
		}
	}
}

