/*

jQuery pureOpacity plugin 2.0

$Id: jquery.pureOpacity.js 8 2010-04-04 20:54:25Z christian $

automated implementation of this CSS based solution:
http://www.visibilityinherit.com/code/css-opacity.php

ASSUMPTIONS: 
	- the container to make transparent has a display 
		of block, either by default or user-defined;
	- the container is not styled with IE filters. If 
		opacity is defined in the stylesheet, it is declared 
		ONLY with the opacity property, no filter.

USAGES:
	$(transparent container).pureOpacity(); // opacity is whatever defined in stylesheet or 0.5
	$(transparent container).pureOpacity(0.7); // overrides stylesheet opacity if any
	$(transparent container).pureOpacity({
		'opacity' : 0.4,
		'bgcolor' : 'red'
	});  // overrides stylesheet opacity and bgcolor if any

TESTED:
	On Windows: IE6-7-8, FF3.5.9, Safari4.0.5, Opera10.51

TODO:

*/

(function($) {

var pluginname = 'pureOpacity';

$.fn[pluginname] = function(settings) {
		var defaults = {
			'opacity' : 0.7,
			'bgcolor' : 'transparent'
		};
		var config = {
			'opacity' : -1,
			'bgcolor' : '',
			'transparentClass' : pluginname + '-transparent'
		};

		if (typeof settings == 'number' || typeof settings == 'string')
			config.opacity = settings;
		else if (settings)
			$.extend(config, settings);

		this.each(function() {

			var element = $(this);

			var opacity = selectOpacity([
				config.opacity, 
				element.css('opacity'),
				defaults.opacity
			]);

			var bgcolor = selectColor([
				config.bgcolor,
				element.css('background-color'),
				defaults.bgcolor
			]);

			var position = getPosition(element.css('position'));

			element.css({
				'position' : position,
				'background-color' : 'transparent',
				'opacity' : '1'
			});

			var transparentDiv = $('<div class="' + config.transparentClass + '" />')
				.css({
					'background-color' : bgcolor,
					'bottom' : '0',
					'left' : '0',
					'opacity' : opacity,
					'position' : 'absolute',
					'top' : '0',
					'width' : '100%',
					'z-index' : '-1' // fixes a bug with floated content in IE6
				})
				.prependTo(element);

			fixBugs();

			function fixBugs(){

				// dynamic CSS for IE6 and IE7 in quirks mode
				var style = transparentDiv.get(0).style;
				if (element.innerHeight() > transparentDiv.innerHeight()){
					if (style.setExpression) {
						style.setExpression("height","this.parentNode.clientHeight");
					}
				}
				if (element.innerWidth() > transparentDiv.innerWidth()){
					if (style.setExpression) {
						style.setExpression("width","this.parentNode.clientWidth");
					}
				}

				// IE bug related to alpha filter and clearType
				var style = element.get(0).style;
				if (style.removeAttribute) {
					style.removeAttribute('filter');
				}

			};

			function getPosition(position){
				if (position == 'fixed' || position == 'absolute')
					return position;
				return 'relative';
			};

			function selectOpacity(values) {
				for (var i = 0; i < values.length; i++)
					if (isValidOpacity(values[i]))
						return values[i];
			};

			function selectColor(colors) {
				for (var i = 0; i < colors.length; i++)
					if (isValidColor(colors[i]))
						return colors[i];
			};

			function isValidOpacity(v) {
				var n = Number(v);
				return (n < 1 && n > 0);
			};

			function isValidColor(c) {
				return (String(c).length > 0);
			};

		});

		return this;

	};

})(jQuery);

