/*
* jQuery Co3 Scroll plugin
* 
* Version 1.4
* 29. December 2009
*
* Co3 Interaktivt kompetencebureau
* www.co3.dk
*
* Developer
* Rune ?llgaard Gr?nkj?r
* rg@co3.dk
*
* Created with jQuery v1.3.2
* Tested with jQuery v1.3.2
*
* Useage:
* Wraps a scrollcontent in a very fexible scrollarea
*
* jQuery.fn.CreateGallery = function(settings) {
* // Settings
* settings = jQuery.extend({
*   Corner_TR: 'http://jscripts.s3.co3.dk/Co3Gallery/corner_top_right.png',
*   Corner_BL: 'http://jscripts.s3.co3.dk/Co3Gallery/corner_bottom_left.png',
*   Corner_BR: 'http://jscripts.s3.co3.dk/Co3Gallery/corner_bottom_right.png',
*   Corner_TL: 'http://jscripts.s3.co3.dk/Co3Gallery/corner_top_left.png',
*   loaderGfx: 'http://jscripts.s3.co3.dk/Co3Gallery/ajax-loader_ny.gif',
*   ArrowPrev: 'http://jscripts.s3.co3.dk/Co3Gallery/arrow_back.png',
*   ArrowNext: 'http://jscripts.s3.co3.dk/Co3Gallery/arrow_next.png',
*   Close: 'http://jscripts.s3.co3.dk/Co3Gallery/btn_close.png',
*   Play: 'http://jscripts.s3.co3.dk/Co3Gallery/btn_play.png',
*   Stop: 'http://jscripts.s3.co3.dk/Co3Gallery/btn_stop.png',
*   BlankImage: 'http://jscripts.s3.co3.dk/Co3Gallery/x.gif',
*   TempThumb: null,
*   loaderGfxRevolutionTime: 750,
*  CloseOnBlockClick: true,
*   PlacePlayInside: false,
*    SideMargin: 25,
*   PaddingTop: 25,
*  PaddingRight: 25,
*   PaddingBottom: 25,
*   PaddingLeft: 25,
*   ImageMarginTop: 25,
*   ImageMarginBottom: 10,
*   resizeSpeed01: 400,
*   resizeSpeed02: 400,
*   resizeSpeed03: 400,
*   zIndex: 1000,
*   blockOpacity: 0.8,
*   blockBgColor: "black",
*   playSpeed: 4000,
*   useKeyboardShortcuts: true,
*   showImageList: false,
*   imageListHeight: 60,
*   imageListImageMargin: 5,
*   imageListChosenBorderColor: '#000000',
*   imageListChosenBorderWidth: 1,
*   imageListChosenMarginWidth: 2,
*   imageInformationHeight: 85,
*   imageInformationTemplate: '<div id="Co3Gallery_ImageHeader">[header]</div>[description]<br />St?rrelse: [width] x [height] og [imagesize] KB<br />Billede [imagenumber] af [numberofimages]<br /><a href="[href]">Download billedet</a>',
*   onClosed: null,
*   onOpen: null,
*   onOpened: null
* }, settings);
* 
* Changelog:
* Version 1.0:
* Created: 08. March 2009
*
* Version 1.1
* Created: 05. June 2009
* Changed:
* The way events are bound to the scrollarrows have been changed. They now work when the scrollcontent is being changed runtime.
*
* Version 1.1.1
* Created: 01. Maj 2009
* Changed:
* The calculation of the miniimagewidth has been changed
* The getting of the href attr in imagearea have been repaired
*
* Version 1.2
* Created: 16. September 2009
* Changed:
* Preload has been disabled in IE7 and IE8 due to stack overflows accurring
*
* Version 1.3
* Created: 12. november 2009
* Changed:
* The Co3.Block version 1.3.3 has now been incorporated
* Three new events have been added. onClosed, onOpen and onOpened
*
* Version 1.4
* Created: 29. december 2009
* Changed:
* If ArrowPrev and/or ArrowNext is set empty they will now not be showed and you cannot switch between the images
* The same is now the case with the Play button
*
*/

/*
* jQuery Extention
* Returns a custom Co3Gallery object
*/
jQuery.fn.CreateGallery = function(settings) {
  // Settings
  settings = jQuery.extend({
    Corner_TR: 'http://jscripts.s3.co3.dk/Co3Gallery/corner_top_right.png',
    Corner_BL: 'http://jscripts.s3.co3.dk/Co3Gallery/corner_bottom_left.png',
    Corner_BR: 'http://jscripts.s3.co3.dk/Co3Gallery/corner_bottom_right.png',
    Corner_TL: 'http://jscripts.s3.co3.dk/Co3Gallery/corner_top_left.png',
    loaderGfx: 'http://jscripts.s3.co3.dk/Co3Gallery/ajax-loader_ny.gif',
    ArrowPrev: 'http://jscripts.s3.co3.dk/Co3Gallery/arrow_back.png',
    ArrowNext: 'http://jscripts.s3.co3.dk/Co3Gallery/arrow_next.png',
    Close: 'http://jscripts.s3.co3.dk/Co3Gallery/btn_close.png',
    Play: 'http://jscripts.s3.co3.dk/Co3Gallery/btn_play.png',
    Stop: 'http://jscripts.s3.co3.dk/Co3Gallery/btn_stop.png',
    BlankImage: 'http://jscripts.s3.co3.dk/Co3Gallery/x.gif',
    TempThumb: null,
    loaderGfxRevolutionTime: 750,
    CloseOnBlockClick: true,
    PlacePlayInside: false,
    SideMargin: 25,
    PaddingTop: 25,
    PaddingRight: 25,
    PaddingBottom: 25,
    PaddingLeft: 25,
    ImageMarginTop: 25,
    ImageMarginBottom: 10,
    resizeSpeed01: 400,
    resizeSpeed02: 400,
    resizeSpeed03: 400,
    zIndex: 1000,
    blockOpacity: 0.8,
    blockBgColor: "black",
    playSpeed: 4000,
    useKeyboardShortcuts: true,
    showImageList: false,
    imageListHeight: 60,
    imageListImageMargin: 5,
    imageListChosenBorderColor: '#000000',
    imageListChosenBorderWidth: 1,
    imageListChosenMarginWidth: 2,
    imageInformationHeight: 85,
    imageInformationTemplate: '<div id="Co3Gallery_ImageHeader">[header]</div>[description]<br />St?rrelse: [width] x [height] og [imagesize] KB<br />Billede [imagenumber] af [numberofimages]<br /><a href="[href]">Download billedet</a>',
    onClosed: null,
    onOpen: null,
    onOpened: null
  }, settings);
  
  return new jQuery.Co3Gallery(this, settings);
};

