Event.observe(window, 'load', function()
{
	Lightbox.init();
});

Event.observe(window, 'resize', function()
{
	if ($('lightbox') && $('lightbox').style.display == 'block') Lightbox.resize(Lightbox.currentImage.width, Lightbox.currentImage.height);
});

Event.observe(window, 'scroll', function()
{
	if ($('lightbox') && $('lightbox').style.display == 'block') Lightbox.resize(Lightbox.currentImage.width, Lightbox.currentImage.height);
});


var Lightbox = {
	currentImage : null,
	galleryImages : new Array(),
	imageTitles : new Array(),
	imageIndex : 0,
	
	init : function()
	{
		var objOverlay = document.createElement('div');
		objOverlay.id = 'lbox-overlay';
		objOverlay.onclick = function() { Lightbox.hide(); }
		
		var objLoadingImage = document.createElement('img');
		objLoadingImage.id = 'loading_img';
		objLoadingImage.setAttribute('src','lightbox/loading.gif');
		objLoadingImage.setAttribute('alt','');
		
		var objLightbox = document.createElement('div');
		objLightbox.id = 'lightbox';
		
		
		var objImageCont = document.createElement('div');
		objImageCont.id = 'lbox-image_cont';
		
		var objImage = document.createElement('img');
		objImage.setAttribute('src','');
		objImage.setAttribute('alt','');
		
		objImageCont.appendChild(objImage);
		
		
		var objNav = document.createElement('div');
		objNav.id = 'lbox-nav';
		
		var objBackBtn = document.createElement('a');
		objBackBtn.id = 'lbox-back_btn';
		objBackBtn.setAttribute('href','javascript: ');
		objBackBtn.onmouseover = function() { Lightbox.hideNavButton('next'); Lightbox.showNavButton('back'); };
		objBackBtn.onmouseout = function() { Lightbox.hideNavButton('back'); };
		objBackBtn.onclick = function() { Lightbox.changeImage(-1); };
		objBackBtn.appendChild(document.createTextNode('Előző'));
		
		var objNextBtn = document.createElement('a');
		objNextBtn.id = 'lbox-next_btn';
		objNextBtn.setAttribute('href','javascript: ');
		objNextBtn.onmouseover = function() { Lightbox.hideNavButton('back'); Lightbox.showNavButton('next'); };
		objNextBtn.onmouseout = function() { Lightbox.hideNavButton('next'); };
		objNextBtn.onclick = function() { Lightbox.changeImage(1); };
		objNextBtn.appendChild(document.createTextNode('Következő'));
		
		objNav.appendChild(objBackBtn);
		objNav.appendChild(objNextBtn);
		
		objImageCont.appendChild(objNav);
		
		
		var objBottom = document.createElement('div');
		objBottom.id = 'lbox-bottom';
		
		var objCloseButton = document.createElement('a');
		objCloseButton.id = 'lbox-close_btn';
		objCloseButton.setAttribute('href','javascript: ');
		objCloseButton.onclick = function() { Lightbox.hide(); };
		objCloseButton.appendChild(document.createTextNode('Bezár'));
		
		var objParagraph = document.createElement('p');
		
		objBottom.appendChild(objCloseButton);
		objBottom.appendChild(objParagraph);
		
		
		objLightbox.appendChild(objImageCont);
		objLightbox.appendChild(objBottom);
		
		document.body.appendChild(objOverlay);
		document.body.appendChild(objLoadingImage);
		document.body.appendChild(objLightbox);
		
		$$('a').each( function(anchor)
		{
			if (anchor.getAttribute('href') && anchor.getAttribute('rel') && anchor.getAttribute('rel').indexOf('lightbox') > -1)
			{
				Event.observe(anchor, 'click', function(eventObj)
				{
					Event.stop(eventObj);
					Lightbox.show(this);
				});
			}
		});
	},
	
	show : function(obj)
	{
		if (obj != null)
		{
			Lightbox.galleryImages = new Array();
			
			if (obj.getAttribute('rel').indexOf('[') > -1 && obj.getAttribute('rel').indexOf(']') > -1)
			{
				galleryName = obj.getAttribute('rel').substr(obj.getAttribute('rel').indexOf('[') + 1, obj.getAttribute('rel').indexOf(']') - obj.getAttribute('rel').indexOf('[') - 1);
				
				$$('a').each( function(anchor)
				{
					if (anchor.getAttribute('rel') && anchor.getAttribute('rel').indexOf('lightbox') > -1 && anchor.getAttribute('rel').indexOf('[') > -1 && anchor.getAttribute('rel').indexOf(']') > -1 && anchor.getAttribute('rel').substr(anchor.getAttribute('rel').indexOf('[') + 1, anchor.getAttribute('rel').indexOf(']') - anchor.getAttribute('rel').indexOf('[') - 1) == galleryName)
					{
						if (obj == anchor) Lightbox.imageIndex = Lightbox.galleryImages.push(anchor.getAttribute('href')) - 1;
						else Lightbox.galleryImages.push(anchor.getAttribute('href'));
						
						if (anchor.getElementsByTagName('img')[0] && anchor.getElementsByTagName('img')[0].getAttribute('alt')) Lightbox.imageTitles.push(anchor.getElementsByTagName('img')[0].getAttribute('alt'));
						else Lightbox.imageTitles.push('');
					}
				});
			}
			else
			{
				Lightbox.galleryImages.push(obj.getAttribute('href'));
				
				if (obj.getElementsByTagName('img')[0] && obj.getElementsByTagName('img')[0].getAttribute('alt')) Lightbox.imageTitles.push(obj.getElementsByTagName('img')[0].getAttribute('alt'));
				else Lightbox.imageTitles.push('');
				
				Lightbox.imageIndex = 0;
			}
		}
		
		Lightbox.changeImage(0);
	},
	
	changeImage : function(direction)
	{
		$('lightbox').style.display = 'none';
		
		var pageSize = Lightbox.getPageSize();
		
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = pageSize[1];
		var pageHeight = pageSize[0];
		
		$('lbox-overlay').style.width = windowWidth + 'px';
		$('lbox-overlay').style.height = pageHeight + 'px';
		
		$('lbox-overlay').style.display = 'block';
		
		
		$('loading_img').style.top = (((windowHeight - 22) / 2) + document.documentElement.scrollTop) + 'px';
		$('loading_img').style.left = (((windowWidth - 126) / 2) + document.documentElement.scrollLeft) + 'px';
		
		$('loading_img').style.display = 'block';
		
		
		if ((direction == -1 && Lightbox.imageIndex > 0) || (direction == 1 && Lightbox.imageIndex < Lightbox.galleryImages.length - 1)) Lightbox.imageIndex += direction;
		
		Lightbox.currentImage = new Image();
		Lightbox.currentImage.onload = function()
		{
			$$('#lightbox img')[0].setAttribute('src',Lightbox.galleryImages[Lightbox.imageIndex]);
			$$('#lbox-bottom p')[0].innerHTML = (Lightbox.galleryImages.length > 1 ? 'Kép ' + (Lightbox.imageIndex + 1) + ' / ' + Lightbox.galleryImages.length + '<br />' : '') + (Lightbox.imageTitles[Lightbox.imageIndex] ? '<span>' + Lightbox.imageTitles[Lightbox.imageIndex] + '</span>' : '');
			
			Lightbox.resize(Lightbox.currentImage.width, Lightbox.currentImage.height);
			
			$('loading_img').style.display = 'none';
			$('lightbox').style.display = 'block';
			
			$('lightbox').style.top = (((windowHeight - $('lightbox').offsetHeight) / 2) + document.documentElement.scrollTop) + 'px';
			$('lightbox').style.left = (((windowWidth - $('lightbox').offsetWidth) / 2) + document.documentElement.scrollLeft) + 'px';
		};
		
		Lightbox.currentImage.src = Lightbox.galleryImages[Lightbox.imageIndex];
		
		if (Lightbox.imageIndex > 0)
		{
			prevImage = new Image();
			prevImage.src = Lightbox.galleryImages[Lightbox.imageIndex - 1];
		}
		
		if (Lightbox.imageIndex < Lightbox.galleryImages.length - 1)
		{
			nextImage = new Image();
			nextImage.src = Lightbox.galleryImages[Lightbox.imageIndex + 1];
		}
	},
	
	resize : function(imageWidth, imageHeight)
	{
		var pageSize = Lightbox.getPageSize();
		
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = pageSize[1];
		var pageHeight = pageSize[0];
		
		$('lbox-overlay').style.width = windowWidth + 'px';
		$('lbox-overlay').style.height = pageHeight + 'px';
		
		
		var minWidth = 300;
		var minHeight = 300;
		var maxWidth = windowWidth - 100;
		var maxHeight = windowHeight - 100;
		
		if (maxWidth > minWidth && imageWidth > minWidth && imageWidth > maxWidth)
		{
			var resizeBy = maxWidth / imageWidth;
			
			imageWidth = Math.round(imageWidth * resizeBy);
			imageHeight = Math.round(imageHeight * resizeBy);
		}
		else if (maxWidth < minWidth && imageWidth > minWidth)
		{
			var resizeBy = minWidth / imageWidth;
			
			imageWidth = Math.round(imageWidth * resizeBy);
			imageHeight = Math.round(imageHeight * resizeBy);
		}
		
		if (maxHeight > minHeight && imageHeight > minHeight && imageHeight > maxHeight)
		{
			var resizeBy = maxHeight / imageHeight;
			
			imageWidth = Math.round(imageWidth * resizeBy);
			imageHeight = Math.round(imageHeight * resizeBy);
		}
		else if (maxHeight < minHeight && imageHeight > minHeight)
		{
			var resizeBy = minHeight / imageHeight;
			
			imageWidth = Math.round(imageWidth * resizeBy);
			imageHeight = Math.round(imageHeight * resizeBy);
		}
		
		
		$('lightbox').style.width = $$('#lightbox img')[0].style.width = imageWidth + 'px';
		$('lbox-image_cont').style.height = $$('#lightbox img')[0].style.height = imageHeight + 'px';
		
		if (Lightbox.imageIndex > 0) $('lbox-back_btn').style.display = 'block';
		else $('lbox-back_btn').style.display = 'none';
		
		if (Lightbox.imageIndex < Lightbox.galleryImages.length - 1) $('lbox-next_btn').style.display = 'block';
		else $('lbox-next_btn').style.display = 'none';
		
		
		if ($('lightbox').style.display == 'block') $('lightbox').style.top = (((windowHeight - $('lightbox').offsetHeight) / 2) + document.documentElement.scrollTop) + 'px';
		if ($('lightbox').style.display == 'block') $('lightbox').style.left = (((windowWidth - $('lightbox').offsetWidth) / 2) + document.documentElement.scrollLeft) + 'px';
	},
	
	hide : function()
	{
		$('lbox-overlay').style.display = 'none';
		$('loading_img').style.display = 'none';
		$('lightbox').style.display = 'none';
		
		$$('#lightbox img')[0].setAttribute('src','');
	},
	
	showNavButton : function(button)
	{
		if (button == 'back' && Lightbox.imageIndex > 0) $('lbox-back_btn').style.backgroundImage = 'url(lightbox/btn-prev.png)';
		else if (Lightbox.imageIndex < Lightbox.galleryImages.length - 1) $('lbox-next_btn').style.backgroundImage = 'url(lightbox/btn-next.png)';
	},
	
	hideNavButton : function(button)
	{
		if (button == 'back') $('lbox-back_btn').style.backgroundImage = 'url(lightbox/empty.jpg)';
		else $('lbox-next_btn').style.backgroundImage = 'url(lightbox/empty.jpg)';
	},
	
	getPageSize : function()
	{
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY)
		{
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		}
		else if (document.body.scrollHeight > document.body.offsetHeight)
		{
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		}
		else
		{
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight)
		{
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
		{
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		}
		else if (document.body)
		{
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
		
		if (yScroll < windowHeight) pageHeight = windowHeight;
		else pageHeight = yScroll;
		
		if (xScroll < windowWidth) pageWidth = windowWidth;
		else pageWidth = xScroll;

		
		arrayPageSize = new Array(pageHeight,windowHeight)
		return arrayPageSize;
	}
};