﻿(function($) {
    $.fn.imagePopup = function(options) {
        var dlgNum = 0;
        var waitForFinalEvent = (function() {
            var timers = {};
            return function(callback, ms, uniqueId) {
                if (!uniqueId) {
                    uniqueId = "process" + Math.floor(Math.random() * 9999999999999999999);
                }
                if (timers[uniqueId]) {
                    clearTimeout(timers[uniqueId]);
                }
                timers[uniqueId] = setTimeout(callback, ms);
            };
        })();
        function calcDialogH() {
            return 90;
        }
        function calcDialogW() {
            return 42;
        }
        function resizeImage(iw, ih) {
            var ww = $(window).width();
            var wh = $(window).height();
            var nw = iw; 
            var nh = ih; 
            ww = ww - calcDialogW();
            wh = wh - calcDialogH();
            if ((ih > wh) || (iw > ww)) {
                if (ih > wh) {
                    nh = wh;
                    nw = Math.round(nh * (iw / ih));
                }
                if (nw > ww) {
                    fiw = ww;
                    nh = Math.round(fiw * (nh / nw));
                }
            }
            return { height: nh, width: nw };
        }
        function imageHTML(nw, nh, imgPath) {
            return '<img style="border:none;width:' + nw + 'px;height:' + nh + 'px;" src="' + imgPath + '"/>';
        }
        $(window).resize(function() {
            resize = true;
            dlgNum = 0;
            waitForFinalEvent(function() {
                console.log(" ++++++++++++++++ WINDOW RESIZED ++++++++++++++++++");
                //grab the new size of image and write function to insert 
                //new dimensions into style attribute into IMG inside DIV ID

            }, 500, "resizeCall-1");
        });
        var settings = {
            autoOpen: false,
            width: 'auto',
            height: 'auto',
            position: 'center',
            modal: true,
            resizable: false,
            show: 'slide',
            hide: 'slide'

        };
        if (options) {
            $.extend(settings, options);
        }
        return this.each(function() {
            var imgName = $(this).find("a").attr("href");
            var lrgImgSrc = imgName;
            var img = new Image();
            var dlg;
            var currentElement = this;

            $(img).load(function() {
                var newImgSize = resizeImage(img.width, img.height); //returns new image string
                var lrgImg = imageHTML(newImgSize.width, newImgSize.height, lrgImgSrc);
                settings.width = newImgSize.width + calcDialogW();
                dlg = $('<div id="popupID_' + dlgNum + '"></div>').append(lrgImg).dialog(settings);
                $(currentElement).children('a').click(function() {
                    dlg.dialog('open');
                    return false;
                });
                dlgNum++;
            }).error(function() {
                console.log('Error loading image');
            }).attr('src', lrgImgSrc);
        }); 
    } 
})(jQuery);
