if(typeof(Controls) == 'undefined')
	var Controls = {};
if(typeof(Controls.UI) == 'undefined')
	Controls.UI = {};
if(typeof(Controls.UI.ColorFader) == 'undefined'){
	Controls.UI.ColorFader = {};
	
	Controls.UI.ColorFader = function(){
		this.Color = function(r, g, b){
			this.R = r;
			this.G = g;
			this.B = b;
			
			this.toString = function(){
				return this.R + ", "+ this.G + ", "+ this.B;
			}
			this.toHTMLString = function(){
				var r = Math.floor(this.R).toString(16);
				if(r.length < 2)
					r = "0"+ r;
				var g = Math.floor(this.G).toString(16);
				if(g.length < 2)
					g = "0"+ g;
				var b = Math.floor(this.B).toString(16);
				if(b.length < 2)
					b = "0"+ b;
				return "#"+ r + g + b;
			}
		}
		
		this.FromColorString = null;
		this.ToColorString = null;
		this.Target = null;
		this._fromColorRgb = new this.Color(0, 0, 0);
		this._toColorRgb = new this.Color(0, 0, 0);
		this._currentColorRgb = new this.Color(0, 0, 0);
		this._timerCookie = null;
		this.Steps = 20;
		this.Interval = 100;
		this.OnColorChangedDelegate = null;
		this._currentStep = 0;
		
		this.Fade = function(){
			if(this.FromColorString == null || this.ToColorString == null || this.Target == null || this.OnColorChangedDelegate == null){
				alert("Not Enough Parameters");
				return;
			}
			var fromColor = this.FromColorString.toString().replace("#", "");
			this._fromColorRgb.R = parseInt(fromColor.substr(0, 2), 16);
			this._fromColorRgb.G = parseInt(fromColor.substr(2, 2), 16);
			this._fromColorRgb.B = parseInt(fromColor.substr(4, 2), 16);
			//alert(this._fromColorRgb.R + ", "+ this._fromColorRgb.G + ", "+ this._toColorRgb.B);
			//alert(this._fromColorRgb);
			
			var toColor = this.ToColorString.toString().replace("#", "");
			this._toColorRgb.R = parseInt(toColor.substr(0, 2), 16);
			this._toColorRgb.G = parseInt(toColor.substr(2, 2), 16);
			this._toColorRgb.B = parseInt(toColor.substr(4, 2), 16);
			
			this._currentColorRgb = new this.Color(this._fromColorRgb.R, this._fromColorRgb.G, this._fromColorRgb.B);
			this._timerCookie = window.setInterval(Controls.Delegates.CreateDelegate(this, this.OnTimer), this.Interval);
			this._currentStep = 0;
		}
		
		this.Cancel = function(){
			window.clearInterval(this._timerCookie);
		}
		
		this.OnTimer = function(){
			if(this._currentStep > this.Steps){
				window.clearInterval(this._timerCookie);
				return;
			}
			this._currentColorRgb.R = this._fromColorRgb.R + (this._toColorRgb.R - this._fromColorRgb.R) * this._currentStep / this.Steps;
			this._currentColorRgb.G = this._fromColorRgb.G + (this._toColorRgb.G - this._fromColorRgb.G) * this._currentStep / this.Steps;
			this._currentColorRgb.B = this._fromColorRgb.B + (this._toColorRgb.B - this._fromColorRgb.B) * this._currentStep / this.Steps;
			this.OnColorChangedDelegate(this.Target, this._currentColorRgb.toHTMLString());
			//this.Target.style.backgroundColor = this._currentColorRgb.toHTMLString();
			this._currentStep++;
		}
	}
}
