/*
 * jquery.lightbox.js - ultra basic jQuery Lightbox Plugin
 *
 * by Michael Guitton (michael/at/guitton/dot/name)
 *
 */

'function' == typeof jQuery && (function($, undefined) {

/*@cc_on
@if (@_jscript_version < 5.6)
    return;
@end @*/

    $.fn.lightbox = function(options) {

        var cache    = {};

        var settings = {};
        for (var i in $.fn.lightbox.defaults) {
            settings[i] = $.extend({}, $.fn.lightbox.defaults[i]);
            if ('[object Object]' == Object.prototype.toString.call(options[i])) {
                settings[i] = $.extend(settings[i], options[i]);
            }
        }

        var siblings = this;

        function lightbox(e) {

            var eh = window.opera ? 'keypress' : 'keydown';

            var canvas = {
/*@cc_on
@if (@_jscript_version == 5.6)
                width:  (document.body.scrollWidth > document.body.offsetWidth ?
                    document.body.scrollWidth : document.body.offsetWidth)
                    + 'px',
                height: (document.body.scrollHeight > document.body.offsetHeight ?
                   document.body.scrollHeight : document.body.offsetHeight)
                + 'px'
@else @*/
                width:  '100%',
                height: '100%'
/*@end @*/
            };

            var $overlay = $('<div>')
                .attr(
                    'style',
                    'width: ' + canvas.width + '; height: ' + canvas.height
                )
                .css($.extend({}, settings.overlay))
                .one('click', remove)
                .appendTo('body');

            var $img = $('<img>');

            function append(that) {
                var title = that.title || that.txt || this.alt;
                $img.attr('id', settings.data.id)
                    .attr('src', that.href)
                    .attr('title', title)
                    .css($.extend({}, settings.img, scale(this)))
                    .one('click', remove)
                    .appendTo('body');
                this.onload = null;
            };

            function remove() {
                $img.remove();
                $overlay.remove();
                $(document).unbind(eh, keys);
            }

            function update(that) {
                var title = that.title || that.text || this.alt;
                $img.attr('src', that.href)
                    .attr('title', title)
                    .css($.extend({}, settings.img, scale(this)));
                this.onload = null;
            }

            function keys(e) {
                switch (e.keyCode) {
                    case 27:
                        remove();
                        break;
                    case 37:
                    case 38:
                        siblings.index = (--siblings.index + siblings.length)
                            % siblings.length;
                        load(siblings[siblings.index], update);
                        break;
                    case 32:
                    case 39:
                    case 40:
                        siblings.index = ++siblings.index % siblings.length;
                        load(siblings[siblings.index], update);
                        break;
                    default:
                        return;
                }
                e.stopPropagation();
                e.preventDefault();
            }

            function load(that, callback) {
                var $img = $('img', that);
                if (!that.href) {
                    if (0 == $img.length) { return; }
                    that.href = $img.attr('src');
                }
                if (!cache[that.href]) {
                    cache[that.href] = new Image();
                    cache[that.href].src = that.href;
                    if (0 != $img.length) {
                        $img = $img.slice(0, 1);
                        cache[that.href].alt = $.trim($img.attr('alt')) || $.trim($img.attr('title'));
                    }
                }
                if (cache[that.href].complete) {
                    callback.apply(cache[that.href], [that]);
                }
                else {
                    cache[that.href].onload = function() {
                        callback.apply(this, [that]);
                    };
                }
            }

            for (var i = 0; siblings[i]; i++) {
                if (this.href == siblings[i].href) {
                    siblings.index = i;
                    break;
                }
            }

            load(this, append);

            ($(document)[eh])(keys);

            e.preventDefault();
        }

        function resize() {
            var image = $('#' + settings.data.id)[0];
            if (image && image.src) {
                $(image).css(scale(cache[image.src]))
            }
        }

        function scale(image) {
            var delta   =  parseInt(settings.data.delta, 10) || 100;
            var border  = (parseInt(
                settings.img.border
                && settings.img.border.substring
                && settings.img.border.substring(
                    settings.img.border.lastIndexOf(
                        ' ', settings.img.border.indexOf('px')
                    ) + 1
                ),
                10
            ) || 0) * 2;
            var padding = (parseInt(settings.img.padding, 10) || 0) * 2;

            var viewport = {
                width: $('body').width(),
                height: $(window).height()
            };

            var w = image.width;
            var h = image.height;

            delta += border + padding;

            var x = Math.max(
                viewport.width  - delta,
                parseInt(settings.data['min-width'], 10)  || 0
            );
            var y = Math.max(
                viewport.height - delta,
                parseInt(settings.data['min-height'], 10) || 0
            );

            if (w > x) {
                h = h * x / w;
                w = x;
                if (h > y) {
                    w = w * y / h;
                    h = y;
                }
            } else if (h > y) {
                w = w * y / h;
                h = y;
                if (w > x) {
                    h = h * x / w;
                    w = x;
                }
            }
            return {
                'left': Math.max(
                    (viewport.width  - w - border - padding) / 2,
                    0
                ),
                'top':  Math.max(
                    (viewport.height - h - border - padding) / 2,
                    0
                ),
                'width':  w + 'px',
                'height': h + 'px'
            };
        }

        $(window).bind('resize', resize);

        return this.each(function() {
            var $this = $(this);
            $this.bind('click', lightbox);
        });
    };

    $.fn.lightbox.defaults = {
        data: {
            'delta': 100,
            'id': 'lightbox'
        },
        overlay: {
            'position': 'fixed',
            'z-index': 999,
            'left': 0,
            'top': 0,
            'background-color': '#000',
            'opacity': 0.75
        },
        img: {
            'position': 'fixed',
            'z-index': 999,
            'left': 0,
            'top': 0,
            'background-color': '#ccc'
        }
    };

/*@cc_on
@if (@_jscript_version == 5.6)
    $.fn.lightbox.defaults.overlay.position
        = $.fn.lightbox.defaults.img.position
        = 'absolute';
@end @*/

})(jQuery);

