3     function normalizeNumber(pageNumber, pageCount){
 
   4         // Page number should be >= 1, <= pageCount; 0 if pageCount = 0
 
   5         var pageNumber = parseInt(pageNumber, 10);
 
  12                 pageNumber == Infinity ||
 
  13                 pageNumber == -Infinity ||
 
  17         if (pageNumber > pageCount)
 
  23     function bounds(galleryWidth, galleryHeight, imageWidth, imageHeight){
 
  27             minX: galleryWidth - imageWidth,
 
  28             minY: galleryHeight - imageHeight
 
  32     function normalizePosition(x, y, galleryWidth, galleryHeight, imageWidth, imageHeight){
 
  33         var b = bounds(galleryWidth, galleryHeight, imageWidth, imageHeight);
 
  35             x: Math.min(b.maxX, Math.max(b.minX, x)),
 
  36             y: Math.min(b.maxY, Math.max(b.minY, y))
 
  40     function fixImageSize(){
 
  47     function ScanGalleryPerspective(options){
 
  48         var old_callback = options.callback || function() { };
 
  50         this.vsplitbar = 'GALERIA';
 
  52         options.callback = function(){
 
  57             if (this.config().page == undefined)
 
  58                 this.config().page = CurrentDocument.galleryStart;
 
  59             this.$element = $("#side-gallery");
 
  60             this.$numberInput = $('.page-number', this.$element);
 
  66             this.$image = $('.gallery-image img', this.$element).attr('unselectable', 'on');
 
  69             this.$numberInput.change(function(event){
 
  70                 event.preventDefault();
 
  71                 self.setPage($(this).val());
 
  74             $('.start-page', this.$element).click(function(){
 
  75                 self.setPage(CurrentDocument.galleryStart);
 
  78             $('.previous-page', this.$element).click(function(){
 
  79                 self.setPage(parseInt(self.$numberInput.val(),10) - 1);
 
  82             $('.next-page', this.$element).click(function(){
 
  83                 self.setPage(parseInt(self.$numberInput.val(),10) + 1);
 
  86             $('.zoom-in', this.$element).click(function(){
 
  90             $('.zoom-out', this.$element).click(function(){
 
  91                 self.alterZoom((-0.2));
 
  94             $(window).resize(function(){
 
  95                 self.dimensions.galleryWidth = self.$image.parent().width();
 
  96                 self.dimensions.galleryHeight = self.$image.parent().height();
 
  99             this.$image.load(function(){
 
 100                 console.log("Image loaded.")
 
 102             }).bind('mousedown', function() {
 
 103                                 self.imageMoveStart.apply(self, arguments);
 
 108                         old_callback.call(this);
 
 111         $.wiki.SidebarPerspective.call(this, options);
 
 114     ScanGalleryPerspective.prototype = new $.wiki.SidebarPerspective();
 
 116     ScanGalleryPerspective.prototype._resizeImage = function(){
 
 117         var $img = this.$image;
 
 125             width: $img.width() * this.zoomFactor,
 
 126             height: $img.height() * this.zoomFactor,
 
 127             originWidth: $img.width(),
 
 128             originHeight: $img.height(),
 
 129                     galleryWidth: $img.parent().width(),
 
 130             galleryHeight: $img.parent().height()
 
 133         if (!(this.dimensions.width && this.dimensions.height)) {
 
 134             setTimeout(function(){
 
 139         var position = normalizePosition($img.position().left, $img.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 144             width: $img.width() * this.zoomFactor,
 
 145             height: $img.height() * this.zoomFactor
 
 149     ScanGalleryPerspective.prototype.setPage = function(newPage){
 
 150         newPage = normalizeNumber(newPage, this.doc.galleryImages.length);
 
 151         this.$numberInput.val(newPage);
 
 152                 this.config().page = newPage;
 
 153         $('.gallery-image img', this.$element).attr('src', this.doc.galleryImages[newPage - 1]);
 
 156     ScanGalleryPerspective.prototype.alterZoom = function(delta){
 
 157         var zoomFactor = this.zoomFactor + delta;
 
 158         if (zoomFactor < 0.2)
 
 162         this.setZoom(zoomFactor);
 
 165     ScanGalleryPerspective.prototype.setZoom = function(factor){
 
 166         this.zoomFactor = factor;
 
 168         this.dimensions.width = this.dimensions.originWidth * this.zoomFactor;
 
 169         this.dimensions.height = this.dimensions.originHeight * this.zoomFactor;
 
 171         // var position = normalizePosition(this.$image.position().left, this.$image.position().top, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 179         ScanGalleryPerspective.prototype.imageMoved = function(event){
 
 180                 event.preventDefault();
 
 182                 // origin is where the drag started
 
 183                 // imageOrigin is where the drag started on the image
 
 185                 var newX = event.clientX - this.origin.x + this.imageOrigin.left;
 
 186                 var newY = event.clientY - this.origin.y + this.imageOrigin.top;
 
 188                 var position = normalizePosition(newX, newY, this.dimensions.galleryWidth, this.dimensions.galleryHeight, this.dimensions.width, this.dimensions.height);
 
 198         ScanGalleryPerspective.prototype.imageMoveStart = function(event){
 
 199                 event.preventDefault();
 
 208                 this.imageOrigin = self.$image.position();
 
 209                 $(document).bind('mousemove.gallery', function(){
 
 210                         self.imageMoved.apply(self, arguments);
 
 211                 }).bind('mouseup.gallery', function() {
 
 212                         self.imageMoveStop.apply(self, arguments);
 
 218         ScanGalleryPerspective.prototype.imageMoveStop = function(event){
 
 219                 $(document).unbind('mousemove.gallery').unbind('mouseup.gallery');
 
 225     ScanGalleryPerspective.prototype.onEnter = function(success, failure){
 
 226         $.wiki.SidebarPerspective.prototype.onEnter.call(this);
 
 230         this.doc.refreshGallery({
 
 231             success: function(doc, data){
 
 233                                 console.log("gconfig:", self.config().page );
 
 234                                 self.setPage( self.config().page );
 
 235                 $('#imagesCount').html("/" + doc.galleryImages.length);
 
 237                 $('.error_message', self.$element).hide();
 
 238                 if(success) success();
 
 240             failure: function(doc, message){
 
 242                 $('.error_message', self.$element).show().html(message);
 
 243                 if(failure) failure();
 
 249         ScanGalleryPerspective.prototype.onExit = function(success, failure) {
 
 253     $.wiki.ScanGalleryPerspective = ScanGalleryPerspective;