var bg_element = "bg";
var img_element = "bgimg";
var panel, request;
var current_page;

function fillBackground(){
	var divWidth=parseInt(document.getElementById(bg_element).clientWidth)/2;
	var divHeight=parseInt(document.getElementById(bg_element).clientHeight)/2;
	var img_container = document.getElementById(img_element)
	img_container.style.width = (img_container.clientWidth < divWidth) ? divWidth + "px" : "auto";
	img_container.style.height = (img_container.clientHeight < divHeight) ? divHeight + "px" : "auto";
}

function init(){
	fillBackground();
	reloadPanel();
	setInterval(pollPageState,500);
}

function reloadPanel(){
	var current_page_arr = window.location.hash.split('#');
	current_page=current_page_arr[1];
	if((current_page=='undefined')||(current_page=='')){
		current_page='menu';
	}
	getInformation(current_page);
}

function pollPageState(){
	var current_page_arr = window.location.hash.split('#');
	if((current_page_arr[1]=='undefined')||(current_page_arr[1]=='')){
		current_page='menu';
		getInformation(current_page);
		return;
	} 
	if (current_page_arr[1] == current_page) {
       return;
    }
	current_page=current_page_arr[1];
	getInformation(current_page);
}

function refreshPanel(){
	if(panel == 'menu_items'){
		setMenuItemsLoadingDisplay('inline');
	}
	if(panel == 'main'){
		setInformationLoadingDisplay('inline');
	}
	if(panel == 'menu_selection'){
		setOrderLoadingDisplay('inline');
	}
	if (xmlhttp.readyState==4){
		document.getElementById(panel).innerHTML=xmlhttp.responseText;
		window.location.hash=current_page;
	}
}

function setMenuItemsLoadingDisplay(d_state){
	document.getElementById('menu_loading').style.display=d_state;
}

function setInformationLoadingDisplay(d_state){
	document.getElementById('information_loading').style.display=d_state;
}

function setOrderLoadingDisplay(d_state){
	document.getElementById('order_loading').style.display=d_state;
}

function getInformation(page_name){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="get_information.php";
	if(page_name == null){
		url=url+"?sid="+Math.random();
	} else {
		url=url+"?page_name="+page_name+"&sid="+Math.random();
	} 
	panel="main";
	current_page=page_name;
	xmlhttp.onreadystatechange=refreshPanel;
	xmlhttp.open("GET",url, true );
	xmlhttp.send(null);
}

function getItems(cat_id){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="get_items.php";
	if(cat_id == null){
		url=url+"?sid="+Math.random();
	} else {
		url=url+"?cat_id="+cat_id+"&sid="+Math.random();
	} 
	panel="menu_items";
	xmlhttp.onreadystatechange=refreshPanel;
	xmlhttp.open("GET",url, true );
	xmlhttp.send(null);
	
}

function addItem(item_id){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="add_item.php";
	if(item_id == null){
		url=url+"?sid="+Math.random();
	} else {
		url=url+"?item_id="+item_id+"&current_page="+current_page+"&sid="+Math.random();
	} 
	panel="menu_selection";
	xmlhttp.onreadystatechange=refreshPanel;
	xmlhttp.open("GET",url, true );
	xmlhttp.send(null);
}

function selectOption(item_order_no,option){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="update_order_calc.php";
	if(item_order_no == null){
		url=url+"?sid="+Math.random();
	} else {
		url=url+"?action=updateoptions&current_page="+current_page+"&item_order_no="+item_order_no+"&option="+option+"&sid="+Math.random();
	} 
	panel="menu_selection";
	xmlhttp.onreadystatechange=refreshPanel;
	xmlhttp.open("GET",url, true );
	xmlhttp.send(null);
}

function clearOrder(){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="update_order_calc.php";
	url=url+"?action=clearall&current_page="+current_page+"&sid="+Math.random();
	panel="menu_selection";
	xmlhttp.onreadystatechange=refreshPanel;
	xmlhttp.open("GET",url, true );
	xmlhttp.send(null);
}

function removeOrderItem(item_order_no){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="update_order_calc.php";
	if(item_order_no == null){
		url=url+"?sid="+Math.random();
	} else {
		url=url+"?action=removeone&current_page="+current_page+"&item_order_no="+item_order_no+"&sid="+Math.random();
	} 
	panel="menu_selection";
	xmlhttp.onreadystatechange=refreshPanel;
	xmlhttp.open("GET",url, true );
	xmlhttp.send(null);
}

function GetXmlHttpObject(){
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject){
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

window.onload = init; 
 
