﻿
App.Information = function() {

    /* Private */

    /* Properties */

    var cmp = {};





    /* Methods */

    var init = function() {

        /* Constructor */

        //open menu from category ID
        $('#ic-' + cmp.categoryID + ' UL').show();
        
        
        //search form
        cmp.infoSearchForm = new TVI.Form({
        
            ID: 'infoSearchForm',
            local: true,
            validate: false,
            success: function(){
            
                var keywords = this.field('keywords').val();
                
                if (keywords === ''){ this.reset(); return; }
            
                window.location.href = '/camping-info/search/' + keywords;
            
            }
        
        });
        
        cmp.infoSearchForm.field('keywords').val(cmp.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: cmp.articleID,
                    type: 'info',
                    remove: remove
                },
                success: function(d){
                
                    btn.find('.TVI-button-text').text(remove ? 'add to favourites' : 'added to favourites'); 
                
                }
            
            });
        
        });


        //set comment form
        if ($('#infoAddCommentForm').length > 0){

            cmp.infoAddCommentForm = new TVI.Form({
        
                ID: 'infoAddCommentForm',
                url: TVI.handlers + 'App.Info.aspx/addComment',
                data: {
                    articleID: cmp.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();
                
                    window.location.href = window.location.href;
                
                }
        
            });

            //set captcha
            Recaptcha.create('6Lcyz70SAAAAALnL7l8FLBnW8wJGXuQA8w8UP_lu', 'recaptcha', { theme: 'white' });
        
        }
        
        
        App.menu('campingExtras');

    };





    /* Public */

    TVI.apply(cmp, {

        /* Properties */

        articleID: 0,
        categoryID: 0,
        keywords: ''

        /* Methods */

    });


    TVI.ready(init);


    return cmp;


} ();
