var landscape = false;
var types = {syn: "synonyms", 
             ant: "antonyms", 
             rel: "related terms", 
             sim: "similar terms",
             usr: "user suggestions"};

var historyData = new Array();
var historyHash = new Object();
var historyCookie = null;

function setup() {
  orientationChanged();
  historyCookie = new Cookie("ThesaurusHistory");
  var oldHistory = [];
  if (historyCookie.get()) {
    try {
      oldHistory = eval("[" + historyCookie.get() + "]");
    }
    catch (e) {
    }
  }
  for ( var i = 0; i < oldHistory.length; i++) {
    addToHistory(oldHistory[i]);
  }
}

function send() {
  var word = $("Source").value;
  if (word.length > 0) { 
    load(word);
  }  
}

function load(word) {
  var parent = $("Data").parentNode;
  parent.removeChild($("Data"));
  var data = document.createElement("script");
  data.type = "text/javascript";
  data.src = "http://words.bighugelabs.com/api/2/0a7f0a5c32ee2336ce122a46f463934b/" + encodeURIComponent(word) + "/json?callback=receive";
  data.id = "Data";
  data.onerror = function() {
    $("Loading").style.display = "none";
    $("Result").innerHTML = "Request Failed";    
  }
  data.onload = function() {
    $("Loading").style.display = "none";
    if ($("Result").innerHTML.length == 0) {
      $("Result").innerHTML = "Word not found";    
    }  
  }
  parent.appendChild(data);
  addToHistory(word);
  $("Result").innerHTML = "";    
  $("Loading").style.display = "block";
}

function receive(data) {
  var result = "";
  for (var key in data) {
  	result += "<div class='wordType'>" + key + ":";
  	for (var type in data[key]) {
  	  var typeWord = types[type];
  	  if (!typeWord) {
  	    typeWord = type;
  	  }  	  
    	result += "<div class='wordType2'>" + typeWord + ":<div class='words'>";
    	var words = data[key][type]; 
    	for (var i = 0; i < words.length; i++) {
    	  if (i > 0) {
    	    result += ", "
    	  }
    		result += "<div class='word' onclick='insert(\"" + words[i] + "\")'>" + words[i] + "</div>";
    	}
    	result += "</div></div>";
  	}
  	result += "</div>";
  }
  $("Result").innerHTML = result;
}

function insert(word) {
  $("Source").value = word;
  window.scrollTo(0, 50);
  //window.open("#Form", "_self");
}

function addToHistory(word) {
  $("NoHistory").style.display = "none";
  $("HistoryClear").style.display = "block";
  var element = historyHash[word];
  if (element != null) {
    historyData.splice(element.index, 1);
    element.parentNode.removeChild(element);
  }  
  element = document.createElement("div");
  element.className = "word";
  element.onclick = function() {
    insert(word);
  }
  element.innerHTML = word;
  historyData.push(element);
  element.index = historyData.length - 1;
  element.word = word;
  historyHash[word] = element;
  if ($("History").firstChild) {
    if ($("History").innerHTML.length > 0) {
      element.innerHTML = element.innerHTML + ", ";
    }
    $("History").insertBefore(element, $("History").firstChild);
  }
  else {
    $("History").appendChild(element);
  }
  $("HistorySection").style.display = "block";
  if (historyData.length > 30) {
    historyData[0].parentNode.removeChild(historyData[0]);
    delete historyHash[historyData[0]];
    historyData.shift();
  }
  if (historyData[0].innerHTML.indexOf(", ") >= 0) {
    historyData[0].innerHTML = historyData[0].innerHTML.substring(0, historyData[0].innerHTML.length - 2);
  }
  saveHistory();
}

function clearHistory() {
  $("NoHistory").style.display = "block";
  $("HistoryClear").style.display = "none";
  $("History").innerHTML = "";
  $("HistorySection").style.display = "none";
  historyData = new Array();
  historyHash = new Object();
  saveHistory();
}

function saveHistory() {
  var text = "";
  for ( var i = 0; i < historyData.length; i++) {
    if (i > 0) {
      text += ",";
    }
    text += "'" + historyData[i].word + "'";
  }
  historyCookie.store(text);
}

function $(id) {
  return document.getElementById(id);    
}

function tellFriend() {
  var body = "Hi,<br><br>I just stumbled upon this iPhone thesaurus:" +
      "<br><br>http://thesaurus.speedymarks.com<br><br>" +
      "Retrieve the synonyms for every word." +
      "<br><br>Best regards";
  window.open("mailto:?subject=Thesaurus on the iPhone&body=" + body, "_self");  
}

function orientationChanged() {
  if (window.orientation != undefined) {
    landscape = window.orientation != 0 && window.orientation != 180;
  }
  else {
    landscape = window.innerWidth > window.innerHeight;
  }  
  setTimeout(function() {window.scrollTo(0,1)}, 1);
}
