// $Id: $

/**
 * reflection.js v2.0
 * http://cow.neondragon.net/stuff/reflection/
 * Freely distributable under MIT-style license.
 */
 
/* From prototype.js */

var global_reflect = new Reflection();

jQuery.fn.reflect = function() {
	var p = this.get(0);
	var canvas = global_reflect.reflect_img(p);

	if (canvas != null)
		p.parentNode.replaceChild(canvas, p);
}

var defaultReflectionHeight = 0.2;
var defaultReflectionOpacity = 0.5;

var Reflection_Instances = new Array();

function Reflection(options) {
	if (!Reflection_Instances) {
		Reflection_Instances = new Array();
	}

	this.id = Reflection_Instances.length;
	Reflection_Instances[this.id] = this;
	
	doptions = { "height" : Reflection.defaultReflectionHeight, "opacity" : Reflection.defaultReflectionOpacity }
	
	if (options) {
		for (var i in doptions) {
			if (!options[i]) {
				options[i] = doptions[i];
			}
			
			this.options = options;
		}
	} else {
		this.options = doptions;
	}
	
	this.reflect_img = function(img) {
		this.img = img;
		
		try {
			var d = document.createElement('div');
			var p = /* document.getElementById(this.img_id)*/ img;
			
			var reflectionHeight = Math.floor(p.height*this.options['height']);
			var divHeight = Math.floor(p.height*(1+this.options['height']));
			
			var reflectionWidth = p.width;
			
			if (document.all && !window.opera) {
				/* Fix hyperlinks */
				
	            if(p.parentElement.tagName == 'A') {
//	                var d = document.createElement('a');
	                
//	                d.href = p.parentElement.href;
	            }  
	                
				/* Copy original image's classes & styles to div */
//				d.className = newClasses;
//				p.className = 'reflected';
				
				d.style.cssText = p.style.cssText;
				p.style.cssText = 'vertical-align: bottom';
			
				var reflection = document.createElement('img');
				
				reflection.src = p.src;
				reflection.style.width = reflectionWidth+'px';
				reflection.style.display = 'block';
				reflection.style.height = p.height+"px";
				
				reflection.style.marginBottom = "-"+(p.height-reflectionHeight)+'px';
				reflection.style.filter = 'flipv progid:DXImageTransform.Microsoft.Alpha(opacity='+(this.options['opacity']*100)+', style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy='+(this.options['height']*100)+')';
				
				d.style.width = reflectionWidth+'px';
				d.style.height = divHeight+'px';
//				p.parentNode.replaceChild(d, p);
				
			} else {
				var reflection = document.createElement('canvas');
				
				if (reflection.getContext) {
					/* Copy original image's classes & styles to div */
//					d.className = newClasses;
//					p.className = 'reflected';
					
					d.style.cssText = p.style.cssText;
					p.style.cssText = 'vertical-align: bottom';
			
					var context = reflection.getContext("2d");
				
					reflection.style.height = reflectionHeight+'px';
					reflection.style.width = reflectionWidth+'px';
					reflection.height = reflectionHeight;
					reflection.width = reflectionWidth;
					
					d.style.width = reflectionWidth+'px';
					d.style.height = divHeight+'px';
//					p.parentNode.replaceChild(d, p);
					
					context.save();
					
					context.translate(0,p.height-1);
					context.scale(1,-1);
					
					context.drawImage(p, 0, 0, reflectionWidth, p.height);
	
					context.restore();
					
					context.globalCompositeOperation = "destination-out";
					
					var gradient = context.createLinearGradient(0, 0, 0, reflectionHeight);
					
					gradient.addColorStop(1, "rgba(255, 255, 255, 1.0)");
					gradient.addColorStop(0, "rgba(255, 255, 255, "+(1-this.options['opacity'])+")");

					context.fillStyle = gradient;
					context.rect(0, 0, reflectionWidth, reflectionHeight*2);
					context.fill();
				}
				else
					return null;
			}

			d.id = p.id;
			p.id = p.id + '_pic';

			d.appendChild(p);
			d.appendChild(reflection);
			
			return d;
		} catch (e) {
			return null;
	    }
	}
}

