/**
* WebGL Model Viewer - ColladaParser.js
* Copyright © Cyril Diagne 2010.
*
* @author kikko.fr
*/

ColladaParser = function() {
	
	this.path = null;
	this.texturesURL = null;
	this.model = null;
	this.initTime = 0;
}

ColladaParser.prototype = {
	
	events: new EventDispatcher(),
	
	
	load : function(url, texturesURL) {
		this.model = new Model();
		this.path = url.substring(0, url.lastIndexOf("/")+1);
		this.texturesURL = texturesURL || this.path;
		this.initTime = new Date().getTime();
		this.loadFile(url, this.parse);
	},
	
	
	loadFile : function(url, callback) {
		
		log("ColladaParser: Loading:", url);
		
		var req = new XMLHttpRequest();
		var scope = this;
	  	req.onreadystatechange = function() {
			if(req.readyState == 4 && (req.status == 0 || req.status == 200)) {
				callback.apply(scope, [req.responseXML.documentElement]);
			}
		}
	    req.open("GET", url, true);
	    req.setRequestHeader("Content-Type", "text/xml");
	  	req.send(null);
	},
	
	
	parse : function(data) {
				
		var geoNode = data.getElementsByTagName("library_geometries")[0];
		var child = geoNode.firstChild;
		do{
			if(child.tagName == "geometry"){
				this.model.meshes.push(this.parseMesh(child));
			}
		}while(child=child.nextSibling);
	},
	
	
	parseMesh : function(meshNode) {
		
		var mesh = new Mesh();
		var id = meshNode.getAttribute("id");
		
		var vertsNode = meshNode.getElementsByTagName("vertices")[0];
		var vertsInput = vertsNode.getElementsByTagName("input");
		
		return mesh;
	},
	
	getSource: function(id) {
	
		
	}
}
