var Error_Grammar =  {
	S: [
		"<h1> %title </h1> <p class='error'> %error </p>"
	],
	
	title : ["Oh Boy!", "Bugger!", "404... 503... Whatever...", "Not again!", "Yikes!"],
	
	error: ["%description %fix"],
	
	description: [
		"Something went wrong.",
		"You're ugly and your mother dresses you funny. This is why I don't want you do view this page!",
		"Did you expect anything particular to be here?",
		"Some ancient evil from beyond this universe ate the page.",
		"You're not clicking on that link just to read new random error messages, are you?",
		"This page doesn't exist anymore. If it ever has, that is.",
		"No, no, no, you did it all wrong again!"
	],
	fix : [
		"%conj %fix2", "Have you tried %activities ?", "You've been %activities again, haven't you?", "Stop %activities and maybe %fix2"
	],
	conj : ["Maybe you should %activity while", "Okay , %activity while ",  "Why don't you %activity while ", "You could %activity while" ],
	fix2 : [
		"I try to fix that.", 
		"I attend to that matter. Sometime.", 
		"I work on that.",
		"I do some debugging."
		],
		
	activity : [
		"try %activities",
		"improve at %activities",
		"attempt %activities",
	],
		
	activities: [
        "%slackings",
        "%slackings",
        "%slackings",
        "%wear",
        "%food_acts %ingredients %dishes"
    ],
	
    wear: [
        "wearing %colours",
        "wearing %colours %wearables",
    ],
	wearables: ["lab coats", "hoodies", "safety goggles", "umbrellas", "undies"],
    ingredients: ["meat", "fish", "potato", "cabbage", "egg", "turnip", "carrot", "veal", "jalapeno", "lettuce", "milk", "sausage", "ham", "tofu", "cheese", "seafood", "fruit", "cream"],
    dishes: ["pies", "sandwiches", "omelettes", "tacos", "stew", "pizza", "ravioli", "salsa", "jam", "tagine", "casserole", "sorbet", "confit", "trifle"],
    slackings: ["lazing about", "debugging running code", "sleeping", "daydreaming", "wasting time with %groups", "slacking off", "using Microsoft Paint for finishing up graphs", "spending the night in the lab", "talking nonsense", "looking at white walls", "painting meaningless arrows onto whiteboards", "peeking your nose", "writing proposals", "spitting at %groups", "throwing stones at %groups", "ranting at %groups","smiling at %groups", "harassing  %groups"],
    colours: ["red", "orange", "yellow", "green", "blue", "purple", "indigo","violet", "lavender", "white", "black", "brown", "mauve"],
    opinions: ["loves", "hates", "detests", "loathes","gets a kick out of", "dreads", "is ambivalent about","enjoys", "dreams of"],
    smells: ["blank paper", "sweat", "room freshener", "server rooms", "monkey wee","chocolate", "curry", "freshmen", "lab coats", "blood", "vomit","napalm", "rusty metal", "dusty keyboards", "dead lab rats", "fraudulent statistics", "expensive lab equipment", "badly programmed code", "cryptic formulas"],
    
    groups: ["freshers", "strangers", "inanimate objects", "lab rats", "students", "colleagues", "dead cats", "robotic beings", "secretaries", "low-paid staff members", "your imaginary friends", "visitors of this homepage", "sys admins", "computational linguists"],

    food_acts: ["making", "eating", "selling"]
	
		
}

