function GalleryPopupClass (selector) { this.init(selector); }
GalleryPopupClass.prototype = new PopupWindow();
GalleryPopupClass.prototype.constructor = GalleryPopupClass;

GalleryPopupClass.prototype.url = '';
GalleryPopupClass.prototype.photo = null;
GalleryPopupClass.prototype.photo_cont = null;
GalleryPopupClass.prototype.ajax_indicator = null;
GalleryPopupClass.prototype.link_prev = null;
GalleryPopupClass.prototype.link_next = null;
GalleryPopupClass.prototype.current_photo = null;
GalleryPopupClass.prototype.next_photo = null;
GalleryPopupClass.prototype.prev_photo = null;
GalleryPopupClass.prototype.IdCycle = null;
GalleryPopupClass.prototype.SrcCycle = null;
GalleryPopupClass.prototype.lock = false;
GalleryPopupClass.prototype.description_cont = null;
GalleryPopupClass.prototype.description = null;
GalleryPopupClass.prototype.show_description = true;
GalleryPopupClass.prototype.error_timer = null;
GalleryPopupClass.prototype.auto_width = false;
GalleryPopupClass.prototype.init_width = null;
GalleryPopupClass.prototype.min_width = null;

GalleryPopupClass.prototype.init = function(selector, url) {
	var self = this;
	this.window = $(selector);
	this._init();
	this.link_prev = this.find('a.navprev');
	this.link_next = this.find('a.navnext');
	this.photo_cont = this.find('div.thephoto');
	this.photo = new Image();// this.photo_cont.find('img.photo');
	this.photo = $(this.photo);
	this.photo.load(function(event) {
		self.photo_onload(event);
	});
	this.photo_cont.append(this.photo);
	this.ajax_indicator = this.photo_cont.find('img.ajax_indicator');
	this.IdCycle = new CycleClass();
	this.SrcCycle = new CycleClass();
	this.link_next.click(this.load_next.bind(this));
	this.link_prev.click(this.load_prev.bind(this));
	this.description_cont = this.find('div.things');
};

GalleryPopupClass.prototype.centrize_ajax_ind = function() {
	var h = this.photo_cont.height(), w = this.photo_cont.width(),
	    h2 = this.ajax_indicator.height(), w2= this.ajax_indicator.width();
	var _left = parseInt((w-w2)/2), _top = parseInt((h-h2)/4);
	this.ajax_indicator.css({left: _left+'px', top: _top+'px', position: 'absolute'});
};


GalleryPopupClass.prototype.load_prev = function(event) {
	event.preventDefault();
	if (this.prev_photo) {
		clearTimeout(this.error_timer);
		this.load_photo(this.prev_photo.id);
	}
};

GalleryPopupClass.prototype.load_next = function(event) {
	event.preventDefault();
	if (this.next_photo) {
		clearTimeout(this.error_timer);
		this.load_photo(this.next_photo.id);
	}
};

GalleryPopupClass.prototype.refresh_description = function() {
	if (!this.show_description)
		return;
	var data = this.description;
	if (data.description_count)
	{
		var i=0;
		var html = '';
		descrs = parseInt(data.description_count);
		for (i in data.description)
		{
			var item = data.description[i];
			html += '<div class="thing">';
			html += item.title + (item.brand ? (' «'+item.brand+'»') : '') + '<br><em>'+item.cost+' руб.</em>';
			html += '</div>';
			if (descrs-1 > i) {
				/*html += '<div class="border_things"><!-- --></div>';*/
			}
		}
		this.description_cont.find('.items').html(html);
		this.description_cont.show();
	}
},

GalleryPopupClass.prototype.photo_onload = function(event) {
	this.ajax_indicator.hide();
	this.photo.show();
	this.refresh_description();
	this.lock = false;
	if (this.auto_width) {
		this.photo_cont.width('auto');
	}
	this.centrize(null, true);
};

GalleryPopupClass.prototype.load_photo = function(id) {
	if (this.lock)
		return false;
	this.lock = true;
	if (this.auto_width) {
		this.init_width = this.photo_cont.width();
		if (this.visible)
			this.photo_cont.width(this.init_width);
		else
			this.photo_cont.width(this.min_width);
	}
	this.photo.hide();
	this.description_cont.hide();
	this.ajax_indicator.show();
	var index = this.IdCycle.get_index(id);
	this.IdCycle.set_iterator(index);
	this.SrcCycle.set_iterator(index);
	var photo = {id: id, src: this.SrcCycle.get_current()};
	this.prev_photo = {id: this.IdCycle.get_prev(), src: this.SrcCycle.get_prev()};
	this.next_photo = {id: this.IdCycle.get_next(), src: this.SrcCycle.get_next()};
	this.current_photo = photo;
	// ------
	this.link_prev.find('img.navprev').attr('src', this.prev_photo.src);
	this.link_next.find('img.navnext').attr('src', this.next_photo.src);
	if (!this.visible) {
		this.show(null);
	}
	this.centrize_ajax_ind();
	$.ajax({
		url : this.url + '/' + id,
		context : this,
		dataType : 'json',
		success : function(json, textStatus) {
			if ('success' == textStatus && json && json.status=='ok')
			{
				this.description = json.photo;
				this.photo.attr('src', json.src);
			}
			else
				this.onerror(id);
		},
		error : function() {
			this.onerror(id);
		}
	});
};

GalleryPopupClass.prototype.onerror = function(id) {
	this.lock = false;
	var self = this;
	this.error_timer = setTimeout(function() {
		self.load_photo(id);
	}, 1000);
};