/*
* Co3Gallery
* The custom Co3Gallery class
* Preloads all images
* Initiates all events
* On command creates all html needed to show the gallery
* Uses the Co3.Block to block the background
*/
jQuery.Co3Gallery = function(refImages, settings) {
  var co3Gallery = this;
  co3Gallery.chosenBorderPadding = settings.imageListChosenBorderWidth * 2 + settings.imageListChosenMarginWidth * 2;
  jQuery.Co3Gallery.currentGallery = this;

  this.settings = settings;
  this.preloadGfx(refImages.length);
  co3Gallery.galleryImages = [];
  co3Gallery.resizeTimeouts = [];
  var count = 0;
  refImages.each(function() {
    var co3GalleryImage = new jQuery.Co3GalleryImage(this, count);
    co3Gallery.galleryImages[co3Gallery.galleryImages.length] = co3GalleryImage;
    count++;
  });

  for (var i = 0; i < co3Gallery.galleryImages.length; i++) {
    var prevImg = null;
    var nextImg = null;
    if (i > 0) {
      prevImg = co3Gallery.galleryImages[i - 1];
    }
    if (i < co3Gallery.galleryImages.length - 1) {
      nextImg = co3Gallery.galleryImages[i + 1];
    }
    co3Gallery.galleryImages[i].nextImg = nextImg;
    co3Gallery.galleryImages[i].prevImg = prevImg;
    if (i === 0) {
      co3Gallery.isFirst = true;
    } else if (i === co3Gallery.galleryImages.length - 1) {
      co3Gallery.isLast = true;
    }
  }
  if (co3Gallery.galleryImages.length > 0) {
    //co3Gallery.galleryImages[0].loadImageChain();
  }
};

/*
* Preloads all necessary grafics to be used by the gallery
*/
jQuery.Co3Gallery.prototype.preloadGfx = function(numberOfImages) {
  this.cornerSize = 0;
  this.corner_TR = this.preloadGfxImage(this.settings.Corner_TR, true);
  this.corner_TR.css("top", 0).css("right", 0);
  this.corner_BL = this.preloadGfxImage(this.settings.Corner_BL, true);
  this.corner_BL.css("bottom", 0).css("left", 0);
  this.corner_BR = this.preloadGfxImage(this.settings.Corner_BR, true);
  this.corner_BR.css("bottom", 0).css("right", 0);
  this.corner_TL = this.preloadGfxImage(this.settings.Corner_TL, true);
  this.corner_TL.css("top", 0).css("left", 0);
  this.loaderGfx = this.preloadGfxImage(this.settings.loaderGfx, false, function(img) {
    var lGfx = jQuery(img);
    lGfx.css("position", "absolute").css("zIndex", 6).css("top", "50%").css("left", "50%").addClass("loaderGfx");
    lGfx.css("marginTop", -(img.height / 2)).css("marginLeft", -(img.width / 2));
  });
  this.close = this.preloadGfxImage(this.settings.Close);
  this.blankImage = this.preloadGfxImage(this.settings.BlankImage);

  if (numberOfImages > 1) {
    this.arrowPrev = this.preloadGfxImage(this.settings.ArrowPrev);
    this.arrowNext = this.preloadGfxImage(this.settings.ArrowNext);
    if (this.settings.Play !== "") {
      this.play = this.preloadGfxImage(this.settings.Play);
    }
  }
};

/*
* Preloads one image
* If the image is a corner the corner is meassured for use later
* Can be given a function to be run after the image has loaded
*/
jQuery.Co3Gallery.prototype.preloadGfxImage = function(imagePath, isCorner, extraLoadFn) {
  if (!imagePath || imagePath == "") {
    return null;
  }
  var co3Gallery = this;
  var tmpImg = new Image();
  if (isCorner) {
    tmpImg.onload = function() {
      co3Gallery.cornerSize = { width: this.width, height: this.height };
      if (extraLoadFn) {
        extraLoadFn(tmpImg);
      }
    };
  }
  if (extraLoadFn) {
    tmpImg.onload = function() {
      extraLoadFn(tmpImg);
    };
  }
  tmpImg.src = imagePath;

  jQtmpImg = jQuery(tmpImg);
  jQtmpImg.css("position", "absolute");
  return jQtmpImg;
};

/*
* Central method for the plugin
* Is called whenever user wants a specific image shown in the gallery
* The parameter co3GalleryImage is the image to be shown
*/
jQuery.Co3Gallery.prototype.showImage = function(co3GalleryImage) {
  var co3Gallery = this;
  if (!co3GalleryImage.isChainLoaded) {
    co3GalleryImage.loadImage();
    co3GalleryImage.chainLoadFromThis();
  }

  if (!this.block) {
    if (co3Gallery.settings.onOpen) {
      co3Gallery.settings.onOpen(co3Gallery);
    }
    this.block = jQuery.showBlock({
      maxOpacity: co3Gallery.settings.blockOpacity,
      blockBgColor: co3Gallery.settings.blockBgColor,
      blockZIndex: co3Gallery.settings.zIndex,
      closeOnBlockClick: co3Gallery.settings.CloseOnBlockClick,
      animateTime: co3Gallery.settings.resizeSpeed01
    }, function() {
      co3Gallery.buildGallery(co3GalleryImage);
      co3Gallery.co3GalleryImage = co3GalleryImage;
      co3Gallery.mergeImageInformation();
      var gallerySize = co3Gallery.calculateGallerySizes(co3GalleryImage);
      co3Gallery.co3GalleryDiv.animate({
        top: gallerySize.startTop,
        left: gallerySize.startLeft
      }, co3Gallery.settings.resizeSpeed01, jQuery.Co3Gallery.resizeCurrentGallery);
      if (co3Gallery.settings.onOpened) {
        co3Gallery.settings.onOpened(co3Gallery);
      }
    }, function() {
      co3Gallery.block = null;
      co3Gallery.stopTimeouts();
      jQuery.Co3Gallery.stop();
      co3Gallery.remove();
      co3Gallery.unbindKeyboard();
      if (co3Gallery.settings.onClosed) {
        co3Gallery.settings.onClosed(co3Gallery);
      }
    });
  } else {

    co3Gallery.co3GalleryDiv.find("#Co3Gallery_ImageHider").animate({
      opacity: 1
    }, co3Gallery.resizeSpeed01, function() {
      co3Gallery.co3GalleryDiv.find(".galImage").remove();
      co3Gallery.co3GalleryImage = co3GalleryImage;
      co3Gallery.mergeImageInformation();
      co3Gallery.resizeGallery();
    });
  }

  jQuery(window).bind("resize", jQuery.Co3Gallery.resizeCurrentGallery);
};

/*
* Removes the gallery from the "stage"
*/
jQuery.Co3Gallery.prototype.remove = function() {
  if (this.co3GalleryDiv) {
    this.co3GalleryDiv.remove();
  }
};

/*
* Readies the gallery for resizing by hiding several different elements
*/
jQuery.Co3Gallery.prototype.resizeGallery = function() {
  var co3Gallery = this;

  co3Gallery.Co3Gallery_Information.css("display", "none");
  if (co3Gallery.miniImageList) {
    co3Gallery.miniImageList.css("display", "none");
  }

  co3Gallery.close.css("display", "none");

  jQuery(window).unbind("resize", jQuery.Co3Gallery.resizeCurrentGallery);
  jQuery.Co3Gallery.resizeGallery();

};

