/**
 * nudecode.js
 * nudemap code using Google Maps API
 *
 * Date: December, 2007
 */


var START_ICON = new GIcon(G_DEFAULT_ICON, "http://www.google.com/intl/en_us/mapfiles/dd-start.png");
var PAUSE_ICON = new GIcon(G_DEFAULT_ICON, "http://www.google.com/intl/en_us/mapfiles/dd-pause.png");
var END_ICON     = new GIcon(G_DEFAULT_ICON, "http://www.google.com/intl/en_us/mapfiles/dd-end.png");


/**
 * Returns the query string arguments.
 */
function getArgs() {
    var args = new Object();
    var query = document.location.search.substring(1);
    var pairs = query.split("&");
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
        if (pos == -1) continue;
        var argname = pairs[i].substring(0, pos);
        var value = pairs[i].substring(pos+1);
        args[argname] = unescape(value);
    }

    return args;
}


/**
 * This function creates a new DOM document.  Remember this
 * function is creating a new _representation_ of an xml document
 * in memory--it's not creating an xml file.  This function _only_
 * creates the empty local object--it does not populate it.
 * 
 * The code wouldn't be difficult except it has to be a mess to
 * accommodate IE users.  
 *
 * I'm afraid I don't know much about javascript namespaces yet,
 * so I'm not sure the significance or usefulness of the "XML".
 *
 * rootTagName - the name of the root element in the document
 * namespaceURL - the xml namespace thing.  We won't be using it.
 *
 * returns: the xml document, either a DOM document in W3C, or
 * an ActiveXObject in IE.
 */
function newDocument(rootTagName, namespaceURL) {
    if (!rootTagName) rootTagName = "";
    if (!namespaceURL) namespaceURL = "";

    if (document.implementation &&
            document.implementation.createDocument) {
        // This is the standard W3C way to do it
        return document.implementation.createDocument(
                namespaceURL, rootTagName, null);
    } else { // This is the IE way to do it
        // Create an empty document as an ActiveX object
        // If there is no root element, this is all we have to do
        var doc = new ActiveXObject("MSXML2.DOMDocument");

        // If there is a root tag, initialize the document
        if (rootTagName) {
            // Look for a namespace prefix
            var prefix = "";
            var tagname = rootTagName;
            var p = rootTagName.indexOf('"');
            if (p != -1) {
                prefix = rootTagName.substring(0, p);
                tagname = rootTagName.substring(p + 1);
            }

           // if we have a namespace, we must have a namespace prefix
           // if we don't have a namespace, we discard any prefix
           if (namespaceURL) {
               if (!prefix) prefix = "a0"; // What Firefox uses
           } else {
               prefix = "";
           }

           // Create the root element (with optional namespace) as a string of text
           var text = "<" + (prefix?(prefix+":"):"") + tagname
                   + (namespaceURL?(" xmlns:" + prefix + '="' + namespaceURL
                   + '"'):"") + "/>";
            // And parse that text into the empty document
           doc.loadXML(text);
        }
        return doc;
    }
};


/**
 * This function creates a new xml document object (using
 * newDocument() above), and loads a file into it.  Note that
 * load() is a member of the document that is returned, either
 * the DOM object for W3C, or the ActiveXObject for IE.
 *
 * This function loads the file synchronously (which means it
 * doesn't return until the file is loaded).  Use loadAsync()
 * below if you don't want to wait.
 *
 * url - the URL of the xml file to load.
 * returns: the document object gotten from newDocument,
 * with the specified xml file loaded and parsed into it.
 */
function loadXML(url) {
    // Create a new document with the previously defined function
    var xmldoc = newDocument();
    xmldoc.async = false;  // We want to load synchronously
    xmldoc.load(url);         // Load and parse
    return xmldoc;
};


/**
 * Same as XML.load above, except that this one is asynchronous.
 * Since it is asynchronous, it has to notify us when the file has
 * completed loading.  It does that with the callback function
 * passed into it.
 *
 * url - the URL of the xml file to load;
 * callback - the function to call when the file has completed loading.
 * returns: nothing - the return is handled by callback.
 */
loadAsync = function(url, callback) {
    var xmldoc = newDocument();

    // If we created the XML document using createDocument, use
    // onload to determine when it is loaded
    if (document.implementation
            && document.implementation.createDocument) {
        xmldoc.onload = function() {
            callback(xmldoc);
        }
    } else { // Otherwise use onreadystatechange as with XMLHttpRequest
        xmldoc.onreadystatechange = function() {
            if (xmldoc.readyState == 4) {
                callback(xmldoc);
            }
        };
    }

    // Now go start the download and parsing
    xmldoc.load(url);
};    



/**
 * Location object.
 */
function Location(xmlObject) {
    this.address = xmlObject.getElementsByTagName("address")[0].firstChild.nodeValue;
    this.lat = xmlObject.getElementsByTagName("lat")[0].firstChild.nodeValue;
    this.long = xmlObject.getElementsByTagName("long")[0].firstChild.nodeValue;
}


/**
 * Image object.
 *
function Image(xmlObject) {
    this.fullSize = xmlObject.getElementsByTagName("fullSize")[0].firstChild.nodeValue;
    this.thumb =  xmlObject.getElementsByTagName("thumb")[0].firstChild.nodeValue;
}
*/

function initialize() {
    var walkdata = XML.load("walkdata.xml");
    document.write("<br />The walk data!<br />");

    var walks = walkdata.getElementsByTagName("walk");
    document.write("Number of walks: " + walks.length + "<br />");

    for (var i = 0; i < walks.length; i++) {
        var walk = walks.item(i);
        document.write("Name: "
                + walk.attributes.getNamedItem("name").value + "<br />");
        document.write("Model: "
                + walk.attributes.getNamedItem("model").value + "<br />");
        document.write("WalkId: "
                + walk.attributes.getNamedItem("walkId").value  + "<br />");

        document.write("Duration: "
             + walk.getElementsByTagName("duration")[0]
                .attributes.getNamedItem("value").value + " "
             + walk.getElementsByTagName("duration")[0]
                 .attributes.getNamedItem("unit").value + "<br />");
        document.write("Distance: "
             + walk.getElementsByTagName("distance")[0]
                 .attributes.getNamedItem("value").value + " "
             + walk.getElementsByTagName("distance")[0]
                 .attributes.getNamedItem("unit").value + "<br />");
        document.write("Start: "
             + walk.getElementsByTagName("start")[0].firstChild.nodeValue + "<br />");
        document.write("End: "
             + walk.getElementsByTagName("end")[0].firstChild.nodeValue + "<br />");

        var route = walk.getElementsByTagName("route")[0];
        var steps = route.getElementsByTagName("step");
        document.write("Number of steps in route: " + steps.length + "<br />");

        for (var j = 0; j < steps.length; j++) {
            var step = steps.item(j);
            var from = step.getElementsByTagName("from")[0];
            var to = step.getElementsByTagName("to")[0];
            document.write("step #" + (j + 1) + ": "
                    + "(" + from.attributes.getNamedItem("lat").value + ", "
                    + from.attributes.getNamedItem("long").value + ")"
                    + " to "
                    + "(" + to.attributes.getNamedItem("lat").value + ", "
                    + to.attributes.getNamedItem("long").value + ")<br />");
        }

        document.write("<br />");
    }
}
