/********* CLM INIT **********/
if(typeof CLM != 'object') {
	var CLM = {NAME : "CLM"};
};
/*****************************/


/**
* ImageBrowser class
*/
CLM.ImageBrowser = SZN.ClassMaker.makeClass({
	NAME : "ImageBrowser",
	VERSION : "1.0",
	CLASS : "class"
});


/**
* Constructor
*/
CLM.ImageBrowser.prototype.$constructor = function(url, images, optionsObj)
{
	if (images.length == 0) {
		throw new Error('ImageBrowser: at least one image must be given)');
	}
	var rq = new SZN.HTTPRequest();
	rq.setMode("sync");
	var result = rq.send(url, null, null, "get", true);
	if (result && result.status == 200) {
		var body = document.getElementsByTagName('body')[0];
		this.container = SZN.cEl('div');
		body.appendChild(this.container);
		
		this.container.innerHTML = result.data;
		
		/* najit style elementy (v IE nejdou predat pres innerHTML) */
		var data = result.data;
		var styleTag = 'style';
		var s;
		var e;
		while ((s = data.indexOf('<' + styleTag + '>')) != -1) {
			e = data.indexOf('</' + styleTag + '>');
			if (e != -1) {
				var style = data.substr(s + styleTag.length + 2, e - 1 - styleTag.length - 2);
				
				var pa = document.getElementsByTagName('head')[0] ;
				var el = document.createElement('style');
				el.type = 'text/css';
				el.media = 'screen';
				if (el.styleSheet) el.styleSheet.cssText = style;
				else el.appendChild(document.createTextNode(style));
				pa.appendChild(el);
			}
			
			data = data.slice(s + 1);
		}
		
		this.options = {
			'size' : 'auto',                   /* size - auto, dynamic, fixed */
			'width' : 640,                     /* fixed width */
			'height' : 480,                    /* fixed height */
			'positionX' : 'center',            /* X position - center, number of element id */
			'positionY' : 'center',            /* Y position - center, number of element id */
			'ensureVisible' : true,            /* reposition to ensure fully visible */
			'preloadImages' : 3,               /* number of preloaded images from selected */
			'showButtonBar' : true,            /* show prev, next and close button */
			'showButtonPresentation' : true,   /* show presentation button */
			'presentationInterval' : 3000,     /* presentation change interval */
			'showButtonMove' : true,           /* show move button */
			'showThumbnails' : true,           /* show thumbnails */
			'cycleImages' : true,              /* cycle images on move next/previous */
			'clickAction' : 'zoom',            /* big image click event action - none, zoom, close */
			'onChange' : null                  /* on image change event */
		}
		if (optionsObj) {
			for (var p in optionsObj) {
				this.options[p] = optionsObj[p];
			}
		}
		
		this.ec = [];
		this.thumbnails = [];
		
		this.visible = false;
		
		this.elmIBBackground = null;
		this.elmIBBox = null;
		this.elmIBButtons = null;
		this.elmIBButtonMovePrevious = null;
		this.elmIBButtonMoveNext = null;
		this.elmIBButtonPresentation = null;
		this.elmIBButtonMove = null;
		this.elmIBButtonClose = null;
		this.elmIBBigImage = null;
		this.elmIBImageContainerSizer = null;
		this.elmIBImageContainer = null;
		this.elmIBLinkZoom = null;
		this.elmIBImage = null;
		this.elmIBThumbnails = null;
		this.elmIBThumbnailsContainer = null;
		this._findElements(this.container);
		
		this.width = null;
		this.height = null;
		if (this.options.size == 'fixed') {
			this.width = this.options.width;
			this.height = this.options.height;
			SZN.Dom.setStyle(this.elmIBImageContainerSizer, {'width' : this.width + 'px', 'height' : this.height + 'px'});
			if (this.elmIBThumbnails) SZN.Dom.setStyle(this.elmIBThumbnails, {'width' : this.width + 'px'});
		}else if (this.options.size == 'auto') {
			var maxWidth = 0;
			var maxHeight = 0;
			
			for (var i = 0; i < images.length; i++) {
				if (images[i].big.width > maxWidth) {
					maxWidth = images[i].big.width;
				}
				if (images[i].big.height > maxHeight) {
					maxHeight = images[i].big.height;
				}
			}
			this.width = maxWidth;
			this.height = maxHeight;
			SZN.Dom.setStyle(this.elmIBImageContainerSizer, {'width' : this.width + 'px', 'height' : this.height + 'px'});
			if (this.elmIBThumbnails) SZN.Dom.setStyle(this.elmIBThumbnails, {'width' : this.width + 'px'});
		}
		
		this.moving = null;
		this.x = 0;
		this.y = 0;
		this.left = null;
		this.top = null;
		
		if (this.elmIBLinkZoom) {
			if (this.options.clickAction == 'zoom') {
				this.elmIBLinkZoom.target = "_blank";
			}else if (this.options.clickAction == 'none') {
			}else if (this.options.clickAction == 'close') {
				this.ec.push(SZN.Events.addListener(this.elmIBLinkZoom, "click", this, "hide", false, true));
			}
		}
		
		if (this.elmIBButtonMovePrevious) {
			this.elmIBButtonMovePrevious.href = '#';
			this.ec.push(SZN.Events.addListener(this.elmIBButtonMovePrevious, "click", this, "movePrevious", false, true));
		}
		
		if (this.elmIBButtonMoveNext) {
			this.elmIBButtonMoveNext.href = '#';
			this.ec.push(SZN.Events.addListener(this.elmIBButtonMoveNext, "click", this, "moveNext", false, true));
		}
		
		this.presentationRunning = false;
		this.presentationMoveNext = SZN.bind(this, this.presentationMoveNext);
		this.presentationWaiting = false;
		this._fadeIn = SZN.bind(this, this._fadeIn);
		this.imageOpacity = 1;
		if (this.elmIBButtonPresentation) {
			this.elmIBButtonPresentation.style.display = this.options.showButtonPresentation ? 'block' : 'none';
			this.elmIBButtonPresentation.className = 'IBButtonPresentation-off';
			this.elmIBButtonPresentation.href = '#';
			this.ec.push(SZN.Events.addListener(this.elmIBButtonPresentation, "click", this, "startStopPresentation", false, true));
		}
		
		if (this.elmIBButtonMove) {
			this.elmIBButtonMove.style.display = this.options.showButtonMove ? 'block' : 'none';
			this.ec.push(SZN.Events.addListener(this.elmIBButtonMove, "mousedown", this, "_onMouseDown", false, true));
			this.ec.push(SZN.Events.addListener(document, "mouseup", this, "_onMouseUp", false, true));
			this.ec.push(SZN.Events.addListener(document, "mousemove", this, "_onMouseMove", false, true));
		}
		
		if (this.elmIBButtonClose) {
			this.elmIBButtonClose.href = '#';
			this.ec.push(SZN.Events.addListener(this.elmIBButtonClose, "click", this, "hide", false, true));
		}
		
		this.isLoading = false;
		this.isPreloading = false;
		this.preloadStack = new Array();
		this.preloader = null;
		
		this.selectedImage = -1;
		
		this.ec.push(SZN.Events.addListener(document, "keydown", this, "_onKeyDown", false, true));
		
		this._fill(images);
	}else {
		throw new Error('ImageBrowser: init failed (status:' + result.status + ', data:' + result.data + ')');
	}
}


