')
.appendTo($('#hermitage'))
.hide()
.attr(hermitage.bottomPanel.default.attributes)
.css(hermitage.bottomPanel.default.styles)
.css(hermitage.bottomPanel.styles)
text = $('
')
.appendTo(bottomPanel)
.attr(hermitage.bottomPanel.text.default.attributes)
.css(hermitage.bottomPanel.text.default.styles)
.css(hermitage.bottomPanel.text.styles)
# Shows original image of the chosen one
openGallery = (image) ->
$('#hermitage')
.css('z-index', hermitage.zIndex)
.empty()
.show()
createDarkening()
createRightNavigationButton()
createLeftNavigationButton()
createCloseButton()
createBotomPanel()
showImage(indexOfImage(image))
# Shows image with specified index from images array
showImage = (index) ->
img = $('
![]()
')
.attr(hermitage.image.default.attributes)
.css(hermitage.image.default.styles)
.attr('src', hermitage.images[index])
.css(hermitage.image.styles)
.hide()
.appendTo($('#hermitage'))
img.click (event) ->
if event.pageX >= $(window).width() / 2
showNextImage()
else
showPreviousImage()
adjustImage(false, img)
# Shows next image
showNextImage = ->
current = $('img.current')
if current.length is 1
index = indexOfImage(current)
return unless canShowNextAfter(index)
hideCurrentImage()
if index < hermitage.images.length - 1
showImage(index + 1)
else
showImage(0)
# Shows previous image
showPreviousImage = ->
current = $('img.current')
if current.length is 1
index = indexOfImage(current)
return unless canShowPreviousBefore(index)
hideCurrentImage()
if index > 0
showImage(index - 1)
else
showImage(hermitage.images.length - 1)
# Hides current image
hideCurrentImage = ->
current = $('img.current')
if current.length is 1
current.fadeOut hermitage.animationDuration, ->
current.remove()
# Starts fade out animation and clears Hermitage at the end of animation
closeGallery = ->
$('#hermitage :not(#overlay)').fadeOut()
$('#overlay').fadeOut hermitage.animationDuration, ->
$('#hermitage').hide()
.empty()
# Moves image to correct position and sets correct size.
# Then it calls adjusting methods for navigation and close buttons.
# Attributes:
# * `withAnimation` - boolean value determines if adjusting should be animated
# * `image` - currently opened image. It is optional argument and can be evaluated by the method itself.
adjustImage = (withAnimation = false, image = undefined) ->
if image is undefined
image = $('#hermitage img.current')
return unless image.length is 1
index = indexOfImage(image)
# Wait until source image is loaded
$('
![]()
').attr('src', hermitage.images[index]).load ->
# Offset for image position
offsetY = 0
maxWidth = $(window).width() - $('#navigation-left').outerWidth() - $('#navigation-right').outerWidth()
maxHeight = $(window).height()
text = hermitage.texts[index]
if text
offsetY = - $('#hermitage .bottom-panel').outerHeight() / 2
maxHeight -= $('#hermitage .bottom-panel').outerHeight()
$('#hermitage .bottom-panel .text').text(text or '')
if maxWidth <= hermitage.minimumSize.width or maxHeight <= hermitage.minimumSize.height
if maxWidth < maxHeight
maxWidth = hermitage.minimumSize.width
maxHeight = maxWidth * (this.height / this.width)
else
maxHeight = hermitage.minimumSize.height
maxWidth = maxHeight * (this.width / this.height)
scale = 1.0
if this.width > maxWidth or this.height > maxHeight
scale = Math.min(maxWidth / this.width, maxHeight / this.height)
image
.setSize(this.width * scale, this.height * scale, withAnimation)
.center(withAnimation, this.width * scale, this.height * scale, 0, offsetY)
.fadeIn(hermitage.animationDuration)
adjustNavigationButtons(withAnimation, image)
adjustCloseButton(withAnimation, image)
adjustBottomPanel(withAnimation)
# Moves navigation buttons to proper positions
adjustNavigationButtons = (withAnimation, current) ->
return unless hermitage.navigationButtons.enabled
previous = $('#hermitage #navigation-left')
next = $('#hermitage #navigation-right')
# Set correct styles
previous.maximizeLineHeight(withAnimation)
next.maximizeLineHeight(withAnimation)
# Show or hide buttons
currentIndex = indexOfImage(current)
duration = hermitage.animationDuration
if canShowPreviousBefore(currentIndex)
previous.fadeIn(duration)
else
previous.fadeOut(duration)
if canShowNextAfter(currentIndex)
next.fadeIn(duration)
else
next.fadeOut(duration)
# Moves close button to proper position
adjustCloseButton = (withAnimation, current) ->
return unless hermitage.closeButton.enabled
button = $('#hermitage #close-button')
if button.css('display') is 'none'
button.fadeIn(hermitage.animationDuration)
adjustBottomPanel = (withAnimation) ->
panel = $('#hermitage .bottom-panel')
if panel.text() is ''
panel.fadeOut(hermitage.animationDuration)
else
params =
width: "#{$(window).width() - $('#navigation-left').outerWidth() - $('#navigation-right').outerWidth()}px"
left: "#{$('#navigation-left').position().left + $('#navigation-left').outerWidth()}px"
if withAnimation
panel.animate(params, { duration: hermitage.animationDuration, queue: false })
else
panel.css(params)
panel.fadeIn(hermitage.animationDuration)
indexOfImage = (image) ->
href = if $(image).prop('tagName') is 'IMG' then $(image).attr('src') else $(image).attr('href')
hermitage.images.indexOf(href)
canShowNextAfter = (index) ->
if index < hermitage.images.length - 1
true
else
hermitage.looped
canShowPreviousBefore = (index) ->
if index > 0
true
else
hermitage.looped
# Initialize gallery on page load
$(document).ready(hermitage.init)
$(document).on('page:load', hermitage.init)