window.addEvent('domready', function () {
    if (window.location.hash == '#resources') {
        window.location = '/resources/';
        return true;
    }
    
    Element.implement({
        'hideWorkaround': function() {
            var hideWorkaround = this.retrieve('hideWorkaround');
            if (!hideWorkaround || hideWorkaround === null) {
                this.store('hideWorkaround', true);
                this.setStyles({
                    'left': '-99999px',
                    'position': 'absolute',
                    'visibility': 'hidden'
                });
            }
        },

        'showWorkaround': function() {
            var hideWorkaround = this.retrieve('hideWorkaround');
            if (hideWorkaround && hideWorkaround !== null ) {
                this.store('hideWorkaround', false);
                this.setStyles({
                    'left': 'auto',
                    'position': 'relative',
                    'visibility': 'visible'
                });
            }
        },
        
        'showOnlyChildren': function(except) {
            this.getChildren('.post').each(function(child) {
                if (!child.match(except)) {
                    child.hideWorkaround();
                } else {
                    child.showWorkaround();
                }
            });
        }
    });

    var Post = new Class({
        initialize: function(post) {
            this.container = new Element(
                'div',{
                    'class': 'post ' + post.PostCategories.slug
                }
            );

            this.postCat = new Element('div',{
                'class': 'postCat',
                'html': post.PostCategories.title
            });
            this.container.grab(this.postCat);

            this.postBody = new Element('div',{
                'class': 'postBody',
                'html': post.Post.body
            });
            this.container.grab(this.postBody);
            
            this.arrowCont = new Element('div',{
                'class': 'arrowCont'
            });
            this.container.grab(this.arrowCont);

            this.postLnk = new Element('a',{
                'href': post.Post.url
            });
            this.arrowCont.grab(this.postLnk);
            
            this.arrowImg = new Element('img',{
                'src': '/img/home/rightArrowBlue.png'
            });
            this.postLnk.grab(this.arrowImg);
            
            return this.container;
        }
    });
    
    var $posts = $('posts');

    function markAllFiltersInactive() {
        $$('#postFilters .filter').removeClass('active');
    }

    var imagesToPreload = [
        '/img/home/postCatMobileViewAll.png',
        '/img/home/postCatMobileQuestionsActive.png',
        '/img/home/postCatMobileSharedActive.png',
        '/img/home/postCatMobileGeneralActive.png',
        '/img/home/postCatMobileAnnouncementsActive.png',

        '/img/home/postCatViewall.png',
        '/img/home/postCatQuestionsActive.png',
        '/img/home/postCatSharedActive.png',
        '/img/home/postCatGeneralActive.png',
        '/img/home/postCatAnnouncementsActive.png'
    ];

    Asset.images(imagesToPreload);
    
    // post filters click events
    var $viewAllFilter = $('viewAllFilter');
    $viewAllFilter.addEvent('click', function(e){
        if (e !== undefined) {
            e.preventDefault();
        }
        
        $posts.getChildren().showWorkaround();
        markAllFiltersInactive();
        $viewAllFilter.addClass('active');
    });
    
    var $questionsFilter = $('questionsFilter');
    $questionsFilter.addEvent('click', function(e){
        if (e !== undefined) {
            e.preventDefault();
        }
        
        $posts.showOnlyChildren('.questions_answers');
        markAllFiltersInactive();
        $questionsFilter.addClass('active');
    });
    
    var $sharedFilter = $('sharedFilter');
    $sharedFilter.addEvent('click', function(e){
        if (e !== undefined) {
            e.preventDefault();
        }

        $posts.showOnlyChildren('.shared_stories');
        markAllFiltersInactive();
        $sharedFilter.addClass('active');
    });
    
    var $generalHealthFilter = $('generalHealthFilter');
    $generalHealthFilter.addEvent('click', function(e){
        if (e !== undefined) {
            e.preventDefault();
        }

        $posts.showOnlyChildren('.general_health');
        markAllFiltersInactive();
        $generalHealthFilter.addClass('active');
    });
    
    $announcementsFilter = $('announcementsFilter');
    $announcementsFilter.addEvent('click', function(e){
        if (e !== undefined) {
            e.preventDefault();
        }

        $posts.showOnlyChildren('.announcements');
        markAllFiltersInactive();
        $announcementsFilter.addClass('active');
    });

    function addPostClickShortCut() {
        $posts.getChildren('.post').each(function(post) {
            function goToInteralLinkUrl() {
                window.location = post.getChildren('.arrowCont a').get('href');
            }
            
            post.removeEvent('click', goToInteralLinkUrl);
            post.addEvent('click', goToInteralLinkUrl);
        });
    }

    addPostClickShortCut();

    var $moreConvoLnk = $('moreConvoLnk');
    
    $moreConvoLnk.store('postOffset', 0);
    
    $moreConvoLnk.addEvent('click', function(e) {
        e.preventDefault();
        
        var newOffest = parseInt($moreConvoLnk.retrieve('postOffset'));
        newOffest++;
        $moreConvoLnk.store('postOffset', newOffest);

        var spinner = new Element('img', { 
            src: '/css/Spinner/spinner.gif',
            styles: {
                display: 'block',
                margin: 'auto',
                clear: 'both',
                padding: '3em 0'
            }
        });

        spinner.fade('hide');
        $posts.grab(spinner);
        spinner.fade('in');
        
        var jsonRequest = new Request.JSON({
            url: '/home/getMorePosts/'+$moreConvoLnk.retrieve('postOffset'),
            onSuccess: function(posts){
                spinner.get('tween').chain(function(){
                    spinner.fade('out').get('tween').chain(function() {
                        spinner.dispose();
                    });
                });

                if (posts.lastSet) {
                    $moreConvoLnk.dispose();
                }

                var activeFilterId = $$('#postFilters .active').get('id');

                posts.posts.each(function(post) {
                    var post = new Post(post);
                    post.fade('hide');
                    post.inject(spinner, 'before');
                    post.fade('in');
                });
                
                $(''+activeFilterId).fireEvent('click');
                addPostClickShortCut();
            }
        }).get();
    });

    $('buyBookBtn').addEvent('click', function(e) {
        e.preventDefault();
        buyBookModal.show();
    });

    $('joinTheConversationBtn').addEvent('click', function(e) {
        e.preventDefault();
        joinConvoModal.show();
    });
});

