/* Component Library
 * Style component
 * by Dave Schontzler
 * (c) 2002, www.stilleye.com
 */

Style =
{
	get : function(obj,sty)
	{
		if(typeof obj == "string") obj = Dom.getElm(obj);
		var sty2 = Style.compact(sty);
		var r = obj.style[sty2];
		if(r=='')
		{
			if(document.defaultView) r = document.defaultView.getComputedStyle(obj,'').getPropertyValue(sty);
			else r = obj.currentStyle[sty2];
		}
		if(r) return r; else return null;
	},
	
	set : function(obj,sty,value)
	{
		if(typeof obj == "string") obj = Dom.getElm(obj);
		if(obj.style.setProperty) obj.style.setProperty(sty, value, null);
		else obj.style[Style.compact(sty)] = value;
	},
	
	compact : function(str)
	{
		var s = str.split('-');	var o = [s[0]];
		for(i=1;(j=s[i]);i++) { o[o.length] = j.charAt(0).toUpperCase(); o[o.length] = j.substring(1) };
		return o.join('');
	},

	getRules : function(wh) // grabs all the styles
	{
		if(document.styleSheets && document.styleSheets.length)
		{
			var i, j, k, m, x, y, s = new Array(), is = true;
			for(i=0; i < document.styleSheets.length; i++)
			{
				j = document.styleSheets.item(i);
				if(wh) is = (j.title || j.id) == wh;
				if(is)
				{
					if(j.cssRules) // mozilla
					{
						for(k=0; m = j.cssRules.item(k); k++)
						{
							if(m.styleSheet && m.styleSheet.cssRules.length) // imported sheets
								for(x=0; y = m.styleSheet.cssRules.item(x); x++) s.push([y.selectorText, y.style.cssText]);
							else s.push([m.selectorText, m.style.cssText])
						}
					}
					else if(j.rules) // explorer
					{
						if(j.imports && j.imports.length)
						{
							for(k=0; m = j.imports[k]; k++)
								for(x=0; y = m.rules[x]; x++)
									s.push([y.selectorText, y.style.cssText]);
						}
						for(k=0; m = j.rules[k]; k++)
								s.push([m.selectorText, m.style.cssText]);
					}
				}
				if(wh && is) break;
				else is = true;
			}
		}
		if(s.length) return s;
		else return null;
	},
	
	extractRule : function(rule, wh)
	{
		var s, i, j, ch, cased = false;
		ch = rule.charAt(0);
		if(ch=='#' || ch=='.') cased = true;
		else rule = rule.toLowerCase();
		s = Style.getRules(wh);
		for(i=0; j = s[i]; i++)
		{
			if(cased)
			{
				if(j[0] == rule) return j[1];
			}
			else if(j[0].toLowerCase() == rule) return j[1];
		}
		return null;
	}
}
