YUI.add('_ag', function(Y) {
 
    Y.namespace('my.ag');
	var _ag = Y.my.ag;
    
	_ag.gallery = function(id,setup)
	{
		this.containerId = /\#/.test(id) ? id : '#'+id;
		this.imageWidth = setup.imageWidth;
		this.imageHeight = setup.imageHeight;
		this.height = setup.height;
		this.currentIndex = setup.currentIndex ? setup.currentIndex : 0;
		this.nextIndex = 0;
		this.imageArray = setup.imageArray;
		this.animRun = false;
		this.delay = setup.delay;
		this.message = 'Посмотреть в полном размере';
		this.thumbSrc = '/js/animated_gallery/thumb.gif';
	}
	
	_ag.gallery.prototype =
	{
		init : function()
		{
			this.container = Y.one(this.containerId);
			this.cWrapper = Y.Node.create('<div id="gw-wrapper">');
			this.cLonger = Y.Node.create('<div id="gw-longer">');
			this.containerCenterX = Math.round(this.container.getX() + this.container.get('offsetWidth')/2)
			
			this.container.setStyle('visibility','hidden');
			this.container.setStyle('height',this.height);
			
			this.container.set('innerHTML','');
			this.cWrapper.appendChild(this.cLonger);
			this.container.appendChild(this.cWrapper);
			
			this.cLonger.setStyle('height',this.height+'px');
			this.cLonger.setStyle('lineHeight',this.height+'px');
			if(Y.UA.ie)
			{
				var span = Y.Node.create('<span style="display:inline-block;height:100%;line-height:100%;vertical-align:middle;">');
				this.cLonger.appendChild(span);
			}
			
			this.createGalleryItems();
			
			var anchors = Y.all('#gw-longer a'), obj = this;
			anchors.each
			(
				function()
				{
					this.on
					(
						'click',
						function(e)
						{ 
							if(this.get('className') != 'current')
							{
								e.halt();
								var position = Y.all('#gw-longer a').indexOf(this);
								obj.moveDirection = ((position - obj.currentIndex) > 0) ? -1 : 1;
								obj.moveTo(position)
							} 
						}
					)
				}
			)
		},
		
		createGalleryItems : function()
		{
			var imgWidth = Math.round(this.imageWidth/2),
				imgHeight = Math.round(this.imageHeight/2),
				obj = this;
			this.imageCounter = 1;
			
			for(var i = 0, l = this.imageArray.length; i < l; i++)
			{
				var image = this.imageArray[i],
					ins = Y.Node.create('<ins id="animated-gallery-ins-'+i+'">'),
					anchor = Y.Node.create('<a href="'+image.href+'" target="blank" id="animated-gallery-link-'+i+'" title="'+((i == this.currentIndex) ? this.message : image.alt)+'">'),
					img = document.createElement('img');
					
				this.cLonger.appendChild(ins);
				ins.appendChild(anchor);
				anchor.appendChild(img);
				img.id = "animated-gallery-image-"+i;
				img.setAttribute('alt',image.alt);
				img.style.width = imgWidth + 'px';
				img.style.height = imgHeight + 'px';
				img.onload = function()
				{
					if(obj.imageCounter == obj.imageArray.length)
					{
						obj.setToPosition(obj.currentIndex);
					}
					obj.imageCounter++;
				}
				img.src = this.thumbSrc;
			}
		},
		
		setToPosition : function(position)
		{
			var targetImage = Y.one('#animated-gallery-image-'+position),
				left = targetImage.get('parentNode').get('parentNode').get('offsetLeft'),
				destination = (left - this.container.get('offsetWidth')/2 + this.imageWidth/2)*-1;
	
			targetImage.setStyles({width : this.imageWidth, height : this.imageHeight});
			targetImage.get('parentNode').set('className','current');
			targetImage.get('parentNode').set('title',this.message);
			targetImage.set('className','current');
			this.cLonger.setStyle('left',destination);
			this.container.setStyle('visibility','visible');
			
			for(var i = 0, l = this.imageArray.length; i < l; i++)
			{
				var image = this.imageArray[i],
					img = Y.one('#animated-gallery-image-'+i);
				img.set('src',image.src);
			}
		},
		
		moveTo : function(position)
		{
			if((this.currentIndex != position) && !this.animRun)
			{
				this.animRun = true;
				var obj = this,
					targetImage = Y.one('#animated-gallery-image-'+position),
					left = targetImage.get('parentNode').get('parentNode').get('offsetLeft'),
					oldImage = Y.one('#animated-gallery-image-'+obj.currentIndex),
					fix = (obj.moveDirection > 0) ? 0 : obj.imageWidth/2,
					destination = (left - obj.container.get('offsetWidth')/2 + obj.imageWidth/2 - fix)*-1;
				
				var imageMove = new Y.Anim
				({
					node : obj.cLonger,
					to : { left : destination },
					easing: Y.Easing.easeOut,
					duration : obj.delay
				});
				
				
				var imageDecrease = new Y.Anim
				({
					node : oldImage,
					to : { width : obj.imageWidth/2, height : obj.imageHeight/2 },
					duration : obj.delay
				});
				
				var imageIncrease = new Y.Anim
				({
					node : targetImage,
					to : { width : obj.imageWidth, height : obj.imageHeight },
					duration : obj.delay
				});
				
				imageMove.on
				(
					'end',
					function()
					{
						obj.animRun = false;
						obj.currentIndex = position;
						oldImage.set('className',''); 
						oldImage.get('parentNode').set('className','');
						oldImage.get('parentNode').set('title',oldImage.get('alt'));
						targetImage.set('className','current'); 
						targetImage.get('parentNode').set('className','current');
						targetImage.get('parentNode').set('title', obj.message);
					}
				);
				imageMove.run();
				imageDecrease.run();
				imageIncrease.run();
			}
		}
	}
	
}, '0.0.1' /* module version */, {
    requires: ['node', 'yui', 'anim']
});

