MM = {};

MM.Gallery = {
    PHOTO_DATA: [],
    CURRENT_ALBUM: 0,
    
    loadGalleryDataBindable: function() {
        var that = this;
        MM.Gallery.loadGalleryDataByAlbum($(this).attr('album_id'), function(data) {
            MM.Gallery.renderPage(1);
        });
        
        return false;
    },
    
    loadGalleryDataByAlbum: function(album_id, success_callback) {
        var that = this;
        $.get('/photos/ajaxGallery.html', { 'album_id': album_id }, function(data) {
            that.PHOTO_DATA = data.photo_data;
            MM.Gallery.Pagination.NUM_PHOTOS = data.photo_count;
            that.CURRENT_ALBUM = album_id;
            success_callback(data);
        }, 'json');
    },
    
    renderPage: function(page) {
        var page_photos = this.getDataSlice(MM.Gallery.Pagination.getOffsetsByPage(page));
        
        // fade out existing photos
        $('.photo_wrapper td.photo_cell img').hide();
        $('.photo_wrapper tr.photo_caption .photo_caption_text').hide();
            
        // replace src tag on left and right photos
        if(page_photos[0] != undefined) {
            this.renderPhoto('nw', page_photos[0]);
            $('#photo_nw').fadeIn(200);
            $('#photo_nw td.photo_cell img').fadeIn(500);
            $('#photo_nw tr.photo_caption .photo_caption_text').fadeIn(500);
        } else {
            $('#photo_nw').fadeOut(200);
        }
        
        if(page_photos[1] != undefined) {
            this.renderPhoto('ne', page_photos[1]);
            $('#photo_ne').fadeIn(200);
            $('#photo_ne td.photo_cell img').fadeIn(500);
            $('#photo_ne tr.photo_caption .photo_caption_text').fadeIn(500)
        } else {
            $('#photo_ne').fadeOut(200);
        }
        
        if(page_photos[2] != undefined) {
            this.renderPhoto('sw', page_photos[2]);
            $('#photo_sw').fadeIn(200);
            $('#photo_sw td.photo_cell img').fadeIn(500);
            $('#photo_sw tr.photo_caption .photo_caption_text').fadeIn(500);
        } else {
            $('#photo_sw').fadeOut(200);
        }
        
        if(page_photos[3] != undefined) {
            this.renderPhoto('se', page_photos[3]);
            $('#photo_se').fadeIn(200);
            $('#photo_se td.photo_cell img').fadeIn(500);
            $('#photo_se tr.photo_caption .photo_caption_text').fadeIn(500)
        } else {
            $('#photo_se').fadeOut(200);
        }
            
        // update current page
        MM.Gallery.Pagination.setCurrentPage(page);
        
        // initiate fancybox (for popups)
        this.initFancybox();
    },
    
    initFancybox: function() {
        $(".photo_wrapper a.fancybox").fancybox({
            'zoomSpeedIn': 300,
            'zoomSpeedOut': 300,
            'overlayShow': true,
            'showCloseButton': true,
        }); 
    },
    
    renderNextPage: function() {
        var next_page = MM.Gallery.Pagination.getNextPage();
        if(next_page) {
            this.renderPage(next_page);
        }
    },
    
    renderPreviousPage: function() {
        var prev_page = MM.Gallery.Pagination.getPreviousPage();
        if(prev_page) this.renderPage(prev_page);
    },
    
    
    renderPhoto: function(photo_selector, photo_data) {
        $('#photo_'+photo_selector+' img').attr('src', '/uploads/photos/'+photo_data.photo);
        $('#photo_'+photo_selector+' a.fancybox').attr('href', '/uploads/photos/'+photo_data.photo);
        $('#photo_'+photo_selector+' .photo_caption .photo_caption_text').html(photo_data.caption);
    },
    
    getDataSlice: function(offsets) {
        return this.PHOTO_DATA.slice(offsets.start, offsets.end);
    },
    
};

MM.Gallery.UI = {
    bindAnchors: function() {
        //$('.album_link').click(MM.Gallery.loadGalleryDataBindable);
    },
};

MM.Gallery.UI.Menu = {
    bindAnchors: function() {
        $('.album_menu .album_category a.album_category_link').click(function() {
            $(this).parent().find('ul').toggle();
            return false;
        });
    },
};

MM.Gallery.Pagination = {
    CURRENT_PAGE: 1,
    NUM_PHOTOS: 0,
    PHOTOS_PER_PAGE: 4,
    
    getCurrentPage: function() {
        return this.CURRENT_PAGE;
    },
    
    setCurrentPage: function(page) {
        this.CURRENT_PAGE = page;
        return this.CURRENT_PAGE;
    },
    
    getCount: function() {
        return this.NUM_PHOTOS;
    },
    
    getNextPage: function() {
        var next_page = this.CURRENT_PAGE + 1;
        return (next_page <= this.getPageCount()) ? next_page : false;
    },
    
    getPreviousPage: function() {
        var prev_page = this.CURRENT_PAGE - 1;
        return (prev_page >= 1) ? prev_page : false;
    },
    
    getPageCount: function() {
        return Math.ceil(this.NUM_PHOTOS / this.PHOTOS_PER_PAGE);
    },
    
    getOffsetsByPage: function(page) {
        var that = this;
        
        var start = this.PHOTOS_PER_PAGE * (page-1);
        var end = start + this.PHOTOS_PER_PAGE;
        
        if(start >= this.NUM_PHOTOS) {
            return false;
        } else {
            return { 'start': start, 'end': end, };
        }
    },
    
    renderPagination: function(page) {
        $('.pagination_wrapper').html('');
        
        $('.pagination_wrapper').append($('<a href="#" class="pagination_link_prev">Prev</a>'));
        for(var i=1; i<=this.getPageCount(); ++i) {
            $('.pagination_wrapper').append($('<a href="#" class="pagination_link_page" page_number="'+i+'">'+i+'</a>'));
        }
        $('.pagination_wrapper').append($('<a href="#" class="pagination_link_next">Next</a>'));
        
        this.bindPaginationAnchors();
    },
    
    bindPaginationAnchors: function() {
        $('.photo_arrows_wrapper .photo_arrow_left a').click(function() { MM.Gallery.renderPreviousPage(); return false; });
        $('.photo_arrows_wrapper .photo_arrow_right a').click(function() { MM.Gallery.renderNextPage(); return false; });
    },
};

MM.Blog = {};

MM.Blog.Comments = {
    
    submitComment: function(form_el) {
        var that = this;
        
        var submit_values = {
            'blog_id': form_el.attr('blog_id'),
            'author': form_el.find('.cf_author').val(),
            'comment': form_el.find('.cf_comment').val(),
        }
        
        $.post('/blog/ajaxSubmitComment.html', submit_values, function(data) {
            
            if(data.has_errors) {
                alert(data.error_text);
            } else {
                that.prependNewComment(data.comment_data);
                form_el.find('.cf_author').val('');
                form_el.find('.cf_comment').val('');
                form_el.parent().hide();
            }
            
        }, 'json');
    },
    
    prependNewComment: function(data) {
        var comment_template = $('#templates .comment').clone();
        comment_template.find('.comment_author').html(data.author);
        comment_template.find('.comment_comment').html(data.comment);
        
        $('.comments_wrapper[blog_id='+data.blog_id+']').prepend(comment_template);
    },
    
    formOnSubmitBindable: function(e) {
        MM.Blog.Comments.submitComment($(this));
        return false;
    },
    
    leaveCommentLinkBindable: function(e) {
        $(this).parent().parent().find('.create_comment_form').toggle();
        return false;
    },
    
};
