(function($){
	$.fn.extend({bxGallery:function(options){
		var defaults={maxwidth:'',maxheight:'',thumbwidth:200,thumbcrop:false,croppercent:.35,thumbplacement:'bottom',thumbcontainer:'',opacity:.7,load_text:'',load_image:'http://i302.photobucket.com/albums/nn92/wandoledzep/spinner.gif',wrapperclass:'outer'}
		var options=$.extend(defaults,options);
		var o=options;
		var cont='';
		var caption='';
		var $outer=$('div#my_thumbs');
		var $orig=this;   // ul main
		var tall=0;
		var wide=0;
		var showing=0;
		var i=0;
		var k=$orig.find('img').size();  // ���-�� ��������
		var current=0;
		var _play=0; //��������� �����������
		var _timerOut = false;
		preload_img();

		function preload_img(){
			$orig.hide();
			if(o.load_text!=''){
				$orig.before('<div id="loading">'+o.load_text+'</div>');}
			else{
				$orig.before('<div id="loading"><img src="'+o.load_image+'" /></div>');
			}

			$orig.parent().find('#loading').css({'textAlign':'center','width':o.maxwidth});
			$orig.find('img').each(function(){
				var the_source=$(this).attr('src');
				var the_img=new Image();
				the_img.onload=function(){
					preload_check();
				};
				the_img.src=the_source;
			});
		}
 		$('#play').click(function(){
 		  if (_play == 0){
 		     $('#btn_play').attr('src','/templates/prototype/images/btn_stop.gif');
 		     _play = 1;
 		     play_list();
          }
          else {
               $('#btn_play').attr('src','/templates/prototype/images/btn_start.gif');
               _play = 0;
          		if (_timerOut) clearTimeout(_timerOut);
          		current=-1;
          }
            	return false;
        });

 		$('#pause').click(function(){
                $('#btn_play').attr('src','/templates/prototype/images/btn_start.gif');
                _play = 0;
            	if (_timerOut) clearTimeout(_timerOut);
            	return false;
        });

		function play_list(){
		   if (_timerOut) clearTimeout(_timerOut);

     	   $orig.find('li').fadeOut();             // �������� ������� ������� ��������
           if (++current > (k-1)) current = 0;
           $orig.find('li').eq(current).fadeIn();      // �������� �� �������� � �������� �

						if($orig.find('li').eq(current).find('img').attr('alt')!=''){        // ���� �������� ����� ������� �����
					    	caption=$orig.find('li').eq(current).find('img').attr('alt');


                            $('div#my_cap').html('<p>'+caption+'</p>');    // ��������� ������ � ���
						}
						showing=current;   /* */
		  _timerOut = setTimeout(function(){play_list()},3500);
		}

		function preload_check(){      // ����������� ��� ������ ��������
			i++;
			if(i==k){init();}
		}


		function init(){
			set_layout();
			set_main_img();
			place_thumbcontainer();
			set_thumbs();
		}

		function set_layout(){
			$orig.parent().find('#loading').hide();
			$orig.show();

			$orig.wrap('<div class="'+o.wrapperclass+'"></div>');  //��������� ��� gallery     outer
			$outer=$orig.parent();
			$orig.find('li').css({'position':'absolute'});      // ������ ���� ������� �� ���������� ����������������
		}


		function set_main_img(){
			$orig.find('img').each(function(){
				var $this=$(this);


			                _name = $this.attr('src').substr(0,($this.attr('src').length-4));

				cont+='<li><img src="'+_name+'-sm.jpg" /></li>';
			});
			$orig.find('li:not(:first)').hide();
			/*$orig.height(tall);
			$orig.width(wide);  */
		//	$outer.find('.caption').width(wide);
		}


		function place_thumbcontainer(){
		   $('div#my_thumbs').prepend('<ul class="thumbs">'+cont+'</ul>');
		   if($orig.find('li').eq(0).find('img').attr('alt')!=''){        // ���� �������� ����� ������� �����
					    	caption=$orig.find('li').eq(0).find('img').attr('alt');
                            $('div#my_cap').html('<p>'+caption+'</p>');    // ��������� ������ � ���
						}
		}
		function set_thumbs(){
			$('div#my_thumbs').find('.thumbs li').each(function(){
				var $this=$(this);
				var $img=$this.find('img');
				var $imgwidth=$img.width();
				var $imgheight=$img.height();

				if(o.thumbcrop){
					$img.width($imgwidth*o.croppercent);
					$img.height(($imgheight/$imgwidth)*$img.width());
					$this.css({'float':'left','width':o.thumbwidth,'height':o.thumbwidth,'overflow':'hidden','cursor':'pointer'});
				}
				else{
					$img.width(o.thumbwidth);
					$img.height(($imgheight/$imgwidth)*o.thumbwidth);
					$this.css({'float':'left','cursor':'pointer'});
					$this.height($img.height());
				}

				$this.click(function(){              // ��������� ����� �� ������
					var x=$('div#my_thumbs').find('.thumbs li').index($this);  // ������ �������
					if(showing!=x){
						$orig.find('li').fadeOut();             // �������� ������� ������� ��������
						$orig.find('li').eq(x).fadeIn();        // �������� �� �������� � �������� �
						if($orig.find('li').eq(x).find('img').attr('alt')!=''){        // ���� �������� ����� ������� �����
					    	caption=$orig.find('li').eq(x).find('img').attr('alt');

							//$(this).replaceWith("<div>" + $(this).text() + "</div>");

                            $('div#my_cap').html('<p>'+caption+'</p>');    // ��������� ������ � ���
						}
						showing=x;
						current=x;
					}
				});
			});

			var $thumb=$outer.find('.thumbs li');
			$thumb.eq(0).addClass('on');
			$thumb.not('.on').fadeTo(0,o.opacity);
			$thumb.click(function(){
				var t=$(this);
				var i=$thumb.index(this);
				if(current!=i){
					$thumb.removeClass('on');
					t.addClass('on');
					$thumb.not('.on').fadeTo(200,o.opacity);
					current=i;
				}
			}).hover(function(){
				$(this).stop().fadeTo(200,1);
			},function(){
				$(this).not('.on').stop().fadeTo(200,o.opacity);
			});
		}
	}});
})(jQuery);
