/*!
	Slimbox v1.63 - The ultimate lightweight Lightbox clone
	(c) 2007-2008 Christophe Beyls <http://www.digitalia.be>
	MIT-style license.
*/

var Slimbox;

(function() {
	var elementsStyle = {}, state = 0, options, images, activeImage, prevImage, nextImage, top, fx, preload, preloadPrev = new Image(), preloadNext = new Image(),
	// State values: 0 (closed or closing), 1 (open and ready), 2+ (open and busy with animation)

	// DOM elements
	overlay, center, image, prevLink, nextLink, caption;

	/*
		Initialization
	*/

	window.addEvent("domready", function() {
		$(document.body).adopt(
			$$([
				overlay = new Element('div', {id: 'overlay'}).addEvent('click', close),
				center = new Element('div', {id: 'center'})
			]).setStyle('display', 'none')
		);

		image = new Element('div', {id: 'image'}).injectInside(center).adopt(
			new Element('a', {id: 'close', href: '#', title: 'Zatvoriť fotografiu'}).addEvent("click", close),
			prevLink = new Element('a', {id: 'prevlink', href: '#',  title: 'Predchádzajúca fotografia'}).addEvent('click', previous),
			nextLink = new Element('a', {id: 'nextlink', href: '#', title: 'Nasledujúca fotografia'}).addEvent('click', next),
			caption = new Element("div", {id: "caption"})
		);

		fx = {
			overlay: new Fx.Tween(overlay, {property: 'opacity', duration: 300}).set(0),
			image: new Fx.Tween(image, {property: 'opacity', duration: 300, onComplete: nextEffect})
		};
		
		//new Element('div', {style: 'display: block;background:url(/images/help-bg.png)'}).injectInside(caption);
	});


	/*
		API
	*/

	Slimbox = {
		open: function(_images, startImage) {
			options = {};

			// The function is called for a single image, with URL and Title as first two arguments
			if (typeof _images == "string") {
				_images = [[_images,startImage]];
				startImage = 0;
			}

			images = _images;
			position();
			setup(true);
			top = window.getScrollTop() + (window.getHeight() / 15);
			fx.resize = new Fx.Morph(center, $extend({duration: 300, onComplete: nextEffect}, {}));
			center.setStyles({top: top, width: 150, height: 150, marginLeft: -75, display: ''});
			fx.overlay.start(0.7);
			state = 1;
			return changeImage(startImage);
		}
	};

	Element.implement({
		slimbox: function(linkMapper) {
			// The processing of a single element is similar to the processing of a collection with a single element
			$$(this).slimbox(linkMapper);

			return this;
		}
	});

	Elements.implement({
		/*
			options:	Optional options object, see Slimbox.open()
			linkMapper:	Optional function taking a link DOM element and an index as arguments and returning an array containing 2 elements:
					the image URL and the image caption (may contain HTML)
			linksFilter:	Optional function taking a link DOM element and an index as arguments and returning true if the element is part of
					the image collection that will be shown on click, false if not. "this" refers to the element that was clicked.
					This function must always return true when the DOM element argument is "this".
		*/
		slimbox: function(linkMapper, linksFilter) {
			linkMapper = linkMapper || function(el) {
				return [el.href, el.title];
			};

			linksFilter = linksFilter || function() {
				return true;
			};

			var links = this;

			links.removeEvents('click').addEvent('click', function() {
				// Build the list of images that will be displayed
				var filteredLinks = links.filter(linksFilter, this);
				return Slimbox.open(filteredLinks.map(linkMapper), filteredLinks.indexOf(this));
			});

			return links;
		}
	});


	/*
		Internal functions
	*/

	function position() {
		overlay.setStyles({top: window.getScrollTop(), height: window.getHeight()});
	}

	function setup(open) {
		['object', window.ie ? 'select' : 'embed'].forEach(function(tag) {
			Array.forEach(document.getElementsByTagName(tag), function(el) {
				if (open) elementsStyle[el] = el.style.visibility;
				el.style.visibility = open ? 'hidden' : elementsStyle[el];
			});
		});

		overlay.style.display = open ? '' : 'none';

		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', position)[fn]('resize', position);
	}


	function previous() {
		return changeImage(prevImage);
	}

	function next() {
		return changeImage(nextImage);
	}

	function changeImage(imageIndex) {
		if ((state == 1) && (imageIndex >= 0)) {
			state = 2;
			activeImage = imageIndex;
			prevImage = ((activeImage) ? activeImage : images.length) - 1;
			nextImage = activeImage + 1;
			if (nextImage == images.length) nextImage = 0;

			$$(prevLink, nextLink, image).setStyle('display', 'none');
			fx.image.set(0);
			center.className = 'loading';

			preload = new Image();
			preload.onload = nextEffect;
			preload.src = images[imageIndex][0];
		}

		return false;
	}

	function nextEffect() {
		switch (state++) {
			case 2:
				center.className = '';
				image.setStyles({backgroundImage: 'url(' + images[activeImage][0] + ')', display: ''});
				$$(image).setStyle('width', preload.width);
				$$(image, prevLink, nextLink).setStyle('height', preload.height);
				$$(nextLink).setStyle('backgroundPosition', ((preload.width) / 2 - 37) + 'px center');

if (images[activeImage][1])
{
caption.style.display = 'block';
caption.set('html', '<div>&nbsp;</div><p><span>' + images[activeImage][1] + '</span></p>');
caption.style.left = preload.width / 2 - 125 + 'px';
caption.style.top = preload.height + 5 + 'px';
}
else
{
caption.style.display = 'none';
}
				if (prevImage >= 0) preloadPrev.src = images[prevImage][0];
				if (nextImage >= 0) preloadNext.src = images[nextImage][0];

				if (center.clientHeight != image.offsetHeight) {
					fx.resize.start({height: image.offsetHeight});
					break;
				}
				state++;
			case 3:
				if (center.clientWidth != image.offsetWidth) {
					fx.resize.start({width: image.offsetWidth, marginLeft: -image.offsetWidth/2});
					break;
				}
				state++;
			case 4:
				fx.image.start(1);
				break;
			case 5:
				if (prevImage >= 0) prevLink.style.display = '';
				if (nextImage >= 0) nextLink.style.display = '';
				state = 1;
		}
	}

	function close() {
		if (state) {
			state = 0;
			preload.onload = $empty;
			for (var f in fx) fx[f].cancel();
			$$(center).setStyle('display', 'none');
			fx.overlay.chain(setup).start(0);
		}

		return false;
	}

})();

Slimbox.scanPage = function() {
	var links = $$('a').filter(function(el) {
		return el.rel && el.rel.test(/^photo/i);
	});
	$$(links).slimbox(null, function(el) {
		return (this == el) || ((this.rel.length > 5) && (this.rel == el.rel));
	});
};
window.addEvent('domready', Slimbox.scanPage);
