<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs author="Pamela Fox" author_affiliation="University of Southern California" author_email="pamela.fox+coolgadget@gmail.com" author_location="Los Angeles,CA" description="Try and type as many profane words in 60 seconds as you can!" height="275" scaling="false" title="Profane Game" title_url="http://imagine-it.org">
<Require feature="setprefs" />
<Require feature="analytics" />
</ModulePrefs>
<UserPref datatype="hidden" default_value="0" name="highscore" />
<Content type="html">
<![CDATA[
<script type="text/javascript">
<pre>function _IG_FetchContent(url, callback, opt_params) {
  var params = opt_params || {};
  params["CONTENT_TYPE"] = gadgets.io.ContentType.TEXT;
  
  function wrapper(callback, obj) {
    callback(obj.data);
  };
  
  var cb = gadgets.util.makeClosure(null, wrapper, callback);
  gadgets.io.makeRequest(url, cb, params);
}
</script>
]]><![CDATA[
<script type="text/javascript">

var currentColor = "even";
var curTime = 120;
var currentWords = new Array();
var currentScore = 0;
var timer;
var gameEnabled = false; 
var wordDefDefault = "<em>Mouse over words that aren't italicized (still being checked) or striken out (=not profane!) to see meaning.</em>";
var rejectedWords = new Array();


var prefs = new gadgets.Prefs(__MODULE_ID__);
_IG_Analytics("UA-964209-1", "/profanegame/");

function loadGame() {
  document.getElementById("highScoreDIV").innerHTML = "<strong>YOUR HIGH SCORE: <span class='scores'>" + prefs.getInt("highscore") + "</span></strong>";   
  document.getElementById("topHighScoreDIV").innerHTML = "<strong>TOP SCORE: <span class='scores>??</span></strong>";

  _IG_FetchContent(("http://imagine-it.org/google/pg_writehiscore.php?rand="+rand(2000)),updateTopHighScore);
}

function updateTopHighScore(response){
  document.getElementById("topHighScoreDIV").innerHTML = "<strong>TOP SCORE: <span class='scores'>" + response + "</span></strong>";
}

function doNothing(){
}

// Function that's called when clock is running
// Determines when time's out and performs game-over functions
function startClock(){
   curTime--;
   document.getElementById("timerDIV").innerHTML = "TIME LEFT: <span class='scores'>" + curTime + '</span>';
   if(curTime==0){
      gameEnabled = false;
      document.getElementById("timerDIV").innerHTML = "<span class='scores'>TIME'S UP!</span>";	
      var words="";
      for(var i=0; i<rejectedWords.length; i++){
        words += rejectedWords[i] + ",";
      }
      _IG_FetchContent("http://imagine-it.org/google/sendprofane.php?words="+words+","+currentScore,doNothing);
      _IG_FetchContent(("http://imagine-it.org/google/pg_writehiscore.php?rand="+rand(2000)+"&hiscore="+currentScore),updateTopHighScore);

      if(prefs.getInt("highscore") < currentScore){
	prefs.set("highscore",currentScore);
        document.getElementById("highScoreDIV").innerHTML = "<strong>YOUR HIGH SCORE: <span class='scores'>" + currentScore + "</span></strong>";   
      }
      curTime = 60;
   } else {
   timer = setTimeout("startClock()",1000);
  }
}

// submit word to table
// calls PHP scripts to check validity of word
function submitWord() {
    var found = false;

    var currentWord = document.getElementById("wordINPUT").value;
    currentWord = currentWord.split(" ")[0].replace(/[.]+/,"");
    document.getElementById("wordINPUT").value = "";

    if(gameEnabled==true){      

      for(var i=0; i<currentWords.length;i++){
 	if(currentWords[i]==currentWord) found=true;
      }
    if(!found){
      var wordsTableBody = document.getElementById("boggleWordsTableBody");
      var newWordTR = document.createElement("TR");
      var newWordTD1 = document.createElement("TD");
      var newWordA = document.createElement("A");
      newWordTR.id = currentWord + "TR";
      newWordTR.className = "even";
      newWordTR.style.fontStyle = "italic";
      newWordA.id = currentWord + "A";
      newWordA.appendChild(document.createTextNode(currentWord));
      newWordTD1.appendChild(newWordA);
      newWordTR.appendChild(newWordTD1);
      wordsTableBody.insertBefore(newWordTR, wordsTableBody.firstChild);
      _IG_FetchContent(("http://imagine-it.org/google/getprofane.php?rand="+rand(2000)+"&word="+currentWord),updateScore);
      currentWords.push(currentWord);
      document.getElementById("wordDefDiv").innerHTML = wordDefDefault;
      }
    } else {
      alert("Press New Game button to begin!");
    }
    return false;
}
function updateScore(response) {
    var responseArray = response.split("\n");
    var word = responseArray[1];
    var found = responseArray[2];

    var wordTR = word + "TR";
    var wordA = word + "A";
    document.getElementById(wordA).href = "#";
    if(found=="0"){
        currentScore -= 1;
        document.getElementById(wordTR).style.textDecoration = "line-through";
	document.getElementById(wordTR).className = "odd";
	rejectedWords.push(word);
   } else {
       	document.getElementById(wordTR).style.fontStyle = "normal";
        currentScore += 2;
	var wordDef = responseArray[3].substring(0,80);
	document.getElementById(wordA).href = "http://www.urbandictionary.com/define.php?term="+word;
	document.getElementById(wordA).target = "_blank";
	document.getElementById(wordA).onmouseover = function() {
	document.getElementById("wordDefDiv").innerHTML = word + " : " + wordDef + "..."; }
   
   }

   document.getElementById("scoreDIV").innerHTML = "SCORE: <span class='scores'>" + currentScore + '</span>';

}

// Does the rand stuff to generate 1-n integer
function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}
gadgets.util.registerOnLoadHandler(function() { loadGame(); });

function startNewGame(){
 
   clearTimeout(timer);
   curTime=60;
   startClock();

   currentScore=0;
   document.getElementById("scoreDIV").innerHTML = "SCORE: <span class='scores'>" + currentScore + '</span>';

   currentWords.length = 0;
   rejectedWords.length = 0;

    var wordsTableBody = document.getElementById("boggleWordsTableBody");
   while(wordsTableBody.firstChild){
     wordsTableBody.removeChild(wordsTableBody.firstChild);
   }   

   gameEnabled=true;
   document.getElementById("wordINPUT").focus();

}


</script>
<style type="text/css">
A {
  color:#000000;
}
A:visited {
  color:#000000;
}

A:active {
  color:#cd8eb6;
}

#gridTable {
   width: 130px;
   height: 150px;
}

