var creationDialog;
var editionDialog;
var duplicationDialog;

function postRecipeCreationForm(fields, params) {
	Skelax.JSON.send({app: 'repasrc', func: 'AddRecipe', funcParams: {fields: fields}, update: 'content'});
	creationDialog.close();
}

function postRecipeEditForm(fields, params) {
	Skelax.JSON.send({app: 'repasrc', func: 'UpdateRecipe', funcParams: {fields: fields}, update: 'content'});
	editionDialog.close();
}

function postRecipeDuplicationForm(fields, params) {
	Skelax.JSON.send({app: 'repasrc', func: 'DuplicateRecipe', funcParams: {fields: fields}, update: 'content'});
}

function postRecipeCopyForm(fields, params) {
	Skelax.JSON.send({app: 'repasrc', func: 'CopyRecipe', funcParams: {fields: fields}, update: 'content'});
	duplicationDialog.close();
}

function postRecipeFoodstuffForm(fields, params) {
	Skelax.JSON.send({app: 'repasrc', func: 'UpdateRecipeFoodstuff', funcParams: {fields: fields}, update: 'blockCenter'});
}

function addFoodstuffToRecipe(fs_id, recipe_id, synonym_id) {
	Skelax.JSON.send({app: 'repasrc', func: 'SetRecipeFoodstuff', funcParams: {fs_id: fs_id, recipe_id: recipe_id, synonym_id: synonym_id}, update: 'blockCenter'});
}

function newRecipe() {
	var os = new Skelax.UI.Dialog({onReady: 
		function() {
			var sl = new Skelax.JSON.Query({onComplete: function(jsonResult) { 
				if (!jsonResult.error) {
					creationDialog = new MooDialog({title: "Création de recette", size: {width: 700, height: 250}});
					creationDialog.setContent(new Element('div').set('html',jsonResult.content));
					creationDialog.open();
					if (jsonResult.js) {
						eval(jsonResult.js);
					}
				}
			}.bind(this)});
			sl.doRequest({app: 'repasrc', tpl: 'recipe/add_recipe'});
		}
	});
}

function editRecipe(recipeId) {
	var os = new Skelax.UI.Dialog({onReady: 
		function() {
			var sl = new Skelax.JSON.Query({onComplete: function(jsonResult) { 
				if (!jsonResult.error) {
					editionDialog = new MooDialog({title: "Modification de recette", size: {width: 700, height: 250}});
					editionDialog.setContent(new Element('div').set('html',jsonResult.content));
					editionDialog.open();
					if (jsonResult.js) {
						eval(jsonResult.js);
					}
				}
			}.bind(this)});
			sl.doRequest({app: 'repasrc', func: 'EditRecipe', funcParams: {id: recipeId}});
		}
	});
}

function duplicateRecipe(recipeId) {
	var os = new Skelax.UI.Dialog({onReady: 
		function() {
			var sl = new Skelax.JSON.Query({onComplete: function(jsonResult) { 
				if (!jsonResult.error) {
					duplicationDialog = new MooDialog({title: "Dupliquer une recette", size: {width: 620, height: 160}});
					duplicationDialog.setContent(new Element('div').set('html',jsonResult.content));
					duplicationDialog.open();
					if (jsonResult.js) {
						eval(jsonResult.js);
					}
				}
			}.bind(this)});
			sl.doRequest({app: 'repasrc', func: 'DuplicateRecipe', funcParams: {id: recipeId}});
		}
	});
}

function postMenuCreationForm(fields, params) {
	Skelax.JSON.send({app: 'repasrc', func: 'AddMenu', funcParams: {fields: fields}, update: 'content'});
	creationDialog.close();
}

function postMenuEditForm(fields, params) {
	Skelax.JSON.send({app: 'repasrc', func: 'UpdateMenu', funcParams: {fields: fields}, update: 'content'});
	editionDialog.close();
}

function newMenu() {
	var os = new Skelax.UI.Dialog({onReady: 
		function() {
			var sl = new Skelax.JSON.Query({onComplete: function(jsonResult) { 
				if (!jsonResult.error) {
					creationDialog = new MooDialog({title: "Création d'un menu", size: {width: 700, height: 180}});
					creationDialog.setContent(new Element('div').set('html',jsonResult.content));
					creationDialog.open();
					if (jsonResult.js) {
						eval(jsonResult.js);
					}
				}
			}.bind(this)});
			sl.doRequest({app: 'repasrc', tpl: 'menu/add_menu'});
		}
	});
}

function editMenu(menuId) {
	var os = new Skelax.UI.Dialog({onReady: 
		function() {
			var sl = new Skelax.JSON.Query({onComplete: function(jsonResult) { 
				if (!jsonResult.error) {
					editionDialog = new MooDialog({title: "Modification de menu", size: {width: 700, height: 180}});
					editionDialog.setContent(new Element('div').set('html',jsonResult.content));
					editionDialog.open();
					if (jsonResult.js) {
						eval(jsonResult.js);
					}
				}
			}.bind(this)});
			sl.doRequest({app: 'repasrc', func: 'EditMenu', funcParams: {id: menuId}});
		}
	});
}

function addRecipeToMenu(re_id, menu_id) {
	Skelax.JSON.send({app: 'repasrc', func: 'SetMenuRecipe', funcParams: {re_id: re_id, menu_id: menu_id}, update: 'blockCenter'});
}

function postMenuRecipeForm(fields, params) {
	Skelax.JSON.send({app: 'repasrc', func: 'UpdateMenuRecipe', funcParams: {fields: fields}, update: 'blockCenter'});
}

