/*
 *	class: CSS
 * 
 * 		A class that builds stylesheets.
 * 		<http://www.revnode.com/oss/css/>
 * 
 * 	license:
 * 
 * 		MIT-style license.
 * 
 * 	author:
 * 
 * 		Marat A. Denenberg, <marat at revnode dot com>
 * 
 * 	copyright:
 * 
 * 		copyright (c) 2008 revnode, <http://www.revnode.com/>
 * 
 * 	inspiration:
 * 
 * 		horseweapon on the mootools forum, <http://forum.mootools.net/viewtopic.php?id=6635>
 * 
 * 	links:
 * 
 * 		CSS implementation by popular browsers:
 * 
 * 		<http://developer.apple.com/documentation/AppleApplications/Reference/SafariCSSRef/index.html>
 * 		<http://www.opera.com/docs/specs/css/>
 * 		<http://developer.mozilla.org/en/docs/Mozilla_CSS_support_chart>
 * 		<http://developer.mozilla.org/en/docs/CSS_Reference:Mozilla_Extensions>
 * 		<http://msdn2.microsoft.com/en-us/library/ms531209.aspx>
 * 
 * 	changelog:
 * 
 * 		added check_rule
 * 		cleaned up code, fixed bugs, Safari support
 * 
 */

/* backported to old mootools by Owen Swerkstrom, Messaging Architects */

var CSS = new Class({
	
	local:
	{
		self:				'CSS',
		limited:			['dpi', 'border-radius'],
		_rule:				''
	},
	
	options:
	{
		rules:				{}
	},
	
	xhtml:
	{
		_style:				null
	},
	
	initialize:				function(options)
	{
		this.local = $merge(this.local, this.options, options, this.xhtml);
	},
	
	destroy:				function()
	{
		if(this.local._style) this.local._style.destroy();
	},
	
	refresh:				function()
	{
		var text = '';
		$each(this.local.rules, function(rule, selector)
		{
			this.local._rule = '';
			$each(rule, this._glue, this);
			text += (this.local._rule == '' ? '' : selector + '\n{\n' + this.local._rule + '}\n');
		}, this);
		
		this.destroy();
		this.local._style = new Element('style');
//		this.local._style.set('type', 'text/css');
		this.local._style.set({'type': 'text/css'});
		this.local._style.inject(document.head);

		if(window.ie) {
			try {
				this.local._style.styleSheet.cssText = text;
			} catch(e) {
			}
		} else if(window.gecko) {
			this.local._style.setHTML(text);
		} else {
//			alert(text);
//			this.local._style.set('text', text);
//			this.local._style.set({'text': text});
			this.local._style.innerText = text;
		}

		return this;
	},
	
	_glue:					function(value, property)
	{
//		alert("value=" + value + "\nproperty=" + property);
/*
		if(this[Browser.Engine.name + '_' + property])
		{
			var pair;
			if(pair = this[Browser.Engine.name + '_' + property](value, property))
			{
				this.local._rule += '\t' + pair[0] + ':' + pair[1] + ';\n';
			}
		}
		else */if(!this.local.limited.contains(property))
		{
			this.local._rule += '\t' + property + ':' + value + ';\n';
		}
	},
	
	add_prop:				function(selector, property, value)
	{
		var rules = {}; rules[selector] = {}; rules[selector][property] = value;
		return this.add_rules(rules);
	},
	
	add_rule:				function(selector, properties)
	{
		var rules = {}; rules[selector] = properties;
		return this.add_rules(rules);
	},
	
	add_rules:				function(rules)
	{
		this.local.rules = $merge(this.local.rules, rules);
		return this;
	},

	set_rules: function(rules)
	{
		this.local.rules = rules;

		return this;
		var text = '';
		$each(this.local.rules, function(rule, selector)
		{
			this.local._rule = '';
			$each(rule, this._glue, this);
			text += (this.local._rule == '' ? '' : selector + '\n{\n' + this.local._rule + '}\n');
		}, this);

		if(window.ie) {
			try {
				this.local._style.styleSheet.cssText = text;
			} catch(e) {
			}
		} else if(window.gecko) {
			this.local._style.setHTML(text);
		} else {
			this.local._style.innerText = text;
		}

		return this;
	},
	
	remove_prop:			function(selector, property)
	{
		delete this.local.rules[selector][property];
		return this;
	},
	
	remove_rule:			function(selector)
	{
		delete this.local.rules[selector];
		return this;
	},
	
	remove_rules:			function(selectors)
	{
		if(selectors)
		{
			selectors.each(this.remove_rule, this);
		}
		else
		{
			this.local.rules = {};
		}
		return this;
	},
	
	check_rule:				function(selector)
	{
		return $defined(this.local.rules[selector]);
	}
	
});

CSS.implement({
	
	// ### TRIDENT ###
		
		'trident_opacity':		function(value, property)
		{
			return ['filter', 'alpha(opacity=' + (value * 100) + ')'];
		},
		
		'trident_dpi':			function(value, property)
		{
			if($defined(window.screen.deviceXDPI))
			{
				return ['font-size', ((96 / window.screen.deviceXDPI) * value).round() + '%'];
			}
			else
			{
				return ['font-size', value + '%'];
			}
		},

	// ### GECKO ###
	
		'gecko_border-radius':	function(value, property)
		{
			return ['-moz-' + property, value];
		},
	
	// ### WEBKIT ###
		
		'webkit_border-radius':	function(value, property)
		{
			return ['-webkit-' + property, value];
		}
	
	// ### PRESTO ###
	
});

