User:Tyilo/ggpcatering.js

$(function {	var flavours = ['bitter', 'fruity', 'savoury', 'sour', 'spicy', 'sweet'];	var max_ingredients = 4;	var different_flavours = 3;	var flavour_values = [4, 2, 1];	var ingredients = {		'raisins':      ['fruity', 'spicy', 'sweet'],		'tupelo honey':  ['sweet', 'fruity', 'spicy'],		'bitter melon':  ['bitter', 'fruity', 'sweet'],		'plum':          ['fruity', 'sweet', 'spicy'],		'cinnamon':      ['spicy', 'sweet', 'bitter'],		'ginger':        ['spicy', 'savoury', 'fruity'],		'paprika':       ['savoury', 'spicy', 'fruity'],		'filo pastry':   ['savoury', 'sweet', 'fruity'],		'sour cream':    ['sour', 'savoury', 'sweet'],		'yoghurt':       ['sour', 'bitter', 'fruity'],		'rhubarb':       ['sour', 'fruity', 'sweet'],		'burned toffee': ['sweet', 'sour', 'spicy'],		'mint':          ['sweet', 'bitter', 'savoury'],		'nutmeg':        ['spicy', 'sour', 'savoury'],		'dandelion':     ['bitter', 'spicy', 'sour'], 'coffee beans': ['bitter', 'savoury', 'sweet'], 'raspberries':  ['fruity', 'sour', 'bitter'], 'pepper':       ['savoury', 'bitter', 'sour'] };	var images = {}; var container = $('#ggpcatering'); if(container.length === 0) { return; }	var inputs = $; $.each(flavours, function(index, flavour) {		var checkbox = $(' ').data('flavour', flavour);		var span = $(' ').text(' ' + capitalize(flavour)).prepend(checkbox);		inputs = inputs.add(span);	}); $(container).append(inputs); $(' Clear ').appendTo('body').click(function {		inputs.find('input').each(function { this.checked = false; });		inputs.change;	}); result = $(' ').appendTo(container); preloadImages; inputs.change(function {		var selected = inputs.find('input:checked');		var left_to_choose = different_flavours - selected.length;		var not_selected = inputs.find('input:not(:checked)');		if(left_to_choose === 0) {			not_selected.attr('disabled', true);			var selected_flavours = [];			selected.each(function { selected_flavours.push($(this).data('flavour')); });			var best = best_ingredients(selected_flavours);			result.html('');			$.each(best, function(index, ingredient) { var image = $(' ').attr('src', images[ingredient]); var div = $(' ').text(capitalize(ingredient)).prepend(image); result.append(div); });		} else {			not_selected.attr('disabled', false);			result.text('Please select ' + left_to_choose + ' more flavour' + (left_to_choose > 1? 's': '') + '.');		}	}); function best_ingredients(selected_flavours) { var values = []; $.each(ingredients, function(ingredient, flavours) {			var value = 0;			$.each(flavours, function(index, flavour) { if($.inArray(flavour, selected_flavours) > -1) { value += flavour_values[index]; }			});			values.push({ ingredient: ingredient, value: value });		});		values.sort(function(a, b) {			return b.value - a.value;		}); var best = []; for(var i = 0; i < max_ingredients; i++) { best.push(values[i].ingredient); }		return best; }	function preloadImages { $.each(ingredients, function(ingredient, values) {			getFile(ingredient + '.png', function(filename) { images[ingredient] = filename; (new Image).src = filename; });		});	}	function getFile(path, callback) { wikia('', function(response) {			callback($(response.parse.text['*']).find('img').attr('src'));		}); }	function wikia(code, callback) { $.ajax({			data: {				action: 'parse',				text: code,				prop: 'text',				format: 'json'			},			dataType: 'json',			type: 'POST',			url: wgScriptPath + '/api.php',			success: callback,		}); }	function capitalize(string) { return string.toLowerCase.replace(/^./, function(match) {			return match.toUpperCase;		}); } });