// jQuery Simple Image Gallery
// by Haroon Rafi
// 6th April 2011

(function ($) {
    $.fn.simpleImageGallery = function (options) {

        // Default settings, do not modify here - pass required settings as plugin options instead
        var defaults = {
            thumbnailListSelector: '.simple-image-gallery-thumbs',
            previewContainerSelector: '.simple-image-gallery-preview',
            enablePreview: true,
            enableLightbox: true,
            lightBoxImageLoading: '/images/lightbox/lightbox-ico-loading.gif',
            lightBoxImageBtnPrev: '/images/lightbox/lightbox-btn-prev.gif',
            lightBoxImageBtnNext: '/images/lightbox/lightbox-btn-next.gif',
            lightBoxImageBtnClose: '/images/lightbox/lightbox-btn-close.gif',
            lightBoxImageBlank: '/images/lightbox/lightbox-blank.gif'
        };

        var pluginData = {
    };

    $container = this;
    var opts = $.extend(defaults, options);
    $previewContainer = $(opts.previewContainerSelector);
    $thumbnailList = $(opts.thumbnailListSelector);

    return this.each(function () {
        if (!opts.enablePreview) $previewContainer.hide();
        else changePreviewImage($thumbnailList.children('li').eq(0).find('a'));

        if (opts.enableLightbox) {
            applyLightBox($(opts.thumbnailListSelector).find('a'))
            if (opts.enablePreview) applyLightBox($previewContainer.find('a'));
        }

        $thumbnailList.find('a').hover(function () {
            changePreviewImage($(this));
        });

        function changePreviewImage($thumbLink) {
            var $previewLink = $previewContainer.find('a');
            var $previewImage = $previewContainer.find('img');
            if ($previewLink.length < 1)
                $previewContainer.append('<a href="' + $thumbLink.attr('href') + '"><img src="' + $thumbLink.attr('rel') + '"></a>');
            else {
                $previewLink.attr('href', $thumbLink.attr('href'));
                $previewImage.attr('src', $thumbLink.attr('rel'));
            }
        }

        function applyLightBox($target) {
            $target.lightBox({
                imageLoading: opts.lightBoxImageLoading,
                imageBtnPrev: opts.lightBoxImageBtnPrev,
                imageBtnNext: opts.lightBoxImageBtnNext,
                imageBtnClose: opts.lightBoxImageBtnClose,
                imageBlank: opts.lightBoxImageBlank
            });
        }
    });
};
})(jQuery);
