// ==UserScript==
// @name           UD IWitness Companion
// @namespace      www.ray3k.com
// @description    Enables users to make IW reports without deactivating other user scripts.
// @include        http://www.urbandead.com/map.cgi*
// @include        http://*urbandead.com/map.cgi*
// @exclude        http://www.urbandead.com/map.cgi?logout
// @exclude        http://*urbandead.com/map.cgi?logout
// ==/UserScript==

//Get the unmodified HTML from the body of the Urban Dead page
var bodyHTML = getBodyHTML();

//find the donate link on the page
var donateLink = findDonateLink();

//if the donate link was found
if (donateLink != null) {
    //add the IWitness links below the donation link
    createIWLinks(donateLink, bodyHTML);
}
//the donate link was not found
else {
    //error
    GM_log("IWitness Companion: Could not locate the donation link");
}

//finds the donation link on the page
function findDonateLink() {
    //find the donate link
    var allDonateLinks, donateLink;
    allDonateLinks = document.evaluate(
        "//a[@href='donate.html']",
        document,
        null,
        XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
        null);
    
    //if the donate link was found
    if (allDonateLinks.snapshotLength > 0) {
        donateLink = allDonateLinks.snapshotItem(0);
        return donateLink;
    }
    //the donate link was not found
    else return null;
}

//Creates links for public and private IWitness reports
function createIWLinks(donateLink, bodyHTML) {
    //create the public link
    var publicLink = document.createElement("a");
    publicLink.href = "Public IWitness Record";
    publicLink.className = "y";
    publicLink.appendChild(document.createTextNode("IW Public"));
    
    //add an on click function to the link
    publicLink.addEventListener('click', function(event) {
        //The following code has been modified from the original IWitness bookmarklet.
        //See http://iwitness.urbandead.info/ for the original IWitness.
        d=new Date();
        wW=window.open("",d);
        wW.document.write(
            "<HTML><BODY>" +
            "<FORM NAME='wF' ACTION='http://iwitness.urbandead.info/wSubPA.php' METHOD='POST'>" +
            "<INPUT NAME='wP' VALUE='PRIVATE'>" +
            "<INPUT NAME='wC' VALUE='" + prompt("enter Iwitness comment - may be blank") + "'>" +
            "<INPUT NAME='wT' VALUE='" + window.document.lastModified + "'>" +
            "<INPUT NAME='wZ' VALUE='" + d.getTimezoneOffset() + "'>" +
            "<INPUT NAME='wV' VALUE='23'>" +
            //add the original bodyHTML instead of the current document.body.innerHTML
            "<TEXTAREA NAME='wS'>" + bodyHTML + "</TEXTAREA>");
        wW.document.getElementsByName("wF")[0].submit();
        //end IWitness code
        
        //prevent click from trying to open the publicLink
        event.stopPropagation();
        event.preventDefault();
    }, true);
    
    //add the link to the page
    donateLink.parentNode.insertBefore(publicLink, donateLink.nextSibling);
    publicLink.parentNode.insertBefore(document.createTextNode(" "), publicLink);
    
    //create the private link
    var privateLink = document.createElement("a");
    privateLink.href = "Private IWitness Record";
    privateLink.className = "y";
    privateLink.appendChild(document.createTextNode("IW Private"));
    
    //add an on click function to the link
    privateLink.addEventListener('click', function(event) {
        //The following code has been modified from the original IWitness bookmarklet.
        //See http://iwitness.urbandead.info/ for the original IWitness.
        d=new Date();
        wW=window.open("",d);
        wW.document.write(
            "<HTML><BODY>" +
            "<FORM NAME='wF' ACTION='http://iwitness.urbandead.info/wSubPA.php' METHOD='POST'>" +
            "<INPUT NAME='wP' VALUE='PRIVATE'>" +
            "<INPUT NAME='wC' VALUE='" + prompt("enter Iwitness comment - may be blank") + "'>" +
            "<INPUT NAME='wT' VALUE='" + window.document.lastModified + "'>" +
            "<INPUT NAME='wZ' VALUE='" + d.getTimezoneOffset() + "'>" +
            "<INPUT NAME='wV' VALUE='23'>" +
            //add the original bodyHTML instead of the current document.body.innerHTML
            "<TEXTAREA NAME='wS'>" + bodyHTML + "</TEXTAREA>");
        wW.document.getElementsByName("wF")[0].submit();
        //end IWitness code
        
        //prevent click from trying to open the privateLink
        event.stopPropagation();
        event.preventDefault();
    }, true);
    
    //add the link to the page
    donateLink.parentNode.insertBefore(privateLink, donateLink.nextSibling);
    privateLink.parentNode.insertBefore(document.createElement("p"), privateLink);
}

//returns
function getBodyHTML() {
    return document.body.innerHTML;
}
