if (typeof DEBUG != 'function') {
	function DEBUG(string) {
		try {
			console.log(string);
		}
		catch (e) {}
	}
}



var Gallery = function() {
	var _private = {
		gallery: null,
		loader: null,
		/*-----------------------------------------------------------------------------------------
		 * center the large image
		 */
		centerLargeImage: function() {
			var image = YAHOO.util.Dom.getElementBy(function(elem) {
				return YAHOO.util.Dom.hasClass(elem, 'img2Col');
			}, 'img', _private.gallery);
			var width = image.offsetWidth;
			var height = image.offsetHeight;

			image.style.position = 'relative';
			image.style.top = '' + Math.floor((241 - height) / 2) + 'px';
			image.style.left = '' + Math.floor((240 - width) / 2) + 'px';
			image.style.visibility = 'visible';
		},
		/*-----------------------------------------------------------------------------------------
		 * center thumbnails
		 */
		centerThumbnails: function() {
			var images = YAHOO.util.Dom.getElementsByClassName('imgHalfCol', 'img', _private.gallery);

/*
			YAHOO.util.Event.addListener(images, 'load', function() {
				var width = this.offsetWidth;
				var height = this.offsetHeight;

				this.style.position = 'relative';
				this.style.top = '' + Math.floor((49 - height) / 2) + 'px';
				this.style.left = '' + Math.floor((48 - width) / 2) + 'px';
			});
*/
		},
		/*-----------------------------------------------------------------------------------------
		 * display the large image for the first thumbnail; if there are no thumbnails, remove the
		 * gallery
		 */
		initLargeImage: function() {
			var boxes = YAHOO.util.Dom.getElementsByClassName('boxHalfCol', 'div', _private.gallery);

			if (boxes.length > 0) {
				_private.replaceLargeImage(boxes[0]);
			}
			else {
				delete _private.loader;
				_private.loader = null;
				_private.gallery.parentNode.removeChild(_private.gallery);
				_private.gallery = null;
			}
		},
		/*-----------------------------------------------------------------------------------------
		 * remove empty thumbnail boxes from the gallery
		 */
		removeEmptyBoxes: function() {
			var boxes = YAHOO.util.Dom.getElementsByClassName('boxHalfCol', 'div', _private.gallery);
			var image;

			for (var i=0; i<boxes.length; i++) {
				image = YAHOO.util.Dom.getElementBy(function(elem) {
					return YAHOO.util.Dom.hasClass(elem, 'imgHalfCol');
				}, 'img', boxes[i]);

				if (!image || image.length == 0) {
					boxes[i].parentNode.removeChild(boxes[i]);
				}
			}
		},
		/*-----------------------------------------------------------------------------------------
		 * remove the large image from a gallery
		 */
		removeLargeImage: function() {
			var boxLarge = YAHOO.util.Dom.getElementBy(function(elem) {
				return YAHOO.util.Dom.hasClass(elem, 'boxImageLarge');
			}, 'div', _private.gallery);
			var imagesLarge = YAHOO.util.Dom.getElementsByClassName('img2Col', 'img', boxLarge);

			for (var i=0; i<imagesLarge.length; i++) {
				imagesLarge[i].parentNode.removeChild(imagesLarge[i]);

				try {
					delete imagesLarge[i];
				}
				catch (e) {}
			}
		},
		/*-----------------------------------------------------------------------------------------
		 * replace the large image of a gallery
		 *
		 * params:
		 *   box: the box containing the thumbnails to enlarge
		 */
		replaceLargeImage: function(box) {
			var boxes;
			var boxLarge;
			var imageSmall;
			var imageLarge;
			var srcSmall;
			var srcLarge;
			var srcLargeOld;

			// get all boxes containing images
			boxes = YAHOO.util.Dom.getElementsByClassName('boxHalfCol', 'div', _private.gallery);
			boxLarge = YAHOO.util.Dom.getElementBy(function(elem) {
				return YAHOO.util.Dom.hasClass(elem, 'boxImageLarge');
			}, 'div', _private.gallery);

			// get data of image to display
			imageSmall = YAHOO.util.Dom.getElementBy(function(elem) {
				return YAHOO.util.Dom.hasClass(elem, 'imgHalfCol');
			}, 'img', box);
			srcSmall = imageSmall.getAttribute('src');
			srcLarge = srcSmall.replace(/\.1003\.jpg$/, '.1001.jpg');   // freigestelltes Bild
			srcLarge = srcLarge.replace(/\.1006\.jpg$/, '.1004.jpg');   // angeschnittenes Bild

			try {
				srcLargeOld = YAHOO.util.Dom.getElementBy(function(elem) {
					return YAHOO.util.Dom.hasClass(elem, 'img2Col');
				}, 'img', boxLarge).getAttribute('src');
			}
			catch (e) {
				srcLargeOld = '';
			}
			if (srcLarge != srcLargeOld) {
				_private.loader.show();

				// remove any large images
				_private.removeLargeImage();

				// create new large image
				imageLarge = document.createElement('img');
				imageLarge.className = 'img2Col';
				imageLarge.setAttribute('src', srcLarge);
				imageLarge.style.visibility = 'hidden';

				if (boxLarge.hasChildNodes()) {
					YAHOO.util.Dom.insertBefore(imageLarge, boxLarge.firstChild);
				}
				else {
					boxLarge.appendChild(imageLarge);
				}
				// mark thumbnail of new large image
				for (var i=0; i<boxes.length; i++) {
					YAHOO.util.Dom.removeClass(boxes[i], 'selected');
				}
				YAHOO.util.Dom.addClass(box, 'selected');

				_private.loader.hide();
			}
		},
		/*-----------------------------------------------------------------------------------------
		 * replace the large image of a gallery (wrapper)
		 */
		replaceLargeImageWrapper: function() {
			_private.replaceLargeImage(this);
		}
	};

	var _public = {
		/*-----------------------------------------------------------------------------------------
		 * center all content images initially found on the page
		 */
		centerImages: function() {
			var imagesFound = false;
			var images;
			var width;
			var height;

			// large images
			images = YAHOO.util.Dom.getElementsByClassName('img2Col', 'img', 'containerContent');

			for (var i=0; i<images.length; i++) {
				imagesFound = true;
				width = images[i].offsetWidth;
				height = images[i].offsetHeight;

				images[i].style.position = 'relative';
				images[i].style.top = '' + Math.floor((241 - height) / 2) + 'px';
				images[i].style.left = '' + Math.floor((240 - width) / 2) + 'px';
				images[i].style.visibility = 'visible';
			}
			// medium images
			images = YAHOO.util.Dom.getElementsByClassName('img1Col', 'img', 'containerContent');

			for (var i=0; i<images.length; i++) {
				imagesFound = true;
				width = images[i].offsetWidth;
				height = images[i].offsetHeight;

				images[i].style.position = 'relative';
				images[i].style.top = '' + Math.floor((113 - height) / 2) + 'px';
				images[i].style.left = '' + Math.floor((112 - width) / 2) + 'px';
				images[i].style.visibility = 'visible';
			}
			// small images
			images = YAHOO.util.Dom.getElementsByClassName('imgHalfCol', 'img', 'containerContent');

			for (var i=0; i<images.length; i++) {
				imagesFound = true;
				width = images[i].offsetWidth;
				height = images[i].offsetHeight;

				images[i].style.position = 'relative';
				images[i].style.top = '' + Math.floor((49 - height) / 2) + 'px';
				images[i].style.left = '' + Math.floor((48 - width) / 2) + 'px';
				images[i].style.visibility = 'visible';
			}
			// repeat this function to fix a timing problem
			if (imagesFound) {
				window.setTimeout(_public.centerImages, 250);
			}
		},
		/*-----------------------------------------------------------------------------------------
		 * initialize the first gallery on the current page (this will not work with multiple
		 * galleries on one page)
		 */
		init: function() {
			var boxLarge;

			// initialize private attributes
			_private.gallery = YAHOO.util.Dom.getElementBy(function(elem) {
				return YAHOO.util.Dom.hasClass(elem, 'boxGallery');
			}, 'div', 'containerContent');

			if (!_private.gallery || _private.gallery.length == 0) {
				_private.gallery = null;
				return;
			}
			boxLarge = YAHOO.util.Dom.getElementBy(function(elem) {
				return YAHOO.util.Dom.hasClass(elem, 'boxImageLarge');
			}, 'div', _private.gallery);
			_private.loader = new YAHOO.widget.Panel('galleryLoader', {
				fixedcenter: false,
				close: false,
				draggable: false,
				zindex: 100,
				modal: false,
				visible: false
			});
			_private.loader.setBody('<div class="colorSetLoader"></div>');
			_private.loader.render(boxLarge);

			// modify gallery elements
			_private.removeEmptyBoxes();
			_private.centerThumbnails();
			_private.initLargeImage();

			var boxesSmall = YAHOO.util.Dom.getElementsByClassName('boxHalfCol', 'div', _private.gallery);

			for (var i=0; i<boxesSmall.length; i++) {
				boxesSmall[i].removeAttribute('onmouseenter');
			}
			YAHOO.util.Event.addListener(boxesSmall, 'mouseenter', _private.replaceLargeImageWrapper);
		}
	};
	return _public;
}();

YAHOO.util.Event.onDOMReady(function() {
	Gallery.init();
	Gallery.centerImages();
});
