﻿
App.Blog = function () {

    /* Private */

    /* Properties */

    var cmp = {};





    /* Methods */

    var init = function () {

        /* Constructor */

        
        
        //set comment form
        if ($('#blogCommentForm').length > 0) {

            // Create the captcha
            // TVI5 Key
            //Recaptcha.create('6LcwesASAAAAAAPEWAH9BGj-8lce3ASjTN3Hm4NZ', 'recaptcha', { theme: 'clean' });
            // Live Key
            Recaptcha.create('6Lf3IcMSAAAAAOtYBVZje8S35njbhNmO0lJA6vl3', 'recaptcha', { theme: 'clean' });
            

            setCommentForm();

        }

    };



    var setCommentForm = function () {

        var currentDate = new Date();

        var month = new Array(12);
        month[0] = "January";
        month[1] = "February";
        month[2] = "March";
        month[3] = "April";
        month[4] = "May";
        month[5] = "June";
        month[6] = "July";
        month[7] = "August";
        month[8] = "September";
        month[9] = "October";
        month[10] = "November";
        month[11] = "December";

        var date = currentDate.getDate() + ' ' + month[currentDate.getMonth()] + ' ' + currentDate.getFullYear();

        var blogCommentForm = new TVI.Form({

            ID: 'blogCommentForm',
            query: 'blogAddComment',
            data: { articleID: App.Blog.articleID },
            success: function () {

                var thisComment = '<div class="blogBrowseArticle"><div class="credit"><span class="date">' + date + '</span><span class="name">' + blogCommentForm.field('name').val() + '</span></div><div class="text">' + blogCommentForm.field('message').val() + '</div></div>'

                if ($('.noComments').length != 0)
                    $('.commentContainer').html(thisComment);
                else
                    $('.commentContainer').append(thisComment);

                $('.TVI-form-submit').show();
                $('.TVI-form-loading').hide();
                blogCommentForm.reset();

            },
            failure: function (d) {
                this.failure(d);
                Recaptcha.reload();
            }
        });

        $('.submitButton').live('click', function () {

            challengeField = $("input#recaptcha_challenge_field").val();
            responseField = $("input#recaptcha_response_field").val();

            TVI.ajax({

                url: "/handlers/App.Blog.aspx/captcha",
                data: {
                    'remoteip': $('#ip').val(),
                    'challenge': challengeField,
                    'response': responseField
                },
                success: function (d) {

                    blogCommentForm.submit({});

                },
                failure: function (d) {
                    blogCommentForm.error('Your verification code was incorrect, please try again');

                    Recaptcha.reload();
                }

            });

            return false;
        });
    };




    /* Public */

    TVI.apply(cmp, {

        /* Properties */

        articleID: 0,
        ipAddress: ''

        /* Methods */

    });


    TVI.ready(init);



    return cmp;


} ();
