if(window.FD) {
  var DomainGallery = new Class({
  
    Extends: FD.Gallery,
    
    planCount: 0,
    planPos: 0,
    
    initialize: function(options) {
      this.addEvent("swap", this.doDynB.bind(this));
      this.parent(options);
      // Check for Plans page. 
      var floorPlan = $$(".floorplan");
      if(floorPlan.length > 0) {
        // Get the prev/next buttons
        var buttons = $$(".top-controls a");
        buttons.addEvent("click", this.swapPlan.create({
          event: true,
          bind: this
        }));
      }
    },
    
    swapPlan: function(e) {
      if(e) {
        new Event(e).stop();
      }
      // Get the button. 
      var button = e.target;
      if(button) {
        if(button.hasClass("prevImage")) {
          this.movePlan(-1);
        } else {
          this.movePlan(1);
        }
      }
    },
    
    movePlan: function(dir) {
      if(dir && window.planImages) {
        var plan = $$('.floorplan img');
        if(plan.length == 1) {
          var i = dir + this.planPos;
          var pL = planImages.length;
          if(i < 0) {
            i = pL - 1;
          } else if(i > pL - 1) {
            i = 0;
          }
          this.planPos = i;
          if(i % 3 == 0) {
            FD.refreshAd("*");
          }
          $('imgPos').setText(i + 1);
          var newPlan = new Element("img", {
            src: planImages[i]
          });
          newPlan.injectAfter(plan[0]);
          plan[0].remove();
          
        /* Changes */
        var link = document.getElementById(window.floorPlanlinkID);
        link.href = window.planLinks[i];
        /* End Changes */
        }
      }
    },
    
    /*
      Function: setPageNumber
      
        This function sets the current page number on all page number identifiers.
    */
    setImageNumber: function(pageNo) {
      var imgPos = $('imgPos');
      if(imgPos) {
        $('imgPos').setText(pageNo + 1);
      }
    }, 
    
    /*
      Function: constructNav
      
        This function constructs the nav, setting click events to swap 
        the main image to that which is clicked.
    */
    constructNav: function() {
      var scroller = this.getScroller();
      if(scroller) {
        var scrollerProps = scroller.getCoordinates();
        this.navH = scrollerProps.height;
        var thumbs = scroller.getElements(this.options.navElementType);
        this.imgC = thumbs.length;
        for(var a = 0, i = thumbs.length; a < i; a++) {
          if(a == 0) {
            this.navW = thumbs[a].offsetWidth;
            if(this.options.startImg) {
              if(thumbs[this.options.startImg]) {
                thumbs[this.options.startImg].addClass(this.options.classSelected);
              }
            } else {
              thumbs[a].addClass(this.options.classSelected);
            }
          }
          if(thumbs[a].hasClass("plan")) {
            thumbs[a].addEvent("click", this.gotoPlan.bind(this, thumbs[a]));
            this.planCount++;
          } else {
            thumbs[a].addEvent("click", this.swap.bind(this, a, true));
          }
          var img = thumbs[a].getElements("img")[0];
          img.id = "FDGalThumb_"+a;
          if(img.offsetHeight < this.navH) {
            var diff = Math.floor((this.navH - img.offsetHeight) / 2);
            if(diff > 1) {
              img.setStyle("margin-top", diff+"px");
            }
          }
        }
        var imgCEl = $('imgC');
        if(imgCEl) {
          imgCEl.setText(this.imgC - this.planCount);
        }
        
        var nav = this.getNav();
        var nW = nav.getStyle("width").toInt();
        this.thumbsVisible = Math.round(nW / this.navW);
        this.parseButtons();
        // Initial Image setting
        if(this.options.startImg) {
          var sImg = this.options.startImg;
          if(sImg <= this.imgC) {
            this.imgPos = this.options.startImg;
            this.moveNavToCurrentImage(1);
          }
        }
        // Bind the on/off button if it exists
        var galSwitch = $(this.options.idSwitch);
        if(galSwitch) {
          scroller.addEvent("click", this.toggleNav.bind(this, null, true));
          galSwitch.addEvent("click", this.toggleNav.bind(this, null, true));
        }
      }
    },
    
    gotoPlan: function(a) {
      if(a && a.href) {
        window.location = a.href;
      }
    },
    
    /*
      Function: moveBy
      
        This function is used to swap the main forward or backwards 
        depending on the value of i.
        i is not the index of the image, but the increment or decrement
        to the selected image index.
    */
    moveBy: function(e, i) {
      if(e) {
        new Event(e).stop();
      } else {
        var e = null;
      }
      if(this.options.hotZone > 0) {
        if(e) {
          this.closeNav();
        }
      } else if(this.navOn) {
        this.toggleNav();
      }
      var pos = this.imgPos + i;
      if(pos == (this.imgC - this.planCount)) {
        pos = 0;
      } else if (pos < 0) {
        pos = this.imgC - 1 - this.planCount;
      }
      this.swap(e, pos);
      this.moveNavToCurrentImage(i);
    },
    
    doDynB: function(e) {
      // Swap ad
      
      if(this.clickNo > 0 && this.clickNo % this.options.adSwapCount == 0) {
        FD.refreshAd("adSpot-iframe-skyscraper");
      }
      var mImg = $(this.options.idMainImg);
      if(mImg && mImg.getParent().getTag() == "a") {
        var anchor = mImg.getParent();
        anchor.href = window.location.href;
        // Do content impression
        var pN;
        if(window['s_pageName']) {
          pN = s_pageName;
        } else if(window['s']) {
          pN = s.pageName;
        }
        FD.doNielsonImpression.delay(50, FD, [pN, anchor]);      
      }
    }
  });
  
  function initDomGallery() {
    if(!window.galleryOptions) {
      galleryOptions = {};
    }
    galleryOptions = $merge(galleryOptions, {
      propGalImages: "galleryImages",
      idScroll: "navScroll",
      classPrev: "prevImage",
      classNext: "nextImage",
      classMoveLinks: "fl"
    });
    var domGallery = new DomainGallery(galleryOptions);
  }
  window.addEvent("initDomGallery", initDomGallery);
  FD.register("DomGallery");
}
