jQuery.fn.useZoom = function(options){
      var options = jQuery.extend({
            zoomerLocation: $(document.body),
            lidColor: '#000',
            lidOpacity: 0.5,
            boxColor: '#fff',
            boxStartWidth: 300,
            boxStartHeight: 300,
            preloadImageURL: '/skin/images/ajax-loader.gif',
            closeImageURL:   '/skin/images/close_pugovka.png',
            nextImageURL:    '/skin/images/next.png',
            prevImageURL:    '/skin/images/prev.png',
            imageHSpace: 10,
            imageVSpace: 10,
            minVSpaceToPageBorder: 50,
            minHSpaceToPageBorder: 50
      }, options);                                                                                                                                                                                        
 
 
 
      //создаю элементы и задаю им параметры
      options.zoomMasterWrap = $('<div style="position: absolute; left: 0; top: 0; display: none; height: '+  options.zoomerLocation.height()  +'px; width: '+  options.zoomerLocation.width()  +'px; left: 0; top: 0; z-index: 5000"></div>');
      options.opacityDiv     = $('<div style="background-color: '+  options.lidColor  +'; opacity: '+ options.lidOpacity +'; -moz-opacity: '+ options.lidOpacity +'; -khtml-opacity: '+ options.lidOpacity +'; filter: alpha(opacity='+ options.lidOpacity * 100 +'); height: 100%; width: 100%; position: absolute; left: 0; top: 0;" ></div>');
      options.imageContainer = $('<div style="background: '+  options.boxColor  +' url('+  options.preloadImageURL  +') no-repeat 50%; position: absolute; z-index:  5002; width: '+  options.boxStartWidth  +'px; height: '+  options.boxStartHeight  +'; left: 50%; top: '+  getCenterView()  +'px; margin-top: -'+  Math.round(options.boxStartHeight / 2)  +'px; margin-left: -'+ Math.round(options.boxStartWidth / 2) +'px;"></div>');
      options.closeImage     = $('<img style="position: absolute; right: 20; top: 20; cursor: pointer;" src='+  options.closeImageURL  +' />');
      options.nextImage      = $('<div style="background: url('+  options.nextImageURL  +') no-repeat right; margin-right: 25px; position: absolute; display: none; width: 50%; height: 100%; right: 0; top: 0; z-index: 5010"></div>');
      options.prevImage      = $('<div style="background: url('+  options.prevImageURL  +') no-repeat left; margin-left: 25px; position: absolute; display: none; width: 50%; height: 100%; left: 0; top: 0; z-index: 5010"></div>');
      options.nakladka       = $('<div style="position: absolute; left: 0; top: 0; width: 100%; height: 100%; z-index: 5010;"></div>');
      options.normalImage       = null;
      options.normalImageWidth  = null;
      options.normalImageHeight = null;
      options.x = null;
      options.currentLink = null;
      options.massiv_ssilok = this.get();
       
      
      //собираю эти элементы
      options.zoomMasterWrap.append(options.opacityDiv);
      options.imageContainer.append(options.nextImage);
      options.imageContainer.append(options.prevImage);
      options.nakladka.append(options.closeImage);
      options.imageContainer.append(options.nakladka);
      options.zoomMasterWrap.append(options.imageContainer);
            
      options.zoomerLocation.append(options.zoomMasterWrap);
      
      //корректировка
      options.closeImage.css({right: 10, top: 10})
      options.zoomMasterWrap.css({height:  options.zoomerLocation.height()});
      //конец корректировкам
      
      options.nakladka.mousemove(function(event) {
         var event  = event.originalEvent;
         if(event.layerX)       options.x = event.layerX; 
         else if(event.offsetX) options.x = event.offsetX;
         else                   options.x = event.x;
         if(options.x > (options.nakladka.width()/2)) {
           options.prevImage.fadeOut(200);
           options.nextImage.fadeIn(200);
         }
         else {
           options.nextImage.fadeOut(200);
           options.prevImage.fadeIn(200);
         }
      }).mousedown(function(event){
           var event  = event.originalEvent;
           if(event.layerX)       options.x = event.layerX; 
           else if(event.offsetX) options.x = event.offsetX;
           else                   options.x = event.x;
           if(options.x > (options.nakladka.width()/2)) {
              for(var i=0; i<options.massiv_ssilok.length; i++) {
                 if(options.massiv_ssilok[i] == options.currentLink) {
                    options.currentLink = (i == (options.massiv_ssilok.length -1)) ? options.massiv_ssilok[0] : options.massiv_ssilok[i+1]
                    break;
                 }
              }
           }
           else {
              for(var i=0; i<options.massiv_ssilok.length; i++) {
                 if(options.massiv_ssilok[i] == options.currentLink){
                    options.currentLink = (i == 0) ? options.massiv_ssilok[options.massiv_ssilok.length -1] : options.massiv_ssilok[i-1]
                    break;
                 }
              }
           }
           
           $(options.normalImage).fadeOut(function(){
           /*-------------------  повтор кода  ----------------------*/
               options.normalImage = new Image();
               $(options.normalImage).css({
                                       display:  'none', 
                                       position: 'absolute', 
                                       left:      Math.round(options.imageHSpace/2), 
                                       top:       Math.round(options.imageVSpace/2)
                                     });
               
               options.normalImage.onload = function(){
                  var originalWidth  = options.normalImageWidth  = this.width;
                  var originalHeight = options.normalImageHeight = this.height;
                  
                  if( (options.normalImageWidth + options.imageHSpace) > MAX_IMAGE_WIDTH  ) {
                       options.normalImageWidth  = MAX_IMAGE_WIDTH - options.imageHSpace;
                       options.normalImageHeight = Math.round( (options.normalImageWidth * originalHeight) / originalWidth ) - options.imageVSpace;
                       $(options.normalImage).width(options.normalImageWidth).height(options.normalImageHeight);
                       
                  }
                                                                                               
                  if( (options.normalImageHeight + options.imageVSpace) > MAX_IMAGE_HEIGHT  ) { 
                       options.normalImageHeight  = MAX_IMAGE_HEIGHT - options.imageVSpace;
                       options.normalImageWidth   = Math.round( (options.normalImageHeight * originalWidth) / originalHeight ) - options.imageHSpace;
                       $(options.normalImage).width(options.normalImageWidth).height(options.normalImageHeight);
                  }
                  
                  options.boxStartWidth  = options.normalImageWidth  + options.imageHSpace;
                  options.boxStartHeight = options.normalImageHeight + options.imageVSpace;
                  
                  options.imageContainer.append(options.normalImage)
                  options.imageContainer.animate({
                                                   width:        options.boxStartWidth,
                                                   marginLeft:  -Math.round(options.boxStartWidth / 2),
                                                   height:       options.boxStartHeight,
                                                   marginTop:   -Math.round(options.boxStartHeight / 2)
                                                 }, 
                                                 function(){$(options.normalImage).fadeIn()}
                                                );
                                                
               }
               options.normalImage.src = options.currentLink.href;
             //---------------- конец повтора кода ----------------------------

           });
            
      }).mouseout(function(){
         options.prevImage.fadeOut(200);
         options.nextImage.fadeOut(200);
      })
      
      
     options.closeImage.mousemove(function(event){
         event.stopPropagation();
         options.prevImage.fadeOut(200);
         options.nextImage.fadeOut(200);
     }).mousedown(function(event){
        event.stopPropagation()
        $(options.normalImage).remove()
        options.zoomMasterWrap.fadeOut(function(){
            options.imageContainer.fadeOut()
        })
     })
     
     
     options.opacityDiv.mousedown(function(event){
       if(event.target == this) {
         event.stopPropagation()
         $(options.normalImage).remove()
         options.zoomMasterWrap.fadeOut(function(){
            options.imageContainer.fadeOut()
         })
       }
     })
     
     
     $(window).scroll(function(){
        options.imageContainer.stop().animate({top: getCenterView()});
     })
      


/* ******************************************************
               Константы
******************************************************* */

    var MAX_IMAGE_WIDTH  = getPageSize()[2] - options.minHSpaceToPageBorder;
    var MAX_IMAGE_HEIGHT = getPageSize()[3] - options.minVSpaceToPageBorder;
    


      
/* ******************************************************
               Вспомогательные функции
******************************************************* */
    
    function getPageYOffset(){
      return window.pageYOffset ? window.pageYOffset : (document.documentElement.scrollTop || document.body.scrollTop);
    };
    
    
    function getCenterView(){
      var center = Math.round(getPageSize()[3]/2) + getPageYOffset();
      return center;
    };
    

    function  getPageSize(){
       var xScroll, yScroll;

       if (window.innerHeight && window.scrollMaxY) {
                  xScroll = document.body.scrollWidth;
                  yScroll = window.innerHeight + window.scrollMaxY;
       } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
                  xScroll = document.body.scrollWidth;
                  yScroll = document.body.scrollHeight;
       } else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
                  xScroll = document.documentElement.scrollWidth;
                  yScroll = document.documentElement.scrollHeight;
       } else { // Explorer Mac...would also work in Mozilla and Safari
                  xScroll = document.body.offsetWidth;
                  yScroll = document.body.offsetHeight;
       }
 
       var windowWidth, windowHeight;
       if (self.innerHeight) { // all except Explorer
                 windowWidth = self.innerWidth;
                 windowHeight = self.innerHeight;
       } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                 windowWidth = document.documentElement.clientWidth;
                 windowHeight = document.documentElement.clientHeight;
       } else if (document.body) { // other Explorers
                 windowWidth = document.body.clientWidth;
                 windowHeight = document.body.clientHeight;
       }
  

       // for small pages with total height less then height of the viewport
       if(yScroll < windowHeight){
          pageHeight = windowHeight;
       } else {
          pageHeight = yScroll;
       }

       // for small pages with total width less then width of the viewport
       if(xScroll < windowWidth){
                 pageWidth = windowWidth;
       } else {
                 pageWidth = xScroll;
       }

       return [pageWidth,pageHeight,windowWidth,windowHeight];
    };
    
