var gallery = new Object();
	gallery.created = false;
var body = Element.extend(document.body);
var html = body.up();
var screenDim;
var imagesInOneRow, rows, actualRow = 1;
var menuImages, indexOfActualMini;

function showGallery(galleryNo, photoNo){
	if(!gallery.created){
		gallery.layer = new Element('div',{id: 'galleryLayer'});
		gallery.content = new Element('div',{id: 'galleryContent'});
		gallery.loading = new Element('img',{id: 'galleryLoading', src: "/gfx/ico/ajax-loader.gif", width: "32", height: "32", alt: "Wgrywanie zdjęcia" });
		gallery.menu = new Element('div',{id: 'galleryMenu'});
		
		gallery.layer.insert(gallery.content);
		gallery.layer.insert(gallery.loading);
		gallery.layer.insert(gallery.menu);
		
		gallery.menuLeft = new Element('a',{id: 'menuLeft',href: '#'}).update('lewo');
		gallery.scroll = new Element('div',{id: 'scroll'});
		gallery.menuRight =new Element('a',{id: 'menuRight',href: '#'}).update('prawo');
		
		gallery.menu.insert(gallery.menuLeft);
		gallery.menu.insert(gallery.scroll);
		gallery.menu.insert(gallery.menuRight);

		body.insert({'top' : gallery.layer});
		gallery.created = true;
		
		Event.observe(window,'resize',setDimensions);
		gallery.menuLeft.observe('click', function(){ changeRow(actualRow - 1);});
		gallery.menuRight.observe('click', function(){ changeRow(actualRow + 1);});
	}
	gallery.layer.show();
	html.setStyle({overflow: 'hidden'});
	showMenu(galleryNo);
	showPhoto(photoNo);
	return false;
}

function hideGallery(){
	html.setStyle({overflow: 'auto'});
	gallery.layer.hide();
}

function setDimensions(){
	screenDim = document.viewport.getDimensions();
	gallery.content.setStyle({height: (screenDim.height - parseInt(gallery.menu.getStyle('height'))) + 'px'});
	gallery.scroll.setStyle({width: (screenDim.width - parseInt(gallery.menuLeft.getStyle('width')) - parseInt(gallery.menuRight.getStyle('width'))) + 'px'});
	imagesInOneRow = (parseInt(gallery.scroll.getStyle('width'))/41).floor();
	rows = (gallery.scroll.select('img').length/imagesInOneRow).ceil();
	changeRow((indexOfActualMini / imagesInOneRow).ceil());
}

function changeRow(tempRow){
	if((tempRow < actualRow && tempRow > 0) || (tempRow > actualRow && parseInt(gallery.scroll.getStyle('top')) > -(parseInt(gallery.scroll.getStyle('height'))*(rows-1)))) {
		actualRow = tempRow;
		gallery.scroll.setStyle({top: (-(actualRow - 1) * parseInt(gallery.scroll.getStyle('height'))) + 'px'});
	}
}

function showMenu(galleryNo){
	new Ajax.Updater('scroll', '/ajax/galeriaController.php', {
		method: 'get',
		parameters: {'id_galeria' :  galleryNo},
		onComplete: function(){
			menuImages = gallery.scroll.select('img');
			setDimensions();
		},
		onFailure: function(){ message('error','W działaniu galerii wystapił błąd, przepraszamy');}
		});
}

function showPhoto(activPhoto){
	new Ajax.Updater('galleryContent', '/ajax/galeriaController.php', {
		method: 'get',
		parameters: {'id_zdjecie' : activPhoto},
		onCreate: function (){
			if($('zdjecie') && !Prototype.Browser.Opera) $('zdjecie').stopObserving('load',showIMG);
			hideIMG();
		},
		onComplete: function (){
				if(Prototype.Browser.Opera){
					showIMG();
				} else {
					$('zdjecie').observe('load',showIMG);
				}
			},
		onFailure: function(){ message('error','W działaniu galerii wystapił błąd, przepraszamy');}
		});
	
	function showIMG(){
		gallery.loading.hide();
		gallery.content.show();
		if(parseInt($('zdjecie').height) > parseInt(gallery.content.getStyle('height'))) {
			var zdjecieNewHeight = (parseInt(gallery.content.getStyle('height')) - 50);
			var procent = zdjecieNewHeight/parseInt($('zdjecie').height);
			$('zdjecie').height = zdjecieNewHeight;
			$('foto').setStyle({width: (parseInt($('zdjecie').width)*procent) + 'px'});
			$('zdjecie').width = parseInt($('zdjecie').width)*procent;
		}
		menuImages.invoke('removeClassName' , 'active');
		var actualMini = gallery.scroll.select('img[alt="' + activPhoto + '"]')[0];
		actualMini.addClassName('active');
		indexOfActualMini = menuImages.indexOf(actualMini) + 1;
		changeRow((indexOfActualMini / imagesInOneRow).ceil());
	}
	function hideIMG(){
		gallery.content.hide();
		gallery.loading.show();
	}
}