(function ($) {
	// Monkey patch jQuery 1.3.1+ to add support for setting or animating CSS
	// scale and rotation independently.
	// 2009 Zachary Johnson www.zachstronaut.com

	// Opera (> 10.50) Support added by Yannic "Pysco68" Staudt
	// http://www.ygraphix.net/ 
	var rotateUnits = 'deg';

	$.fn.rotate = function (val) {
		var style = $(this).css('transform') || 'none';

		// Special tratment for Opera... (by Yannic GraphiX)
		if ($.browser.opera) {
			// access our element... *sorrow*... have to pass with getElementById() :(
			var elem = document.getElementById($(this).attr("id"));
			style = document.defaultView.getComputedStyle(elem, null).getPropertyValue("-o-transform");

			// extract only "rotation-part"
			var m = style.toString().match(/rotate\(([^)]+)\)/);
			(m) ? m : m = 0;  // Is there any match? if not m = 0 (no rotation present)

			// Opera sets values as rad... rest of treatment is done in deg... so converting it
			var radval = m.toString().match(/([0-9\.-])+/);
			var degval = (parseFloat(radval) / Math.PI) * 180;

			// Rebuild the style string
			style = style.toString().replace(/none|rotate\([^)]*\)/, '') + 'rotate(' + degval + rotateUnits + ')';
		}

		if (typeof val == 'undefined') {
			if (style) {
				var m = style.toString().match(/rotate\(([^)]+)\)/);

				if (m && m[1]) {
					return m[1];
				}
			}

			return 0;
		}

		var m = val.toString().match(/^(-?\d+(\.\d+)?)(.+)?$/);
		if (m) {
			if (m[3]) {
				rotateUnits = m[3];
			}

			$(this).css(
                'transform',
                style.toString().replace(/none|rotate\([^)]*\)/, '') + 'rotate(' + m[1] + rotateUnits + ')'
            );


		}
	}

	// Note that scale is unitless.
	$.fn.scale = function (val, duration, options) {
		var style = $(this).css('transform');

		// Special tratment for Opera... (by Yannic GraphiX)
		if ($.browser.opera) {
			// access our element... *sorrow*... have to pass with getElementById() :(
			var elem = document.getElementById($(this).attr("id"));
			style = document.defaultView.getComputedStyle(elem, null).getPropertyValue("-o-transform");

			// extract only "rotation-part"
			var m = style.toString().match(/scale\(([^)]+)\)/);
			(m) ? m : m = 1;  // Is there any match? if not m = 0 (no scale applied)

			// extract only the numbers
			var number = m.toString().match(/([0-9\.-])+/);

			// Rebuild the style string
			style = style.toString().replace(/none|scale\([^)]*\)/, '') + 'scale(' + number + ')';
		}

		if (typeof val == 'undefined') {
			if (style) {
				var m = style.toString().match(/scale\(([^)]+)\)/);
				if (m && m[1]) {
					return m[1];
				}
			}

			return 1;
		}

		$(this).css(
            'transform',
            style.toString().replace(/none|scale\([^)]*\)/, '') + 'scale(' + val + ')'
        );
	}

	// fx.cur() must be monkey patched because otherwise it would always
	// return 0 for current rotate and scale values
	var curProxied = $.fx.prototype.cur;
	$.fx.prototype.cur = function () {
		if (this.prop == 'rotate') {
			return parseFloat($(this.elem).rotate());
		}
		else if (this.prop == 'scale') {
			return parseFloat($(this.elem).scale());
		}

		return curProxied.apply(this, arguments);
	}

	$.fx.step.rotate = function (fx) {
		$(fx.elem).rotate(fx.now + rotateUnits);
	}

	$.fx.step.scale = function (fx) {
		$(fx.elem).scale(fx.now);
	}

	/*
    
	Starting on line 3905 of jquery-1.3.2.js we have this code:
    
	// We need to compute starting value
	if ( unit != "px" ) {
	self.style[ name ] = (end || 1) + unit;
	start = ((end || 1) / e.cur(true)) * start;
	self.style[ name ] = start + unit;
	}
    
	This creates a problem where we cannot give units to our custom animation
	because if we do then this code will execute and because self.style[name]
	does not exist where name is our custom animation's name then e.cur(true)
	will likely return zero and create a divide by zero bug which will set
	start to NaN.
    
	The following monkey patch for animate() gets around this by storing the
	units used in the rotation definition and then stripping the units off.
    
	*/

	var animateProxied = $.fn.animate;
	$.fn.animate = function (prop) {
		if (typeof prop['rotate'] != 'undefined') {
			var m = prop['rotate'].toString().match(/^(([+-]=)?(-?\d+(\.\d+)?))(.+)?$/);
			if (m && m[5]) {
				rotateUnits = m[5];
			}

			prop['rotate'] = m[1];
		}

		return animateProxied.apply(this, arguments);
	}
})(jQuery);

