var Gallery = function(data)
{
	var self		= this;
	self.cursor		= 0;
	self.time		= 400;
	self.data		= data;
	self.length		= self.data.length;
	self.wrapper	= $('gallery_wrapper');
	self.changed	= new Array();
	self.count		= 15;
	self.timer		= null;
	self.createImages = function()
	{
		for(i = 0; i < self.length; i++)
		{
			self.changed[i] = {'id' : i, 'img' : new Element('img', {'src' : self.data[i].src, 'styles' : {'height' : '120px', 'float' : 'left'}}), 'a' : new Element('a', {'href' : self.data[i].href})};
			self.changed[i].a.grab(self.changed[i].img); 
			self.wrapper.grab(self.changed[i].a); 
		}
	}
	self.init = function()
	{
		self.createImages();
		self.events();
		//window.setInterval("gallery.rotate()", self.time);
	}
	self.rotate = function()
	{ 
		// presun v poli prvok
		var item = self.changed.shift();
		self.changed.push(item);
		//zmena obrazka
		
		fxm = new Fx.Tween(item.img);
		fxm.set('tween', {'duration' : self.time});
		//fxm.start('marginLeft', (item.img.width*(-1))+'px');
		fxm.start('marginLeft', '-200px');
		fxm.addEvent('complete', function(){
			item.a = item.a.dispose(); 
			item.img.setStyle('marginLeft', '0px'); 
			item.a.inject(self.wrapper, 'bottom'); 
		});
	} 
	self.rightTimer = function()
	{
		self.clearTimer();
		self.timer = window.setInterval("gallery.rotateRight()", self.time);
	}
	self.leftTimer = function()
	{
		self.clearTimer();
		self.timer = window.setInterval("gallery.rotateLeft()", self.time);
	}
	self.clearTimer = function()
	{
		if(self.timer)
		{
			window.clearInterval(self.timer);
			self.timer = null
		}
	}
	self.rotateLeft = function()
	{ 
		// presun v poli prvok
		var item = self.changed.shift();
		self.changed.push(item);
		//zmena obrazka
		
		fxm = new Fx.Tween(item.img);
		fxm.set('tween', {'duration' : self.time});
		//fxm.start('marginLeft', (item.img.width*(-1))+'px');
		fxm.start('marginLeft', '-200px');
		fxm.addEvent('complete', function(){
			item.a = item.a.dispose(); 
			item.img.setStyle('marginLeft', '0px'); 
			item.a.inject(self.wrapper, 'bottom'); 
		});
	} 
	self.rotateRight = function()
	{ 
		// presun v poli prvok
		var item = self.changed.pop();
		self.changed.unshift(item);
		//zmena obrazka
		
		item.a = item.a.dispose(); 
		item.img.setStyle('marginLeft', '-200px');
		item.a.inject(self.wrapper, 'top');
		
		fxm = new Fx.Tween(item.img);
		fxm.set('tween', {'duration' : self.time});
		fxm.start('marginLeft', '0px');
		/*
		fxm.addEvent('complete', function(){
			item.img.setStyle('marginLeft', '0px'); 
		});*/
	} 
	self.events = function()
	{
		self.changed.each(function(obj)
		{
			obj.a.addEvent('click', function(e){
				iBox.galleryLink(obj.id); 
				e.preventDefault();
			}.bind(obj.a)); 
		});
		/*
		$('arrow_right').addEvent('mouseover', function(){
			self.rightTimer();
		});
		$('arrow_left').addEvent('mouseover', function(){
			self.leftTimer();
		});
		$('arrow_right').addEvent('mouseout', function(){
			self.clearTimer();
		});
		$('arrow_left').addEvent('mouseout', function(){
			self.clearTimer();
		});*/
		$('arrow_left').addEvent('click', function(){
			self.rotateLeft();
		});
		$('arrow_right').addEvent('click', function(){
			self.rotateRight();
		});
		
		$('gallery_wrapper').addEvent('mousewheel', function(event)
		{
			if(event.wheel > 0)
			{
				self.rotateLeft();
			}
			else
			{
				self.rotateRight();
			}
			event.preventDefault()
		});
	}
	window.addEvent('domready', function()
	{
		self.init();
	});
}

