/*
 Product name: MX Lightbox
 Product version: 1.0
 Product description: JavaScript Image Lightbox using jQuery
 Product URL: http://blog.marextafl.cz/mx-lightbox-jquery-lightbox/
 Author name: Marek Štafl
 Author URL: http://blog.marextafl.cz/
 Author contact: marex@marextafl.cz
 License URL: http://blog.marextafl.cz/mx-lightbox-license/
 Help URL: http://blog.marextafl.cz/mx-lightbox-help/
*/
var mx_windowSize = new Array(),
    mx_thumbnailPosition = new Array(),
    mx_thumbnailSize = new Array(),
    mx_bigImageOriginalSize = new Array(),
    mx_bigImageFitSize = new Array(),
    mx_windowScroll = new Array(),
    mx_bigImageUrl, mx_bigImageDesc = '', mx_overlayShow = 0, mx_bigImageShow = 0, mx_currentElement, mx_resizeTimer = false, mx_curElementIndex,
    // NASTAVENÍ
    mx_border = 5, mx_horizontal_space = 50, mx_vertical_space = 50, mx_resize_animation = 2, mx_animation_speed = 10,
    mx_min_image_width = 200, mx_min_image_height = 200, mx_fullimage_target = '_blank', mx_wait_text = 'Na&#269;&iacute;t&aacute;m...',
    mx_title_close = 'Zav&#345;&iacute;t', mx_title_fullsize = 'Zobrazit obr&aacute;zek v pln&eacute; velikosti',
    mx_title_next = 'Dal&scaron;&iacute; &raquo;', mx_title_previous = 'P&#345;edchoz&iacute; &laquo;', mx_image_path = '',
    mx_win_size_error = 'Velikost okna Va&scaron;eho prohl&iacute;&#382;e&#269;e nedovoluje zobrazit n&aacute;hled obr&aacute;zku v minim&aacute;ln&iacute; povolen&eacute; velikosti! Zv&#283;t&scaron;ete, pros&iacute;m, okno prohl&iacute;&#382;e&#269;e.';

function mx_showOtherResize (fullIcon) {
  if (fullIcon == 1) {
    var mx_prevIcon = '#mxLightboxIconFull';
    $('#mxLightboxIconFull').css({
      top: $('#mxLightboxIconClose').position().top+$('#mxLightboxIconClose').height()+10,
      left: $('#mxLightboxIconClose').position().left
    }).animate({
      opacity: 1
    }, 200*mx_animation_speed);
  } else {
    var mx_prevIcon = '#mxLightboxIconClose';
    $('#mxLightboxIconFull').animate({opacity: 0}, 200*mx_animation_speed, function() {
      $(this).css({left: -10000, top: -10000});
    });
  }
  if ($('a.lightbox')[mx_curElementIndex+1] && mx_curElementIndex > 0) {
    $('#mxLightboxIconPrev').animate({
      top: $(mx_prevIcon).position().top+$(mx_prevIcon).height()+10,
      left: $('#mxLightboxIconClose').position().left
    }, 200*mx_animation_speed, function() {
      $('#mxLightboxIconNext').animate({
        top: $('#mxLightboxIconPrev').position().top+$('#mxLightboxIconPrev').height()+10,
        left: $('#mxLightboxIconClose').position().left
      }, 200*mx_animation_speed, function() {
        mx_windowSize['x'] = $(window).width();
        mx_windowSize['y'] = $(window).height();
        $('div#mxLightboxOverlay').css({
          width: mx_windowSize['x']+mx_windowScroll['x'],
          height: mx_windowSize['y']+mx_windowScroll['y']
        });
      });
    });
  } else
  if ($('a.lightbox')[mx_curElementIndex+1]) {
    $('#mxLightboxIconNext').animate({
      top: $(mx_prevIcon).position().top+$(mx_prevIcon).height()+10,
      left: $('#mxLightboxIconClose').position().left
    }, 200*mx_animation_speed, function() {
      mx_windowSize['x'] = $(window).width();
      mx_windowSize['y'] = $(window).height();
      $('div#mxLightboxOverlay').css({
        width: mx_windowSize['x']+mx_windowScroll['x'],
        height: mx_windowSize['y']+mx_windowScroll['y']
      });
    });
  } else
  if (mx_curElementIndex > 0) {
    $('#mxLightboxIconPrev').animate({
      top: $(mx_prevIcon).position().top+$(mx_prevIcon).height()+10,
      left: $('#mxLightboxIconClose').position().left
    }, 200*mx_animation_speed, function() {
      mx_windowSize['x'] = $(window).width();
      mx_windowSize['y'] = $(window).height();
      $('div#mxLightboxOverlay').css({
        width: mx_windowSize['x']+mx_windowScroll['x'],
        height: mx_windowSize['y']+mx_windowScroll['y']
      });
    });
  }
}