//________________________________________________________



     
      //самая работа при нажатии на ссылку с картинкой
      return this.each(function(){
        $(this).click(function(event){event.preventDefault()}).mousedown(function(){
           options.currentLink  = this;
           options.zoomMasterWrap.css({height:  options.zoomerLocation.height()}).fadeIn();
           options.normalImage = new Image();
           
           options.imageContainer.css({top: getCenterView()})

           $(options.normalImage).css({
                                   display:  'none', 
                                   position: 'absolute', 
                                   left:      Math.round(options.imageHSpace/2), 
                                   top:       Math.round(options.imageVSpace/2)
                                 });
           
           options.normalImage.onload = function(){
              var originalWidth  = options.normalImageWidth  = this.width;
              var originalHeight = options.normalImageHeight = this.height;
              
              if( (options.normalImageWidth + options.imageHSpace) > MAX_IMAGE_WIDTH  ) {
                   options.normalImageWidth  = MAX_IMAGE_WIDTH - options.imageHSpace;
                   options.normalImageHeight = Math.round( (options.normalImageWidth * originalHeight) / originalWidth ) - options.imageVSpace;
                   $(options.normalImage).width(options.normalImageWidth).height(options.normalImageHeight);
                   
              }
                                                                                           
              if( (options.normalImageHeight + options.imageVSpace) > MAX_IMAGE_HEIGHT  ) { 
                   options.normalImageHeight  = MAX_IMAGE_HEIGHT - options.imageVSpace;
                   options.normalImageWidth   = Math.round( (options.normalImageHeight * originalWidth) / originalHeight ) - options.imageHSpace;
                   $(options.normalImage).width(options.normalImageWidth).height(options.normalImageHeight);
              }
              
              options.boxStartWidth  = options.normalImageWidth  + options.imageHSpace;
              options.boxStartHeight = options.normalImageHeight + options.imageVSpace;
              
              options.imageContainer.append(options.normalImage)
              
              
              options.imageContainer.animate({
                                               width:        options.boxStartWidth,
                                               marginLeft:  -Math.round(options.boxStartWidth / 2),
                                               height:       options.boxStartHeight,
                                               marginTop:   -Math.round(options.boxStartHeight / 2) 
                                             }, 
                                             function(){$(options.normalImage).fadeIn()}
                                            );
                                            
           }
           options.normalImage.src = this.href;
        })
      });
      

};