(function($) {
    
    var config = {
        enlargement: 2.0,
        addMargin: false
    }; 
    
    var gallery = null,
        list = null,
        imageWrap = null,
        navNext = null,
        navPrev = null,
        zoom = null,
        listWrap = null,
        listSpace = null,
        listPrev = null,
        listNext = null,
        listCount = null,
        elemWidth = null,
        elemHeight = null,
        listWidth = null,
        listSize = null;
         
    var setImageHeight = function(position) {
        newHeight = imageWrap.find('img#image_' + position).height();
        if (newHeight == 0)
        {
            imageWrap.find('img#image_' + position).load(function() {
                newHeight = $(this).height();
                navNext.css('height', newHeight + 'px');
                navPrev.css('height', newHeight + 'px');
                zoom.css('height', newHeight + 'px');
                imageWrap.css('height', newHeight + 'px');   
            });
        }
        else
        {
            navNext.css('height', newHeight + 'px');
            navPrev.css('height', newHeight + 'px');
            zoom.css('height', newHeight + 'px');
            imageWrap.css('height', newHeight + 'px');
        }
    };
    
    var doAnimate = function(position, oldLeft) {
        if (listCount < listSize)
        {
            left = 0;
        }
        else  if (position < listCount - listSize)
        {
            left = position * elemWidth;                
        }
        else
        {
            left = (listCount - listSize) * elemWidth;
        }
        
        list.find('li').removeClass('active');
        list.find('li').eq(position).addClass('active');
        if (oldLeft != left)
        {
            list.stop().animate({
               left: '-' + left + 'px' 
            }, 500);
        }
                        
        imageWrap.find('img').css('z-index', 0);
        imageWrap.find('img#image_' + position).css('z-index', 2).stop().fadeIn(500, function() {
            imageWrap.find('img:not(#image_' + position + ')').hide();    
            imageWrap.find('img#image_' + position).show();
        });
        zoom.attr('href', imageWrap.find('img#image_' + position).attr('src').replace(/^(.*)\/(.*)$/, "$1/$2"));
        
        setImageHeight(position);
    };
    
            
    $.fn.PedroGallery = function(settings) {    
        if (settings) 
        {
            $.extend(config, settings);
        }
        
        return this.each(function() {
            gallery = $(this);
            gallery.addClass('gallery-js');
            
            list = $(this).find('ul');    
			imageWrap = $('<div class="main-photo"></div>').insertAfter(list);
			
			i = 0;
			list.find('li a').each(function() { 
			    curImage = $('<img />');
			    curImage.appendTo(imageWrap);
			    curImage.attr('id', 'image_' + i);
			    curImage.attr('src', $(this).attr('href'));
			    i++;
			});
			
			navNext = $('<a href="" class="next"></a>').appendTo(imageWrap);
            navPrev = $('<a href="" class="prev"></a> ').appendTo(imageWrap);
            zoom = $('<a class="zoom" href=""></a> ').appendTo(imageWrap);
                        
            listWrap = list.wrap('<div class="list-wrap" />').parent();
            listSpace = list.wrap('<div class="list-space" />').parent();
            listSpace.before('<a href="" class="list-prev"></a>');
            listPrev = $('a.list-prev');
            listSpace.after('<a href="" class="list-next"></a>');
            listNext = $('a.list-next');
            
            listCount = list.find('li').length;
            elemWidth = list.find('li:first').outerWidth();
            elemHeight = list.find('li:first').outerHeight();
            listWidth = parseInt(listSpace.css('width'));
            listSize = parseInt(listWidth / elemWidth);
            
            list.find('li:first').addClass('active');
            imageWrap.find('img').hide();
            imageWrap.find('img#image_0').show();
            
            imageWrap.find('img#image_0').load(function() {
                newHeight = $(this).height();
                navNext.css('height', newHeight + 'px');
                navPrev.css('height', newHeight + 'px');
                zoom.css('height', newHeight + 'px');
                imageWrap.css('height', newHeight + 'px');   
            });

            zoom.attr('href', imageWrap.find('img#image_0').attr('src').replace(/^(.*)\/(.*)$/, "$1/$2"));
            zoom.fancybox();
            
            navNext.hover(function() {
                $(this).addClass('next-hover');
            }, function() {
                $(this).removeClass('next-hover');
            });
            
            navPrev.hover(function() {
                $(this).addClass('prev-hover');
            }, function() {
                $(this).removeClass('prev-hover');
            });
            
            zoom.hover(function() {
                $(this).addClass('zoom-hover');
            }, function() {
                $(this).removeClass('zoom-hover');
            });
            
            navNext.click(function(event) {
                event.preventDefault();
                
                oldLeft = parseInt(list.css('left'));
                position = list.find('li.active').index();
                
                position += 1;
                if (position >= listCount)
                {
                    return false;    
                }
                
                doAnimate(position, oldLeft);     
            });
            
            listNext.click(function(event) {
                event.preventDefault();
                
                oldLeft = parseInt(list.css('left'));
                position = list.find('li.active').index();
                
                position += 1;
                if (position >= listCount)
                {
                    return false;    
                }
                
                doAnimate(position, oldLeft);     
            });
            
            navPrev.click(function(event) {
                event.preventDefault();
                
                oldLeft = parseInt(list.css('left'));
                position = list.find('li.active').index();
                
                position -= 1;
                if (position < 0)
                {
                    return false;    
                }
                
                doAnimate(position, oldLeft);  
            });
            
            listPrev.click(function(event) {
                event.preventDefault();
                
                oldLeft = parseInt(list.css('left'));
                position = list.find('li.active').index();
                
                position -= 1;
                if (position < 0)
                {
                    return false;    
                }
                
                doAnimate(position, oldLeft);  
            });
            
            list.find('li a').click(function(event) {
                event.preventDefault();
                
                oldLeft = parseInt(list.css('left'));
                position = $(this).parent().index();
                
                doAnimate(position, oldLeft);
            });
		});
    };
    
})(jQuery);