.noDef {
  text-decoration: line-through;
}

#boggleWordsDIV {
  //overflow: hidden;
  border: 1px solid #000000;
  margin: 2px;
  height:140px;
  //overflow: auto:
  //overflow-y: auto;
  text-align:center;
  overflow:-moz-scrollbars-vertical;
  overflow-y: scroll;
  max-height:180px;
  font-size:10pt;
}


tr {
  text-align: center;
}

tr.odd {
  font-size:8pt;
  background-color: #ff0000;
}
tr.even {
 font-size:8pt;
 background-color: #b6d79f;
}

#wordDefDIV {
 border-top: 1px solid #000000;
 font-size: 10pt;
 padding-top:10px;
}

#boggleBoardDIV {
 width:150px;
 float:left;
}
.scores {
 font-size: 10pt;
 font-weight: bold;
}

</style>
<div style="margin-top: 10px;">
 <div style="margin-bottom:10px;">
  <div style="padding:5px; float:left; font-size:9pt; font-weight:bold;">
   <a href="#" onclick="startNewGame();">[NEW GAME]</a> 
  </div>
  <div id="topHighScoreDIV" style="padding:5px; float:right; font-size:6pt;">
  </div>

  <div id="highScoreDIV" style="padding: 5px; float:right; font-size:6pt;">
  </div>
 </div>
<br clear="all"/>
 <div id="timerDIV" style="float:left; padding:5px; color:#FF0000; font-size:6pt; font-weight:bold">
 </div>
<div id="scoreDIV" style="float:right; font-size:6pt; padding:5px;">
</div>
<br clear="all" />
<div id="boggleBoardDIV" style="float:left;">
 <br/>
 <form onsubmit="return submitWord()">
 <input id="wordINPUT" type="text" size="20" class="gridSquare" value="" />
 </form>
 <div id="wordDefDiv">Rules: Get points by typing profane words above. Press "enter" after entering a word. Click "New Game" to begin.</div>
 </div>
</div>

<div id="boggleWordsDIV">
 <div id="lettersOutDIV">...</div>

 <TABLE id="boggleWordsTable" width="100%">
    <TBODY id="boggleWordsTableBody">
    </TBODY>
 </TABLE>
</div>
<br clear="all"/>
<div style="fo-size:9pt; font-style:italic;">
Like games? Take our <a href="http://imagine-it.org/gamessurvey/" target="_blank">survey on video games</a>.
</div>
<!--
<div style="font-size:9pt; font-style:italic;">
Made possible by the <a href="http://www.webpurify.com" target="_blank">WebPurify</a> & <a href="http://www.urbandictionary.com" target="_blank">UrbanDictionary</a> APIs.
</div>
-->
]]></Content>
</Module>
