(function($) {
    $.fn.reza_lightbox = function() {



	 $('.single .rd-single-content img').parent('a').each(function(i, el) {
 	var reza_href_value = this.href;
  	if (/\.(jpg|png|gif|jpeg|bmp)$/.test(reza_href_value)) {
		$(this).addClass('rd-singleimg-lightbox');
  	}  
	});

        if (jQuery('.rd-lightbox').hasClass('rd-lightbox-active')) {
            $('.rd-singleimg-lightbox').on('click', function(event) {
                $('.rd-lightbox').addClass('rd-lightbox-post-content')

                event.stopPropagation();
                event.preventDefault();
                var images = $('.rd-singleimg-lightbox');
                rd_lightboxActual = this;

				var href = this.getAttribute('href');
				if(href){
					var href_image= href;
				}else{
					var href_image= $(this).find('img').attr('src');
				}
                $('.rd-lightbox-targetimg').css('display', 'none').attr('src', href_image);
                $('.rd-lightbox-targetimg').one('load', function() {
                    $('.rd-lightbox-loading').css('display', 'none');
                    $(this).fadeIn();
                });

                var text = $(this).parent().find('.wp-caption-text').text();
                var display = 'block';
				/*
                if (text == null) {
                    text = '';
                    display = 'none';
                }
*/
                $('.rd-lightbox h3').text(text);
                $('.rd-lightbox').addClass('rd-multi-lightbox').slideToggle('fast');
                var actualId;
                $.each(images, function(index) {
                    if (rd_lightboxActual === images[index]) {
                        actualId = index + 1;
                    }
                });
                if (images.length == 1) {
                    $('.rd-lightbox-moreitems').css('display', 'none');
                }

                $('.rd-lightbox span').text($.rd_lightboxMessage(actualId, images.length));


            });
        }
		
		
		
		
		
        $.rd_lightboxMove = function(direction, allImages) {
            console.log(allImages);
            direction = (direction == 'next') ? 'next' : 'prev';
            var actualId;
            $.each(allImages, function(index) {
                if (allImages[index] === rd_lightboxActual) {
                    actualId = index;
                }
            });

            var iterator;
            if (direction == 'next') {
                iterator = actualId + 1;
                if (actualId == allImages.length - 1) {
                    iterator = 0;
                }
            } else if (direction == 'prev') {
                iterator = actualId - 1;
                if (actualId == 0) {
                    iterator = allImages.length - 1;
                }
            }

            var newImage = allImages[iterator];
			
			
				var href = newImage.getAttribute('href');
				if(href){
					var href_image= href;
				}else{
 					var href_image= $(newImage).find('img').attr('src');
				}
			
            $('.rd-lightbox-targetimg').css('display', 'none').attr('src',href_image);
            $('.rd-lightbox-loading').css('display', 'block');

            $('.rd-lightbox-targetimg').one('load', function() {
                $('.rd-lightbox-loading').css('display', 'none');
                $(this).css('display', 'inline-block');
            });

            var text = $(newImage).parent().find('.wp-caption-text').text();
            var display = 'block';

            if (text == null) {
                text = '';
                display = 'none';
            }
            $('.rd-lightbox h3').text(text).css('display', display);
            $('.rd-lightbox span').text($.rd_lightboxMessage(iterator + 1, allImages.length));
            rd_lightboxActual = newImage;
        };


        $(document).on('click', '.rd-lightbox-post-content.rd-multi-lightbox .rd-lightbox-prevbig', function() {
            $.rd_lightboxMove('prev', $('.rd-singleimg-lightbox'));
        });

        $(document).on('click', '.rd-lightbox-outer,.rd-lightbox-close', function() {
            $('.rd-lightbox').slideToggle('fast');
        });

        $(document).on('click', '.rd-lightbox-close,.rd-lightbox-outer', function() {
            $('.rd-lightbox').removeClass('rd-lightbox-singleimg').removeClass('rd-multi-lightbox');
            $('.rd-lightbox').removeClass('rd-lightbox-post-content').removeClass('rd-lightbox-gallery');
      

        });

        $(document).on('click', '.rd-multi-lightbox.rd-lightbox-post-content .rd-lightbox-nextbig,.rd-multi-lightbox.rd-lightbox-post-content .rd-lightbox-targetimg', function() {
            $.rd_lightboxMove('next', $('.rd-singleimg-lightbox'));
        });



        var rd_lightboxActual = null;

        $.rd_lightboxMessage = function(actual, last) {
            return '' + $('body').reza_translate(actual + ' / ' + last);
        };



        $(document).on('keydown', function(event) {
            if (event.keyCode == 37) {


                if ($('.rd-lightbox').hasClass('rd-multi-lightbox')) {
                    if ($('.rd-lightbox').hasClass('rd-lightbox-post-content')) {

                        $.rd_lightboxMove('prev', $('.rd-single-content img').parent('a'));
                    }
                }
            }
        });
        $(document).on('keydown', function(event) {

            if ($('.rd-lightbox').hasClass('rd-multi-lightbox')) {
                if ($('.rd-lightbox').hasClass('rd-lightbox-post-content')) {
                    if (event.keyCode == 39) {
                        $.rd_lightboxMove('next', $('.rd-single-content img').parent('a'));
                    }
                }
            }

        });






        if (jQuery('.rd-lightbox').hasClass('rd-lightbox-active')) {
            $('.rd-hover-img,.rd-product-lightbox').on('click', function(event) {
                $('.rd-hover-img,.rd-product-lightbox:not(.rd-slick-current)').each(function(i, el) {
					
                    var reza_href_value = this.href;
 					if(reza_href_value){
						var href_image=  this.href;
					}else{
						var href_image= $(this).find('img').attr('src');
					}					
					
                     if (/\.(jpg|png|gif|jpeg|bmp)$/.test(href_image)) {
                        $(this).addClass('rd-singleimg-lightbox');
                    }
                });

                event.stopPropagation();
                event.preventDefault();
                var rand = Math.floor((Math.random() * 10000000) + 1);

                $(this).parents('[class*="rd-el-"],.rd-product-thumbnails-warp').attr('data-lightbox', rand);


                $('.rd-lightbox').addClass('rd-lightbox-gallery').attr('data-rand', rand);
                var images = $(this).parents('[class*="rd-el-"],.rd-product-thumbnails-warp').find('.rd-singleimg-lightbox');
                rd_lightboxActual = this;
	
				var href = this.getAttribute('href');
				if(href){
					var href_image= href;
				}else{
					var href_image= $(this).find('img').attr('src');
				}
			
                $('.rd-lightbox-targetimg').css('display', 'none').attr('src', href_image);
                $('.rd-lightbox-targetimg').one('load', function() {
                    $('.rd-lightbox-loading').css('display', 'none');
                    $(this).slideToggle(0);
                });

                var text = $(this).next().html();
                var display = 'block';
                if (text == null) {
                    text = '';
                    display = 'none';
                }

                $('.rd-lightbox-title').text(text).css('display', display);
                $('.rd-lightbox').addClass('rd-multi-lightbox').slideToggle('fast');
                var actualId;
                $.each(images, function(index) {
                    if (rd_lightboxActual === images[index]) {
                        actualId = index + 1;
                    }
                });
                if (images.length == 1) {
                    $('.rd-lightbox-moreitems').css('display', 'none');


                }
                $('.rd-lightbox').attr('data-item', images.length);
                $('.rd-lightbox span').text($.rd_lightboxMessage(actualId, images.length));



            });
        }





        $(document).on('click', '.rd-multi-lightbox.rd-lightbox-gallery .rd-lightbox-prevbig', function() {
            var rand = $('.rd-lightbox').attr('data-rand');
            $.rd_lightboxMove('prev', $('[data-lightbox="' + rand + '"]').find('.rd-hover-img,.rd-product-lightbox:not(.rd-slick-current)'));
        });



        $(document).on('click', '.rd-multi-lightbox.rd-lightbox-gallery .rd-lightbox-nextbig,.rd-multi-lightbox.rd-lightbox-gallery .rd-lightbox-targetimg', function() {
            var rand = $('.rd-lightbox').attr('data-rand');
            $.rd_lightboxMove('next', $('[data-lightbox="' + rand + '"]').find('.rd-hover-img,.rd-product-lightbox:not(.rd-slick-current)'));
        });




        $(document).on('keydown', function(event) {
            if (event.keyCode == 39) {

                if ($('.rd-lightbox').hasClass('rd-multi-lightbox')) {
                    if ($('.rd-lightbox').hasClass('rd-lightbox-gallery')) {
                        var rand = $('.rd-lightbox').attr('data-rand');
                        $.rd_lightboxMove('next', $('[data-lightbox="' + rand + '"]').find('.rd-hover-img,.rd-product-lightbox:not(.rd-slick-current)'));
                    }
                }
            }
        });
        $(document).on('keydown', function(event) {
            if (event.keyCode == 37) {

                if ($('.rd-lightbox').hasClass('rd-multi-lightbox')) {
                    if ($('.rd-lightbox').hasClass('rd-lightbox-gallery')) {


                        var rand = $('.rd-lightbox').attr('data-rand');
                        $.rd_lightboxMove('prev', $('[data-lightbox="' + rand + '"]').find('.rd-hover-img,.rd-product-lightbox:not(.rd-slick-current)'));

                    }
                }

            }
        });



    };
}(jQuery));