raismth.ns(function() { with (raismth.LIB) {
myDump("[RAISMTH] parsers module initialization\n");
theApp.parsers = {
	parsemData : function(mData) {
		let lData = mData.toLowerCase();
		let isSmooth = false;
		if(lData.indexOf('http://') === 0 && lData.indexOf('.mp4') == -1 && lData.indexOf('.wmv') == -1) {
			isSmooth = true;
		} else if(lData.indexOf('http://') != 0 && lData.indexOf('mms://') == -1 ) {
			let oParser = new DOMParser();
			let oDOM = oParser.parseFromString(mData, "text/xml");
			let oObj = raismth.parsers.xmlToJson(oDOM);
			mData = oObj.playList[0].playListItems[0].playListItem[0].mediaSource;
			isSmooth = true;
		}
		let ret = new Array();
		ret[0] = mData;
		ret[1] = isSmooth;
		return ret;
	},

	parseURI : function(uri) {
		return uri.substring(uri.indexOf('=') + 1);
	},

	xmlToJson : function (xml) {
	
		// Create the return object
		var obj = {};

		if (xml.nodeType == 1) { // element
			// do attributes
			if (xml.attributes.length > 0) {
				// obj["@attributes"] = {};
				for (var j = 0; j < xml.attributes.length; j++) {
					var attribute = xml.attributes.item(j);
					// fixme
					// obj["@attributes"][attribute.nodeName] = attribute.nodeValue; 
					obj[attribute.nodeName] = attribute.value;
				}
			}
		} else if (xml.nodeType == 3) { // text section
			obj = xml.value;
		}
		

		// do children
		if (xml.hasChildNodes()) {
			for(var i = 0; i < xml.childNodes.length; i++) {
				var item = xml.childNodes.item(i);
				var nodeName = item.nodeName;
				if(nodeName.indexOf('#text') != -1) { // fixme
				} else if (typeof(obj[nodeName]) == "undefined") {
					obj[nodeName] = [];
					obj[nodeName].push(raismth.parsers.xmlToJson(item));
					// obj[nodeName] = raismth.parsers.xmlToJson(item);					
				} else {
					if (typeof(obj[nodeName].push) == "undefined") {
						var old = obj[nodeName];
						obj[nodeName] = [];
						obj[nodeName].push(old);
					}
					obj[nodeName].push(raismth.parsers.xmlToJson(item));
				}
			}
		}		
		return obj;
	},

	check_smth_manifest : function(mData) {
		let parser = new DOMParser();
		let dom = parser.parseFromString(mData, "application/xml");
		if(dom.documentElement.childNodes[0].textContent.indexOf('non well-formed') != -1) {
			mData = raismth.parsers.normalize(mData);
			dom = parser.parseFromString(mData, "application/xml");
			if(dom.documentElement.childNodes[0].textContent.indexOf('non well-formed') != -1) return false;
		}
		return dom;
	},

	parse_smth_manifest : function(dom){
		return raismth.parsers.xmlToJson(dom);
	},

        normalize : function(s) {
                // myDump('[RAISMTH] s:' + s + '\n');
                var i, f = 0;
                var a = [];
                s += '';
                f = s.length;
                // f = 10;
                var m = 0;
                for(i = 2; i < f; i++) {
                        if(i % 2 == 0) {
                                if(s.charCodeAt(i) != 10 && s.charCodeAt(i) != 13) {
                                        a[m] = s.charCodeAt(i);
                                        a[m] = String.fromCharCode(a[m]);
                                        m++;
                                }
                        }
                }
                var b = a.join('');
                // b = parseInt(b, 16);
                return b;
        },

}

}});