/**
* Destructor
*/
CLM.ImageBrowser.prototype.$destructor = function()
{
	for (var i = 0; i < this.ec.length; i++) {
		SZN.Events.removeListener(this.ec[i]);
	}
	for (var i = 0; i < this.thumbnails.length; i++) {
		this.thumbnails[i].$destructor();
	}
	
	SZN.Dom.clear(this.container);
	var body = document.getElementsByTagName('body')[0];
	body.removeChild(this.container);
	
	for (var p in this) { this[p] = null; }
}


/**
* Finds elements
*/
CLM.ImageBrowser.prototype._findElements = function(elmContainer)
{
	var elms;
	
	elms = SZN.Dom.getElementsByClass('IBBackground', elmContainer);
	if (elms.length > 0) {
		this.elmIBBackground = elms[0];
	}

	elms = SZN.Dom.getElementsByClass('IBBox', elmContainer);
	if (elms.length > 0) {
		this.elmIBBox = elms[0];
	}else {
		throw new Error('ImageBrowser: IBBox not found');
	}
	
	elms = SZN.Dom.getElementsByClass('IBButtons', this.elmIBBox);
	if (elms.length > 0) {
		this.elmIBButtons = elms[0];
		
		elms = SZN.Dom.getElementsByClass('IBButtonMovePrevious', this.elmIBButtons);
		if (elms.length > 0) {
			this.elmIBButtonMovePrevious = elms[0];
		}
		
		elms = SZN.Dom.getElementsByClass('IBButtonMoveNext', this.elmIBButtons);
		if (elms.length > 0) {
			this.elmIBButtonMoveNext = elms[0];
		}
		
		elms = SZN.Dom.getElementsByClass('IBButtonPresentation', this.elmIBButtons);
		if (elms.length > 0) {
			this.elmIBButtonPresentation = elms[0];
		}
		
		elms = SZN.Dom.getElementsByClass('IBButtonMove', this.elmIBButtons);
		if (elms.length > 0) {
			this.elmIBButtonMove = elms[0];
		}
		
		elms = SZN.Dom.getElementsByClass('IBButtonClose', this.elmIBButtons);
		if (elms.length > 0) {
			this.elmIBButtonClose = elms[0];
		}
	}
	
	elms = SZN.Dom.getElementsByClass('IBBigImage', this.elmIBBox);
	if (elms.length > 0) {
		this.elmIBBigImage = elms[0];
		
		elms = SZN.Dom.getElementsByClass('IBImageContainerSizer', this.elmIBBigImage);
		if (elms.length > 0) {
			this.elmIBImageContainerSizer = elms[0];
		}
		
		elms = SZN.Dom.getElementsByClass('IBImageContainer', this.elmIBImageContainerSizer);
		if (elms.length > 0) {
			this.elmIBImageContainer = elms[0];
			
			elms = SZN.Dom.getElementsByClass('IBLinkZoom', this.elmIBImageContainer);
			if (elms.length > 0) {
				this.elmIBLinkZoom = elms[0];
			}
			
			elms = SZN.Dom.getElementsByClass('IBImage', this.elmIBImageContainer);
			if (elms.length > 0) {
				this.elmIBImage = elms[0];
			}else {
				throw new Error('ImageBrowser: IBImage not found');
			}
		}else {
			throw new Error('ImageBrowser: IBImageContainer not found');
		}
	}else {
		throw new Error('ImageBrowser: IBBigImage not found');
	}
	
	elms = SZN.Dom.getElementsByClass('IBThumbnails', this.elmIBBox);
	if (elms.length > 0) {
		this.elmIBThumbnails = elms[0];
		
		elms = SZN.Dom.getElementsByClass('IBThumbnailsContainer', this.elmIBThumbnails);
		if (elms.length > 0) {
			this.elmIBThumbnailsContainer = elms[0];
		}
	}
}


