
var AMcal = {

	// What calendar are we displaying D,W,M,Y
	showing: '',

	// What type of calendar are we displaying (C or J)
	version: 'C',

	current_dt: new Date(),

	// Calendar data
	data: {
		D: null,
		W: null,
		M: null,
		Y: null
	},

	get_first_day: function () {
		var loop = 0;
		var first_obj = null;
		while (first_obj == null && this.data[this.showing] != null && loop < this.data[this.showing].length) {
			first_obj = this.data[this.showing][loop++];
		}
		return first_obj;
	},

	get_last_day: function () {
		var last_obj = null;
		if (this.data[this.showing] == null) {
			return null;
		}
		var loop = this.data[this.showing].length - 1;
		while (last_obj == null && loop >= 0) {
			last_obj = this.data[this.showing][loop--];
		}
		return last_obj;
	},

	// "Previous" button to go to previous period
	prev_button: function (/*btn_obj*/) {
		if (this.data[this.showing] == null) {
			return;
		}
		if (this.get_first_day() == null) {
			return;
		}

		this.transition_hide();

		var oneday = this.get_first_day();
		var startdt = new Date(oneday.cyy, oneday.cmm - 1, oneday.cdd - 1);

		this.get_data(startdt);
		return;
	},

	get_location_params: function () {
		var param = '';
		if (this.location_data.zip != null) {
			param += '&zc=' + this.location_data.zip;
		}
		if (this.location_data.country != null) {
			param += '&coid=' + this.location_data.country;
		}
		if (this.location_data.state != null) {
			param += '&stid=' + this.location_data.state;
		}
		if (this.location_data.city != null) {
			param += '&ctid=' + this.location_data.city;
		}
		if (this.location_data.timezone != null) {
			param += '&tz=' + this.location_data.timezone;
		}
		return param;
	},

	change_date_civil: function (newdt) {
		if (newdt != null) {
			this.transition_hide();
			this.get_data(newdt);
		}
		return;
	},

	change_date_jewish: function (newdt) {
		if (newdt != null) {
			this.transition_hide();
			this.get_data(newdt);
		}
		return;
	},

	get_data: function (startdt) {

		var params = 'v=' + this.version + '&t=' + this.showing;
		var startdt_str = '';
		if (startdt == null) {
			startdt = this.current_dt;
		}
		if (startdt != null) {
			startdt_str = '' + startdt.getFullYear() + '-' + (1 + startdt.getMonth()) + '-' + startdt.getDate();
			params += '&s=' + startdt_str;
		}
		params += this.get_location_params();

		new Ajax.Updater('incoming_cal_data', '/js/calendar_data.php',
			{
				method: 'GET',
				parameters: params,
				evalScripts: true
			}
		);
	},

	// "Next" button to go to previous period
	next_button: function (/*btn_obj*/) {
		if (this.data[this.showing] == null) {
			return;
		}
		if (this.get_first_day() == null) {
			return;
		}

		this.transition_hide();

		var oneday = this.get_last_day();
		var startdt = new Date(oneday.cyy, oneday.cmm - 1, oneday.cdd + 1);

		this.get_data(startdt);
		return;
	},

	import_days: function (cal_type, obj) {

		var loop = 0;

		if (obj != null && obj[0] != null) {
			var from_dt = new Date(obj[0].cyy, obj[0].cmm - 1, obj[0].cdd);
			var to_dt = new Date(obj[obj.length - 1].cyy, obj[obj.length - 1].cmm - 1, obj[obj.length - 1].cdd);
			if (this.current_dt < from_dt || this.current_dt > to_dt) {
				this.current_dt = from_dt;
			}
		}

		switch (cal_type) {
			case 'D': case 'W':
				this.data[cal_type] = obj;
				break;
			case 'M': // Calendar must start on a Sunday
				var first_dow = obj[0]['dow'];
				for (loop = 0; loop < first_dow; loop++) {
					obj.unshift(null);
				}
				while (obj.length < 42) {
					obj.push(null);
				}
				this.data[cal_type] = obj;
				break;
			case 'Y': // Add spacers for blanks at bottom of year calendar, adjust for jewish vs civil
				var last_month = 1;
				var day_obj = null;
				var new_list = $A();
				var day_count = 0;
				var current_month = 1;
				var is_jleap = (this.version == 'C' ? false : (obj.length > 355 ? true : false));

				while (obj.length > 0) {
					day_obj = obj.shift();
					current_month = (this.version == 'C' ? day_obj.cmm : day_obj.jmm);

					if (last_month != current_month) {
						while (day_count < 31) {
							new_list.push(null);
							day_count ++;
						}

						last_month = (this.version == 'C' ? day_obj.cmm : day_obj.jmm);
						day_count = 1;
					}
					else {
						day_count ++;
					}

					new_list.push(day_obj);
				}

				this.data[cal_type] = new_list;

				if (is_jleap) {
					while (this.data[cal_type].length < 403) {
						this.data[cal_type].push(null);
					}
				}
				else {
					while (this.data[cal_type].length < 372) {
						this.data[cal_type].push(null);
					}
				}

				break;
		}

		AMcal.render();
		this.transition_show();
	},

	transition_hide: function () {
		var cal_prefix = this.get_prefix();
		$(cal_prefix+'prev').style.visibility = 'hidden';
		$(cal_prefix+'next').style.visibility = 'hidden';
		$('cal_layout_'+this.showing+'_loading_div').style.visibility = 'visible';
		$('cal_layout_'+this.showing).addClassName('cal_opaque');
	},

	transition_show: function () {
		var cal_prefix = this.get_prefix();
		$(cal_prefix+'prev').style.visibility = 'visible';
		$(cal_prefix+'next').style.visibility = 'visible';
		$('cal_layout_'+this.showing+'_loading_div').style.visibility = 'hidden';
		$('cal_layout_'+this.showing).removeClassName('cal_opaque');
	},

	set_civil: function () {
		this.version = 'C';
		this.change(this.showing);
	},

	set_jewish: function () {
		this.version = 'J';
		this.change(this.showing);
	},

	// Switch to a different panel
	change: function (cal_type) {

		this.showing = cal_type;
		this.set_tab();

    	$('cal_layout_D_div').hide();
	    $('cal_layout_W_div').hide();
    	$('cal_layout_M_div').hide();
	    $('cal_layout_Y_div').hide();

	    this.transition_hide();

	    this.get_data();
    	$('cal_layout_'+cal_type+'_div').style.display = 'block';
	    fixfooter();

	    return;
	},

	set_tab: function() {

		switch (this.showing) {
		case 'D':
			$('cal_image_D').src = 'images/calendar/day_active.gif';
			$('cal_image_W').src = 'images/calendar/week_inactive.gif';
			$('cal_image_M').src = 'images/calendar/month_inactive.gif';
			$('cal_image_Y').src = 'images/calendar/year_inactive.gif';
			break;
		case 'W':
			$('cal_image_D').src = 'images/calendar/day_inactive.gif';
			$('cal_image_W').src = 'images/calendar/week_active.gif';
			$('cal_image_M').src = 'images/calendar/month_inactive.gif';
			$('cal_image_Y').src = 'images/calendar/year_inactive.gif';
			break;
		case 'Y':
			$('cal_image_D').src = 'images/calendar/day_inactive.gif';
			$('cal_image_W').src = 'images/calendar/week_inactive.gif';
			$('cal_image_M').src = 'images/calendar/month_inactive.gif';
			$('cal_image_Y').src = 'images/calendar/year_active.gif';
			break;
		default:
			$('cal_image_D').src = 'images/calendar/day_inactive.gif';
			$('cal_image_W').src = 'images/calendar/week_inactive.gif';
			$('cal_image_M').src = 'images/calendar/month_active.gif';
			$('cal_image_Y').src = 'images/calendar/year_inactive.gif';
			break;
		}

		return ;
	},

	get_prefix: function () {
		var cal_prefix = '';
		switch (this.showing) {
			case 'D':
				cal_prefix = 'cd_'; // cal_day_
				break;
			case 'W':
				cal_prefix = 'cw_'; // cal_week_
				break;
			case 'M':
				cal_prefix = 'cm_'; // cal_month_
				break;
			case 'Y':
				cal_prefix = 'cy_'; // cal_year_
				break;
		}
		return cal_prefix;
	},
//				window.setTimeout("Event.stopObserving('" + tmp0.id + "_hc', 'click', this.showTagObserver);
//

	// cw_d_0_hc
	observer_list: {},

	//

	// Set observe for the week cell to switch to Day page with proper date
	set_week_click: function (div_id, oneday) {
		var cmd =
			"AMcal.clear_week_click('" + div_id + "'); " +
			"AMcal.observer_list." + div_id + " = function(event){ " +
				"AMcal.current_dt = new Date(" + oneday.cyy + ", " + oneday.cmm + " - 1, " + oneday.cdd + "); " +
				"AMcal.change('D'); " +
			"};" +
			"Event.observe('" + div_id + "', 'click', AMcal.observer_list." + div_id + "); ";

		window.setTimeout(cmd, 250);
	},
	clear_week_click: function (div_id) {
		if (this.observer_list[div_id] == null) {
			return;
		}
		Event.stopObserving(div_id, 'click', this.observer_list[div_id]);
		return;
	},

	// Show calendar data on current panel
	render: function () {

		if (this.data[this.showing] == null) {
			return false;
		}

		if (this.showing == 'M' || this.showing == 'Y') {
			return this.render_month();
		}

		var cal_prefix = this.get_prefix();
		if (cal_prefix == '')
			return false;

		// Construct and display page title

		var first_obj = this.get_first_day();
		var last_obj = this.get_last_day();

		var page_title_c = this.construct_date_range_title_c(this.showing, this.version, first_obj, last_obj);
		var page_title_j = this.construct_date_range_title_j(this.showing, this.version, first_obj, last_obj);

		if (this.version == 'J') {
			$(cal_prefix+'page_title').innerHTML = page_title_j + ' / ' + page_title_c;
		} else {
			$(cal_prefix+'page_title').innerHTML = page_title_c + ' / ' + page_title_j;
		}

		// Loop through dates and push data into each day
		var oneday = null;
		var show_flag = false;
		var loop = null;
		var tmp0 = null;
		var tmp1 = null;
		var tmp2 = null;
		var tmp_cls = null;

		for (loop = 0; loop < this.data[this.showing].length; loop++) {
			oneday = this.data[this.showing][loop];
			show_flag = false;
			tmp0 = $(cal_prefix+'d_'+loop);

			// If no data for this day (or non-existent day [month/year calendar]) then hide day
			if (oneday == null) {
				if (tmp0 != null) {
					tmp0.hide();
					if ((tmp1 = $(cal_prefix+'d_'+loop+'_h')) != null) {
						tmp1.show();
					}
				}
				continue;
			}

			// Check title/date on this day
			if (this.showing == 'W') {
				this.set_week_click(tmp0.id + '_hc', oneday);
				if ((tmp1 = $(cal_prefix+'d_'+loop+'_title_j')) != null) {
					tmp1.innerHTML = oneday.jdd + '&nbsp;&nbsp;' + oneday.jmm_name;
				}
				// Event.observe('cw_d_0', 'click', function(event){ AMcal.change('D'); AMcal.get_data(new Date(2008,8,3)); });
				if ((tmp1 = $(cal_prefix+'d_'+loop+'_title_c')) != null) {
					tmp1.innerHTML = oneday.dow_name + ',&nbsp;&nbsp;' + oneday.cmm_name + '&nbsp;&nbsp;' + oneday.cdd;
				}
			}

			if ((tmp2 = $(cal_prefix+'d_'+loop+'_omer_d')) != null) {
				if (oneday['omer_d'] != null) {
					show_flag = true;
					tmp2.innerHTML = 'Omer ' + oneday['omer_d'];
				} else {
					tmp2.innerHTML = '';
				}
			}

			if ((tmp2 = $(cal_prefix+'d_'+loop+'_omer_n')) != null) {
				if (oneday['omer_n'] != null) {
					show_flag = true;
					tmp2.innerHTML = 'Omer ' + oneday['omer_n'];
				} else {
					tmp2.innerHTML = '';
				}
			}

			// Check events on this day
			if ((tmp2 = $(cal_prefix+'d_'+loop+'_events_titles')) != null) {
				if (oneday['el'] != null) {
					show_flag = true;

					// Populate events into the HTML
					var event_text = '';
					var event_loop = 0;
					for (event_loop = 0; event_loop < oneday['el'].length; event_loop++) {
						event_text += '<h1>' + oneday['el'][event_loop].jyy + ' [' + (oneday['el'][event_loop].cmm_name != null ? oneday['el'][event_loop].cmm_name + ' ' + oneday['el'][event_loop].cdd + ', ' : '') + (oneday['el'][event_loop].cyy < 0 ? (0 - oneday['el'][event_loop].cyy) + 'BCE' : oneday['el'][event_loop].cyy) + '] - ' + oneday['el'][event_loop].title + '</h1>';
					}
					tmp2.innerHTML = event_text;
				} else {
					tmp2.innerHTML = '';
				}
			}

			if ((tmp2 = $(cal_prefix+'d_'+loop+'_events_text')) != null) {
				if (oneday['el'] != null && this.showing == 'D') {
					show_flag = true;

					var event_text = '';
					var event_loop = 0;
					for (event_loop = 0; event_loop < oneday['el'].length; event_loop++) {
						event_text += '<h1>' + oneday['el'][event_loop].jyy + ' [' + (oneday['el'][event_loop].cmm_name != null ? oneday['el'][event_loop].cmm_name + ' ' + oneday['el'][event_loop].cdd + ', ' : '') + (oneday['el'][event_loop].cyy < 0 ? (0 - oneday['el'][event_loop].cyy) + 'BCE' : oneday['el'][event_loop].cyy) + '] - ' + oneday['el'][event_loop].title + '</h1>';
						if (oneday['el'][event_loop].desc != null &&  oneday['el'][event_loop].desc != '') {
							event_text += '<h2>' + oneday['el'][event_loop].desc + '</h2>';
						}
					}
					tmp2.innerHTML = event_text;
				} else {
					tmp2.innerHTML = '';
				}
			}

			// Check holiday class changes
			if ((tmp1 = $(cal_prefix+'d_'+loop+'_hc')) != null) {
				tmp2 = $('cal_layout_' + this.showing + '_div');

				if (typeof tmp2 == 'object') {
					if ((tmp_cls = tmp2.getAttribute('hmc')) != null) {
						tmp1.removeClassName(tmp_cls);
						if (oneday.holtype == 'M') {
							tmp1.addClassName(tmp_cls);
						}
					}

					if ((tmp_cls = tmp2.getAttribute('hnc')) != null) {
						tmp1.removeClassName(tmp_cls);
						if (oneday.holtype == 'N') {
							tmp1.addClassName(tmp_cls);
						}
					}
				}
			}

			// Check holidays on this day
			/*if ((tmp2 = $(cal_prefix+'d_'+loop+'_holiday_img')) != null) {
				tmp2.style.display = 'none';
			}*/

			// Populate holidays into the HTML
			if ((tmp1 = $(cal_prefix+'d_'+loop+'_hol_m_text')) != null) {
				if (oneday.holtype != null && oneday.holtype == 'M') {
					if (oneday.title != null) {
						show_flag = true;

						tmp1.innerHTML = oneday.title;
						tmp1.show();
					} else {
						tmp1.innerHTML = '';
						tmp1.hide();
					}
				}
				else {
					tmp1.innerHTML = '';
					tmp1.hide();
				}
			}

				/*if ((tmp2 = $(cal_prefix+'d_'+loop+'_holiday_img')) != null) {
					tmp2.innerHTML = '<img onError="this.style.display = \'none\';" src="/images/calendar/holiday/' + oneday['title'].replace(/ /g, '') + '.gif" align="right" alt=""/>';
					tmp2.style.display = 'block';
				}*/

			// Populate holidays into the HTML
			if ((tmp1 = $(cal_prefix+'d_'+loop+'_hol_n_text')) != null) {
				if (oneday.holtype != null && oneday.holtype == 'N') {
					if (oneday.title != null) {
						show_flag = true;

						tmp1.innerHTML = oneday.title;
						tmp1.show();
					} else {
						tmp1.innerHTML = '';
						tmp1.hide();
					}
				} else {
					tmp1.innerHTML = '';
					tmp1.hide();
				}
			}
				/*if ((tmp2 = $(cal_prefix+'d_'+loop+'_holiday_img')) != null) {
					tmp2.innerHTML = '<img onError="this.style.display = \'none\';" src="/images/calendar/holiday/' + oneday['title'].replace(/ /g, '') + '.gif" align="right" alt=""/>';
					tmp2.style.display = 'block';
				}*/

			// Candle lighting time?
			if ((tmp1 = $(cal_prefix+'d_'+loop+'_candlelighting')) != null) {
				if ((tmp2 = $(cal_prefix+'d_'+loop+'_clt')) != null) {
					if (oneday.cltm != null && oneday.cltm != '') {
						show_flag = true;
						tmp2.innerHTML = oneday.cltm;
						tmp1.show();
					} else {
						tmp1.hide();
					}
				}
			}

			// Havdalah time?
			if ((tmp1 = $(cal_prefix+'d_'+loop+'_havdalah')) != null) {
				if ((tmp2 = $(cal_prefix+'d_'+loop+'_hvt')) != null) {
					if (oneday.hvtm != null && oneday.hvtm != '') {
						show_flag = true;
						tmp2.innerHTML = oneday.hvtm;
						tmp1.show();
					}
					else {
						tmp1.hide();
					}
				}
			}

			// Special image/HTML?  For chanukah candle days?
			// Show day if any content added, otherwise hide content?
			if (tmp0 != null) {
				if (show_flag) {
					tmp0.show();
					if ((tmp1 = $(cal_prefix+'d_'+loop+'_h')) != null) {
						tmp1.hide();
					}
				} else {
					tmp0.hide();
					if ((tmp1 = $(cal_prefix+'d_'+loop+'_h')) != null) {
						tmp1.show();
					}
				}
			}
		}
		fixfooter();
	},

	// Show calendar data on current panel
	render_month: function () {

		if (this.data[this.showing] == null) {
			return false;
		}

		var cal_prefix = this.get_prefix();
		if (cal_prefix == '')
			return false;

		// Construct and display page title

		var first_obj = this.get_first_day();
		var last_obj = this.get_last_day();

		var page_title_c = this.construct_date_range_title_c(this.showing, this.version, first_obj, last_obj);
		var page_title_j = this.construct_date_range_title_j(this.showing, this.version, first_obj, last_obj);

		if (this.version == 'J') {
			$(cal_prefix+'page_title').innerHTML = page_title_j + ' / ' + page_title_c;
		} else {
			$(cal_prefix+'page_title').innerHTML = page_title_c + ' / ' + page_title_j;
		}

		// Loop through dates and push data into each day
		var oneday = null;
		var show_flag = false;
		var loop = null;
		var tmp0 = null;
		var tmp1 = null;
		var tmp2 = null;

		if (this.showing == 'M') {
			// Clear cells
			for (loop = 0; loop < this.data[this.showing].length; loop++) {
				oneday = this.data[this.showing][loop];
				tmp0 = $(cal_prefix+'d_'+loop);

				if (typeof tmp0 == 'object') {
					tmp0.innerHTML = '';
				}
			}

			// Hide / show row
			var row_tmp = null;
			for (var i = 0; i < 6; i++) {
				row_tmp = $('cm_row_' + i);
				if (typeof row_tmp == 'object') {
					oneday = this.data[this.showing][i * 7];
					if (i > 0 && oneday == null) {
						row_tmp.hide();
					}
					else {
						row_tmp.show();
					}
				}
			}
		}

		var is_jleap = false;
		var pos = 0;
		var month = 0;
		var current_month = 0;

		if (this.showing == 'Y' && this.version == 'J' && this.data[this.showing].length > 372) {
			is_jleap = true; // leap Jewish
		}

		if (this.showing == 'Y') {
			//this.set_cell_width(is_jleap);
			this.set_cols(is_jleap);
			this.set_title(is_jleap);
			this.set_row();
		}

		for (loop = 0; loop < this.data[this.showing].length; loop++) {
			oneday = this.data[this.showing][loop];
			pos = loop;

			if (this.showing == 'Y') {
				if (oneday != null) {
					current_month = (this.version == 'C' ? oneday.cmm : oneday.jmm);
				}

				if (oneday != null && month != current_month) {
					month = current_month;
				}

				if (!is_jleap && month > 11) {
					pos = loop + 31;
				}
			}

			show_flag = false;
			tmp0 = $(cal_prefix+'d_'+pos);
			tmp0.hide();

			// If no data for this day (or non-existent day [month/year calendar]) then hide day
			if (oneday == null) {
				if (tmp0 != null) {
					tmp0.hide();
					if ((tmp1 = $(cal_prefix+'d_'+pos+'_h')) != null) {
						//tmp1.show();
						tmp1.style.display = 'block';
					}
				}
				continue;
			}
			show_flag = true;

			var html_full = '';
			switch (this.showing) {
				case 'M':
					html_full = '<div class="ms8 %%hmc%%%%hnc%%" id="holiday_class" onClick="AMcal.change(\'D\'); AMcal.get_data(new Date(%%title_year_c%%, %%title_month_c%% - 1, %%title_day_c%%));">'
								+ '<div>'
					if (this.version == 'C') {
						html_full += '<div style="float: right;">%%cell_day%%&nbsp;%%cell_monthname%%</div>'
					}
					else {
						html_full += '<div style="float: right;">%%cell_monthname%%&nbsp;%%cell_day%%</div>'
					}
					html_full += '<b>%%cell_date%%</b>'
								+ '</div>';
					html_full += '<div style="width: 97px; height: 35px; padding-top: 3px;">'

					if (oneday['holtype'] != null) {
						html_full += '<div style="text-align: center;">'
					   				+ '%%holiday_img%%'
									+ '<div>%%hol_m_text%%</div>'
									+ '<div>%%hol_n_text%%</div>'
									+ '</div>'
					}
					if (oneday.omer_d != null) {
						html_full += '<div class="month_omer_d">Omer ' + oneday.omer_d + '</div>';
					}
					html_full += '</div>';

					if (oneday.cltm != null) {
						html_full += '<div class="month_candles" %%candlelighting_display%%>'
									+ '<table border="0" cellpadding="0" cellspacing="0" width="97">'
									+ '<tbody>'
									+ '<tr>'
									+ '<td width="8"><img src="images/calendar/img_c01.png" alt="" height="18" width="8"></td>'
									+ '<td class="ms6" width="13"><img src="images/calendar/img_c04.png" alt="" height="14" width="13"></td>'
									+ '<td class="ms6" align="center">%%candlelighting_time%%</td>'
									+ '<td width="8"><img src="images/calendar/img_c02.png" alt="" height="18" width="8"></td>'
									+ '</tr></tbody></table></div>';
					}

					if (oneday.hvtm != null) {
				   		html_full += '<div class="month_havdalah" %%havdalah_display%%>'
				   					+ '<table border="0" cellpadding="0" cellspacing="0" width="97">'
				   					+ '<tbody><tr>'
				   					+ '<td width="8"><img src="images/calendar/img_c01.png" alt="" height="18" width="8"></td>'
				   					+ '<td class="ms6" width="18"><img src="images/calendar/img_c_stars.png" alt="" height="15" width="18"></td>'
				   					+ '<td class="ms6" align="center">%%havdalah_time%%</td>'
				   					+ '<td width="8"><img src="images/calendar/img_c02.png" alt="" height="18" width="8"></td>'
				   					+ '</tr></tbody></table></div>';
				   	}

					if (oneday.omer_n != null) {
						html_full += '<div class="month_omer_n">Omer ' + oneday.omer_n + '</div>';
					}

					html_full += '</div>';
					break;
				case 'Y':
						html_full = '<table class="cy_hvcl %%hmc%%%%hnc%%"><tr><td onClick="AMcal.change(\'D\'); AMcal.get_data(new Date(%%title_year_c%%, %%title_month_c%% - 1, %%title_day_c%%));">';

						if (oneday.cltm != null || oneday.hvtm != null) {
							if (oneday.cltm != null) {
								html_full += '<div class="year_layover" %%candlelighting_display%%><img src="images/calendar/img_ico.png" width="15" height="15" alt="" style="margin-left: 4px; float: left;"><span class="year_candlelighting">%%candlelighting_time%%</span></div>';
							}

							if (oneday.hvtm != null) {
								html_full += '<div class="year_layover" %%havdalah_display%%><img src="images/calendar/img_g_stars.png" width="15" height="15" alt="" style="margin-left: 4px;  float: left;"><span class="year_havdalah">%%havdalah_time%%</span></div>';
							}
						}
						else {
							if (this.version == 'C') {
								html_full += '%%cell_day%%&nbsp;%%cell_monthname%%';
							}
							else {
								html_full += '%%cell_monthname%%&nbsp;%%cell_day%%';
							}
						}

						html_full += '</td></tr></table>';
					break;
			}

			// Check title/date on this day

			html_full = html_full.replace(/%%title_year%%/g, oneday[this.version.toLowerCase() + "yy"]);
			html_full = html_full.replace(/%%title_year_c%%/g, oneday["cyy"]);
			html_full = html_full.replace(/%%title_year_j%%/g, oneday["jyy"]);
			html_full = html_full.replace(/%%title_month%%/g, oneday[this.version.toLowerCase() + "mm"]);
			html_full = html_full.replace(/%%title_month_c%%/g, oneday["cmm"]);
			html_full = html_full.replace(/%%title_month_j%%/g, oneday["jmm"]);
			html_full = html_full.replace(/%%title_monthname%%/g, oneday[this.version.toLowerCase() + "mm_name"]);
			html_full = html_full.replace(/%%title_monthname_c%%/g, oneday["cmm_name"]);
			html_full = html_full.replace(/%%title_downame%%/g, oneday["dow_name"]);
			html_full = html_full.replace(/%%title_day%%/g, oneday[this.version.toLowerCase() + "dd"]);
			html_full = html_full.replace(/%%title_day_c%%/g, oneday["cdd"]);

			html_full = html_full.replace(/%%cell_monthname%%/g, (this.version == 'C' ? oneday["jmm_name"] : (this.showing == 'Y' ? oneday["cmm_name"].substr(0, 3) : oneday["cmm_name"])));
			html_full = html_full.replace(/%%cell_day%%/g, (this.version == 'C' ? oneday["jdd"] : oneday["cdd"]));
			html_full = html_full.replace(/%%cell_date%%/g, oneday[this.version.toLowerCase() + "dd"]);
/*
			// Check events on this day
			if (oneday['el'] != null) {
				show_flag = true;

				// Populate events into the HTML
				var event_text = '';
				var event_loop = 0;
				for (event_loop = 0; event_loop < oneday['el'].length; event_loop++) {
					event_text += '<h1>' + oneday['el'][event_loop].jyy + ' [' + (oneday['el'][event_loop].cmm_name != null ? oneday['el'][event_loop].cmm_name + ' ' + oneday['el'][event_loop].cdd + ', ' : '') + (oneday['el'][event_loop].cyy < 0 ? (0 - oneday['el'][event_loop].cyy) + 'BCE' : oneday['el'][event_loop].cyy) + '] - ' + oneday['el'][event_loop].title + '</h1>';
				}
				html_full = html_full.replace(/%%events_titles%%/g, event_text);

				event_text = '';
				event_loop = 0;
				for (event_loop = 0; event_loop < oneday['el'].length; event_loop++) {
					event_text += '<h1>' + oneday['el'][event_loop].jyy + ' [' + (oneday['el'][event_loop].cmm_name != null ? oneday['el'][event_loop].cmm_name + ' ' + oneday['el'][event_loop].cdd + ', ' : '') + (oneday['el'][event_loop].cyy < 0 ? (0 - oneday['el'][event_loop].cyy) + 'BCE' : oneday['el'][event_loop].cyy) + '] - ' + oneday['el'][event_loop].title + '</h1>';
					if (oneday['el'][event_loop].desc != null &&  oneday['el'][event_loop].desc != '') {
						event_text += '<h2>' + oneday['el'][event_loop].desc + '</h2>';
					}
				}
				html_full = html_full.replace(/%%events_text%%/g, event_text);
			}
*/
			// Check holiday class changes

			if (oneday['holtype'] == 'M') {
				html_full = html_full.replace(/%%hmc%%/g, 'hm_bg');
			} else {
				html_full = html_full.replace(/%%hmc%%/g, '');
			}

			if (oneday['holtype'] == 'N') {
				html_full = html_full.replace(/%%hnc%%/g, 'hn_bg');
			} else {
				html_full = html_full.replace(/%%hnc%%/g, '');
			}

			// Check holidays on this day

			if (oneday['holtype'] != null) {
				show_flag = true;
				html_full = html_full.replace(/%%holiday_img%%/g, '<img style="display: none;" onLoad="this.style.display = \'block\';" src="images/calendar/holiday/' + oneday['title'].replace(/ /g, '') + '.gif" align="right" alt=""/>');
				if (oneday['holtype'] == 'M') {
					html_full = html_full.replace(/%%hol_m_text%%/g, oneday['title']);
					html_full = html_full.replace(/%%hol_n_text%%/g, '');
				} else {
					html_full = html_full.replace(/%%hol_n_text%%/g, oneday['title']);
					html_full = html_full.replace(/%%hol_m_text%%/g, '');
				}
			} else {
				html_full = html_full.replace(/%%holiday_img%%/g, '');
				html_full = html_full.replace(/%%hol_m_text%%/g, '');
				html_full = html_full.replace(/%%hol_n_text%%/g, '');
			}

			// Candle lighting time?
			if (oneday['cltm'] != null && oneday['cltm'] != '') {
				show_flag = true;
				html_full = html_full.replace(/%%candlelighting_time%%/g, oneday['cltm']);
				html_full = html_full.replace(/%%candlelighting_display%%/g, 'style="display: block;"');
			} else {
				html_full = html_full.replace(/%%candlelighting_time%%/g, '');
				html_full = html_full.replace(/%%candlelighting_display%%/g, 'style="display: none;"');
			}

			// Havdalah time?

			if (oneday['hvtm'] != null && oneday['hvtm'] != '') {
				show_flag = true;
				html_full = html_full.replace(/%%havdalah_time%%/g, oneday['hvtm']);
				html_full = html_full.replace(/%%havdalah_display%%/g, 'style="display: block;"');
			} else {
				html_full = html_full.replace(/%%havdalah_time%%/g, '');
				html_full = html_full.replace(/%%havdalah_display%%/g, 'style="display: none;"');
			}

			// Special image/HTML?  For chanukah candle days?

			// Show day if any content added, otherwise hide content?

			if (tmp0 != null) {
				tmp0.innerHTML = html_full;
				if (show_flag) {
					tmp0.show();
					if ((tmp1 = $(cal_prefix+'d_'+pos+'_h')) != null) {
						tmp1.hide();
					}
				} else {
					tmp0.hide();
					if ((tmp1 = $(cal_prefix+'d_'+pos+'_h')) != null) {
						tmp1.show();
					}
				}
			}

		}
		fixfooter();
	},

	construct_date_range_title_c: function (cal_type, cal_version, start_dt, end_dt) {
		var page_title = '';

		if (cal_type == 'D') {
			return start_dt["cmm_name"] + ' ' + start_dt["cdd"] + ', ' + start_dt["cyy"];
		}

		if (cal_type == 'M' && this.version == 'C') {
			return start_dt["cmm_name"] + ', ' + start_dt["cyy"];
		}

		if (cal_type == 'Y' && this.version == 'C') {
			return start_dt["cyy"];
		}

		page_title  = start_dt["cmm_name"];
		page_title += ' ';
		page_title += start_dt["cdd"];

		if (start_dt["cmm"] == end_dt["cmm"] && start_dt["cyy"] == end_dt["cyy"]) {
			page_title += ' - ' + end_dt["cdd"] + ', ' + end_dt["cyy"];
			return page_title;
		}

		if (start_dt["cyy"] != end_dt["cyy"]) {
			page_title += ', ' + start_dt["cyy"];
		}

		page_title += ' &mdash; ';
		page_title += end_dt["cmm_name"] + ' ' + end_dt["cdd"] + ', ' + end_dt["cyy"];

		return page_title;
	},

	construct_date_range_title_j: function (cal_type, cal_version, start_dt, end_dt) {
		var page_title = '';

		if (cal_type == 'D') {
			return start_dt["jdd"] + ' ' + start_dt["jmm_name"] + ' ' + start_dt["jyy"];
		}

		if (cal_type == 'M' && this.version == 'J') {
			return start_dt["jmm_name"] + ' ' + start_dt["jyy"];
		}

		if (cal_type == 'Y' && this.version == 'J') {
			return start_dt["jyy"];
		}

		page_title  = start_dt["jdd"];
		if (start_dt["jmm"] == end_dt["jmm"] && start_dt["jyy"] == end_dt["jyy"]) {
			page_title += ' - ' + end_dt["jdd"] + ' ' + end_dt["jmm_name"] + ' ' + end_dt["jyy"];
			return page_title;
		}

		page_title += ' ';
		page_title += start_dt["jmm_name"];

		if (start_dt["jyy"] != end_dt["jyy"]) {
			page_title += ' ' + start_dt["jyy"];
		}

		page_title += ' - ';
		page_title += end_dt["jdd"] + ' ' + end_dt["jmm_name"] + ' ' + end_dt["jyy"];

		return page_title;
	},

	location_data: {
		zip: null,
		country: null,
		state: null,
		city: null,
		timezone: null
	},

	show_location: function () {
		$('locationData').show();
		$('cal_location').innerHTML = '';
		fixfooter();
	},

	hide_location: function () {
		$('locationData').hide();
		$('cal_location').innerHTML = '[ <a href="#" onclick="AMcal.show_location(); return false;">Set Location</a> ]';
		this.set_location_name();
		fixfooter();
	},

	set_location: function (zip_code, country_cd, state_cd, city_cd, timezone) {
		this.hide_location();
		this.location_data.zip = zip_code;
		this.location_data.country = country_cd;
		this.location_data.state = state_cd;
		this.location_data.city = city_cd;
		this.location_data.timezone = timezone;

	    this.transition_hide();

		this.get_data();
		this.set_location_name();
	    fixfooter();
	},

	set_location_name: function () {

		var params = this.get_location_params();

		new Ajax.Updater('cal_location_name', '/js/calendar_location.php',
			{
				method: 'GET',
				parameters: params,
				evalScripts: true
			}
		);
	},

	set_cols: function (is_leap) {
		var pos = 341;
		var tmp = null;
		var cal_prefix = this.get_prefix();

		while (pos < 372) {
			tmp = $(cal_prefix + 'd_' + pos + '_hc');

			if (typeof tmp == 'object') {
				if (is_leap) {
					tmp.style.display = 'block';
				}
				else {
					tmp.style.display = 'none';
				}
			}
			pos ++;
		}

		tmp = $('cm_title_12');

		if (typeof tmp == 'object') {
			if (is_leap) {
				tmp.style.display = 'block';
			}
			else {
				tmp.style.display = 'none';
			}
		}
	},

	set_row: function() {
		var cal_prefix = this.get_prefix();
		var tmp = $(cal_prefix + 'row_30');

		if (typeof tmp == 'object') {
			if (this.version == 'J') {
				tmp.hide();
			}
			else {
				tmp.show();
			}
		}
	},

	set_title: function (is_leap) {
		var civil = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', '', 'dec'];
		var jewish_l = ['tishrei', 'cheshvan', 'kislev', 'tevet', 'shevat', 'adar I', 'adar II', 'nisan', 'iyar', 'sivan', 'tamuz', 'av', 'elul'];
		var jewish = ['tishrei', 'cheshvan', 'kislev', 'tevet', 'shevat', 'adar', 'nisan', 'iyar', 'sivan', 'tamuz', 'av', '', 'elul'];
		var tmp = null;

		for (var i = 0; i < 13; i++) {
			tmp = $('cm_title_' + (i + 1));

			if (typeof tmp == 'object') {
				if (this.version == 'C') {
					tmp.innerHTML = civil[i];
				}
				else {
					if (is_leap) {
						tmp.innerHTML = jewish_l[i];
					}
					else {
						tmp.innerHTML = jewish[i];
					}
				}
			}
		}
	},

	set_cell_width: function (is_jleap) {
		var width = '';
		var width_j = '';
		var cell = null;

		if (is_jleap) {
			width = '59px';
			width_j = '62px';
		}
		else {
			width = '64px';
			width_j = '67px';
		}

		for (var i = 0; i < 403; i++) {
			cell = $('cy_d_' + i + '_hc');

			if (typeof cell == 'object') {
				if (i < 372) {
					cell.style.width = width;
				}
				else {
					cell.style.width = width_j;
				}
			}
		}

		for (var i = 1; i < 13; i++) {
			cell = $('cm_title_' + i);
			if (typeof cell == 'object') {
				cell.style.width = width;
			}
		}

		cell = $('cm_title_13');
		if (typeof cell == 'object') {
			cell.style.width = width_j;
		}
	}
};
