container = container || document;
var el, rest;
if (loc === "body") {
return document.body;
} else if (loc === "head") {
return document.head;
} else if (loc === "document") {
return document;
} else if (loc.indexOf("body") === 0) {
el = document.body;
try {
return findElement(loc.substr(("body").length), el);
} catch (e) {
if (e instanceof elementFinder.CannotFind) {
e.prefix = "body" + e.prefix;
}
throw e;
}
} else if (loc.indexOf("head") === 0) {
el = document.head;
try {
return findElement(loc.substr(("head").length), el);
} catch (e) {
if (e instanceof elementFinder.CannotFind) {
e.prefix = "head" + e.prefix;
}
throw e;
}
} else if (loc.indexOf("#") === 0) {
var id;
loc = loc.substr(1);
if (loc.indexOf(":") === -1) {
id = loc;
rest = "";
} else {
id = loc.substr(0, loc.indexOf(":"));
rest = loc.substr(loc.indexOf(":"));
}
el = document.getElementById(id);
if (! el) {
throw elementFinder.CannotFind("#" + id, "No element by that id", container);
}
if (rest) {
try {
return findElement(rest, el);
} catch (e) {
if (e instanceof elementFinder.CannotFind) {
e.prefix = "#" + id + e.prefix;
}
throw e;
}
} else {
return el;
}
} else if (loc.indexOf(":nth-child(") === 0) {
loc = loc.substr((":nth-child(").length);
if (loc.indexOf(")") == -1) {
throw "Invalid location, missing ): " + loc;
}
var num = loc.substr(0, loc.indexOf(")"));
num = parseInt(num, 10);
var count = num;
loc = loc.substr(loc.indexOf(")") + 1);
var children = container.childNodes;
el = null;
for (var i=0; i<children.length; i++) {
var child = children[i];
if (child.nodeType == document.ELEMENT_NODE) {
if (child.className.indexOf("togetherjs") != -1) {
continue;
}
count--;
if (count === 0) {