var jgallery = new function () {
	this.eImage;
	this.eImages;	
	this.eUp;
	this.eDown;
	this.eLoader;
	this.id;
	
	var images = new Array();
	var current = -1;
	var offset = 0;
	
	var MAX_WIDTH = 555;
	var MAX_HEIGHT = 415;
	
	this.init = function () {
		var controller = this;
		
		this.eUp.onclick = function () { controller.onUp(); }
		this.eDown.onclick = function () { controller.onDown(); }
		
		this.eUp.style.visibility = 'hidden';
		this.eDown.style.visibility = 'hidden';
		
		this.initScroller();
	}
	
	this.add = function (full, thumb, description, id, width, height) {
		var controller = this;
		
		var eimg = document.createElement('img');
		 eimg.src = thumb;
		 eimg.style.display = 'none';
		 eimg.onclick = function() { controller.onThumb(this) }
		
		var info = {
		 'full' : full,
		 'thumb' : thumb,
		 'description' : description,
		 'eimg' : eimg,
		 'id' : id,
		 'width' : width,
		 'height' : height
		}
		
		images.push(info);
		
		return eimg;
	}
	
	this.initScroller = function () {
		for (var num in images) {
			var info = images[num];
			this.eImages.appendChild(info.eimg);
		}
		
		this.recountScroller();
	}
	
	this.recountScroller = function () {
		var count = images.length;
		
		if (offset > 0) {
			this.eUp.style.visibility = 'visible';
		} else {
			this.eUp.style.visibility = 'hidden';
		}
		
		if (count - offset > 3) {
			this.eDown.style.visibility = 'visible';
		} else {
			this.eDown.style.visibility = 'hidden';
		}
		
		for (var num in images) {
			var info = images[num];
			
			if (num < offset) {
				info.eimg.style.display = 'none';
			} else if (num >= offset + 3) {
				info.eimg.style.display = 'none';
			} else {
				info.eimg.style.display = 'inline';
			}
		}
	}
	
	this.onUp = function () {
		var count = images.length;
		
		if (offset != 0) {
			offset--;
		}
		
		this.recountScroller();
	}
	
	this.onDown = function () {
		var count = images.length;
		
		if (offset + 3 != count) {
			offset++;
		}
		
		this.recountScroller();
	}
	
	this.onThumb = function (ethumb) {
		for (var num in images) {
			var info = images[num];
			
			if (info.eimg == ethumb) {
				if (num - offset <= 0) {
 					this.onUp();
 				} else if (num - offset >= 2) {
 					this.onDown();
 				}
				
				this.show(num);
				break;
			}
		}
	}
	
	this.hide = function (num) {
		var info = images[num];
		
		info.eimg.className = null;
		this.eImage.src = null;
	}
	
	this.show = function (num) {
		if (current != -1) {
			this.hide(current);
		}
		
		var controller = this;
		var info = images[num];
		
		info.eimg.className = 'selected';
		
		if ((info.width / MAX_WIDTH) > (info.height / MAX_HEIGHT)) {
			this.eImage.style.width = MAX_WIDTH + 'px';
			this.eImage.style.height = parseInt((info.height * MAX_WIDTH) / info.width) + 'px';
		} else {
			this.eImage.style.width = parseInt((info.width * MAX_HEIGHT) / info.height) + 'px';
			this.eImage.style.height = MAX_HEIGHT + 'px';
		}
		
		var eImage = this.eImage;
		
		this.id = info.id;
		
		this.eLoader.style.display = 'block';
		this.eImage.onload = function () {
// 			eImage.style.marginLeft = 0;
// 			eImage.style.marginTop = '32px';
// 			
// 			if (eImage.offsetWidth < MAX_WIDTH) {
// 				eImage.style.marginLeft = ((MAX_WIDTH - eImage.offsetWidth) / 2) + 'px';
// 			}
// 			
// 			if (eImage.offsetHeight < MAX_HEIGHT) {
// 				eImage.style.marginTop = (32 + (MAX_HEIGHT - eImage.offsetHeight) / 2) + 'px';
// 			}
			
			controller.eLoader.style.display = 'none'
		}
		
		this.eImage.src = info.full;
		current = num;
	}
}