﻿var MenuAds = {
	count: 0,
	selectedIndex: -1,
	autoRotate: true,
	rotateIntervalId: 0,
	rotateInterval: 4500,

	RotateTo: function(n)
	{
		n = Math.round(n);
		if (n >= this.count) n = 0;
		if (n < 0) n = this.count - 1;
		//if (n == this.selectedIndex) return false;
		//alert(document.getElementById('MenuAdSlider').style.left + '\n' + n);
		document.getElementById('MenuAdSlider').style.left = '-' + (n * 180) + 'px';
		this.selectedIndex = n;
		return false;
	},

	RotateBy: function(n, resetInterval)
	{
		n = Math.round(n);
		if (n > 1) n = 1;
		if (n < -1) n = -1;
		this.RotateTo(this.selectedIndex + n);
		if (resetInterval == true) this.ResetInterval();
		return false;
	},

	TogglePlay: function()
	{
		this.autoRotate = (this.autoRotate == true) ? false : true;
		// If autoRotate is not boolean, it should be considered as false.
		if (this.autoRotate)
		{
			this.Play();
		}
		else
		{
			this.Pause();
		}
		return false;
	},

	Play: function()
	{
		this.autoRotate = true;
		this.RotateBy(1);
		//alert(this.rotateInterval);
		this.rotateIntervalId = window.setInterval("MenuAds.RotateBy(1);", this.rotateInterval);
		document.getElementById('MenuAdPlayImage').src = 'images/menu/adControls/pause.gif';
	},

	AutoNext: function()
	{
		this.RotateBy(1);
	},

	Pause: function()
	{
		this.autoRotate = false;
		window.clearInterval(this.rotateIntervalId);
		document.getElementById('MenuAdPlayImage').src = 'images/menu/adControls/play.gif';
	},

	ResetInterval: function()
	{
		window.clearInterval(this.rotateIntervalId);
		this.rotateIntervalId = window.setInterval("MenuAds.RotateBy(1);", this.rotateInterval);
	},

	Init: function(count, autoRotate, rotateInterval)
	{
		this.count = count;
		this.rotateInterval = rotateInterval;
		if (autoRotate != false)
		{
			this.Play();
		}
		else
		{
			this.RotateTo(0);
		}
	}

};