/*
	reflection.js for mootools v1.2
	by Christophe Beyls (http://www.digitalia.be) - MIT-style license
*/

var Reflection = {

	add: function(imgID, options){
		var img = $(imgID);
		if (img.getTag() != 'img') {
			return;
		}
		options = {
			args: [img, options]
		};
		if (window.ie) {
			options.delay = 50;
		}
		img.preload = new Image();
		img.preload.onload = Reflection.reflect.create(options);
		img.preload.src = img.src;
	},

	remove: function(imgID){
		var img = $(imgID);
		if ((img) && (img.preload)) {
			img.preload.onload = null;
		}
		if ((img) && (img.getTag() == 'img') && (img.className == 'reflected')){
			img.className = img.parentNode.className;
			img.style.cssText = img.backupStyle;
			img.parentNode.replaceWith(img);
		}
	},

	reflect: function(img, options){
		if (typeof(img) != 'undefined') {
			if (!((typeof img.currentTarget) == 'undefined')) {
				img = $(img.currentTarget);
			}

			// Added DKD Standart PAR config for the opacity
			var thisOpacity = par.reflection.opacity;

			options = $extend({
				height: 0.33,
				opacity: thisOpacity
			}, options || {});

			Reflection.remove(img);

			var canvas;
			var canvasHeight = Math.floor(img.height * options.height);

			if (window.ie){
				if (!window.ie6) {
					canvas = new Element('img', {
						'src': img.src,
						'styles': {
							'width': 		img.width,
							'marginBottom': -img.height+canvasHeight,
							'filter': 		'flipv progid:DXImageTransform.Microsoft.Alpha(opacity='+(options.opacity*100)+', style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy='+(options.height*100)+')'
						}
					});
				} else {
					// NO REFLECTION IN IE6 (PNG Problems)
					canvas = new Element('span');
				}
			} else {
				canvas = new Element('canvas', {
					'styles': {
						'width':  img.width,
						'height': canvasHeight
					}
				});
				if (!canvas.getContext) {
					return;
				}
			}

			if (true) {
				var div = new Element('div').adopt(img, canvas);
			} else {
				var div = new Element('div').injectAfter(img).adopt(img, canvas);
			}
			div.className     = img.className;
			div.style.cssText = img.backupStyle = img.style.cssText;
			div.removeClass('reflect').setStyles({'width': img.width, 'height': canvasHeight+img.height});
				// Set Styles and Classes
			img.style.cssText = 'vertical-align: bottom';
			img.className     = 'reflected';

			if (window.ie) {

				if (window.ie6) {
			  		// Insert new span
					addSpan = new Element('span', {
						'styles': {
							filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled="true", sizingMethod="crop", src="' + img.src + '")',
							display: "inline-block"
						}
					});
					addSpan.injectAfter(img).adopt(img);
					img.setStyles({
						background: 'none',
						filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=0)"
					});
				}

				return;
			}

			var context = canvas.setProperties({'width': img.width, 'height': canvasHeight}).getContext('2d');
				context.save();
				context.translate(0, img.height-1);
				context.scale(1, -1);
				context.drawImage(img, 0, 0, img.width, img.height);
				context.restore();
				context.globalCompositeOperation = 'destination-out';
			var gradient = context.createLinearGradient(0, 0, 0, canvasHeight);
				gradient.addColorStop(0, 'rgba(255, 255, 255, ' + (1 - options.opacity) + ')');
				gradient.addColorStop(1, 'rgba(255, 255, 255, 1.0)');
				context.fillStyle = gradient;
				context.rect(0, 0, img.width, canvasHeight);
				context.fill();
		}
	},

	addFromClass: function(){
		var css_statement = "img." + par.reflection.cssClass;
		$$(css_statement).each( function(img){
			this.add(img);
		}.bind(this));
	}
};

