// <!-- $Date: 2011-06-07 21:17:04 +0200 (Tue, 07 Jun 2011) $ $Revision: 64 $ -->
// Based on Blend 2.1 for jQuery 1.3+
// Copyright (c) 2010 Jack Moore - jack@colorpowered.com
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php

function highlightimage(m, nopartial){

	nopartial = !!nopartial;
	//console.log('highlightimage',m,nopartial);
	var re = new RegExp(/(.*)(\.png)(.*)/);
	var onoff = ''
	if(nopartial){ onoff = '-ie'; } ;
	if (re.exec(m)) {
		return RegExp.$1+'-highlight'+onoff+RegExp.$2+RegExp.$3;
	};
	return m.replace(/(^.*)(\.[^'")]*?([)]?)$)/,"$1-highlight$2$3");
};

(function ($, window) {

    $.fn.blend = function (speed, nopartial) {
		var $this = this,
		background = 'background',
		padding = 'padding',
		properties = [
		    background + 'Color',
		    background + 'Image',
		    background + 'Repeat',
		    background + 'Attachment',
		    background + 'Position', // Standards browsers
		    background + 'PositionX', // IE only
		    background + 'PositionY', // IE only
			padding + 'Top',
			padding + 'Left',
			padding + 'Right',
			padding + 'Bottom',
			'width',
			'height'
		];
		
		//console.log($this.selector);
		
		speed = parseInt(speed, 10) || $.fn.blend.speed;
		nopartial = nopartial || $.fn.blend.nopartial;
		
		// 1. Check to see if the jQuery object contains elements.
		// 2. Check to see if the effect has already been applied
		// 3. Check to see that the visitor is not using FireFox 2
		// or lower due to a bug that does not allow JavaScript to retrieve the CSS background position.
		// More info: https://bugzilla.mozilla.org/show_bug.cgi?id=316981
		if ($this[0] && !$this.find('.jsblend')[0] && !($.browser.mozilla && parseFloat($.browser.version) < 1.9)) {
			
			$this.each(function () {
				
				var base = this,
				i,
				style = base.currentStyle || window.getComputedStyle(base, null),
				layer = '<span style="position:absolute;top:0;left:0;display:block"/>',
				$content = $(layer),
				$background = $(layer),
				$foreground = $(layer);
				
				//console.log($(base).css("background-image"));
				
				if (base.style.position !== 'absolute') {
					base.style.position = 'relative';
				}
				
				for (i = 0; properties[i]; i += 1) {
					if (style[properties[i]]) {
						$background[0].style[properties[i]] = $foreground[0].style[properties[i]] = $content[0].style[properties[i]] = style[properties[i]];
					}
				}
				
				var m=highlightimage($(base).css("background-image"),nopartial);
				//console.log(m);
				$foreground[0].style.backgroundImage = m;
				$content[0].style.backgroundImage = $content[0].style.backgroundColor = '';
				
				$foreground.css({'opacity':0});
				
				$(base).wrapInner($content);
				$(base).prepend($background);
				$(base).prepend($foreground);
				
				$(base).bind('mouseenter mouseleave', function (e) {
					if (e.type === 'mouseenter') {
						$background.stop().fadeTo(speed, 0);
						$foreground.stop().fadeTo(speed,1);
					} else {
						$background.stop().fadeTo(speed, 1);
						$foreground.stop().fadeTo(speed,0);
					}
				});
			});
		}
		
		return $this;
	};
	
	$.fn.blend.speed = 400;
	$.fn.blend.nopartial = false;
	
}(jQuery, this));
