oMTLib = {utils:{}}


oMTLib.utils.colourBlend = function(hexStart, hexEnd, nSteps){
	hexStart = hexStart.replace("#", "")
	hexEnd 	= hexEnd.replace("#", "")
	if(hexStart.length < 6) hexStart = hexStart + hexStart;
	if(hexEnd.length < 6) hexEnd = hexEnd + hexEnd;
	this.r = parseInt(hexStart.substring(0, 2), 16)
	this.g = parseInt(hexStart.substring(2, 4), 16)
	this.b = parseInt(hexStart.substring(4, 6), 16)
	this.nSteps = nSteps;
	this.nStepR = parseInt((parseInt(hexEnd.substring(0, 2), 16) - this.r) / this.nSteps);
	this.nStepG = parseInt((parseInt(hexEnd.substring(2, 4), 16) - this.g) / this.nSteps);
	this.nStepB = parseInt((parseInt(hexEnd.substring(4, 6), 16) - this.b) / this.nSteps);
	this.aR = []; this.aG = []; this.aB = [];
	for(var i=0; i<this.nSteps-1; i++){
		this.aR[i] = this.r + (i * this.nStepR);
		this.aG[i] = this.g + (i * this.nStepG);
		this.aB[i] = this.b + (i * this.nStepB);
	};
	this.aReturn = [];
	for(i=0; i<this.nSteps-1; i++){
		this.aReturn[i] = this.decimalToHex(this.aR[i]) + this.decimalToHex(this.aG[i]) + this.decimalToHex(this.aB[i]);
	};
	this.aReturn[this.aReturn.length] = hexEnd;
	return this.aReturn;
};

oMTLib.utils.decimalToHex = function(nDecimal){
	var sHex = "0123456789ABCDEF";
	var sReturn = sHex.substr(nDecimal&15,1);
	while(nDecimal > 15){
		nDecimal >>= 4;
		sReturn = sHex.substr(nDecimal&15,1) + sReturn;
	};
	if(sReturn.length<2) sReturn = "0" + sReturn;
	return sReturn;
};

oMTLib.utils.rgbToHex = function(sRGB){
	if(sRGB.indexOf("rgb") == -1) return sRGB;
	var a = sRGB.split("(")[1].replace(")", "").split(",");
	return "#" + this.decimalToHex(a[0]) + this.decimalToHex(a[1]) + this.decimalToHex(a[2]);
};

oMTLib.utils.getFromSelector = function(sRule){	
	function _build(o, n){
		if(n == aPath.length){
			aReturn[aReturn.length] = o;
		}else{
			var aTemp = [];
			if(aPath[n].indexOf("#") > -1){
				aTemp[aTemp.length] = o.getElementById(aPath[n].split("#")[1]);
			}else if(aPath[n].indexOf(".") > -1){
				var a = oMTLib.utils.getElementsByClassName(aPath[n].split(".")[1], aPath[n].split(".")[0], o);
				for(var i=0; i<a.length; i++){
					aTemp[aTemp.length] = a[i];
				};
			}else{
				var a = o.getElementsByTagName(aPath[n]);
				for(var i=0; i<a.length; i++){
					aTemp[aTemp.length] = a[i];
				};
			};
			for(var j=0; j<aTemp.length; j++){
				if(aTemp[j]){
					_build(aTemp[j], n+1);
				};
			};
		};
	};
	while(sRule.split("#").length > 2){
		sRule = sRule.substring(sRule.indexOf("#")+1, sRule.length)
		sRule = sRule.substring(sRule.indexOf(" ")+1, sRule.length)
	};
	var aPath = sRule.split(" ");
	var oContext = document;
	var aReturn = [];
	_build(oContext, 0);
	if(aReturn.length){
		return aReturn;
	};
	return null;
};

oMTLib.utils.getElementsByClassName = function(sClass, sTag, d){
	d = d || document;
	sTag = sTag || "*";
	var aReturn = [];
	var sReg = new RegExp("(^|\\s)" + sClass + "(\\s|$)");
	var a = d.getElementsByTagName(sTag);
	for(var i=0, j=a.length; i<j; i++){
		if(sReg.test(a[i].className)){
			aReturn[aReturn.length] = a[i];
		};
	};
	return aReturn;
};
