
//window.addEvent('domready', function () {
//
//        var LeftScroll = new Fx.Scroll('container', {
//                wait: false,
//                duration: 3000,
//                offset: {'x': -40, 'y': 0},
//                transition: Fx.Transitions.Quad.easeInOut
//        });
//
//        var RightScroll = new Fx.Scroll('container', {
//                wait: false,
//                duration: 3000,
//                transition: Fx.Transitions.Quad.easeInOut
//        });
//        $('gonext').addEvent('click', function(event){
//		event = new Event(event).stop();
//                LeftScroll.toElement('clients');
//                document.getElementById('goback').style.display = 'inline';
//                document.getElementById('gonext').style.display = 'none';
//
//        });
//        $('goback').addEvent('click', function(event){
//                event = new Event(event).stop();
//                RightScroll.toElement('first');
//                document.getElementById('goback').style.display = 'none';
//                document.getElementById('gonext').style.display = 'inline';
//        });
//
//});

window.addEvent('domready', function () {

        var navigationAnimationController = new NavigationAnimationController();

        $('gonext').addEvent('click', function(event){
		event = new Event(event).stop();
                navigationAnimationController.scrollNext();
        });
        $('goback').addEvent('click', function(event){
                event = new Event(event).stop();
                navigationAnimationController.scrollPrev();
        });

});


var NavigationContext = new Class({
    initialize: function(config){
        this.config = config ? config : {};
        this.query = this.config.query ? this.config.query : '.box';

        this.pageBoxes = $$(this.query);
        this.pageCount = Math.ceil( this.pageBoxes.length / 2 );
        
        this.firstPage = 0;
        this.lastPage = this.pageCount - 1;
        this.setCurrentPage(this.firstPage);
    },

    // internal function
    set: function(curr, next, prev){
        this.currentPage = curr;
        this.nextPage = next;
        this.prevPage = prev;
    },

    // sets the current page
    setCurrentPage: function(page){
        if(page < this.firstPage){
            // set to first page if index is out of lower bounds
            this.setCurrentPage(this.firstPage);

        }else if(page == this.firstPage){
            // if first page, then prev page is null
            this.set(this.firstPage, 1, null);

        }else if(page == this.lastPage){
            // if last page, then next page is null
            this.set(this.lastPage, null, this.lastPage - 1);

        }else if(page > this.lastPage){
            // set to last page if index is out of upper bounds
            this.setCurrentPage(this.lastPage);

        }else{
            // default
            this.set(page, page + 1, page - 1);
        }
//        console.info('----------------------');
//        console.info('current page: ' + this.currentPage);
//        console.info('next page: ' + this.nextPage);
//        console.info('prev page: ' + this.prevPage);
    },

    getCurrentPage: function() {
        return this.currentPage;
    },

    getNextPage: function(){
        if(this.nextPage != null){
            return this.nextPage;
        }else{
            return this.currentPage;
        }
    },

    getPreviousPage: function(){
        if(this.prevPage != null){
            return this.prevPage;
        }else{
            return this.currentPage;
        }
    }
});

var NavigationAnimationController = new Class({
    initialize: function(config){
        this.config = config ? config : {};
        this.animationContainer = this.config.animationContainer ? this.config.animationContainer : 'container';

        this.leftScroll = new Fx.Scroll(this.animationContainer, {
            wait: false,
            duration: 3000,
            offset: {'x': -40, 'y': 0},
            transition: Fx.Transitions.Quad.easeInOut
        });

        this.rightScroll = new Fx.Scroll(this.animationContainer, {
            wait: false,
            duration: 3000,
            offset: {'x': -18, 'y': 0},
            transition: Fx.Transitions.Quad.easeInOut
        });

        this.navigationContext = new NavigationContext();
    },

    scrollToPage: function(page){
        var boxIndex = page * 2;
        if(page > this.navigationContext.getCurrentPage()){
            this.rightScroll.toElement(this.navigationContext.pageBoxes[boxIndex]);
        }else if(page < this.navigationContext.getCurrentPage()){
            this.leftScroll.toElement(this.navigationContext.pageBoxes[boxIndex]);
        }
        this.navigationContext.setCurrentPage(page);
        this.displayBtns(page);
    },

    scrollNext: function(){
        this.scrollToPage(this.navigationContext.getNextPage());
    },

    scrollPrev: function(){
        this.scrollToPage(this.navigationContext.getPreviousPage());
    },

    showPrevBtn: function(){
        document.getElementById('goback').style.display = 'inline';
    },

    hidePrevBtn: function(){
        document.getElementById('goback').style.display = 'none';
    },

    showNextBtn: function(){
        document.getElementById('gonext').style.display = 'inline';
    },

    hideNextBtn: function(){
        document.getElementById('gonext').style.display = 'none';
    },

    displayBtns: function(page){
        if(page == this.navigationContext.firstPage){
            this.hidePrevBtn();
        }else if(page == this.navigationContext.lastPage){
            this.hideNextBtn();
        }else{
            this.showNextBtn();
            this.showPrevBtn();
        }
    }
})
