if(rarebrick == undefined) var rarebrick = new Object();
rarebrick.html2xml = new Object();

/*
 * functions below are used to create an xml valid string from html markup
 */
 

rarebrick.html2xml.getXml = function(flashid) {
	var elem = document.getElementById(flashid);
	var flashElem = new rarebrick.html2xml.Node('div');
	
	rarebrick.html2xml.iterateElem(elem, flashElem);
	
	//alert('flashElem: ' + flashElem.toString());
	
	return escape(flashElem.toString());
}



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
	for(var i=0; i<theNode.attributes.length; i++) {
		if(theNode.attributes[i].value == 'null' || theNode.attributes[i].value == '') continue;
		newNode.addAttribute(theNode.attributes[i].name, theNode.attributes[i].value);
	}
	
	currentXMLElem.attachChild(newNode);
	
	return newNode;
}


/*
 * TextNode Object def 
 */
rarebrick.html2xml.TextNode = function(nodeValue) {
	this.nodeValue = nodeValue;
	this.writeToBuffer = function(buffer) {
		//buffer.push('\n');
		buffer.push(this.nodeValue);
		//buffer.push('\n');
	}
}

/*
 * Node Object def 
 */
rarebrick.html2xml.Node = function(nodeName) {
	this.nodeName = nodeName.toLowerCase();
	this.children = [];
	this.attributes = [];
	
	this.attachChild = function(child) {
		this.children.push(child);
	}
	this.addAttribute = function(attrName, attrValue) {
		this.attributes.push(' ' + attrName + '="' + attrValue + '"');
	}
	
	this.toString = function() {
		var buffer = [];
		this.writeToBuffer(buffer);
		return buffer.join("");
	}
	this.writeToBuffer = function(buffer) {
		buffer.push('\n');
		//buffer.push('[[' + this.nodeName + ']]');
		buffer.push(this.writeNodeOpen(buffer));
		for(var i=0; i<this.children.length; i++) {
			this.children[i].writeToBuffer(buffer);
		}
		buffer.push(this.writeNodeClose(buffer));
		buffer.push('\n');
	}
	this.writeNodeOpen = function(buffer) {
		var nodeOpen;
		if(this.attributes.length == 0) {
			buffer.push('<' + this.nodeName + '>');
		} else {
			buffer.push('<');
			buffer.push(this.nodeName);
			for(var i=0; i<this.attributes.length; i++) {
				buffer.push(this.attributes[i]);
			}
			buffer.push('>');
		}
	}
	this.writeNodeClose = function(buffer) {
		buffer.push('</' + this.nodeName + '>');
	}
	
}




