﻿// <!--
function showuseruploads(href) {
    var openargs = 'width=350,height=300,scrollbars=yes'
    window.open(href, 'useruploads', openargs);
    return false;
}
// -->

//json call for photo
function AddPhoto(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 AddPhotoParametersJson(AddPhotoProxyServiceParameters, RecaptchaChallenge, RecaptchaResponse, File, Abstract, Title, Points) {
    // { "RecaptchaChallenge":"{RecaptchaChallenge}", "RecaptchaResponse":"{RecaptchaResponse}", "File" : "{File}", "Abstract" : "{Abstract}", "Title" : "{Title}", "Points" : {Points} }
    var parametersJson = AddPhotoProxyServiceParameters;
    parametersJson = parametersJson.replace("{RecaptchaChallenge}", RecaptchaChallenge);
    parametersJson = parametersJson.replace("{RecaptchaResponse}", RecaptchaResponse);
    parametersJson = parametersJson.replace("{File}", File);
    parametersJson = parametersJson.replace("{Abstract}", Abstract);
    parametersJson = parametersJson.replace("{Title}", Title);
    parametersJson = parametersJson.replace("{Points}", Points);
    return parametersJson;
}

function openAddPhoto() {
    return function() {
        var authenticated = ($("#AddPhoto").attr("isauthenticated") == 'true');

        if (authenticated) {
            alert('Login first to add photos.');
            return false;
        }

        $('#AddPhoto').lightbox_me({
            centered: true,
            onLoad: function() {
                $('#AddPhoto #AddPhotoText').focus();
            },
            closeSelector: '.AddPhotoCancel'
        });

        if (!authenticated) {
            RecaptchaShow($("#AddPhoto").attr("recaptchapublickey"), 'AddPhotoRecaptcha', 'white');
        }
        return false;
    };
};


// Fan Talk control
$('#AddPhotoLink').ready(function() {

    // lightbox setup on login click
    $('#AddPhotoLink #AddPhotoLinkButton').click(openAddPhoto());

});

var fanId = $("#AddPhoto").attr("fanid");
var artistId = $("#AddPhoto").attr("artistid");

// Uploadify control
$(document).ready(function() {
    $('#AddPhotoFile').uploadify({
        'uploader': 'http://localhost/TodaysChristianMusic/SiteInfo/Media/uploadify.swf',
        'script': 'http://localhost/TodaysChristianMusic/SiteInfo/Handlers/Upload.ashx',
        'scriptData': { 'fanId': fanId, 'artistId': artistId },
        'cancelImg': 'http://localhost/TodaysChristianMusic/SiteInfo/Media/cancel.png',
        'auto': true,
        'multi': false,
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.png;*.gif;*.jpeg',
        'queueSizeLimit': 1,
        'sizeLimit': 4000000,
        'buttonText': 'Choose Image',
        'folder': '',
        'onAllComplete': function(event, queueID, fileObj, response, data) {
            alert(fileObj);
            if (response == "true") {
                $('#AddPhoto #AddPhotoImageStatus').text("Image was successfully attached.");
            } else {
                $('#AddPhoto #AddPhotoImageStatus').text("We were unable to attach the image. Please try again.");
            }
        }
    });
});
     

// Add Photo Popup
$('#AddPhoto').ready(function() {

    // clear title text box on enter if default 
    $('#AddPhoto #AddPhotoTitle').focus(function() {
        if ($.trim($(this).val()) == $(this).attr("default")) {
            $(this).val('');
        }
    });

    // set default title text box if empty
    $('#AddPhoto #AddPhotoTitle').blur(function() {
        if ($.trim($(this).val()) == '') {
            $(this).val($(this).attr("default"));
        }
    });

    // clear caption text box on enter if default 
    $('#AddPhoto #AddPhotoCaptionText').focus(function() {
        if ($.trim($(this).val()) == $(this).attr("default")) {
            $(this).val('');
        }
    });

    // set default caption text box if empty
    $('#AddPhoto #AddPhotoCaptionText').blur(function() {
        if ($.trim($(this).val()) == '') {
            $(this).val($(this).attr("default"));
        }
    });

    // if press enter in caption click the button
    $('#AddPhoto #AddPhotoCaptionText').keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('#AddPhoto #AddPhotoSubmit').click();
            return false;
        } else {
            return true;
        }
    });

    // AddPhotoSubmit Click
    $('#AddPhoto #AddPhotoSubmit').click(function() {

        var errorMessage = '';
        var hasError = false;
        var setFocus;

        // trim any spaces
        $('#AddPhoto #AddPhotoFilename').val($.trim($('#AddPhoto #AddPhotoFilename').val()));
        $('#AddPhoto #AddPhotoTitle').val($.trim($('#AddPhoto #AddPhotoTitle').val()));
        $('#AddPhoto #AddPhotoCaptionText').val($.trim($('#AddPhoto #AddPhotoCaptionText').val()));

        if (($('#AddPhoto #AddPhotoCaptionText').val() == $('#AddPhoto #AddPhotoCaptionText').attr('default')) || ($('#AddPhoto #AddPhotoCaptionText').val() == '')) {
            errorMessage = errorMessage + '-Enter ' + $('#AddPhoto #AddPhotoCaptionText').attr('default') + '\n';
            setFocus = $('#AddPhoto #AddPhotoCaptionText');
            hasError = true;
        }

        if (($('#AddPhoto #AddPhotoTitle').val() == $('#AddPhoto #AddPhotoTitle').attr('default')) || ($('#AddPhoto #AddPhotoTitle').val() == '')) {
            errorMessage = errorMessage + '-Enter ' + $('#AddPhoto #AddPhotoTitle').attr('default') + '\n';
            setFocus = $('#AddPhoto #AddPhotoTitle');
            hasError = true;
        }

        if (($('#AddPhoto #AddPhotoFilename').val() == ($('#AddPhoto #AddPhotoFilename').val() == ''))) {
            errorMessage = errorMessage + '-Select an image to upload\n';
            setFocus = $('#AddPhoto #AddPhotoFilename');
            hasError = true;
        }

        if (hasError) {
            errorMessage = 'Please address the following:\n\n' + errorMessage + '\n';
            alert(errorMessage);
            $(setFocus).focus();
            return false;
        } else {
            var parametersJson = AddPhotoParametersJson($("#AddPhoto").attr("proxyserviceparameters"), RecaptchaChallenge(), RecaptchaResponse, $("#AddPhoto #AddPhotoFilename").val(), $("#AddPhoto #AddPhotoCaptionText").val(), $("#AddPhoto #AddPhotoTitle").val(), $("#AddPhoto").attr("points"));
            AddPhotoArticle($("#AddPhoto").attr("proxyserviceurl"), parametersJson);
        }

        return false;
    });

});