/*
* Resizes the gallery to fit the chosen image
* Does this with several animations of the various elements
* When animations are ended the image is added and all elements are shown
* Before resizing the method checks if the image is loaded. If not a timeout i set to try again, thereby waiting for the image to be loaded.
*/
jQuery.Co3Gallery.resizeGallery = function() {

  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  co3Gallery.stopTimeouts();
  var co3GalleryImage = co3Gallery.co3GalleryImage;
  if (co3GalleryImage.galImageIsLoaded && co3Gallery.galleryImages[0].isAllRefLoaded()) {
    co3Gallery.loaderGfx.remove();
    var gallerySize = co3Gallery.calculateGallerySizes(co3GalleryImage);

    co3Gallery.Co3Gallery_cross_vertical.animate({
      height: gallerySize.height,
      width: gallerySize.crossWidth
    }, co3Gallery.settings.resizeSpeed02);
    co3Gallery.Co3Gallery_cross_horizontal.animate({
      height: gallerySize.crossHeight,
      width: gallerySize.width
    }, co3Gallery.settings.resizeSpeed02);
    co3Gallery.co3GalleryDiv.animate({
      top: gallerySize.top,
      left: gallerySize.left,
      width: gallerySize.width,
      height: gallerySize.height
    }, co3Gallery.settings.resizeSpeed02, function() {
      if (co3GalleryImage.galImageIsLoaded) {

        co3Gallery.co3GalleryDiv.append(co3GalleryImage.jQgalImage);

        co3Gallery.Co3Gallery_PrevLink.css("width", gallerySize.width / 2).css("height", gallerySize.imageHeight).css("top", gallerySize.imageTop).css("left", 0);
        co3Gallery.Co3Gallery_NextLink.css("width", gallerySize.width / 2).css("height", gallerySize.imageHeight).css("top", gallerySize.imageTop).css("left", gallerySize.width / 2);

        co3Gallery.Co3Gallery_ImageHider.css("width", gallerySize.imageWidth).css("height", gallerySize.imageHeight).css("top", gallerySize.imageTop).css("left", co3Gallery.settings.PaddingLeft).css("zIndex", 3);
        co3GalleryImage.jQgalImage.css("width", gallerySize.imageWidth).css("height", gallerySize.imageHeight).css("top", gallerySize.imageTop).css("left", co3Gallery.settings.PaddingLeft).css("zIndex", 2);
        co3Gallery.createImageList(gallerySize);
        co3Gallery.Co3Gallery_ImageHider.animate({
          opacity: 0
        }, co3Gallery.settings.resizeSpeed03, function() {
          jQuery.Co3Gallery.placeImageInformation(gallerySize);
          co3Gallery.close.css("display", "block");
          if (co3Gallery.play) {
            co3Gallery.play.css("display", "block");
          }
        });
        jQuery(window).bind("resize", jQuery.Co3Gallery.resizeCurrentGallery);
      }
    });
  } else {
    co3Gallery.co3GalleryDiv.append(co3Gallery.loaderGfx);
    co3Gallery.resizeTimeouts[co3Gallery.resizeTimeouts.length] = window.setTimeout(jQuery.Co3Gallery.resizeGallery, co3Gallery.settings.loaderGfxRevolutionTime);
  }
};

/*
* Clears all resize timeouts
*/
jQuery.Co3Gallery.prototype.stopTimeouts = function() {
  for (var i = 0; i < this.resizeTimeouts.length; i++) {
    window.clearTimeout(this.resizeTimeouts[i]);
  }
  this.resizeTimeouts = [];
};

/*
* Builds up all html used for the gallery
* Will png-fix all png's if the users browser i IE6 or lower.
* Binds all events used by the opened gallery
*/
jQuery.Co3Gallery.prototype.buildGallery = function(co3GalleryImage) {
  var htmlStats = this.getHtmlStats();
  var co3Gallery = this;
  jQuery.Co3Gallery.currentGallery = co3Gallery;
  var cornerSize = 0;
  if (this.cornerSize) {
    cornerSize = this.cornerSize
  }
  co3Gallery.ImageListCreated = false;
  this.co3GalleryDiv = jQuery(document.createElement("div"));
  var co3GalleryDiv = this.co3GalleryDiv;
  this.startWidth = this.cornerSize.width * 2;
  this.startHeight = this.cornerSize.height * 2;
  var startWidth = this.startWidth;
  var startHeight = this.startHeight;
  var startTop = co3GalleryImage.jQrefImage.position().top + co3GalleryImage.refImageHeight / 2 - startHeight / 2;
  var startLeft = co3GalleryImage.jQrefImage.position().left + co3GalleryImage.refImageWidth / 2 - startWidth / 2;

  this.Co3Gallery_cross_vertical = jQuery('<div id="Co3Gallery_cross_vertical" style="position:absolute; height:' + startHeight + '; top:0; background-color:#ffffff;"></div>');
  this.Co3Gallery_cross_vertical.css("width", startWidth - this.cornerSize.width * 2).css("height", startHeight).css("left", this.cornerSize.width);
  this.Co3Gallery_cross_horizontal = jQuery('<div id="Co3Gallery_cross_horizontal" style="position:absolute; left:0; background-color:#ffffff;"></div>');
  this.Co3Gallery_cross_horizontal.css("height", startHeight - this.cornerSize.height * 2).css("width", startHeight).css("top", this.cornerSize.height);

  this.Co3Gallery_ImageHider = jQuery('<div id="Co3Gallery_ImageHider" style="position:absolute; z-index:4; background-color:#ffffff;"></div>');

  this.Co3Gallery_PrevLink = jQuery('<div id="Co3Gallery_PrevLink" style="position:absolute; z-index:4; background-color:#ffffff;"></div>');
  this.Co3Gallery_NextLink = jQuery('<div id="Co3Gallery_NextLink" style="position:absolute; z-index:4; background-color:#ffffff;"></div>');
  this.Co3Gallery_PrevLink.css("opacity", 0);
  this.Co3Gallery_NextLink.css("opacity", 0);
  if (this.galleryImages.length > 1) {
    if (this.arrowPrev) {
      this.Co3Gallery_PrevLink.click(jQuery.Co3Gallery.prev).hover(jQuery.Co3Gallery.prevHover, jQuery.Co3Gallery.prevOut).css("cursor", "pointer");
    }
    if (this.arrowNext) {
      this.Co3Gallery_NextLink.click(jQuery.Co3Gallery.next).hover(jQuery.Co3Gallery.nextHover, jQuery.Co3Gallery.nextOut).css("cursor", "pointer");
    }
  }

  this.Co3Gallery_Information = jQuery('<div id="Co3Gallery_Information" style="position:absolute; display:none; z-index:1; overflow:hidden;"><div id="Co3Gallery_ImageInformation"></div></div>');
  if (co3Gallery.settings.showImageList && !co3Gallery.ImageListCreated) {
    this.miniImageList = jQuery('<div id="MiniImageList" style="position:absolute; overflow:hidden; left:50%; display:none;"></div>');
    this.miniImageListContent = jQuery('<div id="MiniImageListContent" style="position:relative;"></div>');
    this.miniImageList.append(this.miniImageListContent);
  }

  this.close.css("top", 0).css("right", 0).css("zIndex", 7).css("cursor", "pointer").click(jQuery.Co3Gallery.closeCurrentGallery).css("display", "none");

  co3GalleryDiv.append(this.corner_TR).append(this.corner_BL).append(this.corner_BR).append(this.corner_TL).append(this.Co3Gallery_cross_vertical).append(this.Co3Gallery_cross_horizontal).append(this.Co3Gallery_ImageHider).append(this.Co3Gallery_PrevLink).append(this.Co3Gallery_NextLink).append(this.Co3Gallery_Information).append(this.close).append(this.miniImageList);

  if (co3Gallery.play) {
    var playBottom = this.play[0].height;
    var playRight = this.cornerSize.width;
    if (this.settings.PlacePlayInside) {
      playBottom = 0;
    }
    this.play.css("position", "absolute").css("bottom", -playBottom).css("right", playRight).css("zIndex", 5).css("cursor", "pointer").css("display", "none");
    this.play.click(jQuery.Co3Gallery.startPlay);
    co3GalleryDiv.append(this.play)
  }

  var test = jQuery("#MiniImageList");
  co3GalleryDiv.attr("id", "Co3Gallery").css("position", "absolute").css("top", startTop).css("left", startLeft).css("width", startWidth).css("height", startHeight).css("zIndex", co3Gallery.settings.zIndex + 1);
  jQuery("body").prepend(co3GalleryDiv);

  if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) && RegExp.$1 < 7) {
    co3GalleryDiv.find("img[src$=.png]").each(function() {
      var imgToFix = jQuery(this);
      imgToFix.width(imgToFix.width());
      imgToFix.height(imgToFix.height());
      this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "',sizingMethod='crop')";
      this.src = co3Gallery.settings.BlankImage;
    });
  }

  this.bindKeyboard();
};

