﻿//json call for comment
function AddCommentVideo(proxyserviceurl, parametersJson) {
    $.getJSON(proxyserviceurl
				, { ParametersJson: parametersJson, CacheBustDateTimeStamp: new Date().toString() }
				, function(json) {
				    json = JSON.parse(json.d);
				    if (json.Error) {
				        alert(json.Message);
				        if (json.RecaptchaError) RecaptchaReload();
				    } else {
				        window.location.reload()
				    }
				}
		);
}

function AddCommentVideoParametersJson(AddCommentVideoProxyServiceParameters, RecaptchaChallenge, RecaptchaResponse, VideoId, Comment) {
    // { "RecaptchaChallenge":"{RecaptchaChallenge}", "RecaptchaResponse":"{RecaptchaResponse}", "VideoId" : {VideoId}, "Comment" : "{Comment}" }
    var parametersJson = AddCommentVideoProxyServiceParameters;
    parametersJson = parametersJson.replace("{RecaptchaChallenge}", RecaptchaChallenge);
    parametersJson = parametersJson.replace("{RecaptchaResponse}", RecaptchaResponse);
    parametersJson = parametersJson.replace("{VideoId}", VideoId);
    parametersJson = parametersJson.replace("{Comment}", htmlentities(Comment));
    return parametersJson;
}

function openAddCommentVideo(allowGuest) {
    return function() {
        var authenticated = ($("#AddCommentVideo").attr("isauthenticated") == 'true');

        if (!authenticated && !allowGuest) {
            alert('Login first to comment as a member.');
            return false;
        }

        $('#AddCommentVideo').lightbox_me({
            centered: true,
            zIndex: 999,
            onLoad: function() {
                $('#AddCommentVideo #AddCommentVideoText').focus();
            },
            closeSelector: '.AddCommentVideoCancel'
        });
        if (!authenticated) {
            RecaptchaShow($("#AddCommentVideo").attr("recaptchapublickey"), 'AddCommentVideoRecaptcha', 'white');
        }
        return false;
    };
};

function htmlentities(str) {
    var i, output = '', len, chr = '';
    len = str.length;
    for (i = 0; i < len; i++) {
        char = str[i].charCodeAt(0);
        if ((char > 47 && char < 58) || (char > 62 && char < 127)) {
            output += str[i];
        } else {
            output += "&#" + str[i].charCodeAt(0) + ";";
        }
    }
    return output;
}

// Fan Talk control
$('#FanTalk').ready(function() {

    // lightbox setup on login click
    $('#FanTalk #FanTalkAddCommentVideoGuest').click(openAddCommentVideo(true));
    $('#FanTalk #FanTalkAddCommentVideo').click(openAddCommentVideo(false));

});

// Add Comment Popup
$('#AddCommentVideo').ready(function() {

    // clear keyword text box on enter if default email
    $('#AddCommentVideo #AddCommentVideoText').focus(function() {
        if ($.trim($(this).val()) == $(this).attr("default")) {
            $(this).val('');
        }
    });

    // set default keyword text box if empty
    $('#AddCommentVideo #AddCommentVideoText').blur(function() {
        if ($.trim($(this).val()) == '') {
            $(this).val($(this).attr("default"));
        }
    });


    // if press enter in email click the button
    $('#AddCommentVideo #AddCommentVideoText').keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('#AddCommentVideo #AddCommentVideoSubmit').click();
            return false;
        } else {
            return true;
        }
    });

    // AddCommentArticleSubmit Click
    $('#AddCommentVideo #AddCommentVideoSubmit').click(function() {

        var errorMessage = '';
        var hasError = false;
        var setFocus;

        // trim any spaces
        $('#AddCommentVideo #AddCommentVideoText').val($.trim($('#AddCommentVideo #AddCommentVideoText').val()));

        if (($('#AddCommentVideo #AddCommentVideoText').val() == $('#AddCommentVideo #AddCommentVideoText').attr('default')) || ($('#AddCommentVideo #AddCommentVideoText').val() == '')) {
            errorMessage = errorMessage + '-Enter ' + $('#AddCommentVideo #AddCommentVideoText').attr('default') + '\n';
            setFocus = $('#AddCommentVideo #AddCommentVideoText');
            hasError = true;
        }

        if (hasError) {
            errorMessage = 'Please address the following:\n\n' + errorMessage + '\n';
            alert(errorMessage);
            $(setFocus).focus();
            return false;
        } else {
            var parametersJson = AddCommentVideoParametersJson($("#AddCommentVideo").attr("proxyserviceparameters"), RecaptchaChallenge(), RecaptchaResponse, $("#AddCommentVideo").attr("VideoId"), $("#AddCommentVideo #AddCommentVideoText").val());
            AddCommentVideo($("#AddCommentVideo").attr("proxyserviceurl"), parametersJson);
        }

        return false;
    });

});