/**
* Loader
*/
CLM.ImageBrowser.prototype._bigImageLoaded = function()
{
	this.isLoading = false;
	SZN.Dom.removeClass(this.elmIBImageContainer, "IBLoading");
	this.elmIBImage.style.visibility = "visible";
	this.thumbnails[this.selectedImage].preloaded = true;
	if (!this.presentationWaiting) {
		this._fadeIn();
	}
	this._preloadNextImage();
}


/**
* Fills
*/
CLM.ImageBrowser.prototype._fill = function(images)
{
	this.ec.push(SZN.Events.addListener(this.elmIBImage, "load", this, "_bigImageLoaded", false, true));
	
	var elmTableBody = SZN.cEl("tbody");
	var elmTr = SZN.cEl("tr");
	if (this.elmIBThumbnailsContainer) {
		SZN.Dom.append(
			[this.elmIBThumbnailsContainer, elmTableBody],
			[elmTableBody, elmTr]
		);
	}
	for (var i = 0; i < images.length; i++) {
		this.thumbnails.push(new CLM.ImageBrowser.ThumbnailImage(this, i, this.elmIBThumbnailsContainer ? elmTr : null, images[i]));
	}
}


/**
* Hides window
*/
CLM.ImageBrowser.prototype.hide = function(e)
{
	if (this.visible) {
		this.visible = false;
		if (e) {
			SZN.Events.cancelDef(e);
		}
		if (SZN.Browser.client == 'ie' && SZN.Browser.version <= 6) {
			SZN.Dom.elementsHider(window, false, "show");
		}
		this.elmIBBox.style.display = 'none';
		
		if (this.elmIBBackground) {
			this.elmIBBackground.style.display = 'none';
		}
	}
}