/*
* Binds a method to the documents keyup event
*/
jQuery.Co3Gallery.prototype.bindKeyboard = function() {
  jQuery(document).bind("keyup", jQuery.Co3Gallery.bindKeyboard);
};

/*
* Unbinds a method to the documents keyup event
*/
jQuery.Co3Gallery.prototype.unbindKeyboard = function() {
  jQuery(document).unbind("keyup", jQuery.Co3Gallery.bindKeyboard);
};

/*
* Creates the miniimagelist for the opened gallery.
*/
jQuery.Co3Gallery.prototype.createImageList = function(gallerySize) {
  var co3Gallery = this;
  var chosenBorderPadding = co3Gallery.settings.imageListChosenBorderWidth * 2 + co3Gallery.settings.imageListChosenMarginWidth * 2;
  if (co3Gallery.settings.showImageList) {
    if (!co3Gallery.ImageListCreated) {
      if (!co3Gallery.galleryImages[0].isAllRefLoaded()) {
        window.setTimeout(function() {
          createImageList(gallerySize);
        }, co3Gallery.settings.loaderGfxRevolutionTime);
        return;
      }
      co3Gallery.ImageListCreated = true;
      var co3GalleryImage = co3Gallery.co3GalleryImage;
      var miniImageListImages = [co3GalleryImage];

      //var miniImageListWidth = 0;
      var miniImageListWidth = chosenBorderPadding;

      var isHigh = true;
      var highDone = false;
      var lowDone = false;
      var lastIndex = co3GalleryImage.index;

      for (var i = 0; i < co3Gallery.galleryImages.length; i++) {
        if (!co3Gallery.galleryImages[i].refMiniImageWidth) {
          co3Gallery.galleryImages[i].updateSizes();
        }
        miniImageListWidth += co3Gallery.galleryImages[i].refMiniImageWidth;
      }

      for (var i = 0; i < co3Gallery.galleryImages.length; i++) {
        var miniImageListImage = co3Gallery.galleryImages[i];
        var refClone = miniImageListImage.jQrefImage.clone();
        refClone.css("marginRight", co3Gallery.settings.imageListImageMargin).css("cursor", "pointer");
        refClone.miniImageListImage = miniImageListImage;
        var miniImageWidth = miniImageListImage.refMiniImageWidth - chosenBorderPadding;
        var miniImageHeight = miniImageListImage.refMiniImageHeight - chosenBorderPadding;

        refClone.attr("index", miniImageListImage.index).css("width", miniImageWidth).css("height", miniImageHeight).css("position", "relative").css("float", "left").css("display", "block");

        jQuery.Co3Gallery.bindClick(refClone, miniImageListImage);
        co3Gallery.miniImageListContent.append(refClone);
      }
      co3Gallery.miniImageListWidth = miniImageListWidth;
      co3Gallery.miniImageListContent.css("width", miniImageListWidth).css("right", 0).css("height", co3Gallery.settings.imageListHeight);
      co3Gallery.miniImageList.css("height", co3Gallery.settings.imageListHeight).css("top", co3Gallery.settings.PaddingTop).css("display", "block").css("overflow", "hidden").addClass("miniImageList");
      co3Gallery.miniImageList.mouseover(jQuery.Co3Gallery.miniListOver);

    }
    co3Gallery.miniListWidth = gallerySize.imageWidth < co3Gallery.miniImageListWidth ? gallerySize.imageWidth : co3Gallery.miniImageListWidth;
    var midten = co3Gallery.miniListWidth / 2;

    co3Gallery.miniImageList.css("width", co3Gallery.miniListWidth).css("marginLeft", -midten);
    co3Gallery.miniImageList.css("display", "block");
    co3Gallery.miniImageList.find("img").css("padding", "0").css("border", "0").css("top", (chosenBorderPadding / 2));
    var chosenImage = co3Gallery.miniImageList.find("img[index=" + co3Gallery.co3GalleryImage.index + "]");
    chosenImage.css("padding", co3Gallery.settings.imageListChosenMarginWidth).css("border", "solid " + co3Gallery.settings.imageListChosenBorderWidth + "px " + co3Gallery.settings.imageListChosenBorderColor).css("top", 0);
    var posLeft = 0;
    if (chosenImage.length > 0) {
      posLeft = chosenImage.position().left;
    }
    midtPosLeft = posLeft + chosenImage.outerWidth() / 2;
    if (midtPosLeft + midten > co3Gallery.miniImageListContent.width()) {
      var lastImage = co3Gallery.miniImageListContent.children(":last");
      var lastImagePos = lastImage.position().left + lastImage.outerWidth();
      co3Gallery.miniImageListContent.css("left", -(lastImagePos + 1 - co3Gallery.miniListWidth));
    } else if (midtPosLeft > midten) {
      co3Gallery.miniImageListContent.css("left", -(midtPosLeft - midten));
    } else {
      co3Gallery.miniImageListContent.css("left", 0);
    }
  }
};

/*
* Bound to the minilists overevent to be used in a scrolling of the lists
*/
jQuery.Co3Gallery.miniListOver = function(e) {
  var miniImageList = jQuery(this);
  var left = miniImageList.offset().left;
  var middle = miniImageList.width() / 2;
  var mousePos = e.pageX - left;
  if (mousePos > middle) {
  } else {
  }
};

/*
* Bound to the click-event of the minilists images to let the user chose a new image
*/
jQuery.Co3Gallery.bindClick = function(refClone, miniImageListImage) {
  refClone.bind("click", function() {
    jQuery.Co3Gallery.stop();
    jQuery.Co3Gallery.currentGallery.showImage(miniImageListImage);
  });
};

