	$().ready(function() {

		loadShoppingCart('','','','','','LOADCART');
		loadMenu();

		function loadMenu(){
			$.ajax({
				   type: "POST",
				   url: "servlet/MenuItemServlet",
				   data: "operation=LOADMENU",
				   dataType: 'xml',
				   timeout: 2000,
				   success: function(xml){
				//alert(xml);
						$(xml).find('Menu').each(function(){
							var item_text = "";
							var isOddRow = false;
							var count = 1;
					        $(this).find('MenuItems').each(function(){
						        item_text = item_text+'<thead valign="top"><tr><th scope="col" colspan="5">'+$(this).find('Family').text()+'</th></tr></thead><tbody>';
					        	$(this).find('MenuItem').each(function(){
					        		if($(this).find('ItemPublished').text() == "true" || $(this).find('ItemPublished').text() == "1"){
					        			var oddRow = "";
								        if(isOddRow){
								        	oddRow = "class=\"odd\"";
								        	isOddRow = false;
								        }else{
								        	isOddRow = true;
								        }
							        	item_text = item_text+'<tr '+oddRow+' id=\"'+$(this).find('ItemId').text()+'\">';
							        	item_text = item_text+'<td width=\"20\">'+ count++ +'.</td>';
							        	item_text = item_text+'<td width=\"300\" class=\"'+$(this).find('ItemName').text()+'\" ><b><a href=\"#\" id=\"'+$(this).find('ItemDescription').text()+'\" >'+$(this).find('ItemName').text()+'</a></b><br>';
							        	item_text = item_text+'<div class=\"drop_down\" style=\"display:none;\"><img style="height:150px;width:250px;border:0px;" src=\"'+$(this).find('ItemImageName').text()+'" alt=\"'+$(this).find('ItemName').text()+'\"/><br>'+$(this).find('ItemDescription').text()+'</div></td>';
							        	item_text = item_text+'<td width=\"40\" class=\"'+$(this).find('ItemNetPrice').text()+'\">$'+$(this).find('ItemNetPrice').text()+'</td>';
							        	item_text = item_text+'<td valign=\"bottom\" width=\"15\"><input style=\"text-align:center;width:25px;height:12px;font-size:10px;font-family:Verdana,sans-serif;\" type=\"text\" name=\"ItemQuantity\" class=\"quantityid\" value=\"1\"></td>';
							        	item_text = item_text+'<td style=\"text-align:center;\" valign=\"bottom\"><input type=\"button\" class=\"groovybutton\" name=\"ADDTOCART\" value=\"ADD\" title=\"Click Here To Add This Item To Your Order Cart\" /></td></tr>';

					        		}
						        });
					        	item_text = item_text+'</tbody>';
					        });
					        $('#Item_Menu_Table').html(item_text);
					        $('.quantityid').keyup(function () {
							    var value = $(this).val();
						      var i;
						      value = value.toString();
						        for (i = 0; i < value.length; i++)
						        {
						           var c = value.charAt(i);
						           if (isNaN(c)) 
						  	   {
						  		alert("Given value is not a number");
						  		$(".quantityid").attr("value","1");
						  		return false;
						  	   }
						        }
						   

						    }).keyup();

/*					        $('.drop_down').unbind().bind("mouseenter mouseleave", function(e){
								if(e.type == 'mouseenter'){
									$(e.target).slideDown('normal');
								}else if (e.type == 'mouseleave'){
									$(e.target).slideUp('slow');
								}
				        	});
					        $('#Item_Menu_Table').find('div.drop_down').each(function(){

					        });*/
					    });
				   }
			});
		}
		
		
		function loadShoppingCart(iId,iName,iDesc,iPrice,iQuantity,operation){
			var params = "";
			if(operation == 'ADDTOCART'){
				params = params+"itemId="+iId+"&itemName="+iName+"&itemDesc="+iDesc+"&itemPrice="+iPrice+"&itemQuantity="+iQuantity+"&operation="+operation;
			}else if(operation == 'REMOVEFROMCART'){
				params = params+"itemId="+iId+"&operation="+operation;
			}else{
				params = params+"operation="+operation;
			}

			$.ajax({
				   type: "POST",
				   url: "servlet/SessionOrderCartServlet",
				   data: params,
				   dataType: 'xml',
				   timeout: 1000,
				   success: function(xml){
						$(xml).find('Order').each(function(){
							var item_text = "";
							var isOddRow = false;
					        $(this).find('OrderLine').each(function(){
					        	var oddRow = "";
						        if(isOddRow){
						        	oddRow = "class=\"odd\"";
						        	isOddRow = false;
						        }else{
						        	isOddRow = true;
						        }
					        	item_text = item_text+'<tr '+oddRow+' id=\"'+$(this).find('ItemId').text()+'\">';
					        	item_text = item_text+'<td>'+$(this).find('ItemName').text()+'</td>';
					        	item_text = item_text+'<td width="25">'+$(this).find('LineQuantity').text()+'</td>';
					        	item_text = item_text+'<td width="50">$'+$(this).find('LineNetPrice').text()+'</td>'
					        	item_text = item_text+'<td width="15"valign=\"bottom\"><a href=\"#\"><img src=\"images/remove.png\" style="border:0px;" alt=\"Remove\" title=\"Remove\"/></a></td></tr>';
					        });
					        if("" == item_text){
								item_text = item_text+'<tr><td align=\"center\" colspan=\"4\"><b>Your Cart is Empty</b></td></tr>';
						    }
					        $("#shopCartTBody").html(item_text);

					        var footer = "";
					        $(this).find('OrderHeader').each(function(){
					        	footer = '<tr><th colspan="2">Total</th><th colspan="2">$'+$(this).find('OrderTotalPrice').text()+'</th></tr>';
					        });

					        $("#shopCartTFoot").html(footer);
					    });

				   }
			});
		}

		$('#orderCartContinue').click(function (e){
			
			location.href = 'order.jsp';
		});

		$(function(){
			$("table.zebra tr:even").addClass("even");
			$("table.zebra tr:odd").addClass("odd");
		});

		var modal_first = true;

		$('#Item_Menu_Table').click(function (e) {
			if ($(e.target).attr('class') == 'groovybutton'){
				var pattern = /^[0-9]{1}$/;
				var quantity=$('.quantityid').val();
				//alert(quantity);
				
				
				if('San Jose'==$('#CURRENT_LOCATION').val()){
					alert('Online ordering is not available at San Jose Location');
					return false;
				}
				var itemId = $(e.target).parent().parent().attr('id');
				var itemName = $(e.target).parent().parent().children(':nth-child(2)').attr('class');
				var itemDesc = $(e.target).parent().parent().children(':nth-child(2)').children(':first').children(':first').attr('id');
				var itemPrice = $(e.target).parent().parent().children(':nth-child(3)').attr('class');
				var itemQuantity = $(e.target).parent().parent().children(':nth-child(4)').children(':first').val();

//				alert('ItemId:'+itemId+':ItemName:'+itemName+':ItemDesc:'+itemDesc+':ItemPrice:'+itemPrice+':itemQuantity:'+itemQuantity);

				var anivDate = new Date();
				if(anivDate.getDate() == 11 && anivDate.getMonth() == 0 && anivDate.getFullYear() == 2009){
					if(!(itemId == '1006' || itemId == '1007')){
						alert('Today on the occasion of Peacock\'s first year anniversary, We are serving only Chicken Dum Biryani and Veg Dum Biryani. However we are having a speciality lunch and dinner buffet.');
						return false;
					}
				}

				loadShoppingCart(itemId,itemName,itemDesc,itemPrice,itemQuantity,'ADDTOCART');
			}else if($(e.target).attr('class') == 'jqmModal'){
 			    var loadData = function(){
					$('.modal-item-name').html($(e.target).parent().parent().attr('class'));
					$('.modal-item-img').html('Description');
					$('.modal-item-desc').html('Description');
 			    }

				$('#itemmodal').jqm({
					overlay: 75,
					modal: true,
					target:'div.modalcontents',
					trigger:'a.jqmModal'
				});
				$(e.target).click();

			}else if($(e.target).attr('class') == 'item_modal_dialog'){
				var imagename = $(e.target).parent().parent().attr('id');
				$("#item_modal").attr('title',$(e.target).parent().parent().attr('class'));
				$(".modal-item-desc").html($(e.target).attr('id'));
				$(".modal-item-img").html('<img style="height:150px;width:200px;border:0px;" src=\"'+imagename+'\" alt=\"'+$(e.target).attr('id')+'\" title=\"'+$(e.target).attr('id')+'\"/>');
				$("#item_modal").show();
				$("#item_modal").dialog({
				    modal: true,
				    overlay: {opacity: 0.5,background: "black"},
				    height:'150px',
				    width:'400px'
				});
			}

			return false;
		});

/*		$('#Item_Menu_Table').hover(
				function (e) {
					if($(e.target).attr('id') == 'drop_down'){
						console.debug('Hover Start:'+$(e.target).attr('id'));
//						$(e.target).find('> div').slideDown('normal');
						$(e.target).find('> div').show();
					}
				},
				function (e){
					if($(e.target).attr('id') == 'drop_down'){
						console.debug('Hover End:'+$(e.target).attr('id'));
//						$(e.target).find('> div').slideUp('slow');
						$(e.target).find('> div').hide();
					}
				}
		);*/

/*
		$('#Item_Menu_Table').mouseover(function(e){
			if($(e.target).attr('class') == 'item_modal_dialog'){
				$("#item_modal").attr('title',$(e.target).parent().parent().attr('class'));
				$(".modal-item-desc").html($(e.target).attr('id'));
				$(".modal-item-img").html('<img src=\"images/biryani.png\" style="border:0px;" alt=\"'+$(e.target).attr('id')+'\" title=\"'+$(e.target).attr('id')+'\"/>');
				if($("#item_modal").dialog('isOpen')){
				}else{
					alert('not opened');
					$("#item_modal").show();
					$("#item_modal").dialog({
					    modal: true,
					    overlay: {opacity: 0.5,background: "black"},
					    height:'150px',
					    width:'400px'
					});
				}
			}
		});

		$('#Item_Menu_Table').mouseout(function(e){
			if($("#item_modal").dialog('isOpen')){
				$("#item_modal").dialog('close');
			}
		});

		var bgFadeInt = window.setInterval(function() {
			$("#item_modal").show();
			if($("#item_modal").dialog('isOpen')){
				$("#item_modal").dialog('close');
			}
			$("#item_modal").dialog({
			    modal: true,
			    overlay: {opacity: 0.5,background: "black"},
			    height:'150px',
			    width:'400px',
			    show:1500
			});
//			window.clearInterval(bgFadeInt);
		},60000);
*/

		$('#shopCartTBody').click(function (e) {
			
			if($(e.target).attr('title') == 'Remove'){
				var itemId = $(e.target).parent().parent().parent().attr('id');
				loadShoppingCart(itemId,'','','','','REMOVEFROMCART');
			}
			return false;
		});

		$('.jqmClose').jqmHide('slow');
	});