// @name      The Fade Anything Technique
// @namespace http://www.axentric.com/aside/fat/
// @version   1.0-RC1
// @author    Adam Michela
// @author    Kate Rhodes (made it work correctly in Safari)

var Fat = {
	make_hex : function (r,g,b) 
	{
		r = r.toString(16); if (r.length == 1) r = '0' + r;
		g = g.toString(16); if (g.length == 1) g = '0' + g;
		b = b.toString(16); if (b.length == 1) b = '0' + b;
		return "#" + r + g + b;
	},
	fade_element : function (id, fps, duration, from, to) 
	{
		if (!fps) fps = 30;
		if (!duration) duration = 3000;
		if (!from || from=="#") from = "#ffffb4";
		if (!to) to = this.get_bgcolor(id);
		
		var frames = Math.round(fps * (duration / 1000));
		var interval = duration / frames;
		var delay = interval;
		var frame = 0;
		
		if (from.length < 7) from += from.substr(1,3);
		if (to.length < 7) to += to.substr(1,3);
		
		var rf = parseInt(from.substr(1,2),16);
		var gf = parseInt(from.substr(3,2),16);
		var bf = parseInt(from.substr(5,2),16);
		var rt = parseInt(to.substr(1,2),16);
		var gt = parseInt(to.substr(3,2),16);
		var bt = parseInt(to.substr(5,2),16);
		
		var r,g,b,h;
		while (frame < frames)
		{
			r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
			g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
			b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
			h = this.make_hex(r,g,b);
		
			setTimeout("Fat.set_bgcolor('"+id+"','"+h+"')", delay);

			frame++;
			delay = interval * frame; 
		}
		setTimeout("Fat.set_bgcolor('"+id+"','"+to+"')", delay);
	},
	set_bgcolor : function (id, c)
	{
		var o = document.getElementById(id);
		if (o) o.style.backgroundColor = c;
	},
	get_bgcolor : function (id)
	{
		var o = document.getElementById(id);
		while(o)
		{
			var c;
			/*if (window.getComputedStyle) {
				c = window.getComputedStyle(o,null).getPropertyValue("background-color");
				alert("computed style: " + c)
			} */
			var got_style = getStyle(o);
			if(got_style != null && ! got_style.match(/rgba\s*\(0\s*,\s*0\s*,\s*0\s*,\s*0\)/)){
				
				c = got_style;
			}
			
			if (o.currentStyle) {
				c = o.currentStyle.backgroundColor; 
			} 
			if ((c != "" && c != "transparent" && !(c===undefined)) || o.tagName == "BODY") { 
				//alert("breaking: " + c);
				break; 
			}
			o = o.parentNode;
		}
		if (c == undefined || c == "" || c == "transparent") {
			c = "#FFFFFF";
		}
		var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
		
		if (rgb) {
			c = this.make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
		} else {
			var rgba = c.match(/rgba\s*\((\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\)/);
			if (rgba){
				c = this.make_hex(parseInt(rgba[1]),parseInt(rgba[2]),parseInt(rgba[3]));
			}
		}
		return c;
	}
	
	
}

function getStyle(el) {
	if(el.currentStyle){
		return el.currentStyle.backgroundColor;
	}
	if(document.defaultView){
		return document.defaultView.getComputedStyle(el, '').getPropertyValue("background-color");
	}
	return null;
}

/*6/9/05 -- for next release, flagNow can be deprecated for targetFade*/
var flagNow = function (el)
{
	if (!el) return false;
	var theEl = el;
	
	var flag = function(e){
		Fat.fade_element(theEl.getAttribute('id'));
	}
	
	var init = function(e){
		addEvent(window, 'load', flag);
		flag();
	}
	init();
}
DomDeco.register('.flagNow', flagNow);


/* target fade */
var targetFade = function(el){
	//onload fire
    var currentURL = unescape(window.location);
    if (currentURL.indexOf('#')>-1){
		Fat.fade_element(currentURL.substring(currentURL.indexOf('#')+1,currentURL.length));
	}
}
addEvent(window, 'load', targetFade);
