ajaxGalleryOpened = false;
function displayAjaxGallery(idGallery, imagePathToLoad, nrMinImage)
{

	if (ajaxGalleryOpened == true ) return;

	ajaxGalleryOpened = true;

	var bg = null;

	if ($('bgExist')) return;

	bg = new Element("div");
	bg.writeAttribute('id', 'bgExist');

	/* ukrywanie selektw pod ie6 */
	if (Prototype.Browser.IE)
	{
		if (!/MSIE [789]/.test((navigator.appVersion +"")))
		{
			$$('select').each(function(node){
				node.hide();
			});
		}
	}

	new Ajax.Request('frame_nh.php?ajax_gallery_id='+idGallery, {
	  onCreate: function(transport){
      var innerHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
    	var windowWidth = document.viewport.getWidth();
    	var windowHeight = document.viewport.getHeight();
    	var pageOffset = document.viewport.getScrollOffsets();

    		bg.setStyle({
		    			zIndex: 800,
		    			position: 'absolute',
		    			background: '#000000',	/* tlo dookola */
		    			width: windowWidth + 'px',
		    			height: innerHeight + 'px',
       				top: 0,
		    			left: 0,
		    			opacity: 0.6,
		    			cursor: 'pointer'
					});


    		document.body.appendChild(bg);
		},

	  onComplete: function(transport) {

	    if (200 == transport.status)
	    {
	    	var galleryAjax = new Element("div");

	    	var innerHeight = (document.height !== undefined) ? document.height : document.body.offsetHeight;
	    	var windowWidth = document.viewport.getWidth();
	    	var windowHeight = document.viewport.getHeight();
	    	var pageOffset = document.viewport.getScrollOffsets();
	    	
	    	if (innerHeight < windowHeight)
	    	{
	    		innerHeight = windowHeight;
	    	}



		    document.body.appendChild(galleryAjax);
		    galleryAjax.update("");
		    galleryAjax.update(transport.responseText);
				bg.setStyle({
		    			zIndex: 800,
		    			position: 'absolute',
		    			width: windowWidth + 'px',
		    			height: innerHeight + 'px',
		    			top: 0,
		    			left: 0,
		    			cursor: 'pointer'
					});

				galleryAjax.setStyle({
					zIndex: 801,
					position: 'absolute',
					width: 'auto'
				});


				var galleryWidth = $('ajax_gallery_container').getWidth();
				var leftOffset = Math.round((windowWidth - galleryWidth ) / 2 );
				var topOffset = (windowHeight - galleryAjax.getHeight() ) / 2;
				
				if (topOffset < 0)
				{
					topOffset = pageOffset.top;
				}
				else
				{
				  topOffset += pageOffset.top;
				}



				galleryAjax.setStyle({
						left: leftOffset + "px",
						top: topOffset + "px",
						width: galleryWidth + "px"
					});



				onScollHandle = function()
				{
				  return;
				  /*
				    var pageOffset = document.viewport.getScrollOffsets();
						var topOffset = pageOffset.top + 80;

						//if (scrollTimer) scrollTimer.stop();

						scrollTimer = new PeriodicalExecuter(function(pe)
						{
							galleryAjax.setStyle({
								top: topOffset + "px"
							});
							pe.stop();
						},0.15);
					*/
				};

				hideScroll = function() {
						if (Prototype.Browser.IE)
						{
							if (!/MSIE [789]/.test((navigator.appVersion +"")))
							{
								$$('select').each(function(node){
									node.show();
								});
							}
						}
					  Event.stopObserving(window, 'resize', onResizeHandle);
					  Event.stopObserving(window, 'scroll', onScollHandle);
		  			galleryAjax.remove();
						bg.remove();
				}

				onResizeHandle = function()
				{
					Event.stopObserving(window, 'resize', onResizeHandle);
					Event.stopObserving(window, 'scroll', onScollHandle);
					hideScroll();
				}

				Event.observe(window, 'scroll', onScollHandle);
				Event.observe(window, 'resize', onResizeHandle);

				bg.observe('click', function(event) { hideScroll(); });

				$('ajax_gallery_close').observe('click', function(event) { hideScroll(); });

				if (imagePathToLoad)
				{
					ajaxGalleryLoadImage(null, imagePathToLoad, nrMinImage);
				}


		  } // end if;
		  else

		  {
		  	/* ukrywanie selektw pod ie6 */
				if (Prototype.Browser.IE)
				{
					if (!/MSIE [789]/.test((navigator.appVersion +"")))
					{
						$$('select').each(function(node){
							node.show();
						});
					}
				}
			}

		  ajaxGalleryOpened = false;

	  }
	});
}


function ajaxGalleryLoadImage(objSender, imgPath, nrImage)
{

  $('ajax_gallery_min').select('.min_image_ajax_gallery').each(function(item, index)
  {
  	  if (nrImage == index)
	  {
	  	objSender = item;
	  }
	  
	  if (objSender == null && index == 0 && !nrImage)
	  {
	    objSender = item; 
	  }
	  
	  
	  
	  

	  if (item == objSender)
	  {
		  item.setStyle({
		    opacity: 1,
		    border: "solid 1px #dAdAdA"
			});
		}
		else
		{
			item.setStyle({
		    opacity: 0.4,
		    border: "solid 1px #dAdAdA"
			});
		}
	});

	$('ajax_gallery_big').update("");

	var img = new Element("img");
	img.writeAttribute("alt", "");
	img.writeAttribute("src", imgPath);

	var containerHeight = $('ajax_gallery_big').getHeight();
	var containerWidth = $('ajax_gallery_big').getWidth();

	new PeriodicalExecuter(function(pe)
	{
		if (img.complete)
		{

			$('ajax_gallery_big').setStyle({
					overflow: 'auto',
					margin: 'auto'
				});

			if ((img.height > containerHeight) || (img.width > containerWidth) )
			{
				$('ajax_gallery_big').setStyle({
					overflow: 'auto',
					margin: 'auto'
				});
			}

			$('ajax_gallery_big').innerHTML = '';
			$('ajax_gallery_big').appendChild(img);
			pe.stop();
		}
	}, 0.1);
}