/**
* Shows window
*/
CLM.ImageBrowser.prototype.show = function(selectedImage)
{
	if (!this.visible) {
		this.visible = true;
		
		/* nastaveni pozice */
		var docSize = SZN.Dom.getDocSize();
		var scrollPos = SZN.Dom.getScrollPos();
		
		if (SZN.Browser.client == 'ie' && SZN.Browser.version <= 6) {
			SZN.Dom.elementsHider(window, false, "hide");
		}
		if (this.elmIBBackground) {
			var h = document.documentElement.clientHeight;
			if (document.body.clientHeight > h) {
				h = document.body.clientHeight;
			}
			this.elmIBBackground.style.height = h + 'px';
			this.elmIBBackground.style.display = 'block';
		}
		this.elmIBBox.style.display = 'block';

		if (typeof(this.options.positionX) == typeof(0)) {
			this.elmIBBox.style.left = this.options.positionX + 'px';
		}else if (this.options.positionX == 'center') {
			this.elmIBBox.style.left = Math.round(docSize.width / 2 - this.elmIBBox.clientWidth / 2) + scrollPos.x + 'px';
		}else if (typeof(this.options.positionX) == typeof('')) { /* element id */
			var pos = SZN.Dom.getBoxPosition(SZN.gEl(this.options.positionX));
			this.elmIBBox.style.left = pos.left + 'px';
		}
		if (typeof(this.options.positionY) == typeof(0)) {
			this.elmIBBox.style.top = this.options.positionY + 'px';
		}else if (this.options.positionY == 'center') {
			this.elmIBBox.style.top = Math.round(docSize.height / 2 - this.elmIBBox.clientHeight / 2) + scrollPos.y + 'px';
		}else if (typeof(this.options.positionY) == typeof('')) { /* element id */
			var pos = SZN.Dom.getBoxPosition(SZN.gEl(this.options.positionY));
			this.elmIBBox.style.top = pos.top + 'px';
		}
		
		if (this.options.ensureVisible) {
			var w = this.elmIBBox.clientWidth;
			var h = this.elmIBBox.clientHeight;
			var pos = SZN.Dom.getBoxPosition(this.elmIBBox);
			
			var t = pos.top;
			var l = pos.left;
			if (t + h > scrollPos.y + docSize.height) {
				t = scrollPos.y + docSize.height - h;
			}
			if (l + w > scrollPos.x + docSize.width) {
				l = scrollPos.x + docSize.width - w;
			}
			if (t < scrollPos.y) {
				t = scrollPos.y;
			}
			if (l < scrollPos.x) {
				l = scrollPos.x;
			}
			this.top = t;
			this.left = l;
			this.elmIBBox.style.top = this.top + 'px';
			this.elmIBBox.style.left = this.left + 'px';
		}
		
		if (this.options.showButtonBar) {
			this.elmIBButtons.style.display = 'block';
		}else {
			this.elmIBButtons.style.display = 'none';
		}
		
		if (this.options.showThumbnails) {
			this.elmIBThumbnails.style.display = 'block';
		}else {
			this.elmIBThumbnails.style.display = 'none';
		}
	}
	
	if (selectedImage != undefined) {
		this.selectByIndex(selectedImage);
	}else {
		if (this.selectedImage == -1) {
			var sel = 0;
			for (var i = 0; i < this.thumbnails.length; i++) {
				if (this.thumbnails[i].selected) {
					sel = i;
				}
			}
			this.selectByIndex(sel);
		}
	}
}


/**
* Move to next picture
*/
CLM.ImageBrowser.prototype.moveNext = function(e)
{
	if (e) {
		SZN.Events.cancelDef(e);
	}
	this.thumbnails[this.selectedImage + 1 < this.thumbnails.length ? this.selectedImage + 1 : 0]._selectImage();
}


/**
* Move to previous picture
*/
CLM.ImageBrowser.prototype.movePrevious = function(e)
{
	if (e) {
		SZN.Events.cancelDef(e);
	}
	this.thumbnails[this.selectedImage > 0 ? this.selectedImage - 1 : this.thumbnails.length - 1]._selectImage();
}


