/*
 * Простейшая галерея
 * 
 * Автор: Владимир Игнатьев (vladimir.ignatyev@ridev.ru)
 * */
jQuery.fn.extend({
	 sgal: function (op)
	{
		this.each (function(i, b){

			var opa = op || 0.3;
			
			var images = [];	//image preload
			
			$(this).find('img').not('.preview').each(function(i, obj){
				
				images[i] = new Image ();
				images[i].src = $(obj).attr('rel');
				
				if (!$(obj).parent().hasClass('active')) $(obj).css({'opacity': opa});
				else $(obj).css({'opacity': 1.0});
								
				$(obj).mouseover(function(){
					$(this).stop();
					$(this).animate({'opacity':'1.0'});
				});
				$(obj).mouseout(function(){
					$(this).stop();
					if (!$(this).parent().hasClass('active')) {$(this).animate({'opacity':opa});}
				});
				$(obj).click(function(){
					var thumbsrc = $(this).attr('rel');
					var alt = $(this).attr('alt');
					var src = thumbsrc;
					$(b).find('img.preview').attr('alt', alt);
					$(b).find('img.preview').animate({'opacity':0.0}, function (){
						$(this).attr('src', src);
						$(this).animate({'opacity':1.0});
					});
					
					$(b).find('img').not('.preview').each(function(i, obj){
						$(obj).parent().removeClass('active');
						$(obj).stop();
						$(obj).animate({'opacity':opa});
					});
					
					//$(this).addClass('active');
					$(this).parent().addClass('active');
					$(obj).stop();
					$(obj).animate({'opacity': 1.0});
				});
				
				if ($(obj).parent().hasClass('active')){
					$(obj).click();
				}
				
			});
		});
	}
});