if(!com){var com=new Object();}
if(typeof com.lbi=="undefined"){com.lbi=new Object();}
if(typeof com.lbi.utils=="undefined"){com.lbi.utils=new Object();}
if(typeof com.lbi.utils.DomUtils=="undefined"){com.lbi.utils.DomUtils=new Object();}

com.lbi.utils.DomUtils = function(){
}

com.lbi.utils.DomUtils.getInnerHTMLasXML = function (div_id) {
	var str = document.getElementById(div_id).innerHTML;
	var re,m;
	re = new RegExp("<[a-z]+\\s[^>]+>","gi");
	m = re.exec(str);
	if(m != null){
		while(m != null){
			str = str.substring(0,m.index)+com.lbi.utils.DomUtils.__addAttributeQuotes(m[0])+str.substring(m.index+m[0].length,str.length);
			m = re.exec(str);
		}
	}
	re = new RegExp("<(br|img[^>]+)>","gi");
	m = re.exec(str);
	if (m != null) {
		while(m != null){
			if(str.charAt(m.index+m[1].length) != "/"){
				str=str.substring(0,m.index+m[1].length+1)+" /"+str.substring(m.index+m[1].length+1,str.length);
			}else if(str.charAt(m.index+m[1].length-1) != " "){
				str=str.substring(0,m.index+m[1].length)+" "+str.substring(m.index+m[1].length,str.length);
			}
			m = re.exec(str);
		}
	}
	return str;
}
	
com.lbi.utils.DomUtils.__addAttributeQuotes = function(str){
	var re = new RegExp("=[a-z0-9]+","gi");
	m = re.exec(str);
	if(m != null){
		while(m != null){
			str = str.substring(0,m.index+1)+"\""+m[0].substring(1,m[0].length)+"\""+str.substring(m.index+m[0].length,str.length);
			m = re.exec(str);
		}
	}
	return str;
}
//TODO: provide full support for attributes
com.lbi.utils.DomUtils.innerHTML = function (src_obj)
{
	var InnerHTML = com.lbi.utils.DomUtils.innerHTML;
	var getNodeAttributes = com.lbi.utils.DomUtils.getNodeAttributes;
	var str_out='';
	
	var children=src_obj.childNodes;
	
	for (var i=children.length-1; i>=0; i--)
	{
		var c_node=children[i];
		
		if(c_node.nodeType==1) // element
		{
			attr_str = "";
			//attr_str += c_node.getAttributeNode("id") ? getCarouselData(c_node.getAttributeNode("id").nodeValue) : "";
			attr_str += getNodeAttributes(c_node);

			str_out = 	'<' + c_node.tagName + attr_str + '>' + InnerHTML(c_node) +  '</' + c_node.tagName + '>' + str_out;
		}else{ // 3 = text
			str_out = c_node.nodeValue + str_out;			
		}
	}
	return str_out;
}

com.lbi.utils.DomUtils.getNodeAttributes = function($node){
	var isInArray = com.lbi.utils.DomUtils.isInArray;
	var allowed_tags = ["id", "src", "href", "class"];
	var res = "";
	var arr = $node.attributes ? $node.attributes : $node.getAttributes();
	for(var j=0; j<arr.length; j++){
		var attr_node = arr[j];
		if(isInArray (attr_node.nodeName, allowed_tags)){
			res += " " + attr_node.nodeName + "='" + attr_node.nodeValue + "'";
		}
	}
	return res;
}
com.lbi.utils.DomUtils.isInArray = function ($element, $array){
	for(var i=0; i<$array.length; i++){
		if($array[i] == $element) return true;
	}
	return false;
}