/**
* Select by index
*/
CLM.ImageBrowser.prototype.selectByIndex = function(index)
{
	if (index >= 0 && index < this.thumbnails.length) {
		this.thumbnails[index]._selectImage();
	}else {
		throw new Error('ImageBrowser: Index ' + index + ' is out of range');
	}
}


/**
* Starts moving
*/
CLM.ImageBrowser.prototype._onMouseDown = function(e, elm) {
	this.moving = "move";
	this.x = e.clientX;
	this.y = e.clientY;
	if (this.left == null && this.top == null) {
		var pos = SZN.Dom.getBoxPosition(this.elmIBBox, this.container);
		this.left = pos.left;
		this.top = pos.top;
	}
}


/**
* Starts moving
*/
CLM.ImageBrowser.prototype._onMouseUp = function(e, elm) {
	this.moving = null;
}


/**
* Moves
*/
CLM.ImageBrowser.prototype._onMouseMove = function(e, elm) {
	if (!this.moving) { return; }

	var sel = (window.getSelection ? window.getSelection() : document.selection);
	if (!sel) { return; }
	if (sel.empty) { sel.empty(); }
	if (sel.removeAllRanges) { sel.removeAllRanges(); }
	
	var dx = e.clientX - this.x;
	var dy = e.clientY - this.y;
	switch (this.moving) {
		case "move":
			this.left = this.left + dx;
			this.top = this.top + dy;
		break;
	}
	this.x = e.clientX;
	this.y = e.clientY;
	this._restyle();
}


/**
* On key down
*/
CLM.ImageBrowser.prototype._onKeyDown = function(e, elm) {
	var k = e.keyCode ? e.keyCode : e.which;
	if (this.visible) {
		if (k == 39) { /* key right */
			SZN.Events.cancelDef(e);
			this.moveNext();
		}else if (k == 37) { /* key left */
			SZN.Events.cancelDef(e);
			this.movePrevious();
		}else if (k == 27) { /* escape */
			SZN.Events.cancelDef(e);
			if (this.presentationRunning) {
				this.startStopPresentation();
			}else {
				this.hide();
			}
		}else if (k == 116) { /* F5 */
			SZN.Events.cancelDef(e);
			this.startStopPresentation();
		}
	}
}


/**
* Restyles window
*/
CLM.ImageBrowser.prototype._restyle = function() {
	this.elmIBBox.style.left = this.left + 'px';
	this.elmIBBox.style.top = this.top + 'px';
}


/**
* Preloads images
*/
CLM.ImageBrowser.prototype._preloadNextImage = function() {
	if (!this.isLoading && !this.isPreloading) {
		if (this.preloader) {
			this.preloader.$destructor();
			this.preloader = null;
		}
		if (this.preloadStack.length > 0) {
			this.preloader = new CLM.ImageBrowser.ImagePreloader(this, this.preloadStack.pop());
		}
	}
}


/**
* Returns selected image
*/
CLM.ImageBrowser.prototype.getSelectedImage = function() {
	return this.selectedImage;
}


/**
* Returns wheater window is shown
*/
CLM.ImageBrowser.prototype.isVisible = function() {
	return this.visible;
}


/**
* Starts/stops presentation
*/
CLM.ImageBrowser.prototype.startStopPresentation = function(e)
{
	if (e) {
		SZN.Events.cancelDef(e);
	}
	this.presentationRunning = !this.presentationRunning;
	this.elmIBButtonPresentation.className = 'IBButtonPresentation-' + (this.presentationRunning ? 'on' : 'off');
	if (!this.presentationRunning) { /* stop */
	}else { /* start */
		if (!this.presentationWaiting) {
			this.presentationWaiting = true;
			setTimeout(this.presentationMoveNext, this.options.presentationInterval);
		}
	}
}


/**
* Move to next image in presentation mode
*/
CLM.ImageBrowser.prototype.presentationMoveNext = function(e)
{
	this.presentationWaiting = false;
	if (!this.presentationRunning) {
		return;
	}
	this.elmIBBigImage.style.backgroundImage = "url('" + this.thumbnails[this.selectedImage].image.big.url + "')";
	this.imageOpacity = 0.0;
	this.elmIBImage.style.opacity = this.imageOpacity;
	this.elmIBImage.style.MozOpacity = this.imageOpacity;
	this.elmIBImage.style.KhtmlOpacity = this.imageOpacity;
	this.elmIBImage.style.filter = "alpha(opacity=" + (this.imageOpacity * 100) + ")";
	this.moveNext();
}


