3     function normalizeNumber(pageNumber, pageCount){
 
   4         // Numer strony musi być pomiędzy 1 a najwyższym numerem
 
   5         var pageNumber = parseInt(pageNumber, 10);
 
   9         pageNumber == Infinity ||
 
  10         pageNumber == -Infinity ||
 
  14         if (pageNumber > pageCount)
 
  20     function bounds(galleryWidth, galleryHeight, imageWidth, imageHeight){
 
  24             minX: galleryWidth - imageWidth,
 
  25             minY: galleryHeight - imageHeight
 
  29     function normalizePosition(x, y, galleryWidth, galleryHeight, imageWidth, imageHeight){
 
  30         var b = bounds(galleryWidth, galleryHeight, imageWidth, imageHeight);
 
  32             x: Math.min(b.maxX, Math.max(b.minX, x)),
 
  33             y: Math.min(b.maxY, Math.max(b.minY, y))
 
  37     function fixImageSize(){
 
  44     function ScanGalleryPerspective(options){
 
  45         var old_callback = options.callback || function() { };
 
  47                 this.noupdate_hash_onenter = true;
 
  49         options.callback = function(){
 
  54             this.$element = $("#side-gallery");
 
  55             this.$numberInput = $('.page-number', this.$element);
 
  61             this.$image = $('.gallery-image img', this.$element).attr('unselectable', 'on');
 
  64             this.$numberInput.change(function(event){
 
  65                 event.preventDefault();
 
  66                 self.setPage($(this).val());
 
  69             $('.previous-page', this.$element).click(function(){
 
  70                 self.setPage(parseInt(self.$numberInput.val(),10) - 1);
 
  73             $('.next-page', this.$element).click(function(){
 
  74                 self.setPage(parseInt(self.$numberInput.val(),10) + 1);
 
  77             $('.zoom-in', this.$element).click(function(){
 
  81             $('.zoom-out', this.$element).click(function(){
 
  82                 self.alterZoom((-0.2));
 
  85             $(window).resize(function(){
 
  86                 self.dimensions.galleryWidth = self.$image.parent().width();
 
  87                 self.dimensions.galleryHeight = self.$image.parent().height();
 
  90             $('.gallery-image img', this.$element).load(function(){
 
  91                 console.log("Image loaded.")
 
  93             }).bind('mousedown', function() {
 
  94                                 self.imageMoveStart.apply(self, arguments);
 
  99                         old_callback.call(this);
 
 102         $.wiki.Perspective.call(this, options);
 
 105     ScanGalleryPerspective.prototype = new $.wiki.Perspective();
 
 107     ScanGalleryPerspective.prototype._resizeImage = function(){
 
 108         var $img = this.$image;
 
 116             width: $img.width() * this.zoomFactor,
 
 117             height: $img.height() * this.zoomFactor,
 
 118             originWidth: $img.width(),
 
 119             originHeight: $img.height(),
 
 120                     galleryWidth: $img.parent().width(),
 
 121             galleryHeight: $img.parent().height()
 
 124         if (!(this.dimensions.width && this.dimensions.height)) {
 
 125             setTimeout(function(){
 
 130         var position = normalizePosition($img.position().left, $img.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 135             width: $img.width() * this.zoomFactor,
 
 136             height: $img.height() * this.zoomFactor
 
 140     ScanGalleryPerspective.prototype.setPage = function(newPage){
 
 141         newPage = normalizeNumber(newPage, this.doc.galleryImages.length);
 
 142         this.$numberInput.val(newPage);
 
 143                 this.config().page = newPage;
 
 144         $('.gallery-image img', this.$element).attr('src', this.doc.galleryImages[newPage - 1]);
 
 147     ScanGalleryPerspective.prototype.alterZoom = function(delta){
 
 148         var zoomFactor = this.zoomFactor + delta;
 
 149         if (zoomFactor < 0.2)
 
 153         this.setZoom(zoomFactor);
 
 156     ScanGalleryPerspective.prototype.setZoom = function(factor){
 
 157         this.zoomFactor = factor;
 
 159         this.dimensions.width = this.dimensions.originWidth * this.zoomFactor;
 
 160         this.dimensions.height = this.dimensions.originHeight * this.zoomFactor;
 
 162         // var position = normalizePosition(this.$image.position().left, this.$image.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 166             width: this.dimensions.width,
 
 167             height: this.dimensions.height,
 
 176         ScanGalleryPerspective.prototype.imageMoved = function(event){
 
 177                 event.preventDefault();
 
 179                 // origin is where the drag started
 
 180                 // imageOrigin is where the drag started on the image
 
 182                 var newX = event.clientX - this.origin.x + this.imageOrigin.left;
 
 183                 var newY = event.clientY - this.origin.y + this.imageOrigin.top;
 
 185                 var position = normalizePosition(newX, newY, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 195         ScanGalleryPerspective.prototype.imageMoveStart = function(event){
 
 196                 event.preventDefault();
 
 205                 this.imageOrigin = self.$image.position();
 
 206                 $(document).bind('mousemove.gallery', function(){
 
 207                         self.imageMoved.apply(self, arguments);
 
 208                 }).bind('mouseup.gallery', function() {
 
 209                         self.imageMoveStop.apply(self, arguments);
 
 215         ScanGalleryPerspective.prototype.imageMoveStop = function(event){
 
 216                 $(document).unbind('mousemove.gallery').unbind('mouseup.gallery');
 
 222     ScanGalleryPerspective.prototype.onEnter = function(success, failure){
 
 225         $.wiki.Perspective.prototype.onEnter.call(this);
 
 227         this.doc.refreshGallery({
 
 228             success: function(doc, data){
 
 230                                 console.log("gconfig:", self.config().page );
 
 231                                 self.setPage( self.config().page );
 
 233                 $('.error_message', self.$element).hide();
 
 234                 if(success) success();
 
 236             failure: function(doc, message){
 
 238                 $('.error_message', self.$element).show().html(message);
 
 239                 if(failure) failure();
 
 245         ScanGalleryPerspective.prototype.onExit = function(success, failure) {
 
 249     $.wiki.ScanGalleryPerspective = ScanGalleryPerspective;