var Ix_Grammar =  {
    S: [
        "He's a monster. He's a research assistant. He %description .",
        "He's a genetically modified plumber's helper and %description .",
        "Hey may be scary. Maybe because he %description .",
        "His name derives from the Mayan goddess of suicide, but he %description .",
        "The name is Ix. He %description .",
        "He %description but %description .",
        "Ix %description .",
        "He may only be a mascot, but he %description .",
        "Beware, he %description .",
    ],
    
    description: [
        "%opinion",
        "%activity"
    ],
    
    activity: [
        "is %frequencies found %activities %optional_time",
        "is %frequencies seen %activities %optional_time",
        "'s %levels at %activities %optional_time",
        "'s %levels at %activities %optional_time",
        "%optional_frequency %gestures at %groups",
        "%optional_frequency %gestures at %groups",
        "%optional_frequency %gestures at %groups"        
    ],
    
    opinion: [
        "%opinionmod %opinions %activities %optional_time",
        "%opinionmod %opinions %activities %optional_time",
        "%opinionmod %opinions the smell of %smells %optional_time",
        "%opinionmod %opinions the odour of %smells %optional_time",
        "%opinionmod %opinions %smells %optional_time",
        "%opinionmod %opinions %groups %optional_time"
        
    ],
    
    activities: [
        "%slackings",
        "%slackings",
        "%wear",
        "%food_acts %ingredients %dishes"
    ],
    
    opinionmod : [
    	"", "", "secretly", "openly", "blatantly", "privately"
    ],
    
    wear: [
        "wearing %colours",
        "wearing %colours %wearables",
    ],
    
    optional_time: ["", "", "%times"],
    optional_frequency: ["", "", "%frequencies"],
    
    
    wearables: ["lab coats", "hoodies", "glasses", "safety goggles", "umbrellas", "undies"],
    ingredients: ["meat", "fish", "potato", "cabbage", "egg", "turnip", "carrot", "veal", "jalapeno", "lettuce", "milk", "sausage", "ham", "tofu", "cheese", "seafood", "fruit", "cream"],
    dishes: ["pies", "sandwiches", "omelettes", "tacos", "stew", "pizza", "ravioli", "salsa", "jam", "tagine", "casserole", "sorbet", "confit", "trifle"],
    levels: ["awesome", "legendary", "great", "good", "mediocre", "passable", "competent", "bad", "terrible", "dire", "lucky", "irritatingly good", "hardcore", "magnificent", "rubbish"],
    slackings: ["lazing about", "debugging running code", "sleeping", "daydreaming", "wasting time", "slacking off", "using Microsoft Paint for finishing up graphs", "spending the night in the lab", "talking nonsense", "looking at white walls", "harassing students", "painting meaningless arrows onto whiteboards", "peeking his nose", "writing proposals"],
    colours: ["red", "orange", "yellow", "green", "blue", "purple", "indigo","violet", "lavender", "white", "black", "brown", "mauve"],
    opinions: ["loves", "hates", "detests", "loathes","gets a kick out of", "dreads", "is ambivalent about","enjoys", "dreams of"],
    smells: ["blank paper", "sweat", "room freshener", "server rooms", "monkey wee","chocolate", "curry", "freshmen", "lab coats", "blood", "vomit","napalm", "rusty metal", "dusty keyboards", "dead lab rats", "fraudulent statistics", "expensive lab equipment", "badly programmed code", "cryptic formulas"],
    
    frequencies: ["often", "frequently", "occasionally", "sometimes", "never"],
    groups: ["freshers", "strangers", "inanimate objects", "lab rats", "students", "colleagues", "dead cats", "robotic beings", "secretaries", "low-paid staff members", "imaginary friends", "visitors of this homepage", "sys admins", "computational linguists"],
    gestures: ["spits", "waves", "whistles", "throws stones", "rants", "winks","smiles", "grins", "screams"],
    times: ["in the morning", "while sleepy", "during lunch", "after work","on public holidays", "at inappropriate times", "during lectures", "late at night", "during meetings", "outside office hours", "on conferences", "in the lab", "before lectures", "when nobody is around", "when students can't see him"],
    food_acts: ["making", "eating", "selling"]
}

var Parser = {
    parse: function(grammar) {
        var sentence = Parser.parse_node(grammar, grammar.S);
        sentence = Parser.fix_punctuation(sentence);
        return sentence;
    },
    
    fix_punctuation: function(sentence) {
        var punctiation = ['.',',','!','?',':',"'"];
        punctiation.each(function(p) {
            sentence = sentence.gsub(" "+p, p);
        });
        return sentence;
    },
    
    expand: function(node) {
        // First select a child
        child_str = node[Math.floor(Math.random()*node.size())];
        // Split it into elements and return
        return $w(child_str)
    },
    
    parse_node: function(grammar, node) {
        children = Parser.expand(node);
        string = "";
        children.each(function (child) {
            if (child.startsWith("%")) {
                if (grammar[child.substring(1)] == undefined) debug("Node "+child+" not found in grammar!");
                else string += Parser.parse_node(grammar, grammar[child.substring(1)]);
            }
            else string += child + " ";
        });
        return string
    }
    
}