function mx_showBigImageImgResize () {
  $('#mxLightboxImg').animate({
    width: mx_bigImageFitSize['x'],
    height: mx_bigImageFitSize['y'],
    left: mx_border,
    top: mx_border
  }, 300*mx_animation_speed, function() {
    $('#mxLightboxImageDesc').animate({
      width: mx_windowSize['x']-20,
      top: mx_bigImageFitSize['y']+2*mx_border,
      left: Math.round(mx_windowSize['x']/2-(mx_bigImageFitSize['x']+2*mx_border)/2)*(-1)+10
    }, 500*mx_animation_speed, function() {
      $('#mxLightboxIconClose').animate({
        top: 20,
        left: mx_bigImageFitSize['x']
      },200*mx_animation_speed, function() {
        if (mx_bigImageFitSize['x'] != mx_bigImageOriginalSize['x'] || mx_bigImageFitSize['y'] != mx_bigImageOriginalSize['y']) {
          $('#mxLightboxIconFull').attr('href', mx_bigImageUrl).attr('target', mx_fullimage_target).animate({
            top: $('#mxLightboxIconClose').position().top+$('#mxLightboxIconClose').height()+10,
            left: $('#mxLightboxIconClose').position().left
          }, 200*mx_animation_speed, function() {
            mx_showOtherResize(1);
          });
        } else {
          mx_showOtherResize(0);
        }
      });
    });
  });
}

