var UMG = UMG ? UMG : function()
{
	var pub = {};
	var priv = {};
	return pub;
}();
/**
 * @class UMG.TeaserJSPaging
 * @extends Class
 * @constructor
 * @param {String} id The Component Id
 * @param {Object} config The config object
 * @param {Number} config.size The amount of items per page
 * @param {String} config.itemSelector An alternative item selector
 */
UMG.TeaserJSPaging = UMG.TeaserJSPaging || Class.create({
	pages : null,
	controls : null,
	component : null,
	currentPage : 0,
	initialize : function(id, options)
	{
		this.component = $(id);
		UMG.TeaserJSPaging.instances.push(this);
		this.comId = this.component.up('[class^="com-id-"]').className.split(' ')[0];
		if (UMG.TeaserJSPaging.options[this.comId])
		{
			options = UMG.TeaserJSPaging.options[this.comId];
		}
		this.itemSelector = options.itemSelector || '.umg-i';
		this.controls = {
			prev : this.component.down('.umg-btn-prev'),
			'next' : this.component.down('.umg-btn-next')
		};
		this.initControls();
		this.pages = this.initPages(this.itemSelector, options.size);
		if (this.pages.length > 1)
			this.showPage(0);
		else
		{
			this.controls.prev.addClassName('umg-hide').removeClassName('umg-show');
			this.controls.next.addClassName('umg-hide').removeClassName('umg-show');
		}
	},
	initPages : function(selector, size)
	{
		var pages = this.component.select(selector).eachSlice(size || 4, function(el)
		{
			return el;
		});
		return pages;
	},
	initControls : function()
	{
		var self = this;
		this.controls.prev.observe('click', function(ev)
		{
			if (this.hasClassName('umg-hide'))
			{
				ev.stop();
				return false;
			}
			self.navigate(-1);
		});
		this.controls.next.observe('click', function(ev)
		{
			if (this.hasClassName('umg-hide'))
			{
				ev.stop();
				return false;
			}
			self.navigate(1);
		});
	},
	showPage : function(index)
	{
		if (index < 0)
		{
			index = this.pages.length -1;
		}
		else if (index > this.pages.length - 1)
		{
			index = 0;
		}
		this.pages.each(function(page, i)
		{
			if (i != index)
			{
				page.each(function(el, j)
				{
					el.hide()
				});
				this.currentPage = index;
			}
			else
			{
				page.each(function(el, j)
				{
					el.show()
				});
			}
		}, this);
	},
	navigate : function(crement)
	{
		this.showPage(this.currentPage + crement);
	}
});
UMG.TeaserJSPaging.instances = UMG.TeaserJSPaging.instances ||[];

