$(document).ready(function(){
    myPageController = new ComicController();
    myPageController.initClickAction();
});

var ComicController = Controller.extend({
    init: function(){
        this._super();
        this.setBuffer('theVaultListBuffer','A');
        this.setBuffer('theVaultPreviewBuffer','A');
        
        this.isPreviewLoading = false;
        this.currentPreview   = 0;
        
        $('.comicPanels').fadeIn(750);
    },
    
    openTheVault: function() {
        $('.popupTheVault').dialog({title: 'The Vault', width: 675, height: 375, modal: true, resizable: false});
        
        if($('#theVaultListPage').val() == undefined || $('#theVaultListPage').val() == '') {
            this.loadPage(1, 'fade');
            $('#loadNewerComics').removeClass('clickAction');
            $('#loadNewerComics').addClass('disabled');
        }
    },
    
    loadNewerComics: function() {
        var currentPage = parseInt($('#theVaultListPage').val());
        var pagesCount  = parseInt($('#theVaultListPagesCount').val());
        
        if(currentPage > 1) {
            this.loadPage(currentPage - 1, 'dropRight');
            
            if(currentPage == 2) {
                $('#loadNewerComics').removeClass('clickAction');
                $('#loadNewerComics').addClass('disabled');
            }
            $('#loadOlderComics').removeClass('disabled');
            $('#loadOlderComics').addClass('clickAction');
        }
    },
    
    loadOlderComics: function() {
        var currentPage = parseInt($('#theVaultListPage').val());
        var pagesCount  = parseInt($('#theVaultListPagesCount').val());
        
        if(currentPage < pagesCount) {
            this.loadPage(currentPage + 1, 'dropLeft');

            if(currentPage + 1 == pagesCount) {
                $('#loadOlderComics').removeClass('clickAction');
                $('#loadOlderComics').addClass('disabled');
            }
            $('#loadNewerComics').removeClass('disabled');
            $('#loadNewerComics').addClass('clickAction');
        }
    },
    
    loadPage: function(page, effect) {
        var newListBuffer = this.getBuffer('theVaultListBuffer');

        $.ajax({url:      this.baseUrl + 'comic/xloadpage',
                type:     'POST',
                dataType: 'html',
                data:     {page   : page,
                           buffer : newListBuffer},
                success:  function(response) {
                            myPageController.loadContentCallback('theVaultList', response, effect, 'myPageController.loadPageCallback()');
                          }
        });
    },
    
    loadPageCallback: function() {
        this.loadPreview(parseInt($('#theVaultListFirstComicID').val()));
        this.initMouseOverAction();
    },
    
    loadPreview: function(comic_id) {
        if(!this.isPreviewLoading && this.currentPreview != comic_id) {
            this.isPreviewLoading = true;
            this.currentPreview   = comic_id;
            var newPreviewBuffer  = this.getBuffer('theVaultPreviewBuffer');
            
            $.ajax({url:      this.baseUrl + 'comic/xloadpreview',
                    type:     'POST',
                    dataType: 'html',
                    data:     {comic_id   : comic_id,
                               buffer     : newPreviewBuffer},
                    success:  function(response) {
                                myPageController.loadContentCallback('theVaultPreview', response, 'fade', 'myPageController.resetPreviewLoading()');
                              }
            });
        }
    },
    
    resetPreviewLoading: function() {
        this.isPreviewLoading = false;
    }
});
