	// Initializes the gallery
	function gal_init() {
  		gal_loadGalleryPart(0);
	}

	// Loads the image in the gallery
	function gal_loadImage(image) {
  		gal_loadCurrentImage(image + curphotostart);
	}

	// Loads the appropriate part of gallery
	function gal_loadGalleryPart(galpart) {
		gal_loadGalImagesFrom(galpart * galblocknum);
  	}

	// Loads the next gallery part
	function gal_loadNextGalleryPart(dx) {
  		if (curphotostart + galblocknum * dx < galphotonum) {
    			gal_loadGalImagesFrom(curphotostart + galblocknum * dx);
  		}	
	}

	// Loads the previous gallery part
	function gal_loadPreviousGalleryPart(dx) {
  		if (curphotostart - galblocknum * dx>= 0) {
    			gal_loadGalImagesFrom(curphotostart - galblocknum * dx);
  		} 
	}

	
	function gal_loadGalImagesFrom(imageNumber) {	
  		for (var i = 0; i < galblocknum; i++) {
    			var menuLink = document.getElementById("gal_simage" + i);
			
			if (i + imageNumber < galphotonum) {
				menuLink.href = "#";

				var menuImage = 0;
				
				var menuImage = document.getElementById("gal_img" + i);
				
				menuImage.src = image_data[imageNumber + i].path_thumb;
				menuImage.title = image_data[imageNumber + i].title;
				menuImage.alt = image_data[imageNumber + i].title;
				gal_dehighlightThumb(menuLink, menuImage);
				menuLink.style.visibility = "visible";
    			} else {
      				menuLink.style.visibility = "hidden";
    			}
		} 
		
		curphotostart = imageNumber;
  		gal_loadCurrentImage(curphotostart);
	}	
	

	function gal_setText(topnode, text) {
		if (topnode == null)
			return;
		
		var cn = topnode.childNodes.length;
		
		for (var i = cn - 1; i >= 0; i--) {
			var childNode = topnode.childNodes[i];
			if (childNode != null) 
				topnode.removeChild(childNode);
		};

		var node = null;

        	if (text != "") {
               		node = document.createTextNode(text);
        	} else {
               		node = document.createTextNode("");
		}

		topnode.appendChild(node);
	}

	function gal_hideBlock(block) {
		if (block != null) {
			block.style.visibility = "hidden";
		}
	}

	function gal_showBlock(block) {
		if (block != null) {
			block.style.visibility = "visible";
		}
	}

	function gal_highlightThumb(thumb, img) {
	if (thumb != null) {
			thumb.className = "galimgsel";
			//thumb.style.border = "1px solid #6E6E6E";
			//img.style.opacity = "1.0";
			//img.style.filter = "alpha(opacity=100)";
		}
	}

	function gal_dehighlightThumb(thumb, img) {
		if (thumb != null) {
			thumb.className = "";
			//thumb.style.border = "1px solid #E4E4E4";
			//img.style.opacity = ".5";
			//img.style.filter = "alpha(opacity=50)";
		}
	}

	function gal_loadCurrentImage(imageNumber) {
		// The img where we load the images
  		if (imageNumber >= galphotonum) 
    			return;

  		if (imageNumber < 0) 
    			return;

	
		curphoto = imageNumber;
		
		var loadedImage = document.getElementById("loadedimage");  
		if (loadedImage != null) {
			loadedImage.src = image_data[imageNumber].path_big;
			loadedImage.title = image_data[imageNumber].title;
			loadedImage.alt = image_data[imageNumber].title;
		}

		var titleField = document.getElementById("titlefield");
		if (titleField != null) {
              		gal_setText(titleField, image_data[imageNumber].title);
		}
		
		var descriptionField = document.getElementById("descriptionfield");
		if (descriptionField != null) {
			gal_setText(descriptionField, image_data[imageNumber].description);
		}

  		
  		if ((imageNumber >= curphotostart) && (imageNumber < curphotostart + galblocknum)) {
   			for (var i = 0; i < galblocknum; i++) {
      				var menuImage = document.getElementById("gal_simage" + i);
      				var menuImg = document.getElementById("gal_img" + i);
				if (i + curphotostart == curphoto) {
					gal_highlightThumb(menuImage, menuImg);
      				} else {
					gal_dehighlightThumb(menuImage, menuImg);
      				}
    			}
		}

		gal_updateNextPrevLinks();
		gal_updateAllNavBlocks();

	}

	function gal_updateNextPrevLinks() {
		var prevImageLink = document.getElementById("gal_previmage");
		
		if (curphoto == 0) {
			gal_hideBlock(prevImageLink);
		} else {
			gal_showBlock(prevImageLink);
		}

		var nextImageLink = document.getElementById("gal_nextimage");


		if (curphoto == galphotonum - 1) {
			gal_hideBlock(nextImageLink);
		} else {
			gal_showBlock(nextImageLink);
		}

	}

	function gal_loadNextImage() {  
		if (curphoto < galphotonum - 1) {
			if (curphoto + 1 >= curphotostart + galblocknum) {
      				gal_loadGalImagesFrom(curphotostart + galblocknum);
			} else {
    				gal_loadCurrentImage(curphoto + 1);
			}
  		}
	}

	function gal_loadPreviousImage() {
  		if (curphoto > 0) {
    			if (curphoto - 1 < curphotostart) {
				var image2load = curphoto - 1;
				gal_loadGalImagesFrom(curphotostart - galblocknum);
    				gal_loadCurrentImage(image2load);
			} else {
    				gal_loadCurrentImage(curphoto - 1);
			}
  		}
	}

	function gal_updateAllNavBlocks() {
		for (var i = 0; i < galnav; i++) {
			gal_updateBlockNavigation(i);
		}
	}

	function gal_updateBlockNavigation(blockid) {
		var blockstart = Math.ceil(galnavblocks / 2);
		var photostart = curphotostart - blockstart * galblocknum;
		var numstart = Math.ceil(curphotostart / galblocknum) + 2 - blockstart;

		for (var i = 0; i < galnavblocks; i++) {
			photostart = photostart + galblocknum;

			var block = document.getElementById("navblock" + (i + 1) + "_" + blockid);
			if ((photostart < 0) || (photostart >= galphotonum)) {
				gal_hideBlock(block);
			} else {
				gal_setText(block, "" + (numstart + i));
				gal_showBlock(block);
			}
		}

		var leftBlock = document.getElementById("blockleft" + blockid);
		
		if (curphotostart <= 0) {
			gal_hideBlock(leftBlock);
		} else {
			gal_showBlock(leftBlock);
		}

		var rightBlock = document.getElementById("blockright" + blockid);
		if (curphotostart + galblocknum >= galphotonum) {
			gal_hideBlock(rightBlock);
		} else {
			gal_showBlock(rightBlock);
		}
	}
