
RedSquare.DataPager = Base.extend
({
	constructor: function(data, linkPrevious, linkNext, options)
	{
		this.linkPrevious = linkPrevious;
		this.linkNext = linkNext;
		
		this.linkPrevious.style.display = 'block';
		this.linkNext.style.display = 'block';
		
		this.data = data;
		
		this.mode = RedSquare.DataPager.MODE_WRAP;
		
		this.elementBinder = new RedSquare.ElementBinder();
		
		this.index = 0;
		
		if (this.linkNext)
			Event.observe(this.linkNext, 'click', this.linkNextOnClick.bindAsEventListener(this));
	
		if (this.linkPrevious)
			Event.observe(this.linkPrevious, 'click', this.linkPreviousOnClick.bindAsEventListener(this));
		
		this.linkPrevious.onclick = function() { return false };
		this.linkNext.onclick = function() { return false };
		
		
		if (options)
		{
			if (options.mode)
				this.mode = options.mode;
			
			if (options.startRandom)
				this.randomize();
			
			if (options.waitForEvent)
				this.waitForEvent = true;
		}
		
		if (!this.waitForEvent)
			this.display();
	},
	
	randomize: function()
	{
		// make the index jump to a random spot in the data array. note this does not call display
		if (this.data.length > 0)
		{
			this.index = Math.floor( Math.random() * (this.data.length - 1) );
		}
	},
	
	display: function()
	{
		this.displayLinks();
		this.elementBinder.process(this.data[this.index]);	
	},
	
	displayLinks: function()
	{
		this.linkPrevious.show();	
		this.linkNext.show();	
		
		if (this.mode == RedSquare.DataPager.MODE_END)
		{
			if (this.index == this.data.length - 1)
			{
				// hide the next link
				this.linkNext.hide();
			}	
			else if (this.index == 0)
			{
				this.linkPrevious.hide();	
			}
		}	
	},
	
	linkNextOnClick: function(event)
	{
		Event.element(event).blur();
		
		this.index++;
		
		if (this.mode == RedSquare.DataPager.MODE_WRAP)
		{
			if (this.index == this.data.length)
				this.index = 0;
		}
	
		this.display();
		
		Event.stop(event);
		
	},
	
	getData: function(index)
	{
		
	},
	
	linkPreviousOnClick: function(event)
	{
		Event.element(event).blur();
		
		Event.stop(event);
		
		this.index--;
		
		if (this.mode == RedSquare.DataPager.MODE_WRAP)
		{
			if (this.index == -1)
				this.index = this.data.length - 1;	

		}
				
		this.display();
	}
});

Object.extend
(
 	RedSquare.DataPager,
	{
		MODE_WRAP: 0,
		MODE_END: 1		
	}
);