/**
* Fades in image
*/
CLM.ImageBrowser.prototype._fadeIn = function()
{
	if (this.imageOpacity >= 1) {
		if (this.presentationRunning) {
			this.elmIBBigImage.style.backgroundImage = 'none';
			if (!this.presentationWaiting) {
				this.presentationWaiting = true;
				setTimeout(this.presentationMoveNext, this.options.presentationInterval);
			}
		}
		return;
	}
	this.imageOpacity = Math.min(1, this.imageOpacity + 0.2);
	this.elmIBImage.style.opacity = this.imageOpacity;
	this.elmIBImage.style.MozOpacity = this.imageOpacity;
	this.elmIBImage.style.KhtmlOpacity = this.imageOpacity;
	this.elmIBImage.style.filter = "alpha(opacity=" + (this.imageOpacity * 100) + ")";
	setTimeout(this._fadeIn, 50);
}


/**
* ThumbnailImage class
*/
CLM.ImageBrowser.ThumbnailImage = SZN.ClassMaker.makeClass({
	NAME : "ThumbnailImage",
	VERSION : "1.0",
	CLASS : "class"
});


/**
* Constructor
*/
CLM.ImageBrowser.ThumbnailImage.prototype.$constructor = function(imageBrowser, index, container, image)
{
	this.ec = [];
	
	this.imageBrowser = imageBrowser;
	this.index = index;
	this.image = image;
	
	this.preloaded = false;
	
	this.elmTd = null;
	this.elmIBImageContainer = null;
	this.elmIBLinkSelect = null;
	this.elmIBImage = null;
	
	if (container) {
		this.elmTd = SZN.cEl("td");
		this.elmIBImageContainer = SZN.cEl("div", false, "IBImageContainer");
		this.elmIBLinkSelect = SZN.cEl("a", false, "IBLinkSelect");
		this.elmIBLinkSelect.href = image.original.url;
		this.ec.push(SZN.Events.addListener(this.elmIBLinkSelect, "click", this, "_selectImage", false, true));
		this.elmIBImage = SZN.cEl("img", false, "IBImage", {'width' : image.thumbnail.width + 'px', 'height' : image.thumbnail.height + 'px'});
		
		SZN.Dom.append(
			[container, this.elmTd],
			[this.elmTd, this.elmIBImageContainer],
			[this.elmIBImageContainer, this.elmIBLinkSelect],
			[this.elmIBLinkSelect, this.elmIBImage]
		);
		SZN.Dom.addClass(this.elmIBImageContainer, "IBLoading");
		this.ec.push(SZN.Events.addListener(this.elmIBImage, "load", this, "_thumbnailImageLoaded", false, true));
		this.elmIBImage.src = image.thumbnail.url;
	}
}


/**
* Destructor
*/
CLM.ImageBrowser.ThumbnailImage.prototype.$destructor = function()
{
	for (var i = 0; i < this.ec.length; i++) {
		SZN.Events.removeListener(this.ec[i]);
	}
	for (var p in this) { this[p] = null; }
}


/**
* Loader
*/
CLM.ImageBrowser.ThumbnailImage.prototype._thumbnailImageLoaded = function()
{
	SZN.Dom.removeClass(this.elmIBImageContainer, "IBLoading");
}