function mx_resize () {
  if (mx_resizeTimer) {
    clearTimeout(mx_resizeTimer);
  }
  if (mx_bigImageShow == 1) {
    var mx_fSizeX = mx_windowSize['x']-2*mx_horizontal_space-2*mx_border;
    var mx_fSizeY = mx_windowSize['y']-2*mx_vertical_space-2*mx_border;
    if (mx_bigImageOriginalSize['x'] > mx_fSizeX || mx_bigImageOriginalSize['y'] > mx_fSizeY) {
      //alert ('je větší');
      if (mx_fSizeX/mx_fSizeY > mx_bigImageOriginalSize['x']/mx_bigImageOriginalSize['y']) {
        var mx_ratio = mx_fSizeY/mx_bigImageOriginalSize['y'];
        mx_bigImageFitSize['y'] = mx_fSizeY;
        mx_bigImageFitSize['x'] = mx_bigImageOriginalSize['x']*mx_ratio;
      } else {
        var mx_ratio = mx_fSizeX/mx_bigImageOriginalSize['x'];
        mx_bigImageFitSize['x'] = mx_fSizeX;
        mx_bigImageFitSize['y'] = mx_bigImageOriginalSize['y']*mx_ratio;
      }
    } else {
      mx_bigImageFitSize['x'] = mx_bigImageOriginalSize['x'];
      mx_bigImageFitSize['y'] = mx_bigImageOriginalSize['y'];
    }
    // ANIMACE ROZTAŽENÍ
    if (mx_resize_animation == 1) { // OBA SMĚRY SOUČASNĚ
      $('#mxLightboxImageBackground').animate({
        top: Math.round(mx_windowScroll['y']+mx_windowSize['y']/2-mx_bigImageFitSize['y']/2-mx_border),
        height: mx_bigImageFitSize['y']+2*mx_border,
        left: Math.round(mx_windowScroll['x']+mx_windowSize['x']/2-mx_bigImageFitSize['x']/2-mx_border),
        width: mx_bigImageFitSize['x']+2*mx_border
      }, 500*mx_animation_speed, mx_showBigImageImgResize);
    } else
    if (mx_resize_animation == 2) { // NEJPRVE VERTIKÁLNĚ
      $('#mxLightboxImageBackground').animate({
        top: Math.round(mx_windowScroll['y']+mx_windowSize['y']/2-mx_bigImageFitSize['y']/2-mx_border),
        height: mx_bigImageFitSize['y']+2*mx_border
      }, 500*mx_animation_speed)
      .animate({
        left: Math.round(mx_windowScroll['x']+mx_windowSize['x']/2-mx_bigImageFitSize['x']/2-mx_border),
        width: mx_bigImageFitSize['x']+2*mx_border
      }, 500*mx_animation_speed, mx_showBigImageImgResize);
    } else
    if (mx_resize_animation == 3) { // NEJPRVE HORIZONTÁLNĚ
      $('#mxLightboxImageBackground').animate({
        left: Math.round(mx_windowScroll['x']+mx_windowSize['x']/2-mx_bigImageFitSize['x']/2-mx_border),
        width: mx_bigImageFitSize['x']+2*mx_border
      }, 500*mx_animation_speed)
      .animate({
        top: Math.round(mx_windowScroll['y']+mx_windowSize['y']/2-mx_bigImageFitSize['y']/2-mx_border),
        height: mx_bigImageFitSize['y']+2*mx_border
      }, 500*mx_animation_speed, mx_showBigImageImgResize);
    } else { // NEZADÁNO - NEJPRVE VERTIKÁLNĚ
      $('#mxLightboxImageBackground').animate({
        top: Math.round(mx_windowScroll['y']+mx_windowSize['y']/2-mx_bigImageFitSize['y']/2-mx_border),
        height: mx_bigImageFitSize['y']+2*mx_border
      }, 500*mx_animation_speed)
      .animate({
        left: Math.round(mx_windowScroll['x']+mx_windowSize['x']/2-mx_bigImageFitSize['x']/2-mx_border),
        width: mx_bigImageFitSize['x']+2*mx_border
      }, 500*mx_animation_speed, mx_showBigImageImgResize);
    }
  }
}

function mx_showOther (fullIcon) {
  if (fullIcon == 1) {
    var mx_prevIcon = '#mxLightboxIconFull';
  } else {
    var mx_prevIcon = '#mxLightboxIconClose';
  }
  if ($('a.lightbox')[mx_curElementIndex+1] && mx_curElementIndex > 0) {
    $('#mxLightboxIconPrev').attr('href', $('a.lightbox:eq('+(mx_curElementIndex-1)+')').attr('href')).css({
      top: $(mx_prevIcon).position().top+$(mx_prevIcon).height()+10,
      left: $('#mxLightboxIconClose').position().left
    })
    .animate({
      opacity: 1
    }, 200*mx_animation_speed, function() {
      $('#mxLightboxIconNext').attr('href', $('a.lightbox:eq('+(mx_curElementIndex+1)+')').attr('href')).css({
        top: $('#mxLightboxIconPrev').position().top+$('#mxLightboxIconPrev').height()+10,
        left: $('#mxLightboxIconClose').position().left
      })
      .animate({
        opacity: 1
      }, 200*mx_animation_speed);
    });
  } else
  if ($('a.lightbox')[mx_curElementIndex+1]) {
    $('#mxLightboxIconNext').attr('href', $('a.lightbox:eq('+(mx_curElementIndex+1)+')').attr('href')).css({
      top: $(mx_prevIcon).position().top+$(mx_prevIcon).height()+10,
      left: $('#mxLightboxIconClose').position().left
    })
    .animate({
      opacity: 1
    }, 200*mx_animation_speed);
  } else
  if (mx_curElementIndex > 0) {
    $('#mxLightboxIconPrev').attr('href', $('a.lightbox:eq('+(mx_curElementIndex-1)+')').attr('href')).css({
      top: $(mx_prevIcon).position().top+$(mx_prevIcon).height()+10,
      left: $('#mxLightboxIconClose').position().left
    })
    .animate({
      opacity: 1
    }, 200*mx_animation_speed);
  }
}

