/**
 * Overlay Gallery plugin, version: 1.0.0
 * 
 * Copyright (c) 2009 Tero Piirainen
 * http://flowplayer.org/tools/overlay.html#gallery
 *
 * Dual licensed under MIT and GPL 2+ licenses
 * http://www.opensource.org/licenses
 *
 * Since  : July 2009
 * Date: ${date}
 * Revision: ${revision} 
 */
(function($) {
    var t = $.tools.overlay; t.plugins = t.plugins || {}; t.plugins.gallery = { version: '1.0.0', conf: { imgId: 'img', next: '.next', prev: '.prev', info: '.info', progress: '.progress', disabledClass: 'disabled', activeClass: 'active', opacity: 0.8, speed: 'slow', template: '<strong>${title}</strong> <span>Image ${index} of ${total}</span>', autohide: true, preload: true, api: false} }; $.fn.gallery = function(opts) {
        var conf = $.extend({}, t.plugins.gallery.conf), api; $.extend(conf, opts); api = this.overlay(); var links = this, overlay = api.getOverlay(), next = overlay.find(conf.next), prev = overlay.find(conf.prev), info = overlay.find(conf.info), progress = overlay.find(conf.progress), els = prev.add(next).add(info).css({ opacity: conf.opacity }), close = api.getClosers(), index; function load(el) {
            progress.fadeIn(); els.hide(); close.hide(); var url = el.attr("href"); var image = new Image(); image.onload = function() {
                progress.fadeOut(); var img = $("#" + conf.imgId, overlay); if (!img.length) { img = $("<img/>").attr("id", conf.imgId).css("visibility", "hidden"); overlay.prepend(img); }
                img.attr("src", url).css("visibility", "hidden"); var width = image.width; if (image.width > 900) { var aspect_ratio = width / image.height; width = 900; image.height = 900 / aspect_ratio; img.css("width", "900px"); }
                else { img.css("width", image.width + "px"); }
                var left = ($(window).width() - width) / 2; index = links.index(links.filter("[href=" + url + "]")); links.removeClass(conf.activeClass).eq(index).addClass(conf.activeClass); var cls = conf.disabledClass; els.removeClass(cls); if (index === 0) { prev.addClass(cls); }
                if (index == links.length - 1) { next.addClass(cls); }
                var text = conf.template.replace("${title}", el.attr("title") || el.data("title")).replace("${index}", index + 1).replace("${total}", links.length); var padd = parseInt(info.css("paddingLeft"), 10) + parseInt(info.css("paddingRight"), 10); info.html(text).css({ width: width + 20 }); overlay.animate({ width: width, height: image.height, left: left }, conf.speed, function() { img.hide().css("visibility", "visible").fadeIn(function() { if (!conf.autohide) { els.fadeIn(); close.show(); }; $("#boat-gallery").css({ margin: "12px 0 0 12px" }).dcResize(); }); });
            }; image.onerror = function() { overlay.fadeIn().html("Cannot find image " + url); }; image.src = url; if (conf.preload) { links.filter(":eq(" + (index - 1) + "), :eq(" + (index + 1) + ")").each(function() { var img = new Image(); img.src = $(this).attr("href"); }); } 
        }
        function addClick(el, isNext) {
            el.click(function() {
                if (el.hasClass(conf.disabledClass)) { return; }
                var trigger = links.eq(i = index + (isNext ? 1 : -1)); if (trigger.length) { load(trigger); } 
            });
        }
        addClick(next, true); addClick(prev); $(document).keydown(function(evt) {
            if (!overlay.is(":visible") || evt.altKey || evt.ctrlKey) { return; }
            if (evt.keyCode == 37 || evt.keyCode == 39) { var btn = evt.keyCode == 37 ? prev : next; btn.click(); return evt.preventDefault(); }
            return true;
        }); function showEls() { if (!overlay.is(":animated")) { els.show(); close.show(); } }
        if (conf.autohide) { overlay.hover(showEls, function() { els.fadeOut(); close.hide(); }).mousemove(showEls); }
        var ret; this.each(function() { var el = $(this), api = $(this).overlay(), ret = api; api.onBeforeLoad(function() { load(el); }); api.onClose(function() { links.removeClass(conf.activeClass); }); }); return conf.api ? ret : this;
    };
})(jQuery);
