﻿function Banner(name) {
	this.ObjectName = name;
	this.NumberOfBanners = 0;
	this.CurrentIndex = 0,
	this.TargetElement = null;
	this.Options = null;
	this.Width = 0;
	this.ActiveBanner = null;
}

Banner.prototype.Init = function (options, targetId) {
	this.TargetElement = $('#' + targetId);

	this.Options = options;
	this.NumberOfBanners = $(this.TargetElement).children().size();

	this.Width = $(this.TargetElement).width();

	this.ActiveBanner = $("#slides").children()[this.CurrentIndex];
	this.BeginStoryboardAnimate(options.BeginTime);
}

Banner.prototype.ClearAllTimeouts = function () {
	window.clearTimeout(this.AnimateTimeoutID);
}

Banner.prototype.BeginStoryboardAnimate = function (timeOut) {
	this.ClearAllTimeouts();
	this.AnimateTimeoutID = window.setTimeout(this.ObjectName + ".Animate()", timeOut);
}

Banner.prototype.Animate = function () {
	this.Next(false);
}

Banner.prototype.Next = function (reset) {
	var index = this.CurrentIndex;

	if (index < this.NumberOfBanners - 1)
		index++;
	else
		index = 0;

	this.Goto(index);
}

Banner.prototype.Goto = function (index) {
	var banner = this,
		active,
		next;

	if (index >= 0 && index < this.NumberOfBanners) {
		this.ClearAllTimeouts();

		active = this.ActiveBanner;
		next = $("#slides").children()[index];

		$(active).animate({ opacity: 0 }, 2500);
		$(next).animate({ opacity: 1.0 }, 2500, function () {
			banner.BeginStoryboardAnimate(4000);
		});
		this.ActiveBanner = next;
		this.CurrentIndex = index;
	}
}
