﻿
App.Blog = function () {

    /* Private */

    /* Properties */

    var cmp = {};





    /* Methods */

    var init = function () {

        //set search form
        cmp.blogSearchForm = new TVI.Form({
        
            ID: 'blogSearchForm',
            validate: false,
            local: true,
            success: function(){

                var keywords = this.field('keywords').val();
            
                if (keywords === ''){ this.reset(); return; }

                window.location.href = '/camping-info/search/' + keywords;
            
            }
        
        });
        
        
        
        //handle favourite button
        TVI.event('.favouritesButton', 'click', function(){
        
            if (!App.user.loggedIn){
        
                TVI.Popup.show({
                
                    title: 'Favourites',
                    template: 'Please <a href="/login">log in</a> or <a href="/registration">create an account</a> to manage your favourite articles.'
                
                });
                
                return;
            
            }

            //run query adding/removing favourite            
            var btn = $(this);
            var remove = btn.find('.TVI-button-text').text().indexOf('added') > -1;
            
            TVI.Data.query({
            
                query: 'Info_addToFavourites',
                data: {
                    articleID: TVI.Blog.articleID,
                    type: 'blog',
                    remove: remove
                },
                success: function(d){
                
                    btn.find('.TVI-button-text').text(remove ? 'add to favourites' : 'added to favourites'); 
                
                }
            
            });
        
        });



        //handle comment added event to refresh page
        TVI.addListener('blogCommentAdded',function (id) {

            window.location.href = window.location.href.replace('#comments', '').replace('#comment', '');

        });


        //set comment form
        if ($('#blogAddCommentForm').length > 0){

            cmp.blogAddCommentForm = new TVI.Form({
        
                ID: 'blogAddCommentForm',
                url: TVI.handlers + 'App.Blog.aspx/addComment',
                data: {
                    articleID: App.Blog.articleID,
                    ip: cmp.ipAddress
                },
                submit: function(){
                
                    this.submit();
                    
                    this.hiddenData.captchaChallenge = $("input#recaptcha_challenge_field").val();
                    this.hiddenData.captchaResponse = $("input#recaptcha_response_field").val();
                
                },
                success: function(d){

                    this.success();
                
                    TVI.fireEvent('blogCommentAdded', d.newID);
                
                }
        
            });

            //set captcha
            Recaptcha.create('6Lcyz70SAAAAALnL7l8FLBnW8wJGXuQA8w8UP_lu', 'recaptcha', { theme: 'white' });
        
        }


        App.menu('blog');

    };


    TVI.ready(init);


    return cmp;


} ();
