(function($) {
	function Banners(obj, opts, banners) {
		var defaults = {
			classes: {
				banner: "banner",
				roller: "roller",
				carousel_active: "carousel-active-item"
			},
			sels: {
				roller: ".roller",
				banner: {
					base:   ".banner",
					with_background: ".with_background",
					header: ".header",
					text:   ".text",
					more:   ".more"
				},
				carousel: ".carousel",
				carousel_button: ".carousel input",
				carousel_active: ".carousel-active-item"
			},
			move: "left",
			animate: {
				duration: 800
			}
		};
		this.obj = obj;
		this.banners = banners;
		this.opts = $.extend(defaults, opts);
		this.init();
	}

	Banners.prototype.extend = function(opts) {
		this.opts = $.extend(this.opts, opts);
	}

	Banners.prototype.init = function() {
		this.banner_template = $(this.obj).find(this.opts.sels.banner.base).clone(false);
		$(this.obj).find(this.opts.sels.banner.base).remove();

		$(this.obj).find(this.opts.sels.roller)
		.css({
			marginLeft: (- $(this.obj).width()) + "px",
			width:      $(this.obj).width() * 3 + "px",
			height:     $(this.obj).height() + "px",
			"overflow": "hidden"
		});

		for(var i = 0; i < 3; i++) {
			this.banner_template.clone(false)
			.show()
			.css({
				"float": "left",
				"visibility": "hidden"
			})
			.appendTo($(this.obj).find(this.opts.sels.roller));
		}

		this.carousel_button = $(this.obj).find(this.opts.sels.carousel_button).clone(false);
		$(this.obj).find(this.opts.sels.carousel_button).remove();
		for(var j = 0; j < this.banners.length; j++) {
			this.carousel_button.clone(false)
			.appendTo($(this.obj).find(this.opts.sels.carousel));
		}
		$(this.obj).find(this.opts.sels.carousel).show();
	}

	Banners.prototype.set_current_banner = function(index) {
		if(index < 0) {
			index = this.banners.length - (-index % this.banners.length);
		} else if(index > (this.banners.length - 1)) {
			index = index % this.banners.length;
		}

		this.current_banner_index = index;
		$(this.obj).find(this.opts.sels.carousel_active).removeClass(this.opts.classes.carousel_active);
		$(this.obj).find(this.opts.sels.carousel_button).eq(index).addClass(this.opts.classes.carousel_active);
		this.set_banner(1, this.banners[index]);
	}

	Banners.prototype.set_next_banner = function(index) {
		this.next_banner_index = index;
		this.set_banner(2, this.banners[index]);
	}

	Banners.prototype.set_banner = function(index, banner) {
		var banner_place = $(this.obj).find(this.opts.sels.roller).find(this.opts.sels.banner.base).eq(index);
		if(banner_place.css("visibility") != "visible") {
			banner_place.find(this.opts.sels.banner.header).html(banner.header);
			banner_place.find(this.opts.sels.banner.text).html(banner.text);
			banner_place.find(this.opts.sels.banner.more).html(banner.more.text).attr("href", banner.more.href);
			banner_place.find(this.opts.sels.banner.with_background).css({
				"backgroundImage": "url(" + banner["background-image"] +")"
			});
			banner_place.css({
				"visibility": "visible"
			});
			banner_place.attr("class", this.opts.classes.banner);
			banner_place.addClass(banner.addClass);
		}
	}

	Banners.prototype.animate = function(callback) {
		var instance = this;

		if(this.opts.move == "left") {
			$(this.obj).find(this.opts.sels.roller).find(this.opts.sels.banner.base).css("float", "left");
		} else {
			$(this.obj).find(this.opts.sels.roller).find(this.opts.sels.banner.base).css("float", "right");
		}
		$(this.obj).find(this.opts.sels.roller).find(this.opts.sels.banner.base).eq(0)
		.appendTo($(instance.obj).find(instance.opts.sels.roller))
		.css("width", "");

		$(instance.obj).find(instance.opts.sels.roller).find(instance.opts.sels.banner.base).eq(0)
		.css("visibility", "hidden")
		.removeClass(instance.banners[instance.current_banner_index].addClass);

		instance.set_current_banner(instance.next_banner_index);

		if($.isFunction(callback)) {
			callback.call(instance.obj);
		}

	/*
		//moving type
		if(this.opts.move == "left") {
			$(this.obj).find(this.opts.sels.roller).find(this.opts.sels.banner.base).css("float", "left");
		} else {
			$(this.obj).find(this.opts.sels.roller).find(this.opts.sels.banner.base).css("float", "right");
		}
		$(this.obj).find(this.opts.sels.roller).find(this.opts.sels.banner.base).eq(0)
		.animate({
			width: 0
		}, {
			duration: instance.opts.animate.duration,
			complete: function() {
				$(this).appendTo($(instance.obj).find(instance.opts.sels.roller));
				$(this).css("width", "");
				$(instance.obj).find(instance.opts.sels.roller).find(instance.opts.sels.banner.base).eq(0)
				.css("visibility", "hidden")
				.removeClass(instance.banners[instance.current_banner_index].addClass);

				instance.set_current_banner(instance.next_banner_index);

				if($.isFunction(callback)) {
					callback.call(instance.obj);
				}
			}
		});
		*/
	}

	$.fn.extend({
		Banners: function(opts, banners) {
			return this.each(
				function() {
					this.bs = new Banners(this, opts, banners);
				});
		},
		BannersSetCurrentBanner: function(index) {
			return this.each(
				function() {
					if(!this.bs) {
						return;
					}
					this.bs.set_current_banner(index);
				});
		},
		BannersSetNextBanner: function(index) {
			return this.each(
				function() {
					if(!this.bs) {
						return;
					}
					this.bs.set_next_banner(index);
				});
		},
		BannersAnimate: function(callback) {
			return this.each(
				function() {
					if(!this.bs) {
						return;
					}
					this.bs.animate(callback);
				});
		},
		BannersExtend: function(opts) {
			return this.each(
				function() {
					if(!this.bs) {
						return;
					}
					this.bs.extend(opts);
				});
		},
		BannersGetCurrentIndex: function() {
			return this.get(0).bs.current_banner_index;
		}
	});
})(jQuery);
