/*
	Rollover-Effekt für Bilder
	Copyright (c) 2009 Peter Kröner
	Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

var imageRollover = new Class({

	Implements: [Options, Events],
	options: {
		image: '',
		content: ''
	},

	initialize: function(options){
		this.setOptions(options);
		var image = this.options.image;
		var content = this.options.content;
		if (image && content){
			// Erst ersetzen wenn das Bild geladen ist
			image.addEvent('load', function(){
				var imageSize = image.getCoordinates();
				var imageStyle = image.getStyles('position', 'top', 'left', 'bottom', 'right', 'cssFloat', 'zIndex');
				// Der äußere Container (erbt die Position vom Bild)
				var outerContainer = new Element('div', { 'class': 'imageRollover-outerContainer' });
				outerContainer.setStyles({
					'width': imageSize.width,
					'height': imageSize.height,
					'position': imageStyle.position,
					'top': imageStyle.top,
					'left': imageStyle.left,
					'bottom': imageStyle.bottom,
					'right': imageStyle.right,
					'cssFloat': imageStyle.cssFloat,
					'zIndex': imageStyle.zIndex
				});
				// Der innere Container (relativ positioniert)
				var innerContainer = new Element('div', { 'class': 'imageRollover-innerContainer' });
				innerContainer.setStyles({
					'position': 'relative',
					'height': '100%',
					'width': '100%'
				});
				innerContainer.inject(outerContainer);
				// Die Box für den Rollover-Content (inklusive Content)
				var rollover = new Element('div', { 'class': 'imageRollover-rollover' });
				rollover.setStyles({
					'position': 'absolute',
					'top': 0,
					'left': 0,
					'height': '100%',
					'width': '100%',
					'zIndex': 1000,
					'opacity': 0.000001
				});
				if ($type(content) == 'element') {
					rollover.grab(content);
				}
				else {
					rollover.set('html', content);
				}
				// Das Bild darf nicht mehr z-index haben als die Rollover-Box
				image.setStyles({
					'zIndex': 999,
					'top': 0,
					'left': 0,
					'right': 0,
					'bottom': 0
				});
				// Rollover-Box bei Mouseover anzeigen und bei Mouseout ausblenden
				rollover.addEvent('mouseover', function(){ rollover.tween('opacity', 0.9); });
				rollover.addEvent('mouseout', function(){ rollover.tween('opacity', 0.000001); });
				// Das Bild aus dem Dokument nehmen und durch den äußeren Container ersetzen
				outerContainer.inject(image, 'before');
				innerContainer.grab(image).grab(rollover);
			});
		}
	}

});
