var plg_slideshow_photos_ofTheWeek;

var __bind = function (fn, me) {
	return function () {
		return fn.apply(me, arguments);
	};
};

plg_slideshow_photos_ofTheWeek = (function () {
	var Browser = (function () {
		function Browser(opts) {
			var defaults;
			defaults = {
				prev:'.jq_prev',
				next:'.jq_next',
				holder:'.gal_content',
				item:'.jq_item'
			};
			this.options = $.extend({}, defaults, opts);
			this.element = $(this.options.element);
			this.holder = $(this.options.holder, this.element);
			this.btn_prev = $(this.options.prev, this.element);
			this.btn_next = $(this.options.next, this.element);
			this.$me = $(this);
			this.btn_prev.click(__bind(function () {
				this._prev();
				return false;
			}, this));
			this.btn_next.click(__bind(function () {
				this._next();
				return false;
			}, this));
			if ('ajahkey' in this.options && (this.options.ajahkey != null)) {
				this.fetchData();
			}
			this.holder.width(this.holder.width());
			this.holder.height(this.holder.height());
		}

		Browser.prototype._prev = function () {
			var current, next;
			current = $("" + this.options.item + ":visible", this.holder);
			next = current.prev(this.options.item).length ? current.prev(this.options.item) : this.holder.find("" + this.options.item + ":last");
			current.css({
				display:'none'
			});
			this.$me.trigger('prev');
			next.fadeIn('slow');
			next.imgLazyLoad('ill', function (e) {
				if (e.type == 'error') {
					$(this).attr('src', $(this).attr('src') + '?' + new Date().getTime());
				}
			});
		};
		Browser.prototype._next = function () {
			var current, next;
			current = $("" + this.options.item + ":visible", this.holder);
			next = current.next(this.options.item).length ? current.next(this.options.item) : this.holder.find("" + this.options.item + ":first");
			current.css({
				display:'none'
			});
			this.$me.trigger('next');
			next.fadeIn('slow');
			next.imgLazyLoad('ill', function (e) {
				if (e.type == 'error') {
					$(this).attr('src', $(this).attr('src') + '?' + new Date().getTime());
				}
			});
		};
		Browser.prototype.fillHtml = function (elements) {
			return $.each(elements, __bind(function (k, v) {
				var item;
				item = $(v);
				item.css({
					display:'none'
				});
				//item.prepareImgLazyLoad();
				return this.holder.append(item);
			}, this));
		};
		Browser.prototype.fetchData = function () {
			return majax.call_callback('User_Data_Store::ajaxGetData', __bind(function (r) {
				return this.fillHtml(r);
			}, this), [this.options.ajahkey]);
		};
		return Browser;
	})();
	return {
		Browser:Browser
	};
})();
