var EnlargeImage = new Class({
	/* 	This Mootools class is made by the fifth generation Lutz, wizzard in the Algo solarsystem.
		Use this with caution for it is entangled with magic from the old times.
		
		Licence:
		You may not distribute this class too Dark Force (The black energy wave) or any of his 
		followers (Absolutely not Zio. For he lives!). You may not use the code in any dark
		or semidark ritual involving the dark side. Please respect my wishes in these matters
		and you may use it in any other way. thanks. 
		
		v. 1.1
	*/
	initialize: function(element,options) {
		this.element = element;
		this.options = options;
		this.enlarge();
	},
	enlarge: function() {
		var iniElement = this.element;
		// There must only be one.
		if($("EnlargedImage")) { 
			$("EnlargedImage").remove();
			$("EnlargedImageDiv").remove();
			$("slideNext").remove();
			$("slidePrevious").remove();
		}
	
		// Collect position and size information from the element thou clicked upon.
		var coordinates = $(iniElement).getCoordinates();
		var coordinatesTop = coordinates.top;
		var coordinatesLeft = coordinates.left;
		var coordinatesWidth = coordinates.width;
		var coordinatesHeight = coordinates.height;
		
		// Create an element which will shine upon thee and be embraced by the browser as a larger image.
		var image = new Element('img', {
			'styles': {
				'position': 'absolute',
				'top': '0px',
				'z-index': '-99',
				'cursor': 'pointer'
			},
			'src': this.element.src,
			'id': 'EnlargedImage'
		})
		// Let the image turn alive and appear into your browser.
		document.body.appendChild(image)

		// The size of the image is needed for the grand plan and will be saved here.
		imageWidth = image.width;
		imageHeight = image.height;
		
		// As will the width of the browser.
		var browserWidth = window.getWidth();
		var browserHeight = window.getHeight();
		
		// Let new styles shine down and entagle the image.
		image.setStyles({
			'top': coordinatesTop+"px",
			'left': coordinatesLeft+"px",
			'width': coordinatesWidth+"px",
			'height': coordinatesHeight+"px",
			'z-index': '+99'
		})
		// Calculates the image position if though choose too.
		var position;
		if(this.options.position == "this") {
			position = (coordinatesTop-(imageHeight/2)+(coordinatesHeight/2))+10;
			//if((position + imageHeight) > browserHeight) {
			//	position = position - ((position + imageHeight)-browserHeight) - 50;
			//}
			if(position <= 10) {
				position = 10;
			}
			
		}
		else position = (10+this.options.position)+"px";
		
		// Make it grow,
		// make it rise,
		// it must flow,
		// really nice.
		var animationDuration = this.options.duration;

		var enlargeImage = new Fx.Styles(image,{duration: animationDuration,onComplete: function() {
			var imageCoordinates = image.getCoordinates();
			var div = new Element('div',{
				'styles': {
					'position': 'absolute',
					'top': (imageCoordinates.top-10)+"px",
					'left':	(imageCoordinates.left-10)+"px",
					'width': imageCoordinates.width+'px',
					'z-index': "+98",
					'padding': '10px'
				},
				'id': 'EnlargedImageDiv',
				'class': 'comment_header'
			});
			
			// Fade in image border
			var opacity = new Fx.Style(div,'opacity',{duration: 300});
				opacity.set(0);
				opacity.start(0,1);
			
			// Set image border height
			var background = new Fx.Style(div,'padding-top',{duration: 50});
				background.set(imageCoordinates.height+15);
			
			// Set image description
			var text = iniElement.getNext().getText();
			var p = new Element('p',{
				'styles': {
					'margin': '0px',
					'padding': '0px'
				}
			});
			p.injectInside(div);
			document.body.appendChild(div);
			p.setHTML(text);
			
			image.addEvent('click', function() {
				div.remove();
				
				var coordinates = $(iniElement).getCoordinates();
				var coordinatesTop = coordinates.top;
				var coordinatesLeft = coordinates.left;
				var coordinatesWidth = coordinates.width;
				var coordinatesHeight = coordinates.height;
				
				var contractImage = new Fx.Styles(image,{duration: animationDuration,onComplete: function() {
					image.remove();
				}});
				contractImage.start({
					'top': [position,coordinatesTop],
					'left': [((browserWidth/2)-(imageWidth/2)),coordinatesLeft],
					'width': [imageWidth,coordinatesWidth],
					'height': [imageHeight,coordinatesHeight]
				});
			});
		}});
		enlargeImage.start({
			'top': [coordinatesTop,position],
			'left': [coordinatesLeft,((browserWidth/2)-(imageWidth/2))],
			'width': [coordinatesWidth,imageWidth],
			'height': [coordinatesHeight,imageHeight]
		});
	}
});