var Lightbox = Class.create({


	initialize : function(css_selector)
	{
		$$(css_selector).each(function(a){
			a.observe('click', function(event){
//				img = new Element('img', { 'src' : a.href });
//				$(document.body).insert(img);
				this.toggle_lightbox(a.href);
				event.stop();
			}.bind(this));
		}.bind(this));
	},


	toggle_lightbox : function(image)
	{
		if(!$('lightbox_overlay'))
		{
			var size = get_page_size();
			var scroll = get_page_scroll();
			var lightboxTop = scroll + (size[3] / 15);

			//Overlay
			div = new Element('div', { 'id' : 'lightbox_overlay' });
			div.observe('click', function(event){
				this.toggle_lightbox();
			}.bind(this));
			$(document.body).insert(div);
			$('lightbox_overlay').setStyle({ width: size[0] + 'px', height: size[1] + 'px' });

			//Lightbox
			img = new Element('img', { 'src': image, 'id' : 'lightbox' });
			img.hide();

			img.observe('load', function(){
				img.setStyle({
					top: lightboxTop + 'px',
					left: (size[0]/2) - (img.width/2) + 'px'
				});
				img.show();
			});
			img.observe('click', function(ev){
				this.toggle_lightbox();
			}.bind(this));

			window.onscroll = function(scroll_ev){
				var size = get_page_size();
				var scroll = get_page_scroll();
				var lightboxTop = scroll + (size[3] / 15);

				img.setStyle({
					top: lightboxTop + 'px'
				});
			};

			$(document.body).insert(img);
		}
		else
		{
			$('lightbox_overlay').remove();
			$('lightbox').remove();
		}
	}
});



function get_page_size(){

	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){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array($(document.body).getWidth(),pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}


function get_page_scroll(){

	var yScroll = self.pageYOffset || document.documentElement.scrollTop;

	return yScroll;
}

Event.observe(window, 'load', function()
{
	 var light = new Lightbox('a[rel=lightbox]');
});
