(function($) {
	$.fn.thumbnailViewer = function() {
	  var minWidth = 150;
	  var minHeight = 150;
    var maxWidth = 500; // $(window).width() - 100;
    var maxHeight = 500; // $(window).height() - 200;

	  if ($('#thumbnailViewer').size() === 0) {
      $('<div id="thumbnailViewer" align="center"></div>')
        .css('display', 'none')
        .appendTo('body')
        .dialog({
          autoOpen: false,
          modal: true,
          title: "Enlarged Image",
          resizable: false,
          minWidth: minWidth,
          minHeight: minHeight,
          //maxWidth: maxWidth,
          //maxHeight: maxHeight,
          close: function() {
            $(document).unbind('click.thumbnailViewer');
            //$(document.documentElement).unbind('keyup.thumbnailViewer');
          }
        });
    }

		this.each(function(i) {
      var el = this;
			$(el).unbind('click.thumbnailViewer').bind('click.thumbnailViewer', function(e) {
				var img = new Image();
				var title = $(el).attr("dialog_title") || $(el).attr("title") || "Enlarged Image";
				$(img).load(function() {

          var imgWidth = this.width;
          var imgHeight = this.height;

          var boxWidth = imgWidth;
          var boxHeight = imgHeight;

          if (imgWidth < minWidth || imgHeight < minHeight) {
            if (imgWidth > imgHeight) {
              if (imgWidth > minWidth) {
                boxWidth = imgWidth;
              } else {
                boxWidth = minWidth;
              }
              boxHeight = Math.floor(minWidth * imgHeight / imgWidth);
            } else {
              boxWidth = Math.floor(minHeight * imgWidth / imgHeight);
              if (imgHeight > minHeight) {
                boxHeight = imgHeight;
              } else {
                boxHeight = minHeight;
              }
            }
          } else if (imgHeight > maxHeight) {
            boxWidth = Math.floor(maxHeight * imgWidth / imgHeight);
            boxHeight = maxHeight;
            imgWidth = boxWidth;
            imgHeight = boxHeight;
          } else if (imgWidth > maxWidth) {
            boxWidth = maxWidth;
            boxHeight = Math.floor(maxWidth * imgHeight / imgWidth);
            imgWidth = boxWidth;
            imgHeight = boxHeight;
          }

          $(this).attr("width", imgWidth).attr("height", imgHeight);

          $('#thumbnailViewer').empty().append(this);

          if ($(el).hasClass("thumbnail-slideshow")) {
            var slideshow_group = $(el).attr("slideshow_group");
            if (slideshow_group !== '') {
              var slideshow_thumbs = $(".thumbnail[slideshow_group='"+slideshow_group+"']");
              if (slideshow_thumbs.length > 1) {
                var current_position = $.inArray(el, slideshow_thumbs);
                if (current_position > -1) {
                  var button_div =  '<div class="ui-dialog-buttonpane ui-widget ui-state-default ui-corner-all" style="text-align: center"><nobr>';
                      button_div +=   '<button'+((current_position > 0) ? '' : ' disabled="disabled"')+' class="coolButton small" id="thumbnailViewer_prevImg">&lt;&lt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Previous Image</button>&nbsp;&nbsp;';
                      button_div +=   '<button'+((current_position < (slideshow_thumbs.length-1)) ? '' : ' disabled="disabled"')+' class="coolButton small" id="thumbnailViewer_nextImg">Next Image&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;</button>';
                      button_div += '</nobr></div>';

                  $('#thumbnailViewer').append(button_div);
                  $('#thumbnailViewer_prevImg').click(function() { $(slideshow_thumbs[(current_position-1)]).trigger('click'); return false; });
                  $('#thumbnailViewer_nextImg').click(function() { $(slideshow_thumbs[(current_position+1)]).trigger('click'); return false; });

                  if (boxWidth < 300) { boxWidth = 300; }

                  // keyboard navigation
                  /*
                  $(document.documentElement).unbind('keyup.thumbnailViewer').bind('keyup.thumbnailViewer', function(e) {
                    switch (e.keyCode) {
                      case 37:
                        if (current_position <= 0) { return false; }
                        current_position -= 1;
                        $(slideshow_thumbs[current_position]).trigger('click');
                        return false;

                      case 39:
                        if (current_position >= slideshow_thumbs.length) { return false; }
                        current_position += 1;
                        $(slideshow_thumbs[current_position]).trigger('click');
                        return false;
                    }
                    return false;
                  });
                  */

                }
              }
            }
          }

          $('#thumbnailViewer')
            .dialog('close')
            .dialog('option', 'title', title)
            .dialog('option', 'width', boxWidth + 25)
            .dialog('option', 'position', 'center')
            .dialog('open');

          $(document).unbind('click.thumbnailViewer').bind('click.thumbnailViewer', function(e) {
            if (e.button > 0) { return true; }
            if ($(e.target).hasClass("ui-dialog-buttonpane")) { return true; }
            if ($(e.target).hasClass("coolButton")) { return true; }
            $("#thumbnailViewer").dialog('close');
          });

				}).attr("src", $(this).attr("href"));
				return false;
			});
		});

		return this;
	};

})(jQuery);

