// Init to make event listener for clicking words $(init); // Run the scrupt when a word is clicked function init() { $("li").click(getSyn); $("u").click(getSyn); // Some sidebar instructions $("#synonyms div").hide(); $("#synonyms").append('

What\'s this?

When you click a hyperlinked (highlighted) word in the story, starting with a word in the title, related words will show up in this area. You may then choose one of the words, and the story will proceed to write itself with every click! If you want to see a new set of words, you can just click another hyperlinked word in the story.

'); } // end init function initNew() { $("u").unbind().bind("click",getSyn); $("#synonyms div ul li").unbind().bind("click", getSyn).bind("click", insertSyn); } // end init // Make a global variable so you know which word to replace later on var selectedType; // Here's the script. It gets the JSON result from the bighugelabs.com API via a local PHP script. function getSyn() { selectedType = $(this).closest('div').attr("id"); // Set selectedType variable // In case it was hidden from error handling, show it and remove other messages $("#sideMessage").remove(); $("#synonyms div").show(); $("#synonyms div ul").text(""); // Clear the current synonym list var selectedWord = $(this).text(); $.ajaxSetup({ "error":processError }); // If the word doesn't have matches from the following action, take care of it $.getJSON("/getWord.php?word="+selectedWord, processResult); } // end getSyn // Make counter to keep track of how many times the following function runs var newSentenceCount = 0; // And's the script for when you click in the synonym list. function insertSyn() { newSentenceCount++; // Add one to the run counter var selected = $(this).text(); // Find the next hidden sentence, find the first instance therein of a word of the type that was selected, // and replace it with the selected word. $("#story").children("span").each( function(i, l){ if(i == (newSentenceCount - 1)) { var firstMatch = $(this).find('.'+selectedType+':eq(0)'); $(firstMatch).text(selected).attr("class", "wasReplaced"); $(this).show(); // Finally, show the new sentence } }); } // end insertSyn // Callback function on getSyn script completion, to actually do the stuff with the returned data function processResult(data) { // In case it was hidden from error handling, show it and remove other messages $("#sideMessage").remove(); $("#synonyms div").show(); // For each type of word, be it noun, verb, etc. for(synType in data) { var typeSet = data[synType]; // Make var for future reference //$("#page1").append(synType); // Write down if it's a noun or what // For each sub group in the JSON object, which happens to classify things by synonym, antonym, related word, etc. for(synOrAnt in typeSet) { var count = 0; // Set a counter so it doesn't run through too many words var wordSet = typeSet[synOrAnt]; // Make var for future reference // Set var for use in CSS class var liClass; if (synOrAnt == "syn") liClass = "syn"; else if (synOrAnt == "ant") liClass = "ant"; else liClass = "notSynOrAnt"; // For each actual word for(synonym in wordSet) { count = count + 1; // Add to the counter if(count > 12) { break; // Get out of this loop if you've already done a bunch of words } var result = wordSet[synonym]; // Make a var for use $("#synonyms #"+synType+" ul").append('
  • '+result+'
  • '); // Show the resulting synonym } } } $("#story").scrollTop($("#story")[0].scrollHeight); // Scroll to bottom to keep story visible initNew(); // Initialize again so a newly displayed word can be clicked } // end processResult // error handling function processError() { $("#synonyms div").hide(); $("#synonyms").append('

    What\'s this?

    That word doesn\'t have any matches. Click a hyperlinked word in the story to continue.

    '); } // end processError