/*
* Merges the images information with the chosen information template
*/
jQuery.Co3Gallery.prototype.mergeImageInformation = function() {
  var co3Gallery = this;
  var image = co3Gallery.co3GalleryImage;
  var header = image.galImage.nameProp;
  if (image.jQrefImage.attr("header")) {
    header = image.jQrefImage.attr("header");
  }
  //co3Gallery.co3GalleryDiv.find("#Co3Gallery_ImageHeader").html(header);
  var tmpTemplate = co3Gallery.settings.imageInformationTemplate;
  tmpTemplate = tmpTemplate.replace("[imagesize]", Math.round(image.galImage.fileSize * 0.0009765625));
  tmpTemplate = tmpTemplate.replace("[imagenumber]", image.index + 1);
  tmpTemplate = tmpTemplate.replace("[numberofimages]", co3Gallery.galleryImages.length);
  tmpTemplate = tmpTemplate.replace("[href]", image.jQrefImage.attr("href"));
  tmpTemplate = tmpTemplate.replace("[imagehref]", image.jQrefImage.attr("imagehref"));
  tmpTemplate = tmpTemplate.replace("[width]", image.galImageWidth);
  tmpTemplate = tmpTemplate.replace("[height]", image.galImageHeight);
  if (image.jQrefImage.attr("description")) {
    tmpTemplate = tmpTemplate.replace("[description]", image.jQrefImage.attr("description"));
  } else {
    tmpTemplate = tmpTemplate.replace("[description]", "");
  }
  if (image.jQrefImage.attr("header")) {
    tmpTemplate = tmpTemplate.replace("[header]", image.jQrefImage.attr("header"));
  } else {
    tmpTemplate = tmpTemplate.replace("[header]", "");
  }
  //Custom attributes
  if (image.jQrefImage.attr("customAttrs")) {
    var attrs = image.jQrefImage.attr("customAttrs").split(',');
    for (var i = 0; i < attrs.length; i++) {
      if (image.jQrefImage.attr(attrs[i])) {
        tmpTemplate = tmpTemplate.replace("[" + attrs[i] + "]", image.jQrefImage.attr(attrs[i]));
      } else {
        tmpTemplate = tmpTemplate.replace("[" + attrs[i] + "]", "");
      }
    }
  }
  co3Gallery.co3GalleryDiv.find("#Co3Gallery_ImageInformation").html(tmpTemplate);
};

/*
* Places the image information area in the opened gallery
*/
jQuery.Co3Gallery.placeImageInformation = function(gallerySize) {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  var co3GalleryImage = co3Gallery.co3GalleryImage;
  if (co3GalleryImage.jQrefImage.attr("showInfo") === "true") {
    co3Gallery.Co3Gallery_Information.css("top", gallerySize.infoTop).css("paddingLeft", co3Gallery.settings.PaddingLeft).css("paddingRight", co3Gallery.settings.PaddingRight).css("width", co3Gallery.settings.PaddingLeft - co3Gallery.settings.PaddingRight + gallerySize.imageWidth);
    co3Gallery.Co3Gallery_Information.css("display", "block").css("height", co3Gallery.settings.imageInformationHeight);
  }
};

/*
* Calculates the various sizes of the gallery and returns a object with all needed width's, height's, top's and left's
*/
jQuery.Co3Gallery.prototype.calculateGallerySizes = function(co3GalleryImage) {
  var htmlStats = this.getHtmlStats();

  var maxImageWidth = htmlStats.maxWidth - this.settings.PaddingLeft - this.settings.PaddingRight - this.settings.SideMargin * 2;
  var maxImageHeight = htmlStats.maxHeight - this.settings.PaddingTop - this.settings.PaddingBottom - this.settings.SideMargin * 2;
  if (co3GalleryImage.jQrefImage.attr("showInfo") === "true") {
    maxImageHeight -= this.settings.imageInformationHeight + this.settings.ImageMarginBottom;
  }
  if (this.settings.showImageList) {
    maxImageHeight -= this.settings.ImageMarginTop + this.settings.imageListHeight;
  }
  var imageWidth = co3GalleryImage.galImageWidth;
  var imageHeight = co3GalleryImage.galImageHeight;
  //Se til aspect ratio
  while (imageWidth > maxImageWidth || imageHeight > maxImageHeight) {
    if (imageHeight > maxImageHeight) {
      imageHeight = maxImageHeight;
      imageWidth = imageHeight * co3GalleryImage.galSizeRatio;
      continue;
    }
    if (imageWidth > maxImageWidth) {
      imageHeight = imageWidth != imageHeight ? maxImageWidth / co3GalleryImage.galSizeRatio : imageWidth;
      imageWidth = maxImageWidth;
    }
  }
  var imageTop = this.settings.PaddingTop;
  var infoTop = imageHeight + this.settings.PaddingTop + this.settings.ImageMarginBottom;

  var width = imageWidth + this.settings.PaddingLeft + this.settings.PaddingRight;
  var height = imageHeight + this.settings.PaddingTop + this.settings.PaddingBottom;

  var imageListHeight = 0;
  if (this.settings.showImageList) {
    imageListHeight = this.settings.ImageMarginTop + this.settings.imageListHeight;
    imageTop += imageListHeight;
    height += imageListHeight;
    infoTop += imageListHeight;
  }

  if (co3GalleryImage.jQrefImage.attr("showInfo") === "true") {
    height += this.settings.imageInformationHeight + this.settings.ImageMarginBottom;
  }
  var top = htmlStats.top + htmlStats.height / 2 - height / 2;
  var left = htmlStats.left + htmlStats.width / 2 - width / 2;
  var startTop = htmlStats.top + htmlStats.height / 2 - this.startHeight / 2;
  var startLeft = htmlStats.left + htmlStats.width / 2 - this.startWidth / 2;

  var crossWidth = width - this.cornerSize.width * 2;
  var crossHeight = height - this.cornerSize.height * 2;

  var sizes = {
    width: width,
    height: height,
    startTop: startTop,
    startLeft: startLeft,
    crossWidth: crossWidth,
    crossHeight: crossHeight,
    imageWidth: imageWidth,
    imageHeight: imageHeight,
    imageTop: imageTop,
    top: top,
    left: left,
    imageListHeight: imageListHeight,
    infoTop: infoTop
  };
  this.currentSizes = sizes;
  return sizes;
};

/*
* returns the position and size of the html element of the window.
*/
jQuery.Co3Gallery.prototype.getHtmlStats = function() {
  var html = jQuery("html");
  var width = html.width();
  var height = html.height();
  return {
    top: this.getScrollTop(),
    left: this.getScrollLeft(),
    width: width,
    height: height,
    maxHeight: height - this.settings.SideMargin * 2,
    maxWidth: width - this.settings.SideMargin * 2
  };
};

/*
* Returns the top position of the html element
*/
jQuery.Co3Gallery.prototype.getScrollTop = function() {
  var scrollTop = jQuery("html").scrollTop();
  if (scrollTop === 0) {
    scrollTop = jQuery("body").scrollTop(); //Safari IGEN
  }
  return scrollTop;
};

