ajaxGalleryOpened = false;
function displayAjaxGallery(idGallery, imagePathToLoad, nrMinImage,galleryTitle)
{
	
	if (ajaxGalleryOpened == true ) return;
	
	ajaxGalleryOpened = true;

	var bg = null;

	if ($('bgExist')) return;

	bg = new Element("iframe");
	
	bg.writeAttribute('id', 'bgExist');
	
	bg.writeAttribute('frameborder', 0);
	bg.writeAttribute('scrolling', 'no');


	new Ajax.Request('/index/load/?module=offer&file=gallery&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: 2799,
		    			position: 'absolute',		    			
		    			backgroundColor: '#000000',	/* tlo dookola */
						backgroundImage: 'url(image/gallery/ajax_gallery_bg.gif)',	/* tlo dookola */		    			
		    			width: windowWidth + 'px',
		    			height: innerHeight + 'px',
   						top: 0,
		    			left: 0,
		    			opacity: 0.6,
		    			cursor: 'pointer'
					});
		
			
			bg.src = 'image/gallery/gallerybg.php';

			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();



		    document.body.appendChild(galleryAjax);
		    galleryAjax.update("");
		    galleryAjax.update(transport.responseText);
				bg.setStyle({
		    			zIndex: 2800,
		    			position: 'absolute',
		    			width: windowWidth + 'px',
		    			height: innerHeight + 'px',
		    			top: 0,
		    			left: 0,
		    			cursor: 'pointer'
					});

				galleryAjax.setStyle({
					zIndex: 2801,
					position: 'absolute',
					width: 'auto'
				});

				if(typeof galleryTitle!='undefined' )
				{
					$('ajax_gallery_title').update(galleryTitle);
				}
				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() {
						
					  Event.stopObserving(window, 'resize', onResizeHandle);
					  Event.stopObserving(window, 'scroll', onScollHandle);
					  ajaxGalleryOpened = false;
		  			  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);
				
				closeHande = function(ev) 
				{
					
					if (ev.keyCode == '27') {
						hideScroll();	
					}
				}
				
				Event.observe(document, 'keypress', closeHande);

				bg.observe('click', function(event) { hideScroll(); });

				$('ajax_gallery_close').observe('click', function(event) { hideScroll(); });

				if (imagePathToLoad)
				{
					ajaxGalleryLoadImage(null, imagePathToLoad, nrMinImage);
				}


		  } // end if;
		  

		  ajaxGalleryOpened = false;

	  }
	});
}


function ajaxGalleryLoadImage(objSender, imgPath, nrImage)
{

	if($('ajax_gallery_min'))
	{	
	  $('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);
}
