function delegate(object, method){
	return function(){
		return method.apply(object, arguments);
	};
}

function Galery(elBig, elSlide, thumbsPerSlide){
	//alert('call construct' + "\nelBig = " + elBig + "\nelSlide = " + elSlide + "\ntpP = " + thumbsPerSlide);
	this.elSlide        = $("#"+elSlide);
	this.elBig          = $("#"+elBig);
	this.elDisable      = $("#disable"); 
	this.thumbsPerSlide = thumbsPerSlide;
	this.offset         = 0;
	this.groupID        = 0;
	this.thumbsMax      = 20;
	this.thumbs         = null;
	this.isSanatorium   = 0;
	$("#prev, #next").bind("click", delegate(this, this.onSlide));
}

Galery.prototype = {
	onSlide: function(e){
		var oldOffset = this.offset;
		var direct = e.target.id;
		if(direct == "prev") this.offset -= this.thumbsPerSlide;
		if(direct == "next") this.offset += this.thumbsPerSlide;
		if(this.offset < 0) this.offset = 0;
		if(this.offset > this.thumbsMax - this.thumbsPerSlide) 
			this.offset = this.thumbsMax - this.thumbsPerSlide;
		if(oldOffset != this.offset) this.LoadThumbs();
	},
	onClickThumb: function(e){
		var thumb = $(e.target).parents(".thumbs");
		thumb = thumb.length ? thumb : $(e.target);
		var imageUrl = this.path + this.groupID + "/" + this.thumbs[thumb.attr('id')]['NameFile'];
		if (this.photoSection == 1) imageUrl = this.path  + "pictures/" + this.thumbs[thumb.attr('id')]['NameFile'];
		if (this.isSanatorium == 1)
		{
			imageUrl = this.path + this.thumbs[thumb.attr('id')]['sId'] + "/" + this.thumbs[thumb.attr('id')]['NameFile']
		}
		this.elBig.attr('title', this.thumbs[thumb.attr('id')]['Name']);
		var img = this.LoadImage(this.elBig, imageUrl);
		var self = this;
		self.EnableElement();
		this.elBig.attr('src', imageUrl);
		$("#photo-name").text(this.thumbs[thumb.attr('id')]['Name']);
	},
	RenderSlideThumb: function()
	{
		var imgPath = this.path + this.groupID + "/thumb/";
		if (this.photoSection == 1) imgPath = this.path + "thumb/thumb_";
		var htmlSlide = "<table align='center'><tr>";
		for(var key in this.thumbs)
		{
			if (this.isSanatorium == 1) imgPath = this.path + this.thumbs[key]['sId'] + "/thumb/";
			htmlSlide += "<td class='thumbs' id='" + key + "'><img width='130' src='" + imgPath + this.thumbs[key]['NameFile'] + "' title='" + this.thumbs[key]['Name'] + "'></td>";
		}
		htmlSlide += "</tr></table>";
		this.elSlide.html(htmlSlide);
		this.SetHandler();
		this.EnableElement();
	},
	SetHandler: function(){
		$(".thumbs").bind("click", delegate(this, this.onClickThumb));
	},
	LoadThumbs: function()
	{
		//alert('call LoadThumbs\ngroupID = ' + this.groupID);
		this.DisableElement(this.elSlide);
		var self = this;
		$.ajax({
			type   : "POST",
			url    : "/ajax/get-photos.php?s_id=" + this.groupID,
			data   : "offset=" + this.offset + "&limit=" + this.thumbsPerSlide,
			success: function(data){
				//alert(data);
				self.thumbs = eval("(" + data + ")");
				self.RenderSlideThumb();
				self.ShowBigImg();
			}
		});
	},
	LoadImage: function(elImg, src){
		this.DisableElement(elImg);
		var img = new Image();
		img.src = src;
		return img;
	},
	LoadComplete: function(elImg){
		var self = this;
		elImg.effect("transfer", {to: "#bigPhoto"}, 300);
		elImg.queue(function(){
			self.elBig.fadeTo("slow", 1, function(){
				self.EnableElement();
			});
			$(this).dequeue();
		});
	},
	DisableElement: function(el){
		position = el.offset();
		this.elDisable.width (el.width());
		this.elDisable.height(el.height());
		this.elDisable.css({
			top : $.browser.msie ? position.top  - 2 : position.top,
			left: $.browser.msie ? position.left - 2 : position.left
		});
	},
	EnableElement: function(){
		this.elDisable.css({
			top : -10000,
			left: -10000
		});
	},

	ShowBigImg: function()
	{
		var imgPath = this.path + this.groupID + "/" + this.thumbs[this.offset].NameFile;
		if (this.photoSection == 1)
		{
		 imgPath = this.path + "pictures/" +  this.thumbs[this.offset].NameFile;
		}
		if (this.isSanatorium == 1)
		{
			imgPath = this.path + this.thumbs[this.offset].sId + "/" + this.thumbs[this.offset].NameFile;
		}
		this.elBig.attr('src', imgPath);
		$("#photo-name").text(this.thumbs[this.offset].Name);
	}
}
