﻿//json call for adding video
function AddVideo(proxyserviceurl, parametersJson) {
    $.getJSON(proxyserviceurl
				, { ParametersJson: parametersJson, CacheBustDateTimeStamp: new Date().toString() }
				, function(json) {
				    json = JSON.parse(json.d);
				    if (json.Error) {
				        alert(json.Message);
				    } else {
				        window.location.reload()
				    }
				}
		);
}

function AddVideoParametersJson(AddVideoProxyServiceParameters, Link, Title, Caption, ArtistId) {
    // { "Link":"{Link}", "Title":"{Title}", "Caption":"{Caption}", "ArtistId":{ArtistId} }
    var parametersJson = AddVideoProxyServiceParameters;
    parametersJson = parametersJson.replace("{Link}", htmlentities(Link));
    parametersJson = parametersJson.replace("{Title}", htmlentities(Title));
    parametersJson = parametersJson.replace("{Caption}", htmlentities(Caption));
    parametersJson = parametersJson.replace("{ArtistId}", ArtistId);
    return parametersJson;
}

function openAddVideo() {
    return function() {
        var authenticated = ($("#AddVideo").attr("isauthenticated") == 'true');

        if (!authenticated) {
            alert('Login first to add videos.');
            return false;
        }

        $('#AddVideo').lightbox_me({
            centered: true,
            onLoad: function() {
                $('#AddVideo #AddVideoText').focus();
            },
            closeSelector: '.AddVideoCancel'
        });

        //if (!authenticated) {
        //    RecaptchaShow($("#AddVideo").attr("recaptchapublickey"), 'AddVideoRecaptcha', '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
$('#AddVideoLink').ready(function() {

    // lightbox setup on login click
    $('#AddVideoLink #AddVideoLinkButton').click(openAddVideo());

});

var artistId = $("#AddVideo").attr("artistid");

// Add Video Popup
$('#AddVideo').ready(function() {

    // clear link text box on enter if default 
    $('#AddVideo #AddVideoLink').focus(function() {
        if ($.trim($(this).val()) == $(this).attr("default")) {
            $(this).val('');
        }
    });

    // set default link text box if empty
    $('#AddVideo #AddVideoLink').blur(function() {
        if ($.trim($(this).val()) == '') {
            $(this).val($(this).attr("default"));
        }
    });

    // clear title text box on enter if default 
    $('#AddVideo #AddVideoTitle').focus(function() {
        if ($.trim($(this).val()) == $(this).attr("default")) {
            $(this).val('');
        }
    });

    // set default title text box if empty
    $('#AddVideo #AddVideoTitle').blur(function() {
        if ($.trim($(this).val()) == '') {
            $(this).val($(this).attr("default"));
        }
    });

    // clear caption text box on enter if default 
    $('#AddVideo #AddVideoCaptionText').focus(function() {
        if ($.trim($(this).val()) == $(this).attr("default")) {
            $(this).val('');
        }
    });

    // set default caption text box if empty
    $('#AddVideo #AddVideoCaptionText').blur(function() {
        if ($.trim($(this).val()) == '') {
            $(this).val($(this).attr("default"));
        }
    });

    // if press enter in caption click the button
    $('#AddVideo #AddVideoCaptionText').keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('#AddVideo #AddVideoSubmit').click();
            return false;
        } else {
            return true;
        }
    });

    // AddVideoSubmit Click
    $('#AddVideo #AddVideoSubmit').click(function() {

        var errorMessage = '';
        var hasError = false;
        var setFocus;        

        // trim any spaces
        var addVideoLink = $('#AddVideo #AddVideoLink').val($.trim($('#AddVideo #AddVideoLink').val()));
        var addVideoTitle = $('#AddVideo #AddVideoTitle').val($.trim($('#AddVideo #AddVideoTitle').val()));
        var addVideoCaption = $('#AddVideo #AddVideoCaptionText').val($.trim($('#AddVideo #AddVideoCaptionText').val()));

        if (($('#AddVideo #AddVideoLink').val() == $('#AddVideo #AddVideoLink').attr('default')) || ($('#AddVideo #AddVideoLink').val() == '')) {
            errorMessage = errorMessage + '-Please provide a link to the video\n';
            setFocus = $('#AddVideo #AddVideoLink');
            hasError = true;
        }

        if (($('#AddVideo #AddVideoTitle').val() == $('#AddVideo #AddVideoTitle').attr('default')) || ($('#AddVideo #AddVideoTitle').val() == '')) {
            errorMessage = errorMessage + '-Enter ' + $('#AddVideo #AddVideoTitle').attr('default') + '\n';
            setFocus = $('#AddVideo #AddVideoTitle');
            hasError = true;
        }

        if (($('#AddVideo #AddVideoCaptionText').val() == $('#AddVideo #AddVideoCaptionText').attr('default')) || ($('#AddVideo #AddVideoCaptionText').val() == '')) {
            errorMessage = errorMessage + '-Enter ' + $('#AddVideo #AddVideoCaptionText').attr('default') + '\n';
            setFocus = $('#AddVideo #AddVideoCaptionText');
            hasError = true;
        }

        if (hasError) {
            errorMessage = 'Please address the following:\n\n' + errorMessage + '\n';
            alert(errorMessage);
            $(setFocus).focus();
            return false;
        } else {
        var parametersJson = AddVideoParametersJson($("#AddVideo").attr("proxyserviceparameters"), $("#AddVideo #AddVideoLink").val(), $("#AddVideo #AddVideoTitle").val(), $("#AddVideo #AddVideoCaptionText").val(), $("#AddVideo").attr("artistid"));
            AddVideo($("#AddVideo").attr("proxyserviceurl"), parametersJson);
        }

        return false;
    });

});

