if(rarebrick == undefined) var rarebrick = new Object();
rarebrick.html2xml = new Object();

rarebrick.html2xml.attrNames = new Object();
rarebrick.html2xml.attrNames['a'] = ['class', 'href', 'target'];
rarebrick.html2xml.attrNames['p'] = ['class', 'id'];
rarebrick.html2xml.attrNames['div'] = ['class', 'id'];
rarebrick.html2xml.attrNames['span'] = ['class', 'id', 'type'];
rarebrick.html2xml.attrNames['li'] = ['class', 'id'];
rarebrick.html2xml.attrNames['ul'] = ['class', 'id', 'align'];
rarebrick.html2xml.attrNames['img'] = ['class', 'id', 'src'];
rarebrick.html2xml.attrNames['pic'] = ['class', 'id', 'src'];
rarebrick.html2xml.attrNames['h1'] = ['class', 'width', 'id'];
rarebrick.html2xml.attrNames['h2'] = ['class', 'width', 'id', 'x'];



/*
 * functions below are used to create an xml valid string from html markup
 */
 
rarebrick.html2xml.getXml = function(flashid) { 
	var timeObj = new Object();
	timeObj.start = (new Date()).getTime();

	var elem = document.getElementById(flashid);
	var flashElem = new rarebrick.html2xml.Node('div');
	
	timeObj.beforeIter = (new Date()).getTime();
	
	rarebrick.html2xml.iterateElem(elem, flashElem);
	
	timeObj.afterIter = (new Date()).getTime();
	
	var xmlToString = rarebrick.html2xml.nodeToString(flashElem);
	
	timeObj.afterToString = (new Date()).getTime();
	
	var xmlEscaped = escape(xmlToString);
	
	timeObj.end = (new Date()).getTime();
	
	rarebrick.html2xml.analyseTime(timeObj, xmlToString);
	
	return xmlEscaped;
}



rarebrick.html2xml.analyseTime = function(timeObj, xmlToString) {
	return;
	
	var txt = 'Speed Test:\n';
	txt = txt + 'start: ' + timeObj.start + '\n';
	txt = txt + 'beforeIter: ' + (timeObj.beforeIter-timeObj.start) + '\n';
	txt = txt + 'afterIter: ' + (timeObj.afterIter-timeObj.start) + '\n';
	txt = txt + 'afterToString: ' + (timeObj.afterToString-timeObj.start) + '\n';
	txt = txt + 'total time: ' + (timeObj.end-timeObj.start) + '\n\n\n';
	txt = txt + xmlToString;
	
	alert(txt);
}


rarebrick.html2xml.iterateElem = function(elem, currentXMLNode) {
	//first, handle this node
	if(elem.nodeType == 3) currentXMLNode = rarebrick.html2xml.addTextNode(elem, currentXMLNode);
	else if(elem.nodeType == 1) currentXMLNode = rarebrick.html2xml.addDataNode(elem, currentXMLNode);
	
	//now iterate over children
	for(var i=0; i<elem.childNodes.length; i++) {
		var theNode = elem.childNodes[i];
		rarebrick.html2xml.iterateElem(elem.childNodes[i], currentXMLNode);
	}
}

rarebrick.html2xml.addTextNode = function(theNode, currentXMLElem) {
	//add a new text node to XML using this node as a base
	var newNode = new rarebrick.html2xml.TextNode(theNode.nodeValue);
	currentXMLElem.attachChild(newNode);
	
	return currentXMLElem;
}

rarebrick.html2xml.addDataNode = function(theNode, currentXMLElem) {	
	//add a new data node to XML using this node as a base
	var newNode = new rarebrick.html2xml.Node(theNode.nodeName);
	
	//attach any attributes to this node here
	var attrs = rarebrick.html2xml.attrNames[newNode.nodeName];
	if(attrs != undefined) {
		for(var i=0; i<attrs.length; i++) {
			var attr = theNode.attributes[attrs[i]];
			if(attr == null || attr == 'null') continue;
			var val = attr.value;
			if(val == '') continue;
			var name = attr.name;
			newNode.addAttribute(name, val);
		}
	}
	
	currentXMLElem.attachChild(newNode);
	return newNode;
}


/*
 * TextNode Object def 
 */
rarebrick.html2xml.TextNode = function(nodeValue) {
	this.nodeValue = nodeValue;
	this.writeToBuffer = function(buffer) {
		buffer.push(this.nodeValue);
	}
}

/*
 * Node Object def 
 */
rarebrick.html2xml.Node = function(nodeName) {	
	this.nodeName = nodeName.toLowerCase();
	this.children = new Array();
	
	this.attachChild = function(child) {
		this.children.push(child);
	}
	this.addAttribute = function(attrName, attrValue) {
		if(this.attributes == null) this.attributes = new Array();
		this.attributes.push(' ' + attrName + '="' + attrValue + '"');
		if(attrName == 'class') this.attributes.push(' theClass="' + attrValue + '"');
	}
	
	this.writeToBuffer = function(buffer) {
		rarebrick.html2xml.writeNodeToBuffer(this, buffer);
	}
	
}

rarebrick.html2xml.nodeToString = function(node) {
	var buffer = [];
	node.writeToBuffer(buffer);
	return buffer.join("");
}

rarebrick.html2xml.writeNodeToBuffer = function(node, buffer) {
	//buffer.push('\n\n');
	////open node
	if(!node.attributes) {
		buffer.push('<' + node.nodeName + '>');
	} else {
		buffer.push('<');
		buffer.push(node.nodeName);
		for(var i=0; i<node.attributes.length; i++) {
			buffer.push(node.attributes[i]);
		}
		buffer.push('>');
	}
	//children
	for(var i=0; i<node.children.length; i++) {
		if(node.children[i] != undefined) node.children[i].writeToBuffer(buffer);
	}
	//close node
	buffer.push('</' + node.nodeName + '>');
}