//--------------------------------------------------------------------------------
// Keep track of which image we're current looking at
var curImage = null;
var myImage = null;
var arrImage = [];

//--------------------------------------------------------------------------------
//*************************************************************************************************
// KONVERZNI FUNKCE UPRAVENA UZIVATELEM, KTERE ZE JMENA MINIATURY GENERUJE JMENO PLNEHO OBRAZKU
//*************************************************************************************************
  function thumbnail2full( imgName)
  {
    var ilist = [];
    ilist = imgName.split("/");
    for( var i = 0; i < ilist.length; i++)
    {
      if(  ilist[i].toLowerCase() == "thumb")
      {
        ilist[i] = "large";
        ilist[i+1] = ilist[i+1].substr( 3);
      }
    }
    imgName = ilist.join("/");

    return imgName;
  }
//*************************************************************************************************
//*************************************************************************************************

//--------------------------------------------------------------------------------
// Reposition the gallery to be at the center of the page
// even when the page has been scrolled
function adjust( w, h){
		// Locate the gallery
    var obj = id("gallery");
    var objimg = id("gallery_image");

		// Make sure that the gallery exists
		if ( !obj ) return;

		// Find its current height and width
    if ( !w)
		  w = getObjectWidth( objimg); // getWidth( obj );     replace Lukin
    if ( !h)
		  h = getObjectHeight( objimg); //getHeight( obj );    replace Lukin

		// Position the box, vertically, in the middle of the window
		var t = scrollY() -20+ ( getInsideWindowHeight() / 2 ) - ( h / 2 ); //windowHeight

		// But no heigher than the top of the page
		if ( t < 0 ) t = 0;

		// Position the box, horizontally, in the middle of the window
		var l = scrollX() + ( getInsideWindowWidth() / 2 ) - ( w / 2 ); //windowWidth

		// But no less than the left of the page
		if ( l < 0 ) l = 0;

		// Set the adjusted position of the element
		setY( obj, t );
		setX( obj, l );
};

//--------------------------------------------------------------------------------
// Readjust the position of the gallery every time
// the user scrolls the page or resizes the browser
window.onresize = document.onscroll = adjust;

//--------------------------------------------------------------------------------
// Hide the grey overlay and the current gallery
function hideOverlay() {
		// Make sure that we reset the current image
		curImage = null;

		// and hide the overlay and gallery
		hide( id("overlay") );
		hide( id("gallery") );
}

//--------------------------------------------------------------------------------
// Show the grey overlay
function showOverlay() {
		// Find the overlay
		var over = id("overlay");

		// Make it as tall and wide as the entire page
		// (this helps with scrolling)
		over.style.height = Math.max( getInsideWindowHeight(), pageHeight()) + "px";
		over.style.width = Math.max( getInsideWindowWidth(), pageWidth()) + "px";


		// And fade it in
//		fadeIn( over, 50, 10 );    // remove Lukin
    show( over);                 // add Lukin

}

//--------------------------------------------------------------------------------
function anchor2index( curAnchor) {
  var curImage = child( curAnchor);
  var thumbName = curImage.src;
  var largeName = thumbnail2full( thumbName);
  for ( var i=0; i<arrImage.length; i++) {
     if ( largeName == arrImage[ i])
     return i;
  }
  return -1;
}

function index2anchor( curIndex) {
  if ( curIndex >= 0 && curIndex < arrImage.length) {
    var curName = arrImage[ curIndex];
    var tbl = id( "thumbnails");
    var img;
    var imgThumb, imgLarge;
    if ( tbl) {
      var link = tbl.getElementsByTagName( "a");
  				// Go through each of the image links
  				for ( var j = 0; j < link.length; j++ ) {
  				    img = child( link[j]);
  				    if ( img) {
  				       imgThumb = img.src;
  				       imgLarge = thumbnail2full( imgThumb);
  				       if ( curName == imgLarge) {
                   return link[ j];
                 }
              }
  				}
    }
  }
  return undefined;
}


//--------------------------------------------------------------------------------
function getPrevImage( curAnchor) {
   var pos = anchor2index( curAnchor);
   return  index2anchor( pos -1);
}
//--------------------------------------------------------------------------------
function getNextImage( curAnchor) {
   var pos = anchor2index( curAnchor);
   return index2anchor( pos +1);
}
//--------------------------------------------------------------------------------
// Find the previous image and show it
function prevImage() {
		// Locate the previous gallery image and show it
		showImage( getPrevImage( curImage ) );

		// Prevent the link from operating as normal
		if(window.ActiveXObject)
		  stopEvent();
		return false;
}

//--------------------------------------------------------------------------------
// Find the next image and show it
function nextImage() {
		// Locate the next gallery image and show it
		showImage( getNextImage( curImage ) );

		// Prevent the link from operating as normal
		if(window.ActiveXObject)
		  stopEvent();
		return false;
}

