﻿
App.Blog = function () {

    /* Private */

    /* Properties */

    var cmp = {};


    TVI.Blog = {};


    /* Methods */

    var init = function () {

        /* Constructor */

        //set comment form
        if ($('#blogCommentForm').length > 0) {
            // Create the captcha
            //Recaptcha.create('6LeG7MUSAAAAAJ9vYK5LbWk6hjAP-3jlIStLS80C', 'recaptcha', { theme: 'white' });
            // TVI5.co.uk
            Recaptcha.create('6LehFcYSAAAAALrPVFCCCH8mEbSWJm1Sob3KpwSk', 'recaptcha', { theme: 'white' });

            setCommentForm();

        }

    };



    var setCommentForm = function () {

        var currentDate = new Date();

        var month = currentDate.getMonth() + 1

        month = month < 10 ? '0' + month : month;

        var date = currentDate.getDate() + '/' + month + '/' + currentDate.getFullYear();

        var blogCommentForm = new TVI.Form({

            ID: 'blogCommentForm',
            query: 'Blog_addComment',
            data: { articleID: App.Blog.articleID },
            success: function () {

                var content = '' +
                '<div class="blogComment">' +
                    '<span>' + date + ' - </span>' +
                    '<span>' + $('#blogCommentForm-name-control').val() + '</span>' +
                    '<div class="clear"></div>' +
                    '<p class="sansSerif">' + $('#blogCommentForm-message-control').val() + '</p>' +
                '</div>';

                $('.blogComments').prepend(content);

                $('.submitButton').hide();
                $('#blogCommentForm .TVI-form-fields').fadeOut('fast', function () {

                    $('#blogCommentForm .TVI-form-complete').fadeIn('fast');

                    setTimeout(function () {

                        $('#blogCommentForm .TVI-form-complete').fadeOut('fast', function () {

                            $('#blogCommentForm .TVI-form-complete').fadeIn('fast');
                            blogCommentForm.reset();
                            Recaptcha.reload();
                            $('.submitButton').show();

                        });

                    }, 2000);

                });




            },
            failure: function (d) {

                this.failure(d);
                Recaptcha.reload();



            }
        });

        $('.submitButton').live('click', function () {
            challengeField = $("input#recaptcha_challenge_field").val();
            responseField = $("input#recaptcha_response_field").val();

            blogCommentForm.validate({

                success: function () {

                    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;


} ();
