﻿(function($) {
    $.fn.photoStyle = function(options) {
        var that = $(this);

        var opts = $.extend({}, $.fn.photoStyle.defaults, options);

        init(opts);

        function init(opts) {
            var func = fancyBox;
            if (opts.type == 'lightbox')
                func = lightBox;

            if (opts.useGalleryIfAvailable && opts.type == 'fancybox') {
                //fancybox creates a gallery if all of the selected elements have the same rel attributes
                $(that).each(function(i, item) {
                    $(item).attr('rel', 'fancybox');
                });

                func(that, opts);
            }
            else {
                that.each(function(i, item) {
                    func(that, opts);
                });
            }
        }

        function fancyBox(element, opts) {
            $(element).fancybox({
                'padding': opts.padding,
                'zoomOpacity': true,
                'overlayOpacity': opts.overlayOpacity,
                'overlayShow': opts.showOverlay,
                'zoomSpeedIn': (opts.useAnimation ? 600 : 10),
                'zoomSpeedOut': (opts.useAnimation ? 400 : 10),
                'overlayColor': opts.overlayColor
            });

            $('#fancy_inner').css('backgroundColor', opts.paddingColor);
        }

        function lightBox(element, opts) {
            $(element).lightBox({
                imageLoading: '/includes/js/jquery-lightbox/images/lightbox-ico-loading.gif',
                imageBtnPrev: '/includes/js/jquery-lightbox/images/lightbox-btn-prev.gif',
                imageBtnNext: '/includes/js/jquery-lightbox/images/lightbox-btn-next.gif',
                imageBtnClose: '/includes/js/jquery-lightbox/images/lightbox-btn-close.gif',
                imageBlank: '/includes/js/jquery-lightbox/images/lightbox-blank.gif',
                overlayBgColor: opts.overlayColor,
                overlayOpacity: (opts.showOverlay ? opts.overlayOpacity : 0)
            });
        }

        return this;
    }

    $.fn.photoStyle.defaults = {
        type: 'fancybox',
        useAnimation: false,
        showOverlay: false,
        overlayColor: '#000000',
        overlayOpacity: 0.5,
        padding: 10,
        paddingColor: '#FFFFFF',
        useGalleryIfAvailable: true
    }
})(jQuery);