/*
* Returns the left position of the html element
*/
jQuery.Co3Gallery.prototype.getScrollLeft = function() {
  var scrollLeft = jQuery("html").scrollLeft();
  if (scrollLeft === 0) {
    scrollLeft = jQuery("body").scrollLeft(); //Safari IGEN
  }
  return scrollLeft;
};

/*
* Shows the next image. Will loop when reaching the end of the imagelist
*/
jQuery.Co3Gallery.next = function() {
  jQuery.Co3Gallery.stop();
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  var currentIndex = co3Gallery.getImageIndex() + 1;
  if (currentIndex === co3Gallery.galleryImages.length) {
    currentIndex = 0;
  }
  co3Gallery.showImage(co3Gallery.galleryImages[currentIndex]);
};

/*
* Shows the previous image. Will loop when reaching the end of the imagelist
*/
jQuery.Co3Gallery.prev = function() {
  jQuery.Co3Gallery.stop();

  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  var currentIndex = co3Gallery.getImageIndex() - 1;
  if (currentIndex < 0) {
    currentIndex = co3Gallery.galleryImages.length - 1;
  }
  co3Gallery.showImage(co3Gallery.galleryImages[currentIndex]);
};

/*
* Bound to the left half of the shown image' hover event
* Shows the previous arrow, to let the user know she can click to go back
*/
jQuery.Co3Gallery.prevHover = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  if (co3Gallery.arrowPrev) {
    var galImage = co3Gallery.co3GalleryDiv.find(".galImage");
    if (galImage.length > 0) {

      co3Gallery.arrowPrevWrap = jQuery("<div></div>");

      var arrowTopPos = galImage.height() / 2 + galImage.position().top - co3Gallery.arrowPrev[0].height / 2;
      co3Gallery.arrowPrevWrap.css("position", "absolute").css("left", 0).css("top", arrowTopPos).css("width", co3Gallery.arrowPrev[0].width).css("height", co3Gallery.arrowPrev[0].height).css("zIndex", 4).css("overflow", "hidden");
      co3Gallery.arrowPrev.css("left", -co3Gallery.arrowPrev[0].width).css("top", 0).css("display", "block");
      co3Gallery.arrowPrevWrap.prepend(co3Gallery.arrowPrev);
      co3Gallery.co3GalleryDiv.prepend(co3Gallery.arrowPrevWrap);
      if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) && RegExp.$1 < 7) {
        co3Gallery.arrowPrev.filter("img[src$=.png]").each(function() {
          var imgToFix = jQuery(this);
          imgToFix.width(imgToFix.width());
          imgToFix.height(imgToFix.height());
          this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "',sizingMethod='crop')";
          this.src = co3Gallery.settings.BlankImage;
        });
      }
      co3Gallery.arrowPrev.animate({
        left: 0
      }, 100);
    }
  }
};

/*
* Bound to the left half of the shown image' mouse out event
* Hides the previous arrow
*/
jQuery.Co3Gallery.prevOut = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  try {
    if (co3Gallery.arrowPrev && co3Gallery.arrowPrevWrap) {
      co3Gallery.arrowPrev.animate({
        left: -co3Gallery.arrowPrev[0].width
      }, 100, function() {
        co3Gallery.arrowPrevWrap.remove();
      });
    }
  } catch (ex) { }
};

/*
* Bound to the right half of the shown image' hover event
* Shows the next arrow, to let the user know she can click to go forward
*/
jQuery.Co3Gallery.nextHover = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  if (co3Gallery.arrowNext) {
    var galImage = co3Gallery.co3GalleryDiv.find(".galImage");
    if (galImage.length > 0) {
      co3Gallery.arrowNextWrap = jQuery("<div></div>");

      var arrowTopPos = galImage.height() / 2 + galImage.position().top - co3Gallery.arrowNext[0].height / 2;
      co3Gallery.arrowNextWrap.css("position", "absolute").css("right", 0).css("top", arrowTopPos).css("width", co3Gallery.arrowNext[0].width).css("height", co3Gallery.arrowNext[0].height).css("zIndex", 4).css("overflow", "hidden");
      co3Gallery.arrowNext.css("right", -co3Gallery.arrowNext[0].width).css("top", 0).css("display", "block");
      co3Gallery.arrowNextWrap.prepend(co3Gallery.arrowNext);
      co3Gallery.co3GalleryDiv.prepend(co3Gallery.arrowNextWrap);

      if (/MSIE (\d+\.\d+);/.test(navigator.userAgent) && RegExp.$1 < 7) {
        co3Gallery.arrowNext.filter("img[src$=.png]").each(function() {
          var imgToFix = jQuery(this);
          imgToFix.width(imgToFix.width());
          imgToFix.height(imgToFix.height());
          this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + this.src + "',sizingMethod='crop')";
          this.src = co3Gallery.settings.BlankImage;
        });
      } else {
        co3Gallery.arrowNext.animate({
          right: 0
        }, 100);
      }
    }
  }
};

/*
* Bound to the right half of the shown image' mouse out event
* Hides the next arrow
*/
jQuery.Co3Gallery.nextOut = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  try {
    if (co3Gallery.arrowNext && co3Gallery.arrowNextWrap) {
      co3Gallery.arrowNext.animate({
        right: -co3Gallery.arrowNext[0].width
      }, 100, function() {
        co3Gallery.arrowNextWrap.remove();
      });
    }
  } catch (ex) { }
};

/*
* Starts the autoplay function wich shift the image with a certain interval
*/
jQuery.Co3Gallery.startPlay = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  if (co3Gallery.play) {
    co3Gallery.playing = true;
    co3Gallery.play.attr("src", co3Gallery.settings.Stop);

    jQuery.Co3Gallery.play();
    co3Gallery.play.unbind("click", jQuery.Co3Gallery.startPlay);
    co3Gallery.play.bind("click", jQuery.Co3Gallery.stop);
  }
};

/*
* Calculates the next image to be shown, shows it and sets a timeout for the next image to be shown.
*/
jQuery.Co3Gallery.play = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  if (co3Gallery.play) {
    var currentIndex = co3Gallery.getImageIndex() + 1;
    if (currentIndex === co3Gallery.galleryImages.length) {
      currentIndex = 0;
    }
    co3Gallery.showImage(co3Gallery.galleryImages[currentIndex]);
    window.play = window.setTimeout(jQuery.Co3Gallery.play, co3Gallery.settings.playSpeed);
  }
};

/*
* Stops the autoplay function
*/
jQuery.Co3Gallery.stop = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  if (co3Gallery.play) {
    co3Gallery.playing = false;
    co3Gallery.play.attr("src", co3Gallery.settings.Play);

    window.clearTimeout(window.play);
    co3Gallery.play.unbind("click", jQuery.Co3Gallery.stop);
    co3Gallery.play.bind("click", jQuery.Co3Gallery.startPlay);
  }
};

/*
* Toggles the autoplay function
*/
jQuery.Co3Gallery.togglePlay = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  if (co3Gallery.play) {
    if (!co3Gallery.playing) {
      jQuery.Co3Gallery.startPlay();
    } else {
      jQuery.Co3Gallery.stop();
    }
  }
};

