// JavaScript Document
var currentCat = 0;
var currentFoodType = 0;

$(window).load(function() {
	$('#menu_category_'+tab).attr('class', 'tabButActive');
	$('#food_type_0').attr('class', 'selected_food_type');
	/*if($('#rate_form')) {
		var options = {
			//target:        '#rate_star_container',   // target element(s) to be updated with server response
			success:       change_meal_category,  // post-submit callback

			resetForm: true        // reset the form after successful submit
		};

		$('#rate_form').ajaxForm(options);
	}*/
});

function change_meal_category(catid, catnum, deltype) {
	$('#loader').html('<img src=\"/images/loader-brown.gif\" />');
	catid = (catid == -1 ? currentCatId : catid);
	currentCatId = catid;
	$.get('/menu_process.php?action=list_meals&catid='+catid+'&type='+deltype, function(data) {
		eval(data);
		var innerHtml = "";
		if(sections.length > 0) {
			innerHtml += "<div class=\"menuRow\"><div class=\"menuRowLeft\"><a id=\"food_type_0\" href=\"javascript:void(0);\" onclick=\"change_meal_food_type(0, 0);\">All</a>";
			for(var i=0; i<foodTypes.length; i++) {
				innerHtml += " | <a id=\"food_type_"+(i+1)+"\" href=\"javascript:void(0);\" onclick=\"change_meal_food_type("+foodTypes[i][0]+", "+(i+1)+");\">"+foodTypes[i][1]+"</a>";
			}
			innerHtml += "</div><div class=\"menuRowRight\"><strong>Quantity</strong></div></div>";
			for(var j=0; j<sections.length; j++) {
				innerHtml += "<div id=\"section_"+j+"\"><h2 class=\"section_header\" id=\"header_"+j+"\">"+sections[j]["section"]["name"]+"</h2>";
				if(sections[j]["meals"].length >0) {
				item_count=0;
					for(var i=0; i<sections[j]["meals"].length; i++) {

						innerHtml += "<div class=\"menuRow\" id=\""+sections[j]["meals"][i]["food_type"]+"\"><h3 class=\"menuItem\">"+sections[j]["meals"][i]["dish_number"]+" "+sections[j]["meals"][i]["name"]+"</h3><div class=\"menuItemDescr\">"+sections[j]["meals"][i]["desc"]+"</div>";
						if(typeof(sections[j]["meals"][i]["options"]) == "object" && sections[j]["meals"][i]["options"].length >0)
						{
							innerHtml += "<ul class=\"subItems\">\n";
							for(var k = 0; k < sections[j]["meals"][i]["options"].length; k++)
							{
								innerHtml += "<li>\n";
									innerHtml += "<label>"+sections[j]["meals"][i]["options"][k]["name"]+"</label>\n";
									innerHtml += "<div class=\"menuItemPrice\">&pound;"+sections[j]["meals"][i]["options"][k]["price"]+"&nbsp; \n";
  									innerHtml += "<input id=\"select_meal_"+sections[j]["meals"][i]["options"][k]["id"]+"\" type=\"text\" name=\"\" value=\"1\" class=\"qty\" maxlength=\"2\" size=\"2\" />  \n";
  									if(isOpened == 1) {
  										innerHtml += "<a href=\"javascript:void(0);\"><img src=\"/images/add_to_cart.gif\" alt=\"Add to cart\" title=\"Add to cart\" align=\"top\" border=\"0\" onclick=\"add_order_item('"+sections[j]["meals"][i]["options"][k]["id"]+"', getElementById('select_meal_"+sections[j]["meals"][i]["options"][k]["id"]+"').value, '"+deltype+"');\"></a>  \n";
									} else {
										innerHtml += "<a href=\"javascript:void(0);\"><img src=\"/images/add_to_cart_grey.gif\" alt=\"Restaurant Closed\" title=\"Restaurant Closed\" align=\"top\" border=\"0\"></a>  \n";
									}
									innerHtml += "</div>\n";
								innerHtml += "</li>\n";
							}
							innerHtml += "</ul>\n </div>\n";
						} else {
							if(isOpened == 1) {
								innerHtml += "<div class=\"menuItemPrice\">&pound;"+sections[j]["meals"][i]["price"].toFixed(2)+"&nbsp;<input value=\"1\" class=\"qty\" maxlength=\"2\" size=\"2\" type=\"text\" id=\"select_meal_"+sections[j]["meals"][i]["id"]+"\" name=\"select_meal\"/> <a href=\"javascript:void(0);\"><img src=\"/images/add_to_cart.gif\" alt=\"Add to cart\" title=\"Add to cart\" align=\"top\" border=\"0\" onclick=\"add_order_item('"+sections[j]["meals"][i]["id"]+"', getElementById('select_meal_"+sections[j]["meals"][i]["id"]+"').value, '"+deltype+"');\"></a></div></div>";
							} else {
								innerHtml += "<div class=\"menuItemPrice\">&pound;"+sections[j]["meals"][i]["price"].toFixed(2)+"&nbsp;<input value=\"1\" class=\"qty\" maxlength=\"2\" size=\"2\" type=\"text\" id=\"select_meal_"+sections[j]["meals"][i]["id"]+"\" name=\"select_meal\"/> <a href=\"javascript:void(0);\"><img src=\"/images/add_to_cart_grey.gif\" alt=\"Restaurant Closed\" title=\"Restaurant Closed\" align=\"top\" border=\"0\"></a></div></div>";
							}
						}
						item_count++;
					}
					innerHtml += "</div>";

				} else {
					innerHtml += "<div class=\"menuRow\">There are no meals in this section</div>";
				}
			}
		} else {
			innerHtml += "<div class=\"menuRow\">There are no meals in this category</div>";
		}
		$('#menu').html(innerHtml);
		if(currentCat != 4) {
			$('#menu_category_'+currentCat).attr('class', 'tabBut');
		} else {
			$('#menu_category_'+currentCat).attr('class', 'tabBut2');
		}
		if(catnum != 4) {
			$('#menu_category_'+catnum).attr('class', 'tabButActive');
		} else {
			$('#menu_category_'+catnum).attr('class', 'tabButActive2');
		}
		$('#food_type_'+currentFoodType).removeAttr('class');
		$('#food_type_0').attr('class', 'selected_food_type');
		//('#mycarousel').onescroll(catnum);
/*
		var obj;
		obj = document.getElementById('menu3_category_'+catnum);
		obj.className='tabButActive2';
		obj = document.getElementById('menu3_category_'+CurrentCat);
		obj.className='';
*/
		currentCat = catnum;
		currentFoodType = 0;
		$('#loader').html('');
	});
}

