
var prev_pic_width = 162+13;
var big_pic_width = 608;
var items_in_preview_container = 3;

 
 function enable_disable_buttons(mL, co)
 {
	mL = parseInt(mL);
	if(mL == 0) $('.prev').addClass('disabled');
	if(mL < 0) $('.prev').removeClass('disabled');
	hided_left = mL;
	if(hided_left < 0) hided_left = 0-hided_left;
	if( co*prev_pic_width - items_in_preview_container*prev_pic_width - hided_left > 0 ) $('.next').removeClass('disabled');
		else $('.next').addClass('disabled');
 }



(function($){
	$.fn.genGallery = function(options){
		return this.each(function(i){
			var el = this;
			el.index = i;
			$el = $(el);
			el.imgs = $el.find('img');
			if (el.imgs.length == 0)
				return true;
			// обворачиваем превьюшки служебным мусором			
			$el.wrap('<div class="gallery"><div class="gallery-preview"><div class="gallery-preview-cont"></div></div></div>');
			var $gallery = $el.parent().parent().parent();
			// создаем контейнер для отображения основных фотографий
//			$gallery.prepend('<table border="0" cellpadding="0" cellspacing="0" class="gallery-cont-border"><tr><td><img src="/i/w_lt.gif" border="0" alt=""/></td><td background="/i/w_t.gif"></td><td><img src="/i/w_rt.gif" border="0" alt=""/></td></tr><tr><td background="/i/w_l.gif"></td><td><div class="gallery-cont" ><div class="gallery-cont2"><div class="gallery-cont-wrapp" style="width:'+(el.imgs.length*big_pic_width)+'px"></div></div></div></td><td background="/i/w_r.gif"></td></tr><tr><td><img src="/i/w_lb.gif" border="0" alt=""/></td><td background="/i/w_b.gif"></td><td><img src="/i/w_rb.gif" border="0" alt=""/></td></tr></table>');
			$gallery.prepend('<table border="0" cellpadding="0" cellspacing="0" class="gallery-cont-border"><tr><td><div class="gallery-cont" ><div class="gallery-cont2"><div class="gallery-cont-wrapp" style="width:'+(el.imgs.length*big_pic_width)+'px"></div></div></div></td></tr></table>');
			var $galleryCont = $('.gallery-cont .gallery-cont2 .gallery-cont-wrapp', $gallery);
			$galleryPreview = $el.parent().parent();
 
			//if (el.imgs.length > items_in_preview_container)
			{
				// добавляем элементы прокрутки превьюшек
				$galleryPreview.append('<div class="gal_prev_l"></div><div class="gal_prev_r"></div>');
				$galleryPreview.append('<div class="next disabled"></div><div class="prev disabled"></div>');

				var $ul = $('ul', $galleryPreview); 
				var marginLeft = $ul.css('margin-left');
				
				$('.next, .prev', $galleryPreview).click(function(){
					if (window.galleryAnimate == true) return false;

					window.galleryAnimate = true;
					if ( ($(this).hasClass('next')) && (!$(this).hasClass('disabled')) )
					{
						marginLeft = parseInt(marginLeft) - prev_pic_width;
						var s = -((el.imgs.length*prev_pic_width) - (el.imgs.length % items_in_preview_container)*prev_pic_width);
						if (marginLeft < s) {
							window.galleryAnimate = false;
							return false;
						}	
						$ul.animate({
							marginLeft: parseInt(marginLeft) + 'px'
						}, big_pic_width, 'easeInQuint', function(){
							window.galleryAnimate = false;
						});
						enable_disable_buttons(marginLeft, el.imgs.length);
					}
					else
					{
						window.galleryAnimate = false;
						if ( ($(this).hasClass('prev')) && (!$(this).hasClass('disabled')) )
						{
							marginLeft = parseInt(marginLeft) + prev_pic_width;
							if (marginLeft > 0) {
								window.galleryAnimate = false;
								return false;
							}
							$ul.animate({
								marginLeft: parseInt(marginLeft) + 'px'
							}, big_pic_width, 'easeInQuint', function(){
								window.galleryAnimate = false;
							});
							enable_disable_buttons(marginLeft, el.imgs.length);
						}
						else window.galleryAnimate = false;
					}
				});


				enable_disable_buttons(marginLeft, el.imgs.length);
				
			}
			
			el.imgs.each(function(indx){
				var $img = $(this);
				$img.css({opacity:0.5});
				$img.parent('a');
				var image = new Image();

				image.src = $img.parent("a").attr('href');
				image._indx = indx;
				this._image = image;

				var bp = "";
				var ti = "";
				bp = $img.parent("a").attr('rel');
				ti = $img.parent("a").attr('title');

				$galleryCont.append(image);
//				$(image).wrap(ti);
//				$(image).wrap('<div class="gallery-image"><a href="'+bp+'" class="fancypopup" rel="lightbox-tour" title="&nbsp;'+ti+'"></a></div>');

				$(image).addClass('glossy');
				$(image).addClass('noshadow');
				$(image).addClass('iradius12');

				$img.hover(function(){
					$(this).animate({opacity:1}, 200);	
				}, function(){
					if (!$(this).hasClass('current'))
						$(this).animate({opacity:0.2}, 200);
				}).click(function(){
					el.imgs.each(function(){
						if ($(this).hasClass('current'))
							$(this).removeClass('current').css({opacity:0.2});
					});
					$(this).addClass('current').css('opacity', 1);
					$galleryCont.animate({
						marginLeft: (-big_pic_width * this._image._indx) + 'px'
					}, big_pic_width, 'easeInQuint');
									
					return false;
				});
			});
			if ($(el.imgs.get(0)))
				$(el.imgs.get(0)).click();
		});
	}
})(jQuery)