/*
  Copyright (C) 2005-2006 by Domenico De Felice
  Copyright (C) 2005-2006 by Massimiliano Mirra

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

  Authors: Domenico De Felice, <dfdcod [at] gmail [dot] com>
  Massimiliano Mirra, <bard [at] hyperstruct [dot] net>
*/


//---------------------------------------------------------
// Global stuff

var serializer = new XMLSerializer();
var parser = new DOMParser();

var ns_xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
var ns_xhtml="http://www.w3.org/1999/xhtml";
var ns_svg="http://www.w3.org/2000/svg";

function _c(tag, attrs) {
    var ele = document.createElement(tag);
    if (attrs) _set(ele, attrs);
    return ele;
}
function _cxul(tag, attrs) {
    var ele = document.createElementNS(ns_xul, tag);
    if (attrs) _set(ele, attrs);
    return ele;
}
function _cxhtml(tag, attrs) {
    var ele = document.createElementNS(ns_xhtml, tag);
    if (attrs) _set(ele, attrs);
    return ele;
}
function _csvg(tag, attrs) {
    var ele = document.createElementNS(ns_svg, tag);
    if (attrs) _set(ele, attrs);
    return ele;
}
function _set(ele, attrs) {
    if (typeof(attrs) == 'string') {
        _s(ele, 'id', attrs);
        return;
    }
    var attr;
    for (attr in attrs) {
        if (typeof(attrs[attr]) == 'object') {
            _set(ele[attr], attrs[attr]);
        } else {
            if (ele.setAttribute) {
                ele.setAttribute(attr, attrs[attr]);
            } else {
                ele[attr] = attrs[attr];
            }
        }
    }
};
function _a(parent, child) {
    parent.appendChild(child);
}
function _r(ele) {
    ele.parentNode.removeChild(ele);
}
function _s(ele, attr, val) {
    ele.setAttribute(attr, val);
}
function _g(ele, attr) {
    return ele.getAttribute(attr);
}


function _(thing) {
    switch(typeof(thing)) {
    case 'string':
        return document.getElementById(thing);
        break;
    case 'xml':
        return document.getElementById(thing.toString());
        break;
    default:
        return thing;
    }
    return undefined;
}

function createUUID() {
    return [4, 2, 2, 2, 6].map(function(length) {
                                   var uuidpart = "";
                                   for (var i=0; i<length; i++) {
                                       var uuidchar = parseInt((Math.random() * 256)).toString(16);
                                       if (uuidchar.length == 1)
                                           uuidchar = "0" + uuidchar;
                                       uuidpart += uuidchar;
                                   }
                                   return uuidpart;
                               }).join('-');
}

function cloneBlueprint(name) {
    return x('//*[@id="blueprints"]' +
             '//*[@class="' + name + '"]')
        .cloneNode(true);
}

function getElementByAttribute(parent, name, value) {
    for(child = parent.firstChild; child; child = child.nextSibling)
        if(child.getAttribute && child.getAttribute(name) == value)
            return child;

    for(child = parent.firstChild; child; child = child.nextSibling) {
        var matchingChild = getElementByAttribute(child, name, value);
        if(matchingChild)
            return matchingChild;
    }

    return undefined;
}

function x() {
    var contextNode, path;
    if(typeof(arguments[0]) == 'string') {
        contextNode = document;
        path = arguments[0];
    } else {
        contextNode = arguments[0];
        path = arguments[1];
    }

    function resolver(prefix) {
        return 'http://www.w3.org/1999/xhtml';
    }

    return document.evaluate(
        path, contextNode, resolver,
        XPathResult.ANY_UNORDERED_NODE_TYPE, null).singleNodeValue;
}

function toXML(domElement) {
    return new XML(serializer.serializeToString(domElement));
}

function toDOM(description) {
    return parser.parseFromString(
        (typeof(description) == 'xml' ?
         description.toXMLString() : description),
        'application/xhtml+xml').documentElement;
}

function stripUriFragment(uri) {
    var hashPos = uri.lastIndexOf('#');
    return (hashPos != -1 ?
            uri.slice(0, hashPos) :
            uri);
}