function change_meal_food_type(ftid, num) {
	var meals = $('.menuRow:gt(0)');
	var numberOfSections = $('.section_header').length;
	var temp = new Array();
	var currentSection;
	var currentSectionMeals;
	var flag = false;
	for(var i=0; i<meals.length; i++) {
		if(ftid != 0) {
			temp = meals[i].id.substring(1, meals[i].id.length-1).split("|");
			for(var j=0; j<temp.length; j++) {
				if(temp[j] == ftid) {
					flag = true;
				}
			}
			if(!flag) {
				meals[i].style.display = "none";
			} else {
				meals[i].style.display = "block";
			}
			flag = false;
		} else {
			meals[i].style.display = "block";
		}
	}
	for(var i=0; i<numberOfSections; i++) {
		currentSectionMeals = $("#section_"+i+" > .menuRow");
		flag = false;
		for(var j=0; j<currentSectionMeals.length; j++) {
			if(currentSectionMeals[j].style.display == "block") {
				flag = true;
				break;
			}
		}
		if(!flag) {
			$("#section_"+i).css("display", "none");
		} else {
			$("#section_"+i).css("display", "block");
		}
	}
	$('#food_type_'+currentFoodType).removeAttr('class');
	$('#food_type_'+num).attr('class', 'selected_food_type');
	currentFoodType = num;
}

