var ExamplesPagination;
(function($){
    ExamplesPagination = {
    	speed: 500,
    	easing: 'swing',
    	
    	move: function(){
    		var self = this;
    		
    		this.pageContainer.animate({
    			left: 0 - (this.current * $(this.pages[0]).width()) + "px"
    		}, {
    			speed: this.speed,
    			easing: this.easing,
    			complete: function(){
    				self.updatePages();
    			}
    		});
    	},
    	
    	goTo: function(ind){
    		this.current = Math.min(0, Math.max(this.total - 1, ind));
    		this.move();
    	},
    	
    	next: function(){
    		this.current = Math.min(this.total - 1, this.current + 1);
    		this.move();
    	},
    	
    	prev: function(){
    		this.current = Math.max(0, this.current - 1);
    		this.move();
    	},
    	
    	updatePages: function(){
    		if(this.current === 0){
    			this.prevBtn.addClass('disabled');
    		} else {
    			this.prevBtn.removeClass('disabled');
    		}
    		if(this.current == this.total - 1){
    			this.nextBtn.addClass('disabled');
    		} else {
    			this.nextBtn.removeClass('disabled');
    		}
    	},
    	
    	initialize: function(){
    		var self = this;
    		
    		this.pageContainer = $('#pageexamples .navigation .pages');
    		this.pages = $('#pageexamples .navigation .page');
    		this.prevBtn = $('#pageexamples .navigation a.btn_examples_prevnext.prev');
    		this.nextBtn = $('#pageexamples .navigation a.btn_examples_prevnext.next');
    		
    		this.total = this.pages.length;
    		this.current = 0;
    		
    		this.prevBtn.click(function(event){
    			event.preventDefault();
    			self.prev();
    		});
    		this.nextBtn.click(function(event){
    			event.preventDefault();
    			self.next();
    		});
    		
    		this.updatePages();
    	}
    };
    
    $(document).ready(function(){
		if(document.location.pathname.substr(0,9) == "/examples"){
			var gotoSlide = document.location.search.match(/\?hash=([a-zA-Z0-9\-_]+)/);
			if(gotoSlide !== null){
				window.scrollTo(0,0);
				SlideDeckExamples.goTo($('#pageexamples dd').index($('#' + gotoSlide[1])));
			}

            var ssHeight = $('.slidedeck dd .screenshot').height();
            $('.slidedeck dd .screenshot img').each(function(){
                $(this).css({
                    marginTop: ((ssHeight - $(this).height()) / 2)
                });
            });
		}
        
		$('#pageexamples .navigation a.thumb').click(function(event){
			if($('#pageexamples .navigation').hasClass('index')){
				event.preventDefault();
				var example_id = this.href.split("#")[1];
				SlideDeckExamples.goTo($('#pageexamples dd').index($('#' + example_id)) + 1);
			}
		});

		ExamplesPagination.initialize();
    });
})(jQuery);