com.lbi.utils.DomUtils.fixupIETagAttributes = function(inStr)
{
	var outStr = "";

	// Break the tag string into 3 pieces.

	var tagStart = inStr.match(/^<[^\s>]+\s*/)[0];
	var tagEnd = inStr.match(/\s*\/?>$/)[0];
	var tagAttrs = inStr.replace(/^<[^\s>]+\s*|\s*\/?>/g, "");

	// Write out the start of the tag.
	outStr += tagStart;

	// If the tag has attributes, parse it out manually to avoid accidentally fixing up
	// attributes that contain JavaScript expressions.

	if (tagAttrs)
	{
		var startIndex = 0;
		var endIndex = 0;

		while (startIndex < tagAttrs.length)
		{
			// Find the '=' char of the attribute.
			while (tagAttrs.charAt(endIndex) != '=' && endIndex < tagAttrs.length)
				++endIndex;

			// If we are at the end of the string, just write out what we've
			// collected.

			if (endIndex >= tagAttrs.length)
			{
				outStr += tagAttrs.substring(startIndex, endIndex);
				break;
			}

			// Step past the '=' character and write out what we've
			// collected so far.

			++endIndex;
			outStr += tagAttrs.substring(startIndex, endIndex);
			startIndex = endIndex;

			if (tagAttrs.charAt(endIndex) == '"' || tagAttrs.charAt(endIndex) == "'")
			{
				// Attribute is quoted. Advance us past the quoted value!
				var savedIndex = endIndex++;
				while (endIndex < tagAttrs.length)
				{
					if (tagAttrs.charAt(endIndex) == tagAttrs.charAt(savedIndex))
					{
						endIndex++;
						break;
					}
					else if (tagAttrs.charAt(endIndex) == "\\")
						endIndex++;
					endIndex++;
				}

				outStr += tagAttrs.substring(startIndex, endIndex);
				startIndex = endIndex;
			}
			else
			{
				// This attribute value wasn't quoted! Wrap it with quotes and
				// write out everything till we hit a space, or the end of the
				// string.

				outStr += "\"";
				
				var sIndex = tagAttrs.slice(endIndex).search(/\s/);
				endIndex = (sIndex != -1) ? (endIndex + sIndex) : tagAttrs.length;
				outStr += tagAttrs.slice(startIndex, endIndex);				
				outStr += "\"";				
				startIndex = endIndex;
			}
		}
	}

	outStr += tagEnd;

	// Write out the end of the tag.
	return outStr;
}

com.lbi.utils.DomUtils.fixUpIEInnerHTML = function(inStr)
{
	var outStr = "";

	// Create a regular expression that will match:
	//     <!--
	//     <![CDATA[
	//     <tag>
	//     -->
	//     ]]>
	//     ]]&gt;   // Yet another workaround for an IE innerHTML bug.
	//
	// The idea here is that we only want to fix up attribute values on tags that
	// are not in any comments or CDATA.

	var regexp = new RegExp("<\\!--|<\\!\\[CDATA\\[|<\\w+[^<>]*>|-->|\\]\\](>|\&gt;)", "g");
	var searchStartIndex = 0;
	var skipFixUp = 0;
	
	while (inStr.length)
	{
		var results = regexp.exec(inStr);
		if (!results || !results[0])
		{
			outStr += inStr.substr(searchStartIndex, inStr.length - searchStartIndex);
			break;
		}

		if (results.index != searchStartIndex)
		{
			// We found a match but it's not at the start of the inStr.
			// Create a string token for everything that precedes the match.
			outStr += inStr.substr(searchStartIndex, results.index - searchStartIndex);
		}

		if (results[0] == "<!--" || results[0] == "<![CDATA[")
		{
			++skipFixUp;
			outStr += results[0];
		}
		else if (results[0] == "-->" || results[0] == "]]>" || (skipFixUp && results[0] == "]]&gt;"))
		{
			--skipFixUp;
			outStr += results[0];
		}
		else if (!skipFixUp && results[0].charAt(0) == '<')
			outStr += com.lbi.utils.DomUtils.fixupIETagAttributes(results[0]);
		else
			outStr += results[0];

		searchStartIndex = regexp.lastIndex;
	}
	
	return outStr;
};
