var imgGallery;
(function($){
	if(!imgGallery)
	{
		/**
		 *  opts={
		 *    el: <required; selector or element>,
		 *    mainImg: <optional; selector or element>,
		 *    left: <optional; selector or element>,
		 *    right: <optional; selector or element>,
		 *    switcher: <optional; selector or element>,
		 *    switcherItems: <optional; selector or element>,
		 *		imgs: <required 1>[{thumb:'<thumb for mainImg>', full:'<full resolution img for lightbox>'},...],
		 *    noZoomIn: <optional; bool>,
		 *    noMainImgZoomIn: <optional; bool>,
		 *    switcherClick: <optional; bool>,
		 *  }
		 */
		var root=imgGallery=function(opts)
		{
			this.opts=opts;

			this.imgs=opts.imgs;
			// for Lightbox
			this.full=[];
			for(var i=0;i<this.imgs.length;i++)this.full.push(this.imgs[i].full);

			// prefetch
			(function(I){
				setTimeout(function()
				{
					for(var i=0;i<I.imgs.length;i++)
					{
						I.imgs[i].cache={thumb: new Image(), full: new Image()};
						I.imgs[i].cache.thumb.src=I.imgs[i].thumb;
						I.imgs[i].cache.full.src=I.imgs[i].full;
					}
				},0);
			})(this);

			this.el=$(opts.el);
			this.mainImg={el:opts.mainImg?$(opts.mainImg):this.el.find('.bigThumb > .img'), parent: this, index:0};

			if(!opts.noZoomIn)
			{
				this.zoomIn.el=this.el.find('> .button.zoomIn > a');
				if(!opts.noMainImgZoomIn)this.zoomIn.el.add(this.mainImg.el);
			}
			this.zoomIn.parent=this;

			this.left=opts.left?$(opts.left):this.el.find('> .arw.left > a');
			this.right=opts.right?$(opts.right):this.el.find('> .arw.right > a');
			this.switcher={el:opts.switcher?$(opts.switcher):this.el.find('> .switcher'), parent: this};
			this.switcher.items=opts.switcherItems?$(opts.switcherItems):this.switcher.el.find('> .item > a');

			this.scopeW=this.mainImg.el.innerWidth();

			this.first=true;
			this.switcher.items.eq(this.mainImg.index).addClass('active');

			(function(I){
				// more accurate width measure
				$(function(){I.scopeW=I.mainImg.el.innerWidth();});

				if(!opts.noZoomIn)
				{
					I.zoomIn.el.click(function(e)
					{
						e.preventDefault();
						I.zoomIn.process(I.mainImg.index,I.mainImg.el);
					});

					I.switcher.items.click(function(e)
					{
						e.preventDefault();
						I.zoomIn.process(I.switcher.items.index(this),$(this));
					});
				}

				if(opts.switcherClick)
				{
					I.switcher.items.click(function(e)
					{
						e.preventDefault();
						I.toIndex(I.switcher.items.index(this));
					});
				}
				else
				{
					I.switcher.items.mouseenter(function(e){I.toIndex(I.switcher.items.index(this));});
				}

				I.left.click(function(e)
				{
					e.preventDefault();
					I.rToIndex(-1);
				});

				I.right.click(function(e)
				{
					e.preventDefault();
					I.rToIndex(1);
				});
			})(this);
		};

		root.create=function(opts){return new root(opts);};


		root.prototype={
			rToIndex:function(i)
			{
				if(!i)return true;
				var direction=i<0;
				i=i%this.imgs.length;
				if(!i)return true;

				var old=this.mainImg.index;
				i+=this.mainImg.index;

				if(i>=this.imgs.length)i-=this.imgs.length;
				if(i<0)i+=this.imgs.length;

				var oldW=this.imgs[old].cache.thumb.width;
				var newW=this.imgs[i].cache.thumb.width;
				if(this.first)this.mainImg.el.css({backgroundPosition: Math.round((this.scopeW-oldW)/2)+'px 50%'});
				else this.mainImg.el.stop(true);

				(function(I){
					I.mainImg.el.animate({backgroundPosition:(direction?I.scopeW:-I.scopeW)+'px 50%'},'normal','easeOutExpo',function(){
						I.mainImg.el.css({backgroundImage: 'url("'+I.imgs[i].thumb+'")'})
							.css({backgroundPosition: (direction?-I.scopeW:I.scopeW)+'px 50%'}).animate({backgroundPosition: Math.round((I.scopeW-newW)/2)+'px 50%'},'normal','easeOutExpo');
					});
				})(this);

				this.first=false;
				this.mainImg.index=i;
				this.switcher.items.eq(old).removeClass('active');
				this.switcher.items.eq(i).addClass('active');
			},

			toIndex:function(i)
			{
				if(i>=this.imgs.length)i=this.imgs.length-1;
				if(i<0)i=0;
				return this.rToIndex(i-this.mainImg.index);
			},

			zoomIn:{
				process:function(i, el)
				{
					$.fancybox($.extend(true,[],this.parent.full),{
						'orig'					: el,
						'index'					: i,
						'cyclic'				: true,
						'type'					: 'image',
						'transitionIn'	: 'elastic',
						'transitionOut'	: 'elastic',
						'easingIn'			: 'easeOutBack',
						'easingOut'			: 'easeInBack',
						'centerOnScroll': true,
						_:0
					});
				},
				_:0
			},
			_:0
		};
	}

/*
	// Priklad inicializace galerie
	var productGall=imgGallery.create({
		el:'#<tag:idGallery />',
		imgs:[
			{thumb:'<tag:urlDecode(product.imgmain.src3) />', full:'<tag:urlDecode(product.imgmain.src0) />'}
			<loop:product.imgs>,{thumb:'<tag:urlDecode(product.imgs[].src3) />', full:'<tag:urlDecode(product.imgs[].src0) />'}</loop:>
			<loop:product.variants><if:variants[].img.src0 && shop_products_detail_isImage(variants[].img.src0)>,{thumb:'<tag:urlDecode(variants[].img.src3) />', full:'<tag:urlDecode(variants[].img.src0) />'}</if:></loop:>
		],
		_:0
	});
*/
})(jQuery);

