/** 
 * @fileoverview This file contains functions related to the dining panel.
 * @author Mark Hendrickson <mhendric@bowdoin.edu>
 * @version 1.0
 */

/**
 * Toggles the display of restaurant information in a popup.
 * @param {String} action Informs whether to show or hide popup
 * @param {Integer} restaurant Unique ID of restaurant in database
 * @param {Object} event Event object
 */
function toggleRestaurant(action,restaurant,event)
{
	if(!event)
		var event = window.event;	
	
	if(action == 'on')
	{
		var y = getEventPageY(event)-50;

		toggleTint('on');

		var popup = document.createElement('div');
		popup.setAttribute('id','popup');
		popup.style.top = y+'px';
		popup.innerHTML = 'Loading content...';
		popup.onclick = function(event) { stopPropagation(event); };

		var close = document.createElement('div');
		close.className = 'close';
		close.onclick = function(event) { toggleRestaurant('off',null,event); };
		popup.appendChild(close);

		var popup_shadow = document.createElement('div');
		popup_shadow.setAttribute('id','popup_shadow');
		popup_shadow.style.top = (y+2)+'px';

		var column_3 = $('column_3');
		column_3.appendChild(popup);
		column_3.appendChild(popup_shadow);

		var popup = $('popup');
		var popup_shadow = $('popup_shadow');
		popup_shadow.style.width = popup.offsetWidth+'px';
		popup_shadow.style.height = popup.offsetHeight+'px';
		
		var ajax = initAjax();
		ajax.open('GET','restaurant.php?restaurant='+restaurant,true);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		ajax.onreadystatechange = function() { showRestaurant(ajax) };
		ajax.send(null);

		setTimeout("var body = document.getElementsByTagName('body')[0]; body.onclick = function(event) { toggleRestaurant('off',null,event); };",1);
	}
	else
	{
		var column_3 = $('column_3');
		var popup = $('popup');
		var popup_shadow = $('popup_shadow');
		column_3.removeChild(popup);
		column_3.removeChild(popup_shadow);
		toggleTint('off');
		var body = document.getElementsByTagName('body')[0]; 
		body.onclick = null;
	}
}

/**
 * Called exclusively by the toggleRestaurant function; inserts restaurant information into the popup once delivered by AJAX.
 * @param {Object} ajax Ajax object
 * @see #toggleRestaurant
 */
function showRestaurant(ajax)
{
	if(ajax.readyState == 4)
	{
		if(ajax.status == 200)
		{
			var response = ajax.responseXML;
			var html = response.getElementsByTagName('html')[0];

			var popup = $('popup');
			popup.removeChild(popup.firstChild);
			appendNode(firstElement(html),popup);

			var popup_shadow = $('popup_shadow');
			popup_shadow.style.width = popup.offsetWidth+'px';
			popup_shadow.style.height = popup.offsetHeight+'px';
		}
		else
		{
			var popup = $('popup');
			popup.innerHTML = 'Loading failed.';
		}
	}
}

/**
 * Loads menu information for a particular meal and dining location into either the dining panel or one of the dining hall panels.
 * @param {String} menu_location Name of the dining hall; "thorne" or "moulton"
 * @param {Boolean} separate_panel Indicates whether to load the menu into the dining panel (false) or a particular dining hall's panel (true)
 * @return Returns false to prevent form submission.
 * @type Boolean
 */
function loadDiningMenu(menu_location,separate_panel)
{
	if(!separate_panel)
	{
		var meal = $(menu_location+'_meal').value;
		var month = $(menu_location+'_month').value;
		var day = $(menu_location+'_day').value;
		var year = $(menu_location+'_year').value;
	}
	else
	{
		var meal = $(menu_location+'_meal_separate').value;
		var month = $(menu_location+'_month_separate').value;
		var day = $(menu_location+'_day_separate').value;
		var year = $(menu_location+'_year_separate').value;
	}

	if(menu_location == 'thorne')
		menu_location_upper = 'Thorne';
	else if(menu_location == 'moulton')
		menu_location_upper = 'Moulton';

	var ajax = initAjax();
	ajax.open('GET','dining_menu.php?timeStamp='+new Date().getTime()+'&location='+menu_location_upper+'&meal='+meal+'&year='+year+'&month='+month+'&day='+day+'&content_type=xml',true);
	ajax.onreadystatechange = function() { loadDiningMenuCallback(ajax,menu_location,separate_panel) };
	ajax.send(null);

	return false;
}

