
 // unblock when ajax activity stops 
 $(document).ajaxStop($.unblockUI); 


 $(document).ready(function(){
	$(".favourite-on").click(function() {
		mi_faves_post(this);
	});
	$(".favourite-off").click(function() {
		mi_faves_post(this);
	});

	$(".addproductform").submit(function() {
			return mi_cart_post(this);
	});

	if (document.getElementById("add-all-items")) {
		$("#add-all-items").click(function() {
				return mi_all_cart_post();
		});
	}
	
 });


function mi_faves_post(obj) {

	$.post("/menu.html",
		{
		a: "faves",
		prodid: obj.getAttribute("rel"),
		d: "set"
		},
		function(xml){
			mi_faves_callback(xml, obj);
		}
	);
	return false;
}


function mi_cart_post(obj) {

	$.blockUI({ message: $('#cartMessage') }); 

	$.post("/menu.html",
		{
		a: "cart",
		product: obj.product.value,
		ref: obj.ref.value,
		id: obj.id.value,
		num: obj.num.value,
		format: "xml",
		d: "add"
		},
		function(xml){
			mi_cart_callback(xml, obj);
		}
	);
	return false;
}


function mi_all_cart_post(obj) {
	
	$.blockUI({ message: $('#cartMessage') }); 
	
	$("#all-items-form").html("");
	
	var x = 0;	
	$(".addproductform").each(function(){
		$("#all-items-form").append("<input type='hidden' name='prodid"+x+"' value='"+this.id.value+"'>");
		$("#all-items-form").append("<input type='hidden' name='qty"+x+"' value='"+this.num.value+"'>");
		$("#all-items-form").append("<input type='hidden' name='addprod"+x+"' value='Y'>");
		x++;
	});
	$("#all-items-form").append("<input type='hidden' name='a' value='cart'>");
	$("#all-items-form").append("<input type='hidden' name='d' value='add'>");
	$("#all-items-form").append("<input type='hidden' name='format' value='xml'>");
	
	//	$.blockUI({ message: $('#cartMessage') }); 
	$.post("/menu.html",
			$("#all-items-form").serialize(),
			function(xml){	
				// nothing has to be done
				// mi_cart_callback(xml, obj);
			}
	);
	
	return false;
}








function mi_cart_callback(xml, obj) {
	// all done... 
}

function mi_faves_callback(xml, obj) {
 	if ($("status", xml).text() == "REMOVED") {
 		obj.className = "favourite-off";
 	}
 	if ($("status", xml).text() == "ADDED") {
 		obj.className = "favourite-on";
 	}
 	if ($("status", xml).text() == "0") {
		$("#msg").val("");
		$("#subject").val("");
 		// All good
 		// alert($("message", xml).text());

		if ($("div#comments")) {
			$("div#comments").dialog("destroy");
		}
		if ($("div#good_comment")) {
			$("div#good_comment").dialog("destroy");
			$("div#good_comment").dialog({dialogClass: 'newIdea', title: 'Comments Saved!', modal: true, position: 'center', resizeable: false, height: 250, width: 450, buttons: {"OK": function() {$("div#good_comment").dialog("close");}}});
		}
 	}
}


function popupAddIdea() {
	if ($("div#ideas")) {
		$("div#ideas").dialog("destroy");
		$("div#ideas").dialog({dialogClass: 'newIdea', title: 'Add My Idea', modal: true, position: 'center', resizeable: false, height: 300, width: 450, buttons: {"OK": function() { $("form#ideaform").submit(); }, "Cancel": function() {$("div#ideas").dialog("close");}}});
	}
}

function popupMustLogin() {
	if ($("div#must_login_popup")) {
		$("div#must_login_popup").dialog("destroy");
		$("div#must_login_popup").dialog({dialogClass: 'newIdea', title: 'Please Login', modal: true, position: 'center', resizeable: false, height: 100, width: 450, buttons: {"OK": function() {$("div#must_login_popup").dialog("close");}}});
	}
}


function popupAddComment() {
	if ($("div#comments")) {
		$("div#comments").dialog("destroy");
		$("div#comments").dialog({dialogClass: 'newIdea', title: 'Add Comments', modal: true, position: 'center', resizeable: false, height: 300, width: 450, buttons: {"OK": function() { $("form#commentform").submit(); }, "Cancel": function() {$("div#comments").dialog("close");}}});
	}
}


function popupHelpForm() {
	if ($("div#helpdiv")) {
		$("div#helpdiv").dialog("destroy");
		$("div#helpdiv").dialog({dialogClass: 'popupHelp', title: 'Ask Mic', modal: true, position: 'center', resizeable: false, height: 300, width: 450, buttons: {"OK": function() { $("form#helpform").submit(); }, "Cancel": function() {$("div#helpdiv").dialog("close");}}});
	}
}