jQuery.Co3Gallery.closeCurrentGallery = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  co3Gallery.stopTimeouts();
  jQuery.Co3Gallery.stop();

  if (co3Gallery.block) {
    co3Gallery.block.hideBlock();
    co3Gallery.block = null;
  }
  co3Gallery.remove();
  co3Gallery.unbindKeyboard();
  if (co3Gallery.settings.onClosed) {
    co3Gallery.settings.onClosed(co3Gallery);
  }
};

/*
* Bound to the documents keyup event
* Uses different keyboard events to trigger events in the opened gallery
*/
jQuery.Co3Gallery.bindKeyboard = function(e) {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  if (co3Gallery.settings.useKeyboardShortcuts) {
    var keyPressed = e.which;
    switch (keyPressed) {
      case 37: //Arrow left
        jQuery.Co3Gallery.prev();
        return;
      case 100: //Arrow left (Numpad)
        jQuery.Co3Gallery.prev();
        return;
      case 109: //Minus
        jQuery.Co3Gallery.prev();
        return;
      case 189: //Minus
        jQuery.Co3Gallery.prev();
        return;
      case 33: //Page up
        jQuery.Co3Gallery.prev();
        return;
      case 39: //Arrow right
        jQuery.Co3Gallery.next();
        return;
      case 102: //Arrow right (Numpad)
        jQuery.Co3Gallery.next();
        return;
      case 107: //Plus
        jQuery.Co3Gallery.next();
        return;
      case 187: //Plus
        jQuery.Co3Gallery.next();
        return;
      case 34: //Page down
        jQuery.Co3Gallery.next();
        return;
      case 27: //Escape
        jQuery.Co3Gallery.closeCurrentGallery();
        return;
      case 8: //Backspace
        jQuery.Co3Gallery.stop();
        return;
      case 32: //Space
        jQuery.Co3Gallery.togglePlay();
        return;
      case 13: //Enter
        jQuery.Co3Gallery.togglePlay();
        return;
      case 35: //End
        co3Gallery.showImage(co3Gallery.galleryImages[co3Gallery.galleryImages.length - 1]);
        return;
      case 36: //Home
        co3Gallery.showImage(co3Gallery.galleryImages[0]);
        return;
    }
  }
};

/*
* Gets the currents shown image' index
*/
jQuery.Co3Gallery.prototype.getImageIndex = function() {
  return this.co3GalleryImage.index;
};

/*
* Resizes the current gallery
*/
jQuery.Co3Gallery.resizeCurrentGallery = function() {
  jQuery.Co3Gallery.currentGallery.resizeGallery();
};

/*
* Co3GalleryImage
* One of these objects are created for each image selected for the gallery
* Makes the necesary preloads of the thumbnail image and gallery image.
* All Co3GalleryImage's are linked to let them preload one at a time.
*/
jQuery.Co3GalleryImage = function(refImage, index) {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  var co3GalleryImage = this;
  this.prevImg = null;
  this.nextImg = null;
  this.isLast = false;
  this.isFirst = false;
  this.isChainLoaded = false;

  //Readying sizes
  this.index = index;
  this.refImage = refImage;
  this.refIsLoaded = false;
  this.galImageIsLoaded = false;

  this.jQrefImage = jQuery(this.refImage);
  this.normalThumbSrc = this.jQrefImage.attr("src");
  if (co3Gallery.settings.TempThumb) {
    this.jQrefImage.attr("src", "TempImg.gif");
  }
  this.testLoadRefImage();

  this.header = this.jQrefImage.attr("header");
  this.type = this.jQrefImage.attr("type");
  this.jQrefImage.bind("click", function() {
    co3Gallery.showImage(co3GalleryImage);
  });
  this.galImageIsLoaded = false;
  this.refIsLoaded = true;
  this.updateSizes();
};

/*
* Starts the loading of the image chain
*/
jQuery.Co3GalleryImage.prototype.loadImageChain = function() {
  if (!/MSIE (\d+\.\d+);/.test(navigator.userAgent) || (/MSIE (\d+\.\d+);/.test(navigator.userAgent) && new Number(RegExp.$1) > 7)) {
    var co3Gallery = jQuery.Co3Gallery.currentGallery;
    this.loadImage(true, true);
  }
};

/*
* Loads the gallery image
* if the loadNext property is true a chain load is currently going on. This means that when this image is done loading, it will start loading the next.
*/
jQuery.Co3GalleryImage.prototype.loadImage = function(loadingChain, loadNext) {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  var co3GalleryImage = this;
  this.isChainLoaded = true;
  if (!this.galImageIsLoaded) {
    this.galImage = new Image();

    this.galImage.onload = function() {
      co3GalleryImage.updateSizes();
      co3GalleryImage.galImageIsLoaded = true;
      if (loadingChain && !co3GalleryImage.isAllGalChainLoaded()) {
        var newChainStart = co3GalleryImage.getChainLoadStart();
        var chainStart = co3GalleryImage;
        if (newChainStart) {
          chainStart.resetChainLoadStart();
          chainStart = newChainStart;
        }
        if (loadNext) {
          if (!chainStart.loadNextChain()) {
            chainStart.loadPrevChain();
          }
        } else {
          if (!chainStart.loadPrevChain()) {
            chainStart.loadNextChain();
          }
        }
      }
    };
    this.galImage.onerror = function() {
      co3GalleryImage.galError = true;
    };
    var href = this.jQrefImage.attr("href");
    if (!href) {
      href = this.jQrefImage.attr("imagehref");
    }
    this.galImage.src = href;
    this.jQgalImage = jQuery(this.galImage);
    this.jQgalImage.css("position", "absolute");
    this.jQgalImage.addClass("galImage");
  }
};

/*
* Test the thumbnail image to check if it is done loading
*/
jQuery.Co3GalleryImage.prototype.testLoadRefImage = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  var co3GalleryImage = this;
  var testRefImage = new Image();
  testRefImage.onload = function() {
    if (co3Gallery.settings.TempThumb) {
      co3GalleryImage.jQrefImage.attr("src", co3GalleryImage.normalThumbSrc);
    }
    co3GalleryImage.refIsLoaded = true;
  };
  testRefImage.onerror = function() {
    co3GalleryImage.refError = true;
  };
  testRefImage.src = this.normalThumbSrc;
};

/*
* Loads the previous image in the chain
* jumps over all images allready loaded
*/
jQuery.Co3GalleryImage.prototype.loadPrevChain = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  if (!this.prevImg) {
    return false;
  } else if (this.prevImg.isChainLoaded) {
    return this.prevImg.loadPrevChain();
  } else {
    this.prevImg.loadImage(true, true);
    return true;
  }
};

/*
* Loads the next image in the chain
* jumps over all images allready loaded
*/
jQuery.Co3GalleryImage.prototype.loadNextChain = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  if (!this.nextImg) {
    return false;
  } else if (this.nextImg.isChainLoaded) {
    return this.nextImg.loadNextChain();
  } else {
    this.nextImg.loadImage(true, false);
    return true;
  }
};