function mx_showBigImageImg () {
  $('#mxLightboxImg').css({
    width: mx_bigImageFitSize['x'],
    height: mx_bigImageFitSize['y'],
    left: mx_border,
    top: mx_border
  })
  .animate({
    opacity: 1
  }, 300*mx_animation_speed, function() {
    $('#mxLightboxImageDesc').text(mx_bigImageDesc)
    .css({
      top: Math.round((mx_bigImageFitSize['y']+2*mx_border)/2),
      left: Math.round(mx_windowSize['x']/2-(mx_bigImageFitSize['x']+2*mx_border)/2)*(-1)+10
    })
    .animate({
      opacity: 1,
      fontSize: 24,
      top: mx_bigImageFitSize['y']+2*mx_border
    }, 1000*mx_animation_speed, function() {
      $('#mxLightboxIconClose').css({
        top: 20,
        left: mx_bigImageFitSize['x']
      })
      .animate({
        opacity: 1
      },200*mx_animation_speed, function() {
        if (mx_bigImageFitSize['x'] != mx_bigImageOriginalSize['x'] || mx_bigImageFitSize['y'] != mx_bigImageOriginalSize['y']) {
          $('#mxLightboxIconFull').attr('href', mx_bigImageUrl).attr('target', mx_fullimage_target).css({
            top: $('#mxLightboxIconClose').position().top+$('#mxLightboxIconClose').height()+10,
            left: $('#mxLightboxIconClose').position().left
          })
          .animate({
            opacity: 1
          }, 200*mx_animation_speed, function() {
            mx_showOther(1);
          });
        } else {
          mx_showOther(0);
        }
      });
    });
  });
}

