﻿function Banner(name) {
	this.ObjectName = name;
	this.NumberOfBanners = 0;
	this.CurrentBanner = 0,
	this.TargetElement = null;
	this.Options = null;
	this.Width = 0;
}

Banner.prototype.Init = function (options, targetId) {
	var width;

	this.TargetElement = $('#' + targetId);
	this.Options = options;
	this.NumberOfBanners = $(this.TargetElement).children().size();

	width = $(this.TargetElement).width();
	this.Width = width;

	var left = 0;
	$(this.TargetElement).children().each(function () {
		$(this).width(width);
		$(this).css('left', left);
		left += width;
	});
	$(this.TargetElement).width(left);
	this.SetTimeout(options.BeginTime);
}

Banner.prototype.SetTimeout = function (timeOut) {
	this.TimeoutID = window.setTimeout(this.ObjectName + ".Animate()", timeOut);
}

Banner.prototype.Animate = function () {
	var index;

	if (this.CurrentBanner < this.NumberOfBanners - 1)
		index = this.CurrentBanner + 1;
	else
		index = 0;
	this.Goto(index);
}

Banner.prototype.Goto = function (index) {
	var banner,
		left;

	$($("#slidecontrol").children()).removeClass("active");
	$($("#slidecontrol").children()[index]).addClass("active");

	if (index >= 0 && index < this.NumberOfBanners) {
		left = -(index * this.Width);
		window.clearTimeout(this.TimeoutID);
		this.CurrentBanner = index;

		banner = this;
		$(this.TargetElement).animate({ "left": left + "px" }, 500,
			function () {
				banner.SetTimeout(banner.Options.WaitTime);
			});
	}
}
