function preloadImages () {
	var d = document; 
	
	if (d.images) { 
		if (!d.p) 
			d.p = new Array();
		
		var i, j = d.p.length, a = preloadImages.arguments; 
		
		for (i = 0; i < a.length; i++) {
			if (a[i].indexOf ("#") != 0) { 
				d.p[j] = new Image; 
				d.p[j++].src = a[i];
			}
		}
	}
}


function swapImg (img, mouseOver, obj) {
	var images = new Array (
		 new Array (
			 'img/button_mallorca.jpg'
			,'img/button_mallorca_hover.jpg'
		)
		,new Array (
			 'img/button_spanien.jpg'
			,'img/button_spanien_hover.jpg'
		)
		,new Array (
			 'img/button_kanaren.jpg'
			,'img/button_kanaren_hover.jpg'
		)
		,new Array (
			 'img/button_italien.jpg'
			,'img/button_italien_hover.jpg'
		)
		,new Array (
			 'img/button_istrien.jpg'
			,'img/button_istrien_hover.jpg'
		)
		,new Array (
			 'img/button_zypern.jpg'
			,'img/button_zypern_hover.jpg'
		)
		,new Array (
			 'img/button_footer_home.jpg'
			,'img/button_footer_home_hover.jpg'
		)
		,new Array (
			 'img/button_footer_agb.jpg'
			,'img/button_footer_agb_hover.jpg'
		)
		,new Array (
			 'img/button_footer_imprint.jpg'
			,'img/button_footer_imprint_hover.jpg'
		)
	);
	
	obj.src = images[img][mouseOver];
}


function getImageSize (imgWidth, imgHeight, maxWidth, maxHeight) {
	if (typeof maxWidth == 'undefined')
		
	maxWidth = typeof maxWidth == 'undefined' ? 100 : maxWidth;
	maxHeight = typeof maxHeight == 'undefined' ? 100 : maxHeight;
	
	if (imgWidth > maxWidth && imgHeight > maxHeight) {
		factorHeight = imgHeight / maxHeight;
		factorWidth = imgWidth / maxWidth;
		
		factor = factorWidth > factorHeight ? factorWidth : factorHeight;
	}
	else if (imgWidth > maxWidth && imgHeight <= maxHeight) 
		factor = imgWidth / maxWidth;
	else if (imgHeight > maxHeight && imgWidth <= maxWidth) 
		factor = imgHeight / maxHeight;
	else	
		factor = 1;
	
	newWidth = parseInt (imgWidth / factor);
	newHeight = parseInt (imgHeight / factor);
	
	return new Array (
		newWidth,
		newHeight
	);
}


var gallery = Class.create ({
	 imageArray: new Array ()
	
	,initialize: function () {
		this.container = document.getElementById ('container');
		
	 	document.observe('click', (function (obj) {
			var target = obj.findElement ('img[rel^=image]');
			if (target) {
				obj.stop();
				this.swapImage (target);
			}
		}.bind (this)).bind(this));
		
		document.observe ('click', function (obj) {
			var target = obj.findElement ('img[id^=container]');
			if (target) {
				obj.stop();
				window.open(this.container.src, '_blank');
			}
		}.bind (this))
	}
	,swapImage: function (sourceImg) {
		var img = new Image ();
		img.src = sourceImg.src;

		var attr = this.container.name.split (',');
		
		var size = getImageSize (img.width, img.height, attr[0], attr[1]);

		this.container.src = sourceImg.src;
		this.container.width = size[0];
		this.container.height = size[1];	
	}
});

document.observe ('dom:loaded', function () { 
	new gallery (); 
});