function mx_showBigImage () {
  mx_bigImageShow = 1;
  $('#mxLightboxImageBackground').prepend('<img id="mxLightboxImg" style="position: absolute; left: -100000px; top: -100000px;" src="">\n');
  $('#mxLightboxImageDesc').css({
    width: mx_windowSize['x']-20,
    height: 'auto',
    left: 10,
    opacity: 0,
    fontSize: 48
  });
  $('.mxLightboxIcon').css({
    opacity: 0
  });
  $('#mxLightboxImg').attr('src', mx_bigImageUrl).css({
    width: 'auto',
    height: 'auto',
    opacity: 0
  })
  .load(function() {
    $('#mxLightboxWait').css({
      opacity: 0,
      top: -100000
    });
    mx_bigImageOriginalSize['x'] = $(this).width();
    mx_bigImageOriginalSize['y'] = $(this).height();
    var mx_fSizeX = mx_windowSize['x']-2*mx_horizontal_space-2*mx_border;
    var mx_fSizeY = mx_windowSize['y']-2*mx_vertical_space-2*mx_border;
    if (mx_bigImageOriginalSize['x'] > mx_fSizeX || mx_bigImageOriginalSize['y'] > mx_fSizeY) {
      if (mx_fSizeX/mx_fSizeY > mx_bigImageOriginalSize['x']/mx_bigImageOriginalSize['y']) {
        var mx_ratio = mx_fSizeY/mx_bigImageOriginalSize['y'];
        mx_bigImageFitSize['y'] = mx_fSizeY;
        mx_bigImageFitSize['x'] = mx_bigImageOriginalSize['x']*mx_ratio;
      } else {
        var mx_ratio = mx_fSizeX/mx_bigImageOriginalSize['x'];
        mx_bigImageFitSize['x'] = mx_fSizeX;
        mx_bigImageFitSize['y'] = mx_bigImageOriginalSize['y']*mx_ratio;
      }
    } else {
      mx_bigImageFitSize['x'] = mx_bigImageOriginalSize['x'];
      mx_bigImageFitSize['y'] = mx_bigImageOriginalSize['y'];
    }
    // ANIMACE ROZTAŽENÍ
    if (mx_resize_animation == 1) { // OBA SMĚRY SOUČASNĚ
      $('#mxLightboxImageBackground').animate({
        top: Math.round(mx_windowScroll['y']+mx_windowSize['y']/2-mx_bigImageFitSize['y']/2-mx_border),
        height: mx_bigImageFitSize['y']+2*mx_border,
        left: Math.round(mx_windowScroll['x']+mx_windowSize['x']/2-mx_bigImageFitSize['x']/2-mx_border),
        width: mx_bigImageFitSize['x']+2*mx_border
      }, 500*mx_animation_speed, mx_showBigImageImg);
    } else
    if (mx_resize_animation == 2) { // NEJPRVE VERTIKÁLNĚ
      $('#mxLightboxImageBackground').animate({
        top: Math.round(mx_windowScroll['y']+mx_windowSize['y']/2-mx_bigImageFitSize['y']/2-mx_border),
        height: mx_bigImageFitSize['y']+2*mx_border
      }, 500*mx_animation_speed)
      .animate({
        left: Math.round(mx_windowScroll['x']+mx_windowSize['x']/2-mx_bigImageFitSize['x']/2-mx_border),
        width: mx_bigImageFitSize['x']+2*mx_border
      }, 500*mx_animation_speed, mx_showBigImageImg);
    } else
    if (mx_resize_animation == 3) { // NEJPRVE HORIZONTÁLNĚ
      $('#mxLightboxImageBackground').animate({
        left: Math.round(mx_windowScroll['x']+mx_windowSize['x']/2-mx_bigImageFitSize['x']/2-mx_border),
        width: mx_bigImageFitSize['x']+2*mx_border
      }, 500*mx_animation_speed)
      .animate({
        top: Math.round(mx_windowScroll['y']+mx_windowSize['y']/2-mx_bigImageFitSize['y']/2-mx_border),
        height: mx_bigImageFitSize['y']+2*mx_border
      }, 500*mx_animation_speed, mx_showBigImageImg);
    } else { // NEZADÁNO - NEJPRVE VERTIKÁLNĚ
      $('#mxLightboxImageBackground').animate({
        top: Math.round(mx_windowScroll['y']+mx_windowSize['y']/2-mx_bigImageFitSize['y']/2-mx_border),
        height: mx_bigImageFitSize['y']+2*mx_border
      }, 500*mx_animation_speed)
      .animate({
        left: Math.round(mx_windowScroll['x']+mx_windowSize['x']/2-mx_bigImageFitSize['x']/2-mx_border),
        width: mx_bigImageFitSize['x']+2*mx_border
      }, 500*mx_animation_speed, mx_showBigImageImg);
    }
  });
}

function mx_showOverlay () {
  $('div#mxLightboxOverlay').css({
    width: mx_windowSize['x'],
    height: mx_windowSize['y'],
    left: mx_windowScroll['x'],
    top: mx_windowScroll['y']
  })
  .fadeTo(200*mx_animation_speed, .8, function() {
    mx_overlayShow = 1;
    $(this).css({
      width: mx_windowSize['x']+mx_windowScroll['x'],
      height: mx_windowSize['y']+mx_windowScroll['y'],
      left: 0,
      top: 0
    });
  });
  
  var imgImage = mx_currentElement.children('img:first');
  mx_thumbnailPosition['x'] = imgImage.offset().left;
  mx_thumbnailPosition['y'] = imgImage.offset().top;
  mx_thumbnailSize['x'] = imgImage.outerWidth();
  mx_thumbnailSize['y'] = imgImage.outerHeight();
  mx_bigImageUrl = mx_currentElement.attr('href');
  if (imgImage.attr('alt')) {
    mx_bigImageDesc = imgImage.attr('alt');
  } else
  if (imgImage.attr('title')) {
    mx_bigImageDesc = imgImage.attr('title');
  } else
  if (mx_currentElement.attr('title')) {
    mx_bigImageDesc = mx_currentElement.attr('title');
  }
  $('#mxLightboxImageBackground').css({
    width: mx_thumbnailSize['x'],
    height: mx_thumbnailSize['y'],
    left: mx_thumbnailPosition['x'],
    top: mx_thumbnailPosition['y']
  })
  .animate({
    width: Math.round(mx_windowSize['y']/2),
    height: Math.round(mx_windowSize['y']/2),
    left: Math.round(mx_windowScroll['x']+mx_windowSize['x']/2-mx_windowSize['y']/2/2),
    top: Math.round(mx_windowScroll['y']+mx_windowSize['y']/2/2),
    opacity: 1
  }, 1000*mx_animation_speed, function() {
    $('#mxLightboxWait').css({
      opacity: 0,
      width: Math.round(mx_windowSize['y']/2),
      height: 'auto',
      left: 0,
      top: Math.round(mx_windowSize['y']/2)-$(this).height()/2
    }).animate({
      opacity: 1
    }, 300*mx_animation_speed, mx_showBigImage);
  });
  
  return false;
}
    
