function Gallery (egallery) {
	this.PREVIEW_WIDTH = 94;
	
	this.photos   = [];
	this.selected = 0;
	this.left     = 0;
	this.count    = 0;
	this.egallery = egallery | null;
	this.twidth   = this.PREVIEW_WIDTH;
	
	this.disableAnimation = false;
}

Gallery.prototype.init = function (selected) {
	this.events();
	this.draw();
	
	this.select(selected | 0);
}

Gallery.prototype.events = function () {
	var ctrl = this;
	
	$(window).resize(function () {
		ctrl.disableAnimation = true;
		ctrl.redraw();
		ctrl.disableAnimation = false;
	});
	
	$(".ra").click(function() { ctrl.select(ctrl.selected + 1) } );
	$(".la").click(function() { ctrl.select(ctrl.selected - 1) } );
}

Gallery.prototype.draw = function () {
	var ctrl   = this;
	var parent = $(".preview .c", this.egallery);
	parent.empty();
	
	for (var i = 0; i < this.photos.length; i++) {
		var html =
			'<div class="thumb thumb-' + i + '">' +
				'<img src="" alt="" />' +
				'<div class="bg"><!-- --></div>' +
			'</div>';
		
		parent.append(html);
		
		var preview = $(".thumb-" + i, parent);
		$("img", preview).attr("src", this.photos[i].small);
		
		var t = function (index) {
			$(".bg", preview).click(
				function () { ctrl.onThumbClick(index) }
			);
		}; t(i);
	}
	
	this.redraw();
}

Gallery.prototype.redraw = function () {
	var width = $(".preview .scroller", this.egallery).outerWidth();
	
	/*@cc_on
		@if (@_jscript_version == 5.6)
			width =  $(".big", this.egallery).width() - 10;
			$(".preview .scroller").css("width", width + 'px');
		@end
	@*/
	
	var count = this.count = parseInt(width / this.PREVIEW_WIDTH);
	
	if (this.photos.length > 1) {
		var totalm  = width % this.PREVIEW_WIDTH;
		var margin  = parseInt(totalm / (count - 1));
		this.twidth = this.PREVIEW_WIDTH + margin;
		
		$(".preview .scroller .c", this.egallery).css("width", (this.twidth * this.photos.length) + 'px');
		$(".preview .thumb").css("margin-right", margin + 'px');
		
		this.correctMargin(totalm - margin, count);
		this.drawArrows();
		this.scroll();
		
		$(".preview", this.egallery).show();
	} else {
 		$(".preview", this.egallery).hide();
	}
}

Gallery.prototype.correctMargin = function (margin, count) {
	for (var i = this.left; i < count; i++) {
		// tbd
	}
}

Gallery.prototype.drawArrows = function () {
	if (this.left > 0) {
		$(".la", this.egallery).show();
	} else {
 		$(".la", this.egallery).hide();
	}
	
	if (this.photos.length - this.left > this.count) {
		$(".ra", this.egallery).show();
	} else {
 		$(".ra", this.egallery).hide();
	}
}

Gallery.prototype.scroll = function () {
	var position = this.left * this.twidth;
	
	$(".scroller", this.egallery).stop();
	
	if (this.disableAnimation) {
		$(".scroller", this.egallery).scrollLeft(position);
	} else {
		$(".scroller", this.egallery).animate({ scrollLeft : position }, 500);
	}
}

Gallery.prototype.select = function (index) {
	if (index >= this.photos.length) {
		this.selected = this.photos.length - 1;
	} else if (index < 0) {
		this.selected = 0;
	} else {
		this.selected = index;
	}
	
	this.left = this.selected - parseInt(this.count / 2);
	
	if (this.left < 0) { this.left = 0; }
	if (this.left + this.count > this.photos.length - 1) { this.left = this.photos.length - this.count }
	
	$(".thumb", this.egallery).removeClass("thumb-selected");
	$(".thumb-" + this.selected, this.egallery).addClass("thumb-selected");
	
	var src = this.photos[this.selected].full;
	
	this.drawArrows();
	this.scroll();
	
	var overlay = $(".overlay", this.egallery);
	var photo   = $(".photo", this.egallery);
	
	overlay.stop();
	photo.stop();
	
	$(".big", this.egallery).click(function () { GalleryShow(src) });
	
	overlay.hide();
	photo.show();
	photo.css("opacity", 1);
	
	/*@cc_on
		@if (@_jscript_version == 5.7)
			photo.attr("src", this.photos[this.selected].big);
			return;
		@end
	@*/
	
	overlay.ready(function () {
		var swap = function () {
			photo.attr("src", overlay.attr("src"));
			photo.stop();
			
			overlay.hide();
			photo.show();
		}
		
		photo.fadeOut(500);
		overlay.fadeIn(500, swap);
	});
	
	overlay.attr("src", this.photos[this.selected].big);
}

Gallery.prototype.onThumbClick = function (index) {
	if (index == this.selected) { return; }
	
	this.select(index);
}
