/* Set up behaviours */
var mnuOpen = 0
var Rules  = {
	'#which-menu:change' : function(el){
		loadMenu(el.value);
	},
	'#menu-footer:click' : function(el) {
		if (mnuOpen == 0) {
			mnuOpen = 1;
			loadMenu('');
			el.style.background = 'url(/style/images/menu-bottom-out.gif)';
			el.style.backgroundRepeat = 'no-repeat';
			el.style.backgroundPosition = 'bottom center';
		} else if (mnuOpen == 1) {
			mnuOpen = 0;
			clearMenu();
			el.style.background = 'url(/style/images/menu-bottom.gif)';
			el.style.backgroundRepeat = 'no-repeat';
			el.style.backgroundPosition = 'bottom center';
		} else {
			mnuOpen = 0;
			clearMenu();
			el.style.background = 'url(/style/images/menu-bottom.gif)';
			el.style.backgroundRepeat = 'no-repeat';
			el.style.backgroundPosition = 'bottom center';
		}
	}
};

function setMenuStatus (img, msg) {
	/*Effect.Appear('ad-book-status', {duration:0.5});
	if (img) {
		Element.update('ad-book-status', '<img src="/_system/themes/default/images/adBook-working.gif"><div>' + msg + '</div>');
	} else {
		Element.update('ad-book-status', msg);
	}*/
}

function modMenu (in_status) {
	//alert(in_status);
	var menu = $('food-menu');
	var menufooter = $('menu-footer');
	var menustyle = menu.style;
	var footerstyle = menufooter.style;
	if (in_status == 1) {
		menustyle.height = 'auto';
		menustyle.position = 'absolute';
	} else if (in_status == 0) {
		menustyle.height = '208px';
		menustyle.position = 'relative';
	} else {
		menustyle.height = '1px';
		menustyle.height = 'auto';
		mnuOpen = 2
	}
}

function loadMenu(in_id) {
	var opt = {
		asynchronous:true,
		evalScripts:true,
		method: 'post',
		parameters: 'action=load&menu=' + in_id,
		onSuccess: function(t) {
			//setAdBookStatus(false, 'Card loaded');
		},
		// Handle 404
		on404: function(t) {
			//setAdBookStatus(false, 'Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			//setAdBookStatus(false, 'Error ' + t.status + ' -- ' + t.statusText);
		},
		// Completed
		onComplete: function(t) {
			//alert(t.responseText);
			if ((t.responseText.indexOf('There are currently no items') > 0) || in_id !='') {
				modMenu(2);
			} else {
				modMenu(mnuOpen);
			}
			
		}
	}
	var myAjax = new Ajax.Updater('menu-content', '/admin/menuHandler.asp', opt);
}

function clearMenu() {
	var opt = {
		asynchronous:true,
		evalScripts:true,
		method: 'post',
		parameters: 'action=clear',
		onSuccess: function(t) {
			//setAdBookStatus(false, 'Card loaded');
		},
		// Handle 404
		on404: function(t) {
			//setAdBookStatus(false, 'Error 404: location "' + t.statusText + '" was not found.');
		},
		// Handle other errors
		onFailure: function(t) {
			//setAdBookStatus(false, 'Error ' + t.status + ' -- ' + t.statusText);
		},
		// Completed
		onComplete: function() {
			modMenu(mnuOpen);
		}
	}
	var myAjax = new Ajax.Updater('menu-content', '/admin/menuHandler.asp', opt);
}
Event.observe(window, 'load', function(){EventSelectors.start(Rules);}, false);