kepler.skeleton(){
	this.root;
	this.bones = [];
	this.name;
	
	this.rootTransformation;
	
	this.delta = 0;
	this.currentAnimationMatrix;
}

kepler.skeleton.prototype.parse( boneInfo, rootNode ) {
	var bone = kepler.createObject("bone");
	
	this.rootTransformation = kepler.matrix4.fromArray( rootNode.transformation );
	
	bone.isRoot = true;
	bone.transformation = kepler.matrix4.fromArray( boneInfo.transformation );
	bone.globalTransformation = bone.transformation;
	
	this.bones.push(bone);
	this.root = bone;
	
	
	for(var c = 0; c < boneInfo.children.length; c++) {
		
		this.parseChild(bone, boneInfo.children[c]);
		
	}

}

kepler.skeleton.prototype.parseChild( parentBone, childBoneInfo ) {
	var bone = kepler.createObject("bone");
	
	bone.parent = parentBone;
	bone.transformation = kepler.matrix4.fromArray( childBoneInfo.transformation );
	bone.name = childBoneInfo.name;
	bone.globalTransformation = kepler.matrix4.mul( bone.transformation,  parentBone.globalTransformation );
	
	this.bones.push(bone);
	
	parentBone.addChild(bone);
	
	//parse Children
	if(childBoneInfo.children)
	for(var c = 0; c < childBoneInfo.children.length; c++) {
		
		this.parseChild(bone, childBoneInfo.children[c]);
		
	}
	
}

kepler.skeleton.prototype.getBoneByName( name  ) {
	var bones = this.bones;
	for(var c = 0; c < bones.length; c++) {
		var bone = bones[c];

		if(bone.name == name)
			return bone;
		
	}
	
}

kepler.skeleton.prototype.parseAnimation( channels ) {
	for(var c = 0; c < channels.length; c++) {
		var channel = channels[c];
		var name = channel.name;
		
		var bone = this.getBoneByName(name);
		
		if(bone) {
			bone.positionkeys = channel.positionkeys;
			bone.rotationkeys = channel.rotationkeys;
		}
	}
}

kepler.skeleton.prototype.animate( bone, id ) {
	bone.currentAnimationMatrix = kepler.matrix4.identity();
	
	if(bone.positionkeys) {
		
		
		if(bone.positionkeys[id]) {
			var currentTranslation = bone.positionkeys[id][1];
			bone.currentAnimationMatrix = kepler.matrix4.setTranslation(bone.currentAnimationMatrix, currentTranslation);
		}
		
		
		if(bone.rotationkeys[id]) {
			var currentRotation = bone.rotationkeys[id][1];
			bone.currentAnimationMatrix = kepler.matrix4.rotateZYX(bone.currentAnimationMatrix, currentRotation);
		
		}
		//console.log(bone.currentAnimationMatrix);
		
		if(bone.parent)
			bone.globalTransformation = kepler.matrix4.mul( bone.currentAnimationMatrix,  bone.parent.globalTransformation );
		//else	
		//	bone.globalTransformation = kepler.matrix4.identity();
	}

	
	return bone;
	
}