newjax.calendar = function() {

	return {

		name: 'calendar',
		current_page: 'view',
		actions_page: 'index.php',
		size: 'big',
		weeks:4,
		

		show_holidays: 0,
		show_menus: 0,
		show_orders: 0,

		this_week:1,
		this_month: 0,
		this_year: 0,

		user_week: 1,
		user_month: 0,
		user_year: 0,
		user_school: '',
		date_field: '',

		dates:[],
		open_menu: '',
		loaded:0,



		
		do_form: function() {
		
			var data = '';
		
			data += 'action=view';
			if (data) data += '&';
			data += $('cal_form').serialize();
		
			newjax.update('calendar_container_div', newjax.calendar.actions_page, data);
		
		},

		current: function() {

			this.user_month = this.this_month;
			this.user_year = this.this_year;
			this.user_week = this.this_week;
		
			$('calendar_container_div').innerHTML = newjax.loading_div;

			newjax.update('calendar_container_div', newjax.calendar.actions_page, 
			{ calendar_action:'view', week:this.user_week, month:this.this_month, year:this.this_year, size:this.size, school:this.user_school, show_holidays:this.show_holidays, show_menus:this.show_menus, show_orders:this.show_orders },
function(t) {

	newjax.orders.update_total();

});
		
		},
		
		
		
		next: function(type) {
		
		
			if (newjax.calendar.size == 'week') 
				newjax.calendar.weeks = $('weeks_div').innerHTML;

			if (!this.user_month) this.user_month = this.this_month;
			if (!this.user_year) this.user_year = this.this_year;
			if (!this.user_week) this.user_week = this.this_week;

			if (this.user_week == this.weeks) this.user_week = 0;

			if (type == 'month') {

				if (this.user_month == 12) {
				
					this.user_month = 1;
					this.user_year++;
				
				} else {
				
					this.user_month++;
					v = this.user_month;
				
				}

			} else if (type == 'week') {

				this.user_week++;

			} else {
				
				this.user_year++;
			
			}
		
			$('calendar_container_div').innerHTML = newjax.loading_div;
		
			newjax.update('calendar_container_div', newjax.calendar.actions_page, 
				{ calendar_action:'view', week:this.user_week, month:this.user_month, year:this.user_year, size:this.size, school:this.user_school, show_holidays:this.show_holidays, show_menus:this.show_menus, show_orders:this.show_orders },
				function(t) {
					newjax.orders.update_total();
				}
			);
		
		
		
		},
		
		previous: function(type) {


			if (newjax.calendar.size == 'week') 
				newjax.calendar.weeks = $('weeks_div').innerHTML;
		

			if (!this.user_month) this.user_month = this.this_month;
			if (!this.user_year) this.user_year = this.this_year;
			if (!this.user_week) this.user_week = this.this_week;


			if (this.user_week == 1) this.user_week = (parseInt(this.weeks) + 1);

			if (type == 'month') {

				if (this.user_month == 1) {
				
					this.user_month = 12;
					this.user_year--;
				
				} else {
				
					this.user_month--;
				
				}

			} else if (type == 'week') {
				
				this.user_week--;
	
			} else {
				
				this.user_year--;
				
			}
		
			$('calendar_container_div').innerHTML = newjax.loading_div;
		
			newjax.update('calendar_container_div', newjax.calendar.actions_page, 
				{ calendar_action:'view', week:this.user_week, month:this.user_month, year:this.user_year, size:this.size, school:this.user_school, show_holidays:this.show_holidays, show_menus:this.show_menus, show_orders:this.show_orders },
				function(t) {
				
					newjax.orders.update_total();
				
				}
			);
		},



		redraw: function() {

			if (this.user_month) this.this_month = this.user_month;
			if (this.user_year) this.this_year = this.user_year;
		
			newjax.update('calendar_container_div', newjax.calendar.actions_page, 
			{ calendar_action:'view', week:this.user_week, month:this.this_month, year:this.this_year, size:this.size, school:this.user_school, show_holidays:this.show_holidays, show_menus:this.show_menus, show_orders:this.show_orders },
			function(t) {
				newjax.orders.update_total();
			});
		
		},




		highlight_day: function(div, cal_class, sm) {


			if (div.className.search(/cal_day_selected/) != -1) return;

			if (!sm) div.className = 'cal_cell cal_day_hover';
			else div.className = 'left cal_cell_small cal_day_small_hover';


			div.onmouseout = function(e) {

				if (div.className.search(/cal_day_selected/) != -1) return;

				if (!sm) div.className = 'cal_cell '+cal_class;
				else div.className = 'left cal_cell_small '+cal_class;

			}
		},


		select_day: function(date, cal_class, sm) {

			var dates = date.split('-');
			var i;
			var y = dates[0];
			var d = dates[1]; 
			var m = dates[2]; 


			if (d == 0) return;


			for(i = 0; i < newjax.calendar.dates.length; i++) {

				if (newjax.calendar.dates[i] != date) continue;
	
				if (!sm) {

					$('cell_header_cal_date_'+date).className =  ' '+cal_class+'cell_header';
					$('cell_body_cal_date_'+date).className =  ' '+cal_class+'cell_body';
					$('cell_footer_cal_date_'+date).className =  ' '+cal_class+'cell_footer';

				} else {
					$('cal_date_'+date).className = 'left cal_cell_small '+cal_class;
				}

				newjax.calendar.dates[i] = '';
				if ($('dates')) $('dates').value = newjax.calendar.dates;
				if (this.show_holidays) $('message').value = '';
	
				return;

			}

			if (!sm) {

				$('cell_header_cal_date_'+date).className = 'selected_cal_cell_header';
				$('cell_body_cal_date_'+date).className = 'selected_cal_cell_body';
				$('cell_footer_cal_date_'+date).className = 'selected_cal_cell_footer';

			} else {

				$('cal_date_'+date).className = 'left cal_cell_small cal_day_selected';
				$(this.date_field).value = y+'-'+d+'-'+m;
				this.hide_calendar();

			}


			newjax.calendar.dates[newjax.calendar.dates.length] = y+'-'+d+'-'+m;
			if ($('dates')) $('dates').value = newjax.calendar.dates;
			if (this.show_holidays) $('message').value = $('jzpopup').innerHTML;

			return;

		},
		

		

		
		hide_calendar: function() {

			/* EMPTY CAL CONTAINER */
			newjax.calendar.current_page = 'hide';
			newjax.update(
				'calendar_container_div', 
				newjax.calendar.actions_page,
				{ calendar_action:'hide' },
				function(r) {
					/* CALLBACK */
				}
			);

			/* REMOVE CAL CONTAINER */
			$('calendar_container_div').remove();

			return;

		},




		show_calendar: function(div) {

			if (!$(div)) return;

			var top = findtop($(div));
			var h = $(div).offsetHeight;
			var x = top+h;


		
			/* CREATE CAL CONTAINER */
			var calendar_container = new Element('div', { 
				'id':'calendar_container_div', 
				'class':'calendar_container', 
				'style':'position:absolute; width:221px; top:'+x+'px; z-index:1000;'
			});

			/* INSERT CAL CONTAINER */
			if (!$('calendar_container_div')) $(div).insert({ 'before':calendar_container });


			if ($('calendar_container_div')) 
				$('calendar_container_div').innerHTML = newjax.loading_div;





			/* FILL CAL CONTAINER */
			newjax.calendar.current_page = 'view';
			newjax.update(
				'calendar_container_div', 
				newjax.calendar.actions_page,
				{ calendar_action:'view', size:this.size, school:this.user_school, show_holidays:this.show_holidays, show_menus:this.show_menus, show_orders:this.show_orders, month:this.user_month, year:this.user_year },
				function(t) {
					if (newjax.calendar.show_orders) newjax.orders.update_total();
				}
			);

			newjax.calendar.loaded = 1;

			return;
		
		},



		view_calendar: function(div) {

			/* GET DATE FIELD */
			this.date_field = div;
			$(this.date_field).value = '';


			/* RESET SIZE */
			this.size = '';


			/* HIDE CAL */
			if ($('calendar_container_div')) {

				$('calendar_container_div').onmouseout = function(e) {
					//newjax.calendar.hide_calendar();
					return;
				}

			}


			/* SHOW CAL */
			if (!$('calendar_container_div')) {
			
				this.show_calendar(div);
				return;
			
			}
			

		},

		init: function() {


			if ($('calendar_container_div')) {
			
				/* INIT CAL */
				newjax.update('', newjax.calendar.actions_page, { calendar_action:'init', page:newjax.current_page });
				newjax.calendar.show_calendar('calendar_container_div');

			} else {

				//setTimeout("newjax.calendar.init()", 1000);

			}

		}





	}

}();





Event.observe(window, 'load', function() {

	/* AFTER LOADING */
	newjax.calendar.init();

});
