var FF = 0;
function AtLantis_Scroll_Controller()
{
	this.tickerObject = null;
	this.acceptableWidth = 0;
	this.scrollMode = true;
	this.changeTime = 4000;
	this.in_area = false;
	this.horizontal = false;
	this.varid = 1;
	this.cache = new Array();
}

AtLantis_Scroll_Controller.prototype.init_process = function(idname, horizontal, changetime)
{
	var tickerObject = YAHOO.util.Dom.get(idname);
	if (!tickerObject || !tickerObject.childNodes || tickerObject.childNodes.length == 0)
	{
		// alert('Scroll Nesnesi Bulunamadư');
		return false;
	}

	var ScrollController = new AtLantis_Scroll_Controller();
	ScrollController.tickerObject = tickerObject;
	ScrollController.scrollMode = true;
	ScrollController.horizontal = horizontal ? true : false;

	if (changetime !== false)
	{
		ScrollController.changeTime = changetime;
	}

	ScrollController.init_controls();
};

AtLantis_Scroll_Controller.prototype.init_controls = function()
{
	if (!this.horizontal)
	{
		this.clone_last_node();
	}
	else
	{
		var prevElement = YAHOO.util.Dom.getElementsBy(function(){ return true; }, 'span', this.tickerObject);
		for (var i = 0; i < prevElement.length; i++)
		{
			this.cache[this.cache.length] = prevElement[i];
		}
	}

	var all_length = this.horizontal ? YAHOO.util.Dom.getElementsBy(function(){ return true; }, 'span', this.tickerObject).length : this.tickerObject.childNodes.length;
	var part_scroll = Math.round((this.horizontal ? this.tickerObject.scrollWidth : this.tickerObject.scrollHeight) / all_length) - this.acceptableWidth;

	// Die Die Die Microsoft
	if (YAHOO.env.ua.ie == 6)
	{
		part_scroll = Math.round((this.horizontal ? this.tickerObject.scrollWidth : this.tickerObject.scrollHeight) / all_length) - this.acceptableWidth;
	}

	this.tickerObject.all_length = all_length;
	this.tickerObject.part_scroll = part_scroll;
	this.tickerObject.last_scroll = part_scroll * (all_length - 1);

	this.init_scroll_animation();
};

AtLantis_Scroll_Controller.prototype.set_events = function()
{
	YAHOO.util.Event.addListener(this.tickerObject, 'mouseover', AtLantis_Scroll_Controller.prototype.onmouseover, this);
	YAHOO.util.Event.addListener(this.tickerObject, 'mouseout', AtLantis_Scroll_Controller.prototype.onmouseout, this);
};

AtLantis_Scroll_Controller.prototype.clone_last_node = function()
{
	var crtag = (this.horizontal ? 'span' : 'div');
	var newobj = document.createElement(crtag);
	newobj.innerHTML = this.tickerObject.lastChild.innerHTML;

	this.tickerObject.insertBefore(newobj, this.tickerObject.firstChild);
};

AtLantis_Scroll_Controller.prototype.init_scroll_animation = function()
{
	if (this.horizontal)
	{
		this.tickerObject.scrollLeft = this.tickerObject.part_scroll;
	}
	else
	{
		this.tickerObject.scrollTop = this.tickerObject.part_scroll;
	}

	this.set_events();

	var _this = this;

	setTimeout(function(){ _this.begin_scroll_animation() }, this.changeTime);
};

AtLantis_Scroll_Controller.prototype.begin_scroll_animation = function()
{
	var _this = this;
	if (this.horizontal)
	{
		this.altScrollTimer = setTimeout(function(){ _this.alt_scroll_animation(_this.tickerObject.scrollLeft) }, (this.horizontal ? this.changeTime : 100));
	}
	else
	{
		this.altScrollTimer = setTimeout(function(){ _this.alt_scroll_animation(_this.tickerObject.scrollTop) }, (this.horizontal ? this.changeTime : 100));
	}
};

AtLantis_Scroll_Controller.prototype.alt_scroll_animation = function(fromScrollTop)
{
	var _this = this;

	if (this.in_area == true)
	{
		this.altScrollTimer = setTimeout(function(){ _this.alt_scroll_animation(fromScrollTop) }, 50);
		return false;
	}

	var maxScroll = fromScrollTop + this.tickerObject.part_scroll;
	if (this.horizontal)
	{
		if (maxScroll > this.tickerObject.scrollLeft)
		{
			this.tickerObject.scrollLeft += 5;
			this.altScrollTimer = setTimeout(function(){ _this.alt_scroll_animation(fromScrollTop) }, 50);
		}
		else
		{
			this.varid++;
			if (this.tickerObject.scrollLeft >= this.tickerObject.last_scroll)
			{
				this.tickerObject.scrollLeft = 0;
				this.varid = 0;
			}

			if (this.cache[this.varid].className && YAHOO.util.Dom.get(this.cache[this.varid].className))
			{
				YAHOO.util.Dom.get('scrolltabber').innerHTML = YAHOO.util.Dom.get(this.cache[this.varid].className).innerHTML;
			}

			clearTimeout(this.altScrollTimer);
			setTimeout(function(){ _this.begin_scroll_animation() }, this.changeTime);
		}
	}
	else
	{
		if (maxScroll > this.tickerObject.scrollTop)
		{
			this.tickerObject.scrollTop += 5;
			this.altScrollTimer = setTimeout(function(){ _this.alt_scroll_animation(fromScrollTop) }, 50);
		}
		else
		{
			if (this.tickerObject.scrollTop >= this.tickerObject.last_scroll)
			{
				this.tickerObject.scrollTop = 0;
			}

			clearTimeout(this.altScrollTimer);
			setTimeout(function(){ _this.begin_scroll_animation() }, this.changeTime);
		}
	}
};

AtLantis_Scroll_Controller.prototype.onmouseover = function(e, ScrollController)
{
	ScrollController.in_area = true;
};

AtLantis_Scroll_Controller.prototype.onmouseout = function(e, ScrollController)
{
	ScrollController.in_area = false;
};
