DB_Rotator = function ()
{
	// Setup variables
	this.rotation_interval = 5000;
	this.rotation_on = false;
	this.rotation_count = 0;
	this.main_article_number = 1;
	this.no_load = false;
	this.controls = {};
	this.timeout_func;
	this.loop_handlers = [];

	// These hold the rotator_contents' uh content
	this.article_viewers = [];
	this.article_tabs = [];
	this.article_objects = [];

	this.init();
};

	DB_Rotator.prototype.init = 
	function ()
	{
		// Get array of article viewers.
		this.article_viewers = getElementsByTagAndClass(document, "*", "article_viewer");

		// Get array of article tabs.
		this.article_tabs = getElementsByTagAndClass(document, "*", "article_tab");

		// Get a list of article_content dls and then process them into DBObjects.
		var dl_list = getElementsByTagAndClass(document, 'dl', 'article_content');

		for (var i=0; i < dl_list.length; i++)
		{
			this.article_objects.push(createDBObject(dl_list[i]));
		}

		/* Populate the article tabs */
		for (var i=0; i < this.article_tabs.length; i++)
		{
			var self = this;

			var clickFactory = function () 
			{ 
				var index = i;
				return function () { self.loop_to(index);};
			};

			var oArticle = this.article_objects[i];
			var oTab = this.article_tabs[i];
			EventUtil.addEventHandler(oTab, "click", clickFactory()); 
			render_object(oArticle, oTab);
		}
	};

	DB_Rotator.prototype.rotator_loop = 
	function ()
	{
		if (this.rotation_on) 
		{
			this.loop_forward();
			var self = this;
			this.timeout_func = setTimeout(function(){self.rotator_loop();}, this.rotation_interval);
		}
		else 
		{
			this.timeout_func = null;
		}
	};

	DB_Rotator.prototype.loop_to =
	function (loop_num)
	{
		// Call the loop handlers
		this.rotation_count = loop_num;
		this.main_article_number = (this.rotation_count % this.article_objects.length) + 1;

		/* Loop through the article viewers */
		if (this.article_viewers.length > 0)
		{
			for (var i=0; i < this.article_viewers.length; i++)
			{
				var rotator_index = (this.rotation_count + i) % this.article_objects.length;
				var oArticle = this.article_objects[rotator_index];
				var oViewer = this.article_viewers[i];

				// Set proper number to active
				if (i == 0 && this.controls["numbers"])
				{
					for (var j=0; j < this.controls["numbers"].length; j++)
					{
						var oNumber = this.controls["numbers"][j]
						if (j == rotator_index)
							addClassName(oNumber,"active");
						else
							removeClassName(oNumber,"active"); 
					}
				}
				render_object(oArticle, oViewer);
			}
			this.rotation_count++;
		}
		/* Loop through article tabs */
		if (this.article_tabs.length > 0)
		{
			for (var i=0; i < this.article_tabs.length; i++)
			{
				var oTab = this.article_tabs[i];
				if (this.main_article_number == (i+1))
				{
					addClassName(oTab,"active");
				}
				else
				{
					removeClassName(oTab,"active"); 
				}
			}
		}
		this.callLoopHandlers();
	}

	DB_Rotator.prototype.stop =
	function ()
	{
		this.rotation_on = false;
		clearTimeout(this.timeout_func);
	}

	DB_Rotator.prototype.start =
	function ()
	{
		this.rotation_on = true;
		if (!this.timeout_func)
		{
			this.rotator_loop();
		}
	}

	DB_Rotator.prototype.loop_forward =
	function ()
	{
		this.loop_to(this.rotation_count);
	}

	DB_Rotator.prototype.loop_backward =
	function ()
	{
		this.loop_to(this.rotation_count+this.article_objects.length-2);
	}

	DB_Rotator.prototype.activate_controls =
	function (control_id)
	{
		var oControl = document.getElementById(control_id);
		if (!oControl)
		{
			return false;
		}

		// Get elements with className number, which represents active articles
		var oNumbers = getElementsByTagAndClass(oControl, "*", "number");
		if (oNumbers)
		{
			var self = this;
			var count = 0;
			this.controls["numbers"] = [];

			for (var i=0; i < oNumbers.length; i++)
			{
				var oNumber = oNumbers[i];
				this.controls["numbers"][count] = oNumber;
				count++;
				var _mousedown_handler = function (num)
				{
					return function () {
						self.loop_to(num);
					}
				}
				oNumber.onmousedown = _mousedown_handler(i);
			}
		}
		_attachEventToClass = function (cls, func)
		{
			var aObjects = getElementsByTagAndClass(oControl, "*", cls);
			if (aObjects)
			{
				for (var i=0; i < aObjects.length; i++)
				{
					var obj = aObjects[i];
					obj.onmousedown = func;
				}
			}
		}

		var self = this;
		_attachEventToClass("backward", function () { self.loop_backward();})
		_attachEventToClass("forward", function () { self.loop_forward(); })
		_attachEventToClass("stop", function () { self.stop(); })
	};

	DB_Rotator.prototype.attachLoopHandler = 
	function (func)
	{
		if (func)
		{
			this.loop_handlers[this.loop_handlers.length] = func;
		}
	};

	DB_Rotator.prototype.callLoopHandlers = 
	function ()
	{
		for (var i=0; i < this.loop_handlers.length; i++)
		{
			var func = this.loop_handlers[i];
			func();
		}
	};