//--------------------------------------------------------------------------------
// Show the current gallery image
function showImage(cur) {
		// Remember which image we're currently dealing with
    var gallery = id("gallery");
    hide( gallery);
    curImage = cur;
		// Find the gallery image
    var img = id("gallery_image");
		// Remove the image, if there's one already there
    var childElem = child( img);
    if ( childElem )
        img.removeChild( childElem );

		var imgClone = document.createElement("a");

//    var imgClone =  child( cur).cloneNode( true );
		imgClone.onclick = function(){
								// Show the gray background
								hideOverlay();

								// Show the image, in the gallery
//								showImage( this.parentNode );

								// Make sure that the browser doesn't go the
								// image, like it normally would
								return false;
						};

    curImage2 =  child(cur).cloneNode( true );
    var popis =  childText( cur);

    var imgName = curImage2.src;
    imgName = thumbnail2full( imgName);

    myImage = new Image();
    myImage.src = imgName;

    curImage2.src = myImage.src; //imgName;
    imgClone.appendChild( curImage2 );

    img.appendChild( imgClone );
    //id("gallery_title").innerHTML = popis; //child( child( cur)).alt;

    // Hide the next link if we're at the end of the slideshow
    if ( !getNextImage(cur) )
        hide( id("gallery_next") );

    // Otherwise, make sure that it's visible
    else
        show( id("gallery_next") );

    // Hide the previous link if we're at the start of the slideshow
    if ( !getPrevImage(cur) )
        hide( id("gallery_prev") );

    // Otherwise, we need to be sure that it's visible
    else
        show( id("gallery_prev") );

    // Locate the main gallery
    var gallery = id("gallery");

    // Set the correct class (so that it's the correct size)
    gallery.className = cur.className;

    callCompleteTimer(myImage);
}

//--------------------------------------------------------------------------------
function childText( elem ) {
    elem = elem.firstChild;
    // && elem.nodeType != 1
    while ( elem  && elem.nodeType != 3) {
      elem = elem.nextSibling;
    }
    if ( elem) return elem.textContent;
    return "";
}
//--------------------------------------------------------------------------------
function callCompleteTimer()
{
  if ( myImage && myImage.complete && myImage.width && myImage.height)
  {
    finishImage();
  }
  else
  {
    setTimeout("callCompleteTimer();", 300);
  }
}

//--------------------------------------------------------------------------------
function finishImage()
{
  var ww = myImage.width;
  var hh = myImage.height;
  adjust( ww, hh);
  var gallery = id("gallery");
  fadeIn( gallery, 100, 20 );
}



//--------------------------------------------------------------------------------
// Wait for the document to finish loading before modifying
// or traversing the DOM
window.onload = function() {

  initDHTMLAPI();
/*
		 * Create the following DOM structure:
		 * <div id="overlay"></div>
		 * <div id="gallery">
		 *     <div id="gallery_image"></div>
		 *     <div id="gallery_prev"><a href=""><img src="images/s_l.gif"></a></div>
		 *     <div id="gallery_next"><a href=""><img src="images/s_p.gif"></a></div>
		 *     <div id="gallery_title"></div>
		 * </div>
		 */
		// Create the transparent, gray, overlay
		var overlay = document.createElement("div");
		overlay.id = "overlay";

		// Make it so that when the grey background is clicked,
		// the gallery and background are hidden
		overlay.onclick = hideOverlay;

		// Add the overlay into the DOM
		document.body.appendChild( overlay );

		// Create the overall gallery holder
		var gallery = document.createElement("div");
		gallery.id = "gallery";

		// And add in all the organization divs
		gallery.innerHTML = '<div id="gallery_image"></div>' +
				'<div id="gallery_prev"><a href=""><img src="images/s_l.gif"></a></div>' +
				'<div id="gallery_next"><a href=""><img src="images/s_p.gif"></a></div>' +
				'<div id="gallery_title"></div>';

		// Add the gallery into the DOM
		document.body.appendChild( gallery );

		// Handle support for which the next and previous links
		// are clicked within the gallery
		id("gallery_next").onclick = nextImage;
		id("gallery_prev").onclick = prevImage;
/*
		// Locate all the galleries on the site
		var g = byClass( "gallery", "ul" );

		// Go through all of them
		for ( var i = 0; i < g.length; i++ ) {
				// And locate all the links to the slideshow images
				var link = tag( "a", g[i] );

				// Go through each of the image links
				for ( var j = 0; j < link.length; j++ ) {
						// Make it such that, when clicked, they display the
						// image gallery instead of going to the imagae
						link[j].onclick = function(){
								// Show the gray background
								showOverlay();

								// Show the image, in the gallery
								showImage( this.parentNode );

								// Make sure that the browser doesn't go the
								// image, like it normally would
								return false;
						};
				}
		}
*/
  var tbl = id( "thumbnails");
  var img;
  var imgThumb, imgLarge;
  if ( tbl) {
    var link = tbl.getElementsByTagName( "a");
//    alert( link.length);
				// Go through each of the image links
				for ( var j = 0; j < link.length; j++ ) {
				    img = child( link[j]);
				    if ( img) {
				       imgThumb = img.src;
				       imgLarge = thumbnail2full( imgThumb);
               arrImage.push( imgLarge);
//				      alert( img.src);
				    
            }
						// Make it such that, when clicked, they display the
						// image gallery instead of going to the imagae
						link[j].onclick = function(){
								// Show the gray background
								showOverlay();

								// Show the image, in the gallery
//								showImage( this.parentNode );
								showImage( this);

								// Make sure that the browser doesn't go the
								// image, like it normally would
								if(window.ActiveXObject)
								  stopEvent();
								return false;
						};
				}
  }
//  alert( arrImage);

};
