
oMTLib.add("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;
};


oMTLib.utils.events = {

	nEvents : 1,

	addEvent : function(o, sType, oFunction, oScope){
	    if(!oFunction.nID) oFunction.nID = oMTLib.utils.events.nEvents++;
	    if(!o.oEvents) o.oEvents = {};
	    var aEvents = o.oEvents[sType];
	    if(!aEvents){
	        aEvents = o.oEvents[sType] = {};
	        if(o["on" + sType]){
	            aEvents[0] = o["on" + sType];
	        };
	    };
	    aEvents[oFunction.nID] = [oFunction, oScope || o];
	    o["on" + sType] = this._handleEvent;
	},
	
	removeEvent : function(o, sType, oFunction){
	    if(o.oEvents && o.oEvents[sType]){
	        delete o.oEvents[sType][oFunction.nID];
	    };
	},
	
	_handleEvent : function(e){
	    var returnValue = true;
	    var event = e || window.event;
	    var aEvents = this.oEvents[event.type];
	    for(var i in aEvents){			
	        if(aEvents[i][0].apply(aEvents[i][1], [event]) === false){
	            returnValue = false;
	        };			
	    };
	    return returnValue;
	}
};


if(typeof Function.prototype.apply == "undefined"){
	Function.prototype.apply = function(oScope, aArgs){
		var a = [];
		if(!oScope) oScope = window;
		if(!aArgs) aArgs = [];
		for(var i=0; i<aArgs.length; i++){
			a[i] = "aArgs["+i+"]";
		};
		oScope._apply = this;
		var bReturn = eval("oScope._apply(" + a.join(",") + ");");
		delete oScope.__applyTemp__;
		return bReturn;
	};
};
