// ==UserScript==
// @name           UrbanDead notepad
// @namespace      LifeTimeBLOOD
// @description    Provides a notepad under the UrbanDead navigation menu
// @include        http://www.urbandead.com/map.cgi
// @include        http://www.urbandead.com/map.cgi?zoom
// ==/UserScript==


function saveUDnotepad()
{
    //GM_log(document.getElementById("notepad").value);
    GM_setValue("ud_notepad_content", document.getElementById("notepad").value);
    ToggleSavedText(true);
}

var f_notepad = document.createElement("div");
f_notepad.innerHTML = '<br /> \
<form id="form1" name="form1" method="post" action="" onsubmit="return false;"> \
 \
    <textarea name="notepad" id="notepad" cols="35" rows="5">'+ GM_getValue("ud_notepad_content", "Welcome to the UD notepad v1.0!\nYou can now start writing notes in here and save them with the link provided below this form :)") +'</textarea><br /> \
    <a href="" id="savenotepad" onclick="return false;" >Save</a> \
    <div id="ud_notepad_savedtext" class="y" style="width:4em;">&nbsp;Saved!</div> \
 \
</form> \
';

function ToggleSavedText(visible)
{
    if(visible)
    {
      document.getElementById("ud_notepad_savedtext").style.visibility = "visible";
    }
    else
      document.getElementById("ud_notepad_savedtext").style.visibility = "hidden";
}

function doTheMagick()
{
  var atags = document.getElementsByTagName("a");
  for (var i = 0; i < atags.length; ++i)
  {
      //GM_log("Debug: i:" + i + " a:" + atags[i].firstChild.data);
      if(atags[i].firstChild.data == "Donate")
      {
        //GM_log( "Found donate button! " + i );
        atags[i].parentNode.insertBefore(f_notepad, atags[i].nextSibling);
        
        var f_notepad_save = document.getElementById("savenotepad");
        f_notepad_save.addEventListener("click", function() {saveUDnotepad(); }, true);
        
        ToggleSavedText(false);
        //atags[i].appendChild(f_notepad);
        break;
      }
      
  }
}
window.addEventListener('load', doTheMagick, false);
