(function($) {
	//BannerHandler class:
	function BannerHandler(opts) {
		this.defaults = {
			interval : 7000,
			container : "#banners_wrapper",
			transitionDuration : 1200,
			nextBtn : ".btn-next",
			prevBtn : ".btn-prev",
			bannerButtonClass : "btn",
			autoPlay : true,
			stopOnHover : true,
			activeBannerClass : "active"
		};

		this._id_timeout = null;
		this._opts = $.extend({}, this.defaults, opts);
		this._container = $(this._opts.container);
		this._banners = $("li", this._container).get();
		banners_count = 0;
		while(banners_count < this._banners.length) {
			var banner = $(this._banners[banners_count]);
			if(banner.find('img').attr('src').replace(/^\s+|\s+/g, '') == '') {
				banner.remove();
				this._banners.splice(banners_count, 1);
			} else {
				this._banners[banners_count] = banner;
				banners_count++;
			}
		}

		if(banners_count) {
			var buttons = $('<ul class="buttons_list">');
			var banner = this;
			this._buttons = new Array();
			for(var i = 0; i < banners_count; i++) {
				var new_button = $('<li>' + (i + 1) + '</li>').attr('banner', i).click(function() {
					banner.changeBanner(parseInt($(this).attr('banner')));
				});
				buttons.append(new_button);
				this._buttons.push(new_button);
			}
			this._container.append(buttons);
			this._buttons[0].addClass(this._opts.activeBannerClass);
			this._banners[0].addClass(this._opts.activeBannerClass);
		}

		this._nextBtn = $(this._opts.nextBtn, this._container);
		this._prevBtn = $(this._opts.prevBtn, this._container);
		this._currentBanner = 0;
		this._changing = false;
		var that = this;

		if(this._nextBtn.length) {
			this._nextBtn.click(function(e) {
				e.preventDefault();
				that.next()
			}).addClass(this._opts.bannerButtonClass)
		}

		if(this._prevBtn.length) {
			this._prevBtn.click(function(e) {
				e.preventDefault();
				that.prev()
			}).addClass(this._opts.bannerButtonClass)
		}
		
		

		if(this._opts.autoPlay) {
			this.play()
		}
	}

	/*
	 * Changes the displayed banner to the one specified in intBanner.
	 */
	BannerHandler.prototype.changeBanner = function(intBanner) {
		if(!this._changing && intBanner != this._currentBanner) {
			this._changing = true;
			var wasRunning = this._id_timeout !== 0 && this._id_timeout !== null;
			this.stop();
			intBanner = ((intBanner < 0) ? -intBanner : intBanner) % this._banners.length;
			this._banners[this._currentBanner].css({
				display : "block",
				zIndex : 3
			});
			var that = this;
			this._banners[intBanner].css("zIndex", 4).fadeTo(this._opts.transitionDuration, 1, function() {
				that._banners[that._currentBanner].removeAttr("style").removeClass(that._opts.activeBannerClass);
				that._buttons[that._currentBanner].removeClass(that._opts.activeBannerClass);
				that._banners[intBanner].addClass(that._opts.activeBannerClass).removeAttr("style");
				that._buttons[intBanner].addClass(that._opts.activeBannerClass);
				that._currentBanner = intBanner;
				that._changing = false;
				if(wasRunning) {
					that.play()
				}
			});
		}
	}
	/*
	 * next() function
	 *
	 * Displays the next banner, or the first one if the last banner is already being displayed.
	 */
	BannerHandler.prototype.next = function() {
		this.changeBanner(this._currentBanner + 1);
	}
	/*
	 * prev() function
	 *
	 * Displays the previous banner, or the last one if the first banner is already being displayed.
	 */
	BannerHandler.prototype.prev = function() {
		this.changeBanner(this._currentBanner - 1);
	}
	BannerHandler.prototype.play = function() {
		if(this._id_timeout != null) {
			clearInterval(this._id_timeout)
		}
		that = this;
		this._id_timeout = setInterval(function() {
			that.next()
		}, this._opts.interval);
	}
	BannerHandler.prototype.stop = function() {
		if(this._id_timeout != null) {
			clearInterval(this._id_timeout)
		}
		this._id_timeout = 0
	}
	//Banner initialization:
	var banner = new BannerHandler();
})(jQuery)
