/**
	reflection.js for mootools v1.4
	(c) 2006-2008 Christophe Beyls <http://www.digitalia.be>
	MIT-style license.

Dependencies:
	Mootools 1.2 Core: Native - All, Element.Event, Element.Style, DomReady
	Mootools 1.2 More: 
*/

Element.implement({
	reflect: function(options) {
		var img = this;
		if (img.get("tag") != "img") return;

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

		function doReflect() {
			img.unreflect();
			var reflection, reflectionHeight = Math.floor(img.height * options.height), wrapper, context, gradient;

			if (Browser.Engine.trident) {
				reflection = new Element("img", {src: img.src, styles: {
					width: img.width,
					height: img.height,
					marginBottom: -img.height + reflectionHeight,
					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 {
				reflection = new Element("canvas");
				if (!reflection.getContext) return;
			}
			reflection.setStyles({display: "block", border: 0});

			wrapper = new Element(($(img.parentNode).get("tag") == "a") ? "span" : "div").injectAfter(img).adopt(img, reflection);
			wrapper.className = img.className;
			img.store("reflected", wrapper.style.cssText = img.style.cssText);
			wrapper.setStyles({width: img.width, height: img.height + reflectionHeight, overflow: "hidden"});
			img.style.cssText = "display: block; border: 0px";
			img.className = "";
			if (!Browser.Engine.trident) {
				context = reflection.setProperties({width: img.width, height: reflectionHeight}).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";

				gradient = context.createLinearGradient(0, 0, 0, reflectionHeight);
				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, reflectionHeight);
				context.fill();
			}
		}

		if (img.complete) {
			img.unreflect();
			doReflect();
		}
		else img.onload = doReflect;

		return img;
	},

	unreflect: function() {
		var img = this, reflected = this.retrieve("reflected"), wrapper;
		img.onload = $empty;

		if (reflected !== null) {
			wrapper = img.parentNode;
			img.className = wrapper.className;
			img.style.cssText = reflected;
			img.store("reflected", null);
			wrapper.parentNode.replaceChild(img, wrapper);
		}

		return img;
	}
});


/*
	reflection.js for mootools v1.4
	(c) 2006-2008 Christophe Beyls <http://www.digitalia.be>
	MIT-style license.
*/
Element.implement({reflect:function(B){var A=this;if(A.get("tag")!="img"){return }B=$extend({height:0.33,opacity:0.5},B);function C(){A.unreflect();var G,E=Math.floor(A.height*B.height),H,D,F;if(Browser.Engine.trident){G=new Element("img",{src:A.src,styles:{width:A.width,height:A.height,marginBottom:-A.height+E,filter:"flipv progid:DXImageTransform.Microsoft.Alpha(opacity="+(B.opacity*100)+", style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy="+(B.height*100)+")"}})}else{G=new Element("canvas");if(!G.getContext){return }}G.setStyles({display:"block",border:0});H=new Element(($(A.parentNode).get("tag")=="a")?"span":"div").injectAfter(A).adopt(A,G);H.className=A.className;A.store("reflected",H.style.cssText=A.style.cssText);H.setStyles({width:A.width,height:A.height+E,overflow:"hidden"});A.style.cssText="display: block; border: 0px";A.className="";if(!Browser.Engine.trident){D=G.setProperties({width:A.width,height:E}).getContext("2d");D.save();D.translate(0,A.height-1);D.scale(1,-1);D.drawImage(A,0,0,A.width,A.height);D.restore();D.globalCompositeOperation="destination-out";F=D.createLinearGradient(0,0,0,E);F.addColorStop(0,"rgba(255, 255, 255, "+(1-B.opacity)+")");F.addColorStop(1,"rgba(255, 255, 255, 1.0)");D.fillStyle=F;D.rect(0,0,A.width,E);D.fill()}}if(A.complete){A.unreflect();C()}else{A.onload=C}return A},unreflect:function(){var B=this,A=this.retrieve("reflected"),C;B.onload=$empty;if(A!==null){C=B.parentNode;B.className=C.className;B.style.cssText=A;B.store("reflected",null);C.parentNode.replaceChild(B,C)}return B}});

// AUTOLOAD CODE BLOCK (MAY BE CHANGED OR REMOVED)
var Reflection = {
	scanPage: function() {
		$$("img").filter(function(img) { return img.hasClass("reflect"); }).reflect({/* Put custom options here */});
	}
};
window.addEvent("domready", Reflection.scanPage);