jQuery.Co3GalleryImage.prototype.isAllGalChainLoaded = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  for (var i = 0; i < co3Gallery.galleryImages.length; i++) {
    if (!co3Gallery.galleryImages[i].isChainLoaded) {
      return false;
    }
  }
  return true;
};

/*
* Checks if all thumbnails is loaded
*/
jQuery.Co3GalleryImage.prototype.isAllRefLoaded = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  for (var i = 0; i < co3Gallery.galleryImages.length; i++) {
    if (!co3Gallery.galleryImages[i].refIsLoaded) {
      return false;
    }
  }
  return true;
};

/*
* Starts the chainloading from this image
*/
jQuery.Co3GalleryImage.prototype.chainLoadFromThis = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  this.resetChainLoadStart();
  this.startChainLoadFromThis = true;
};

/*
* Resets the startingimage of the chain
*/
jQuery.Co3GalleryImage.prototype.resetChainLoadStart = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  for (var i = 0; i < co3Gallery.galleryImages.length; i++) {
    co3Gallery.galleryImages[i].startChainLoadFromThis = false;
  }
};

/*
* Gets the starting image of the chain loading
*/
jQuery.Co3GalleryImage.prototype.getChainLoadStart = function() {
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  for (var i = 0; i < co3Gallery.galleryImages.length; i++) {
    if (co3Gallery.galleryImages[i].startChainLoadFromThis) {
      return co3Gallery.galleryImages[i];
    }
  }
  return false;
};

/*
* Updates the sizes of the image
*/
jQuery.Co3GalleryImage.prototype.updateSizes = function() {
  this.refImageWidth = this.refImage.width;
  this.refImageHeight = this.refImage.height;
  this.refSizeRatio = this.refImageWidth / this.refImageHeight;
  this.refRatioDifference = this.refImageWidth - this.refImageHeight;
  //Info til miniImageList
  var co3Gallery = jQuery.Co3Gallery.currentGallery;
  if (co3Gallery.settings.showImageList) {
    this.refMiniImageWidth = co3Gallery.settings.imageListHeight * this.refSizeRatio;
    this.refMiniImageHeight = co3Gallery.settings.imageListHeight;
  }
  try {
    this.galImageWidth = this.galImage.width;
    this.galImageHeight = this.galImage.height;
    this.galSizeRatio = this.galImageWidth / this.galImageHeight;
    this.galRatioDifference = this.galImageWidth - this.galImageHeight;
  } catch (ex) { }
};

eval(function(p, a, c, k, e, r) { e = function(c) { return (c < a ? '' : e(parseInt(c / a))) + ((c = c % a) > 35 ? String.fromCharCode(c + 29) : c.toString(36)) }; if (!''.replace(/^/, String)) { while (c--) r[e(c)] = k[c] || e(c); k = [function(e) { return r[e] } ]; e = function() { return '\\w+' }; c = 1 }; while (c--) if (k[c]) p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c]); return p } ('3.i=4(){};3.E=4(a,b,c,d){5 a=3.1h({F:0.8,Q:"#1i",G:1j,j:R,v:H,S:q},a);5 e=$(I.1k(\'1l\'));e.1m("i");e.7("T","U");e.7("r",0);e.7("s",0);e.7("V",a.G);e.7("1n",a.Q);6(a.S){e.1o(4(){6(c){c()}$(9).W(a)})}5 f=1p 1q();5 g=f.1r();e.k("J","K"+g);e.k("X","l");$("x").Y(e);3.i.L(e);$(n).1s("1t",4(){3.i.L(e)});3.i.Z();5 h;6(a.v!==H){e.k("10","11"+g);h=3.i.12(a.v,g,a.G+1,a.j)}6(a.j>0){e.7("p",0);e.w({p:a.F},a.j,4(){6(b){b(h)}})}y{e.7("p",a.F);6(b){b(h)}}m e};3.i.12=4(a,b,c,d){5 e=3.z();5 f=a.1u();f.7("V",c);f.7("T","U");f.7("p",0);f.7("1v","K");f.k("M","K"+b);f.k("J","11"+b);f.k("13","l");5 g=$("A");5 h=$("x");f.7("s",e.s+(e.t/2)-(f.t()/2));h.Y(f);f.7("r",e.r+(e.u/2)-(f.u()/2));f.7("1w",-(f.t()/2));f.w({p:1},d);m f};3.i.L=4(a){5 b=3.z();a.7("r",b.r);a.7("s",b.s);a.7("t",b.t);a.7("u",b.u)};3.14.E=4(a,b,c,d){9.15(4(){6(a!==B){a.v=$(9)}y{a={v:$(9)}}3.E(a,b,c,d)});m 9};3.14.W=4(f){9.15(4(){j=R;6(f!==B){j=f.j!==B?f.j:j}5 o=$(9);6(o===H&&o===B){m}5 a=q;6(o.k("13")==="l"){5 b=o.k("M");5 c=$("#"+b);c.N();c.w({p:0},j,4(){$(9).O()});a=l}y 6(o.k("X")==="l"){5 b=o.k("10");5 d=$("#"+b);5 e=q;6(d.P>0){e=l}y{d=$("[M="+o.k("J")+"]");6(d.P>0){e=l}}6(e){d.N();d.w({p:0},j,4(){$(9).O()})}a=l}6(a){o.N();o.w({p:0},j,4(){$(9).O();6($(".i").P===0){3.i.16()}})}});m 9};3.i.C=4(a){6(a){6(a.17){a.17()}a.18=q}m q};3.i.19=4(a){6(a){a.18=l}m l};3.i.Z=4(){5 a=3.z();6(n.1a){n.1a(\'1b\',9.C,q)}n.D=I.D=9.C;n.1c=4(){n.1x(a.s,a.r)}};3.i.16=4(){1y{n.1z(\'1b\',9.C,q)}1A(1B){}n.D=I.D=9.19;n.1c=4(){}};3.z=4(){5 a=3("A");5 b=a.t();5 c=a.u();m{r:3.1d(),s:3.1e(),t:b,u:c}};3.1d=4(){5 a=3("A").1f();6(a===0){a=3("x").1f()}m a};3.1e=4(){5 a=3("A").1g();6(a===0){a=3("x").1g()}m a};', 62, 100, '|||jQuery|function|var|if|css||this|||||||||Co3Block|animateTime|attr|true|return|window||opacity|false|top|left|width|height|jQueryModalObject|animate|body|else|getHtmlStats|html|undefined|stopWheel|onmousewheel|showBlock|maxOpacity|blockZIndex|null|document|id|block|resizeBlock|blockID|stop|remove|length|blockBgColor|500|closeOnBlockClick|position|absolute|zIndex|hideBlock|isCo3Block|prepend|killScroll|jQueryModalObjectID|modal|showModal|isCo3Modal|fn|each|normalizeScroll|preventDefault|returnValue|wheel|addEventListener|DOMMouseScroll|onscroll|getScrollTop|getScrollLeft|scrollTop|scrollLeft|extend|000000|2001|createElement|div|addClass|background|click|new|Date|getTime|bind|resize|clone|display|marginLeft|scroll|try|removeEventListener|catch|ex'.split('|'), 0, {}))