/**
 * Called exclusively by the loadDiningMenu function; it follows up with the AJAX operations of that function.
 * @param {Object} ajax Ajax object
 * @param {String} menu_location Name of the dining hall; "thorne" or "moulton"
 * @param {Boolean} separate_panel Indicates whether to load the menu into the dining panel (false) or a particular dining hall's panel (true)
 * @see #loadDiningMenu
 */
function loadDiningMenuCallback(ajax,menu_location,separate_panel)
{
	if(ajax.readyState == 4)
	{
		if(ajax.status == 200)
		{
			if(separate_panel)
				var menu_div = $(menu_location+'_menu_separate');
			else
				var menu_div = $(menu_location+'_menu');

			var menu_div_children = menu_div.childNodes;

			while(menu_div_children.length)
				menu_div.removeChild(menu_div_children[0]);

			var response = ajax.responseXML;
			var html = response.getElementsByTagName('html')[0];
			var div = html.firstChild;
			
			appendNode(div,menu_div);
		}
	}
}

/**
 * Toggles the display of a form to choose the meal for which to show a menu.
 * @param {String} menu_location Name of the dining hall; "thorne" or "moulton"
 * @param {Boolean} separate_panel Indicates whether to show the form in the dining panel (false) or in a particular dining hall's panel (true)
 */
function toggleDiningMenuForm(menu_location,separate_panel)
{
	if(!separate_panel)
	{
		var menu_form_link = $(menu_location+'_menu_form_link');
		var menu_form = $(menu_location+'_menu_form');
	}
	else
	{
		var menu_form_link = $(menu_location+'_menu_form_link_separate');
		var menu_form = $(menu_location+'_menu_form_separate');
	}

	if(menu_form.style.display == 'none' || !menu_form.style.display)
	{
		menu_form_link.style.fontWeight = 'bold';
		menu_form.style.display = 'block';
	}
	else
	{
		menu_form_link.style.fontWeight = 'normal';
		menu_form.style.display = 'none';
	}
}

/**
 * Toggles the display of pub menu information in a popup.
 * @param {String} action Informs whether to show or hide popup
 * @param {Integer} section Section of the pub menu to display
 * @param {Object} event Event object
 */
function togglePubMenu(action,section,event)
{
	if(!event)
		var event = window.event;	
	
	if(action == 'on')
	{
		var y = getEventPageY(event)-50;

		toggleTint('on');

		var popup = document.createElement('div');
		popup.setAttribute('id','popup');
		popup.style.top = y+'px';
		popup.innerHTML = 'Loading content...';
		popup.onclick = function(event) { stopPropagation(event); };

		var close = document.createElement('div');
		close.className = 'close';
		close.onclick = function(event) { toggleRestaurant('off',null,event); };
		popup.appendChild(close);

		var popup_shadow = document.createElement('div');
		popup_shadow.setAttribute('id','popup_shadow');
		popup_shadow.style.top = (y+2)+'px';

		var column_3 = $('column_3');
		column_3.appendChild(popup);
		column_3.appendChild(popup_shadow);

		var popup = $('popup');
		var popup_shadow = $('popup_shadow');
		popup_shadow.style.width = popup.offsetWidth+'px';
		popup_shadow.style.height = popup.offsetHeight+'px';
		
		var ajax = initAjax();
		ajax.open('GET','pub_menu.php?section='+section,true);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		ajax.onreadystatechange = function() { showPubMenu(ajax) };
		ajax.send(null);

		setTimeout("var body = document.getElementsByTagName('body')[0]; body.onclick = function(event) { togglePubMenu('off',null,event); };",1);
	}
	else
	{
		var column_3 = $('column_3');
		var popup = $('popup');
		var popup_shadow = $('popup_shadow');
		column_3.removeChild(popup);
		column_3.removeChild(popup_shadow);
		toggleTint('off');
		var body = document.getElementsByTagName('body')[0]; 
		body.onclick = null;
	}
}

/**
 * Called exclusively by the togglePubMenu function; inserts pub menu information into the popup once delivered by AJAX.
 * @param {Object} ajax Ajax object
 * @see #togglePubMenu
 */
function showPubMenu(ajax)
{
	if(ajax.readyState == 4)
	{
		if(ajax.status == 200)
		{
			var response = ajax.responseXML;
			var html = response.getElementsByTagName('html')[0];

			var popup = $('popup');
			popup.removeChild(popup.firstChild);
			appendNode(firstElement(html),popup);

			var popup_shadow = $('popup_shadow');
			popup_shadow.style.width = popup.offsetWidth+'px';
			popup_shadow.style.height = popup.offsetHeight+'px';
		}
		else
		{
			var popup = $('popup');
			popup.innerHTML = 'Loading failed.';
		}
	}
}