/**
* Select image
*/
CLM.ImageBrowser.ThumbnailImage.prototype._selectImage = function(e, elm)
{
	if (e) {
		SZN.Events.cancelDef(e);
	}
	if (this.imageBrowser.selectedImage != this.index) {
		if (this.imageBrowser.selectedImage != -1) {
			if (this.imageBrowser.thumbnails[this.imageBrowser.selectedImage].elmTd) {
				SZN.Dom.removeClass(this.imageBrowser.thumbnails[this.imageBrowser.selectedImage].elmTd, "IBActive");
			}
		}
		this.imageBrowser.selectedImage = this.index;
		if (this.elmTd) SZN.Dom.addClass(this.elmTd, "IBActive");
		
		SZN.Dom.addClass(this.imageBrowser.elmIBImageContainer, "IBLoading");
		this.imageBrowser.elmIBImage.style.visibility = "hidden";
		this.imageBrowser.isLoading = true;
		this.imageBrowser.elmIBImage.src = this.image.big.url;
		
		if (this.imageBrowser.elmIBLinkZoom && this.imageBrowser.options.clickAction != 'none') {
			this.imageBrowser.elmIBLinkZoom.href = this.image.original.url;
		}
		
		if (this.imageBrowser.options.size == 'dynamic') {
			this.imageBrowser.width = this.image.big.width;
			this.imageBrowser.height = this.image.big.height;
			SZN.Dom.setStyle(this.imageBrowser.elmIBImageContainerSizer, {'width' : this.imageBrowser.width + 'px', 'height' : this.imageBrowser.height + 'px'});
			if (this.imageBrowser.elmIBThumbnails) SZN.Dom.setStyle(this.imageBrowser.elmIBThumbnails, {'width' : this.imageBrowser.width + 'px'});
		}
		SZN.Dom.setStyle(this.imageBrowser.elmIBImage, {'width' : this.image.big.width + 'px', 'height' : this.image.big.height + 'px'});
		
		/* scroll thumbs */
		if (this.elmIBImageContainer) {
			var imgPos = SZN.Dom.getBoxPosition(this.elmIBImageContainer, this.imageBrowser.elmIBThumbnails);
			var sl = Math.round(imgPos.left - (this.imageBrowser.width / 2 - this.image.thumbnail.width / 2));
			this.imageBrowser.elmIBThumbnails.scrollLeft = sl;
		}
		
		if (this.imageBrowser.options.preloadImages > 0) {
			if (this.imageBrowser.selectedImage > 0) {
				this.imageBrowser.preloadStack.push(this.imageBrowser.selectedImage - 1);
			}
			for (var i = this.imageBrowser.selectedImage + this.imageBrowser.options.preloadImages; i >= this.imageBrowser.selectedImage + 1; i--) {
				if (i < this.imageBrowser.thumbnails.length) {
					if (!this.imageBrowser.thumbnails[i].preloaded) {
						this.imageBrowser.preloadStack.push(i);
					}
				}
			}
			/* seriznout stack aby nebyl moc veliky */
			this.imageBrowser.preloadStack.splice(this.imageBrowser.options.preloadImages + 1);
		}
		
		if (!this.imageBrowser.options.cycleImages) {
			if (this.imageBrowser.elmIBButtonMoveNext) {
				if (this.imageBrowser.selectedImage + 1 < this.imageBrowser.thumbnails.length) {
					this.imageBrowser.elmIBButtonMoveNext.style.display = 'block';
				}else {
					this.imageBrowser.elmIBButtonMoveNext.style.display = 'none';
				}
			}
			if (this.imageBrowser.elmIBButtonMovePrevious) {
				if (this.imageBrowser.selectedImage > 0) {
					this.imageBrowser.elmIBButtonMovePrevious.style.display = 'block';
				}else {
					this.imageBrowser.elmIBButtonMovePrevious.style.display = 'none';
				}
			}
		}
		
		if (this.imageBrowser.options.onChange) {
			this.imageBrowser.options.onChange(this.index);
		}
	}
}


/**
* ImagePreloader class
*/
CLM.ImageBrowser.ImagePreloader = SZN.ClassMaker.makeClass({
	NAME : "ImagePreloader",
	VERSION : "1.0",
	CLASS : "class"
});


/**
* Constructor
*/
CLM.ImageBrowser.ImagePreloader.prototype.$constructor = function(imageBrowser, index)
{
	this.ec = [];
	
	this.imageBrowser = imageBrowser;
	this.index = index;
	this.imageBrowser.isPreloading = true;
	this.elmIBImage = SZN.cEl("img");
	this.ec.push(SZN.Events.addListener(this.elmIBImage, "load", this, "_imageLoaded", false, true));
	this.elmIBImage.src = this.imageBrowser.thumbnails[this.index].image.big.url;
}


/**
* Loader
*/
CLM.ImageBrowser.ImagePreloader.prototype._imageLoaded = function()
{
	this.imageBrowser.isPreloading = false;
	this.imageBrowser.thumbnails[this.index].preloaded = true;
	this.imageBrowser._preloadNextImage();
}


/**
* Destructor
*/
CLM.ImageBrowser.ImagePreloader.prototype.$destructor = function()
{
	for (var p in this) { this[p] = null; }
}