function show_order(orderid, maxNum){
	$('#loader_cart').html('<img src=\"/images/loader-brown.gif\" />');
	if(orderid != -1) {
		$.get('/cart_process.php?action=show_order&id='+orderid, function(data){
			eval(data);
			var orderItems = "";
			for (i = 0; i < order.length; i++){
				orderItems += "<div class=\"orderName\">";
				if(order[i]['can_delete']==1)
					orderItems += '<a href="javascript:void(0);"><img src="/images/cart_item_remove.gif" align="bottom" title="Remove" alt="Remove" '
                +'onclick="delete_order_item(\''+order[i]["id"]+'\');" /></a> ';
				else orderItems += "<span style='padding-left:10px;'>";
				orderItems += order[i]["name"]+" x "+order[i]["quantity"];
				if(order[i]['can_delete']==0)
					orderItems += "</span>";
				orderItems +="</div>\n";
				orderItems += "<div class=\"orderPrice\">&pound; "+(order[i]["price"]*order[i]["quantity"]).toFixed(2)+"</div>\n";
			}
			$('#orderItems').html(orderItems);

			var totalText = "";
			totalText += "<div class=\"orderName\"><strong>Total:</strong></div>";
			totalText += "<div class=\"orderPrice\">&pound; "+totalPrice.toFixed(2)+"</div>";
			$('#total').html(totalText);

			var innerHtml = "";
			for(i = 1; i <= maxNum; i++) {
				innerHtml += "<a onclick=\"show_order("+i+", false)\">0"+i+"</a> ";
				innerHtml += (i!=maxNum ? "| " : "");
			}
			$('#restNumbers').html(innerHtml);

			$('#loader_cart').html('');
		});
	}
}

function add_order_item(id, qty, delType) {
	$('#loader_cart').html('<img src=\"/images/loader-brown.gif\" />');
	$.get('/cart_process.php?action=add_order_item&id='+id+"&qty="+qty+"&delType="+delType, function(data){
		eval(data);
		if(restOrder == -2) {
			$('#loader_cart').html('');
			window.location.href = window.location.href+"&code=1";
		}
		else if(restOrder == -3) {
			$('#loader_cart').html('');
			window.location.href = window.location.href+"&code=3";
		}
		 else {
			show_order(restOrder, maxNum);
			$('#loader_cart').html('');
		}
	});
}

function delete_order_item(mealid) {
    var success;
    var totalPrice;
    var conf = confirm("You are about to remove a meal from your cart. Do you want to proceed?");
    if(conf) {
        $('#loader_cart').html('<img src=\"/images/loader-brown.gif\" />');
        $.get('/cart_process.php?action=delete_order_item&a=1&id='+mealid, function(data) {
            eval(data);
            if(success == true) {
                show_order(restOrder, maxNum);
            }
            $('#loader_cart').html('');
            var wlh = window.location.href;
            while (wlh.indexOf('code=3') != -1)
            	 wlh = wlh.replace('&code=3','');
            while (wlh.indexOf('code=1') != -1)
            	 wlh = wlh.replace('&code=1','');
            window.location.href = wlh;
            //$('#message').hide();
        });
    } else {
        $('#loader_cart').html('');
    }

}

function clear_cart( id ) {
    var success;
    var conf = confirm("You are about to clear your cart. Do you want to proceed?");
    if(conf) {
        $('#loader_cart').html('<img src=\"images/loader-blue.gif\" />');
        $.get('/order_process.php?action=clear_order&a=1', function(data) {
            eval(data);
            if(success == true) {
                $('#checkoutList div').fadeOut("slow");
                $('#total').html('&pound; 0.0');
                $('#checkoutButton').fadeOut("slow");
                show_order(id, false);
            }
            $('#loader_cart').html('');
        });
    } else {
        $('#loader_cart').html('');
    }
}