(function($){
        
        $.fn.portfolioSlideshow = function(options) {
                
                
                options = $.extend({
                        mainTransitionTimer : 500,
                        imageTransitionTimer : 500,
                        imageWidth : 882
                }, options);
                
                var self = this;
                var items = [];
                var navItems = $(options.navigation);
                var currentIndex = 0;
                var inTransition = false;
                var imageTransition = false;
                var changedHash = false;
                
                init();
                
                function init() {
                        
                        $(self).each(function(index){
                                items.push({
                                        element : this,
                                        scrollPosition : getScrollPosition(index),
                                        imageIndex : 0,
                                        images : $(this).find('.images li'),
                                        controls : $(this).find('.controls li a')
                                });
                                processImages(items[items.length-1]);
                                changeImage(items[items.length-1], 0); //reset to start for firefox
                                bindControls(items[items.length - 1]);
                                
                                $(this).css('position', 'relative');
                                
                                
                        });
                        
                        $(options.container).css('overflow', 'hidden');
                        setContainerHeight();
                        bindNavigation();
                        
                        $(options.container).scrollTop(0); //reset for firefox
                        
                        if (options.start) {
                                goToHash(options.start);
                        }
                }
                
                function bindControls(item) {
                        $(item.controls).each(function(index){
                                
                                $(this).click(function(e){
                                        changeImage(item, index);
                                        
                                        e.preventDefault();
                                });
                        });
                        
                        if (item.controls.length <= 1) {
                                
                                $(item.controls).parent().parent().hide();
                        }
                        
                }
                
                function processImages(item) {
                        $(item.images[0]).parent().css({
                                'position' : 'relative'
                        })
                        
                        $(item.images).each(function(index){
                                $(this).css({
                                        position : 'absolute',
                                        left : index * options.imageWidth
                                });
                        });
                        
                        $(item.element).find(options.imageContainer).scrollLeft(0);//reset for firefox
                }
                
                function getScrollPosition(index) {
                        var height = 0;
                        $(self).each(function(itemIndex){
                                if (itemIndex == index) {
                                     return false;   
                                }
                                
                                height += $(this).outerHeight();
                                
                                
                        });
                        
                        return height;
                }
                
                function setContainerHeight(index) {
                        index = (index || index === 0) ? index : currentIndex;
                        $(options.container).css('height', $(items[index].element).height());
                }
                
                function bindNavigation() {
                        $(navItems[0]).parent().addClass('current');
                        $(navItems).each(function(index){
                                $(this).click(function(e){
                                        
                                        changeItem(index);
                                        e.preventDefault();
                                });
                        });
                        
                        $(window).hashchange(function(e){
                                e.preventDefault();
                                
                                if (!changedHash) {
                                        goToHash(window.location.hash);
                                        changedHash = false;
                                }
                        });
                }
                
                function changeItem(index) {
                        if (inTransition) {
                                return;
                        }
                        
                        inTransition = true;
                        
                        changeSelected(index);
                        //set height as items can have different heights due to content
                        setContainerHeight(index);
                        
                        $(options.container).animate({
                                scrollTop : items[index].scrollPosition
                        }, options.mainTransitionTimer, function(){
                                
                                inTransition = false;
                                currentIndex = index;
                                changeHash(index);
                        });
                }
                
                function changeHash(index) {
                        changedHash = true;
                        //remove id before setting url hash to prevent browser scrolling
                        hash = $(items[index].element).attr('id');
                        $(items[index].element).attr('id', 'temp-' + hash);
                        
                        //set element for older browsers to scroll to
                        var temp = $('<div></div>').attr('id', hash).css({
                                'position' : 'absolute',
                                'top' : $(window).scrollTop()
                        });
                        
                        $('body').prepend(temp);
                        
                        window.location = window.location.pathname + '#' + hash;
                        $(items[index].element).attr('id', hash);
                        
                        $(temp).remove();
                        
                }
                
                function changeSelected(index) {
                        $(navItems[currentIndex]).parent().removeClass('current');
                        $(navItems[index]).parent().addClass('current');
                } 
                
                function goToHash(hash) {
                        if (hash) {
                                hash = hash.substring(1);
                                $(items).each(function(index){

                                        if (hash == $(this.element).attr('id')) {

                                                changeItem(index);
                                                return false;
                                        }
                                });
                        }
                        else {
                                changeItem(0);
                        }
                }
                
                function changeImage(item, index) {
                        
                        if (imageTransition) {
                                return;
                        }
                        
                        imageTransition = true;
                        
                        $(item.controls).removeClass('current');
                        $(item.controls[index]).addClass('current');
                                
                        $(item.element).find(options.imageContainer).animate({
                                scrollLeft : index * options.imageWidth
                        }, options.imageTransitionTimer, function(){
                                
                                item.imageIndex = index;
                                imageTransition = false;
                        });
                }
        }
})(jQuery);
