if (typeof(Rotate360) == 'undefined') {
	var Rotate360 = new Class({
		Implements : [Options, Events],
		options : {
			width : 320,
			height : 240,
			container : null,
			element : null
		},
		initialize : function(source, options) {
			this.setOptions(options);
			this.source = source;
			var rotate360 = this;
			this.element = new Element('div', {
				'class' : 'rotate360',
				styles : {
					width : this.options.width + 'px',
					height : this.options.height + 'px',
					background : 'transparent no-repeat url("' + this.source + '") scroll 0 0'
				},
				events : {
					mouseenter : function(e) {
						if (typeof(rotate360.mouseHandlerDiv) != 'undefined') {
							var myPosition = rotate360.element.getCoordinates();
							rotate360.mouseHandlerDiv.setStyles({
								left : myPosition.left + 'px',
								top : myPosition.top + 'px',
								width : myPosition.width + 'px',
								height : myPosition.height + 'px'
							});
						}
					}
				}
			});
			this.mouseHandlerDiv = new Element('div', {
				styles : {
					position : 'absolute',
					cursor : 'e-resize'
				},
				events : {
					mousemove : function(e) {
						if (typeof(rotate360.mouseHeld) != 'undefined' && rotate360.mouseHeld && typeof(rotate360.previousPageX) != 'undefined' && typeof(rotate360.previousPageY) != 'undefined') {						
							var currentBackgroundPosition = rotate360.element.getStyle('background-position').split(' ');
							currentBackgroundPosition[0] = parseInt(currentBackgroundPosition[0]);
							currentBackgroundPosition[1] = parseInt(currentBackgroundPosition[1]);
							if (typeof(rotate360.rotateX) == 'undefined') rotate360.rotateX = 0;
							rotate360.rotateX += (e.page.x - rotate360.previousPageX) / (360 * (rotate360.options.width / 270) / ((rotate360.image.width * rotate360.image.height) / (rotate360.options.width * rotate360.options.height)));
							var workingAngle = parseInt(rotate360.rotateX);
							currentBackgroundPosition[0] = -rotate360.options.width * (workingAngle % (rotate360.image.width / rotate360.options.width));
							currentBackgroundPosition[1] = -rotate360.options.height * Math.floor(workingAngle / (rotate360.image.height / rotate360.options.height));							
							while (currentBackgroundPosition[0] > 0) currentBackgroundPosition[0] -= rotate360.image.width;
							while (currentBackgroundPosition[0] <= -rotate360.image.width) currentBackgroundPosition[0] += rotate360.image.width;
							while (currentBackgroundPosition[1] > 0) currentBackgroundPosition[1] -= rotate360.image.height;
							while (currentBackgroundPosition[1] <= -rotate360.image.height) currentBackgroundPosition[1] += rotate360.image.height;
							rotate360.element.setStyle('background-position', currentBackgroundPosition[0] + 'px ' + currentBackgroundPosition[1] + 'px');
							rotate360.previousPageX = e.page.x;
							rotate360.previousPageY = e.page.y;
						} else {
							rotate360.previousPageX = e.page.x;
							rotate360.previousPageY = e.page.y;
						}
					},
					mousedown : function(e) {
						e.stop();
						rotate360.mouseHeld = true;
						rotate360.mouseHandlerDiv.setStyles({
							left : 0,
							width : '100%'
						});
					},
					mouseup : function(e) {
						e.stop();
						rotate360.mouseHeld = false;
						rotate360.element.fireEvent('mouseenter');
					}
				}
			}).inject(document.body, 'top');
			this.image = new Asset.image(this.source, {
				onload : function() {
					if (rotate360.options.element) {
						rotate360.element.replaces(rotate360.options.element);
					} else if (rotate360.options.container) {
						rotate360.options.container.adopt(rotate360.element);
					}
				}
			});
		}
	});
	window.addEvent('domready', function() {
		// If an image is a link to a .360 file make it a 360 image.
		$$('a img').each(function(imageLink) {
			var link = imageLink.getParent('a');
			if (/\.360$/.exec(link.href)) {
				imageLink.addClass('rotate360');
				imageLink.replaces(link);
			}
		});
		// Replace images that have the rotate360 class with rotating images.
		$$('img.rotate360').each(function(rotate360) {
			var rotateButton = new Element('img', {
				src : '/bin/rotate360/rotate360.png',
				title : 'This is a 360\u00B0 image - click here to rotate it.',
				styles : {
					'position' : 'relative',
					'vertical-align' : 'bottom',
					'margin-left' : '-32px',
					'width' : '32px', 'height' : '32px',
					'left' : '-16px', 'top' : '-16px',
					'cursor' : 'pointer'
				}
			}).inject(rotate360, 'after');
			rotate360.addEvent('click', function(e) {
				if (rotate360.hasClass('rotate360')) {
					rotateButton.set('src', '/images/throbbers/large.gif');
					var src = rotate360.src.replace(/\.([a-zA-Z]+)$/, '_360.$1');
					var img = new Asset.image(src, {
						onload : function() {
							rotate360.removeClass('rotate360');
							rotateButton.dispose();
							new Rotate360(img.src, {
								width : rotate360.width,
								height : rotate360.height,
								element : rotate360
							});
						}
					});
				}
			});
			rotateButton.addEvent('click', function() {
				rotate360.fireEvent('click');
			});
		});
	});
}