$(document).ready(function() {
  mx_windowSize['x'] = $(window).width();
  mx_windowSize['y'] = $(window).height();
  mx_windowScroll['x'] = $(window).scrollLeft();
  mx_windowScroll['y'] = $(window).scrollTop();
  
  if (!isNaN(mx_setting['border']) && mx_setting['border'] >= 0) { mx_border = mx_setting['border']; }
  if (!isNaN(mx_setting['horizontal_space']) && mx_setting['horizontal_space'] >= 0) { mx_horizontal_space = mx_setting['horizontal_space']; }
  if (!isNaN(mx_setting['vertical_space']) && mx_setting['vertical_space'] >= 0) { mx_vertical_space = mx_setting['vertical_space']; }
  if (!isNaN(mx_setting['resize_animation']) && mx_setting['resize_animation'] > 0 && mx_setting['resize_animation'] < 4) { mx_resize_animation = mx_setting['resize_animation']; }
  if (!isNaN(mx_setting['animation_speed']) && mx_setting['animation_speed'] > 0 && mx_setting['animation_speed'] < 21) {
    mx_animation_speed = Math.round(mx_setting['animation_speed'])/10;
  }
  if (!isNaN(mx_setting['min_image_width']) && mx_setting['min_image_width'] >= 0) { mx_min_image_width = mx_setting['min_image_width']; }
  if (!isNaN(mx_setting['min_image_height']) && mx_setting['min_image_height'] >= 0) { mx_min_image_height = mx_setting['min_image_height']; }
  if (mx_setting['fullimage_target'] || mx_setting['fullimage_target'] == '') { mx_fullimage_target = mx_setting['fullimage_target']; }
  if (mx_setting['wait_text'] || mx_setting['wait_text'] == '') { mx_wait_text = mx_setting['wait_text']; }
  if (mx_setting['title_close'] || mx_setting['title_close'] == '') { mx_title_close = mx_setting['title_close']; }
  if (mx_setting['title_fullsize'] || mx_setting['title_fullsize'] == '') { mx_title_fullsize = mx_setting['title_fullsize']; }
  if (mx_setting['title_next'] || mx_setting['title_next'] == '') { mx_title_next = mx_setting['title_next']; }
  if (mx_setting['title_previous'] || mx_setting['title_previous'] == '') { mx_title_previous = mx_setting['title_previous']; }
  if (mx_setting['win_size_error'] || mx_setting['win_size_error'] == '') { mx_win_size_error = mx_setting['win_size_error']; }
  if (mx_setting['image_path'] || mx_setting['image_path'] == '') { mx_image_path = mx_setting['image_path']; }
  
  $('body').append('<div id="mxLightboxOverlay" style="position: absolute; left: -100000px; top: -100000px;"></div>\n<div id="mxLightboxImageBackground" style="position: absolute; left: -100000px; top: -100000px;"><div id="mxLightboxWait" style="position: absolute; left: -100000px; top: -100000px;">'+mx_wait_text+'</div><div id="mxLightboxImageDesc" style="position: absolute; left: -100000px; top: -100000px;"></div>\n<a id="mxLightboxIconClose" class="mxLightboxIcon" title="'+mx_title_close+'" alt="'+mx_title_close+'" style="position: absolute; left: -100000px; top: -100000px;" href="#"><img src="'+mx_image_path+'mx-close.gif"></a>\n<a id="mxLightboxIconFull" class="mxLightboxIcon" title="'+mx_title_fullsize+'" alt="'+mx_title_fullsize+'" style="position: absolute; left: -100000px; top: -100000px;" href="#"><img src="'+mx_image_path+'mx-fullsize.gif"></a>\n<a id="mxLightboxIconPrev" class="mxLightboxIcon" title="'+mx_title_previous+'" alt="'+mx_title_previous+'" style="position: absolute; left: -100000px; top: -100000px;" href="#"><img src="'+mx_image_path+'mx-prev.gif"></a>\n<a id="mxLightboxIconNext" class="mxLightboxIcon" title="'+mx_title_next+'" alt="'+mx_title_next+'" style="position: absolute; left: -100000px; top: -100000px;" href="#"><img src="'+mx_image_path+'mx-next.gif"></a>\n</div>\n<div id="mxErrorBox" style="position: absolute; left: -100000px; top: -100000px;"></div>');
  $('div#mxLightboxOverlay').css({opacity: 0});
  $('div#mxLightboxImageBackground').css({opacity: 0});
  
  $('a.lightbox').click(
    function() {
      if (mx_min_image_width <= mx_windowSize['x']-(mx_horizontal_space*2+mx_border*2) && mx_min_image_height <= mx_windowSize['y']-(mx_vertical_space*2+mx_border*2)) {
        mx_curElementIndex = $(this).index('a.lightbox');
        mx_currentElement = $('a.lightbox:eq('+mx_curElementIndex+')');
        return mx_showOverlay();
      } else {
        return true;
      }
    }
  );
  
  $('#mxLightboxIconNext').click(function() {
    $('.mxLightboxIcon').animate({
      opacity: 0
    }, 100*mx_animation_speed, function() {
      $(this).css({left: -100000, top: -100000, opacity: 0});
    });
    $('#mxLightboxImageDesc').animate({
      opacity: 0
    }, 100*mx_animation_speed, function() {
      $(this).text('').css({left: -100000, top: -100000, opacity: 0});
    });
    $('#mxLightboxImg').animate({
      opacity: 0
    }, 200*mx_animation_speed, function() {
      $('#mxLightboxImg').remove();
      $('div#mxLightboxImageBackground').animate({
        left: mx_currentElement.children('img:first').offset().left,
        top: mx_currentElement.children('img:first').offset().top,
        width: mx_currentElement.children('img:first').outerWidth(),
        height: mx_currentElement.children('img:first').outerHeight(),
        opacity: .5
      }, 500*mx_animation_speed, function() {
        mx_currentElement = $('a.lightbox:eq('+(mx_curElementIndex+1)+')');
        mx_curElementIndex += 1;
        $('div#mxLightboxImageBackground').delay(100).animate({
          left: mx_currentElement.children('img:first').offset().left,
          top: mx_currentElement.children('img:first').offset().top,
          width: mx_currentElement.children('img:first').outerWidth(),
          height: mx_currentElement.children('img:first').outerHeight()
        }, 300*mx_animation_speed, function() {
          mx_showOverlay();
        });
      });
    });
    return false;
  });
  
  $('#mxLightboxIconPrev').click(function() {
    $('.mxLightboxIcon').animate({
      opacity: 0
    }, 100*mx_animation_speed, function() {
      $(this).css({left: -100000, top: -100000, opacity: 0});
    });
    $('#mxLightboxImageDesc').animate({
      opacity: 0
    }, 100*mx_animation_speed, function() {
      $(this).text('').css({left: -100000, top: -100000, opacity: 0});
    });
    $('#mxLightboxImg').animate({
      opacity: 0
    }, 200*mx_animation_speed, function() {
      $('#mxLightboxImg').remove();
      $('div#mxLightboxImageBackground').animate({
        left: mx_currentElement.children('img:first').offset().left,
        top: mx_currentElement.children('img:first').offset().top,
        width: mx_currentElement.children('img:first').outerWidth(),
        height: mx_currentElement.children('img:first').outerHeight(),
        opacity: .5
      }, 500*mx_animation_speed, function() {
        mx_currentElement = $('a.lightbox:eq('+(mx_curElementIndex-1)+')');
        mx_curElementIndex -= 1;
        $('div#mxLightboxImageBackground').delay(100).animate({
          left: mx_currentElement.children('img:first').offset().left,
          top: mx_currentElement.children('img:first').offset().top,
          width: mx_currentElement.children('img:first').outerWidth(),
          height: mx_currentElement.children('img:first').outerHeight()
        }, 300*mx_animation_speed, function() {
          mx_showOverlay();
        });
      });
    });
    return false;
  });
  
  $(window).scroll(function() {
    mx_windowScroll['x'] = $(window).scrollLeft();
    mx_windowScroll['y'] = $(window).scrollTop();
    var mx_ibPos = {
      'x' : Math.round(mx_windowScroll['x']+mx_windowSize['x']/2-mx_bigImageFitSize['x']/2-mx_border),
      'y' : Math.round(mx_windowScroll['y']+mx_windowSize['y']/2-mx_bigImageFitSize['y']/2-mx_border)
    }
    $('#mainMenu').text(mx_windowScroll['x']+' x '+mx_windowScroll['y']);
    if (mx_overlayShow == 1) {
      if (mx_windowSize['x']+mx_windowScroll['x'] > $('div#mxLightboxOverlay').width()) {
        $('div#mxLightboxOverlay').css({
          width: mx_windowSize['x']+mx_windowScroll['x']
        });
      }
      if (mx_windowSize['y']+mx_windowScroll['y'] > $('div#mxLightboxOverlay').height()) {
        $('div#mxLightboxOverlay').css({
          height: mx_windowSize['y']+mx_windowScroll['y']
        });
      }
    }
    if (mx_bigImageShow == 1) {
      $('div#mxLightboxImageBackground').stop().animate({
        left: mx_ibPos['x'],
        top: mx_ibPos['y']
      });
    }
  });
  
  $(window).resize(function() {
    mx_windowSize['x'] = $(window).width();
    mx_windowSize['y'] = $(window).height();
    mx_windowScroll['x'] = $(window).scrollLeft();
    mx_windowScroll['y'] = $(window).scrollTop();
    
    if (mx_overlayShow == 1) {
      if (mx_min_image_width > mx_windowSize['x']-(mx_horizontal_space*2+mx_border*2) || mx_min_image_height > mx_windowSize['y']-(mx_vertical_space*2+mx_border*2)) {
        $('#mxErrorBox').html(mx_win_size_error).css({
          opacity: .8,
          left: 5+mx_windowScroll['x'],
          top: 5+mx_windowScroll['y'],
          width: mx_windowSize['x']-20,
          height: mx_windowSize['y']-20
        });
        clearTimeout(mx_resizeTimer);
        return false;
      } else {
        $('#mxErrorBox').css({
          opacity: .8,
          left: -100000,
          top: -100000
        });
      }
      $('div#mxLightboxOverlay').css({
        width: mx_windowSize['x']+mx_windowScroll['x'],
        height: mx_windowSize['y']+mx_windowScroll['y']
      });
    }
    if (mx_resizeTimer) {
      clearTimeout(mx_resizeTimer);
    }
    mx_resizeTimer = setTimeout('mx_resize()', 500);
  });
  
  $('a#mxLightboxIconClose').click(function() {
    mx_overlayShow = 0;
    mx_bigImageShow = 0;
    $('div#mxLightboxImageBackground').animate({
      opacity: 0
    }, 300*mx_animation_speed, function() {
      $(this).css({
        left: -100000,
        top: -100000
      });
      $('div#mxLightboxOverlay').animate({
        opacity: 0
      }, 200*mx_animation_speed, function() {
        $(this).css({
          left: -100000,
          top: -100000
        });
        $('#mxLightboxImg').css({left: -100000, top: -100000, opacity: 0});
        $('#mxLightboxImageDesc').css({left: -100000, top: -100000, opacity: 0});
        $('.mxLightboxIcon').css({left: -100000, top: -100000, opacity: 0});
        $('#mxLightboxImageDesc').text('');
        $('#mxLightboxImg').remove();
      });
    });
    return false;
  });
});
