﻿/*javascript file to contain CMS/Jonathan related functions into 'Jonathan' variable.*/

var Jonathan =
{
    State: {},
    DefaultAnimationSpeed: 'slow'
};

Jonathan.Storying =
{
    UpdateBranch: function () {
        var updateBranchForm = $(this);
        var branchSavedSpan = updateBranchForm.find('span.branchSaved');
        var branchImageDiv = updateBranchForm.find('div.image-div');


        Utils.Ajax.SubmitForm(updateBranchForm,
                        'text',
                        function (data, textStatus, XMLHttpRequest) {
                            branchSavedSpan.fadeIn('slow');
                            branchSavedSpan.html('<strong>Saved!</strong>');
                            branchSavedSpan.fadeOut('slow');
                            branchImageDiv.html(data);
                        },
                        null
                       );
        return false;
    },

    UpdateBranchRequirement: function () {
        var updateBranchRequirementForm = $(this);
        var requirementsDiv = $(this).find('div.quality-requirements');
        var maxInput = updateBranchRequirementForm.find('input.max');
        var minInput = updateBranchRequirementForm.find('input.min');


        Utils.Ajax.SubmitForm(updateBranchRequirementForm,
                            'html',
                            function (data, textStatus, XMLHttpRequest) {
                                /*
                                maxInput.val('')
                                minInput.val('')
                                */
                                updateBranchRequirementForm[0].reset()
                                requirementsDiv.html(data);
                            },
                            null
                           );
        return false;
    },

    UpdateQualityEffect: function () {
        var updateQualityEffectForm = $(this);
        var qualitiesDiv = updateQualityEffectForm.find('.successEventEffects');

        var levelInput = updateQualityEffectForm.find('input.level');
        var ifAtLeastInput = updateQualityEffectForm.find('input.ifAtLeast');
        var ifNoMoreThanInput = updateQualityEffectForm.find('input.ifNoMoreThan');
        var setToInput = updateQualityEffectForm.find('input.setTo');

        var setToInputVal = setToInput.val();
        var confirmSetToExactly = false;

        if (!isNaN(setToInputVal)) {
            setToInputVal = parseInt(setToInputVal);
            if (setToInputVal < 0) {
                confirmSetToExactly = true;
            }
        }


        if(!confirmSetToExactly || confirm('Are you sure about \'setting to\' a negative value?')){
            Utils.Ajax.SubmitForm(updateQualityEffectForm,
                            'html',
                            function (data, textStatus, XMLHttpRequest) {
                                updateQualityEffectForm[0].reset()
                                /*
                                levelInput.val('');
                                ifAtLeastInput.val('');
                                ifNoMoreThanInput.val('');
                                setToInput.val('');
                                */
                                qualitiesDiv.html(data);
                            },
                            null
                           );
                        }

        return false;
    },

    AddBranchFor: function () {
        var addBranchForm = $(this);

        Utils.Ajax.SubmitForm(
            addBranchForm,
            'html',
            function (data, textStatus, XMLHttpRequest) {
                var newBranch = $(data).hide();
                $('#branches').append(newBranch);
                newBranch.fadeIn(Jonathan.DefaultAnimationSpeed);
                Utils.UI.ScrollIntoView(newBranch);
            },
            null
        );
        return false;
    },

    UpdateEvent: function () {
        var updateEventForm = $(this);

        Utils.Ajax.SubmitForm(
            updateEventForm,
            'json',
            function (data, textStatus, XMLHttpRequest) {
                updateEventForm.find('.event-image').html(data.EventImage);
                updateEventForm.find('.event-area-image').html(data.AreaImage);
                eventSaved(updateEventForm);
            }
            ,
            null
        );
        return false;
    },
    AddSuccessEvent: function () {
        var addSuccessEventForm = $(this);
        var failureCallback = function () {
            alert('You can only add a success if the branch has quality requirements with difficulty');
        }

        if (!addSuccessEventForm.parents('.branch').find('.quality-requirements a').length) {
            failureCallback();
        } else {

            Utils.Ajax.SubmitForm(
                addSuccessEventForm,
                'html',
                function (data, textStatus, XMLHttpRequest) {
                    if (data) {
                        var newForm = $(data).hide();
                        addSuccessEventForm.parents('div.branchWithChildEvents').find('.successEvent').append(newForm);
                        newForm.fadeIn(Jonathan.DefaultAnimationSpeed);
                        addSuccessEventForm.hide();
                        addSuccessEventForm.parents('div.branchWithChildEvents').find('.add-rare-success-event-form').show();
                    } else {
                        failureCallback();
                    }
                }
            );
        }
        return false;
    },
    AddRareSuccessEvent: function () {
        var addRareSuccessEventForm = $(this);
        var fireChance = addRareSuccessEventForm.find('[name = chance]').val();

        if (!(fireChance && parseInt(fireChance) && fireChance <= 100)) {
            alert('You must enter a percentage fire chance for the rare success event');
        }
        else {
            Utils.Ajax.SubmitForm(
                addRareSuccessEventForm,
                'html',
                function (data, textStatus, XMLHttpRequest) {
                    if (data) {
                        var newForm = $(data).hide();
                        addRareSuccessEventForm.parents('div.branchWithChildEvents').find('.rareSuccessEvent').append(newForm);
                        newForm.fadeIn(Jonathan.DefaultAnimationSpeed);
                        addRareSuccessEventForm.parents('div.branchWithChildEvents').find('.add-rare-success-event-form').hide();
                    }
                }
            );
        }
        return false;
    },
    EventType: {
        Default: 'Default',
        RareSuccess: 'RareSuccess',
        Success: 'Success'
    }
};


/*-----------------------------------------------------------*/

function getSetToRightNowForEventId(eventId) {
    return $("#setToRightNow" + eventId);
}
function getTimeDelayForEventId(eventId) {
    return $("#timeDelay" + eventId);
}
function getmessageForLaterForEventId(eventId) {
    return $("#messageForLater" + eventId);
}
function getSetToTxtForEventId(eventId) {
    return $("#setToTxtForEventId" + eventId);
}
function getLevelTxtForEventId(eventId) {
    return $("#levelTxtForEventId" + eventId);
}
function getIfAtLeastTextForEventId(eventId) {
    return $("#ifAtLeastTxtForEventId" + eventId);
}

function getIfNoMoreThanTextForEventId(eventId) {
    return $("#ifNoMoreThanTxtForEventId" + eventId);
}
function setQualityEffectControls(eventId, qualityId, level, setTo, ifAtLeast, ifNoMoreThan, timeDelay, setToRightNow, messageForLater) {
    var divId = "div#event" + eventId + "QualitiesAffected";
    $(divId + " > .selectQualityId").val(qualityId);

    var setToTxt = getSetToTxtForEventId(eventId);
    var levelTxt = getLevelTxtForEventId(eventId);
    var ifAtLeastText = getIfAtLeastTextForEventId(eventId);
    var ifNoMoreThanText = getIfNoMoreThanTextForEventId(eventId);
    var inputTimeDelay = getTimeDelayForEventId(eventId);
    var inputSetToRightNow = getSetToRightNowForEventId(eventId);
    var inputMessageForLater = getmessageForLaterForEventId(eventId);

    inputTimeDelay.val(timeDelay);
    inputSetToRightNow.val(setToRightNow);
    inputMessageForLater.val(messageForLater);


    if (setTo != -1) {
        setToTxt.children('input').val(setTo);
        levelTxt.children('input').val('');
        levelTxt.hide();
        setToTxt.show();
    }
    else {
        levelTxt.children('input').val(level);
        setToTxt.children('input').val('');
        setToTxt.hide();
        levelTxt.show();
    }
    ifAtLeastText.val(ifAtLeast);
    ifNoMoreThanText.val(ifNoMoreThan);
}

function setQualityEffectControlsToForm(linkInForm, eventId, qualityId, level, setTo, ifAtLeast, ifNoMoreThan) {
    linkInForm = $(linkInForm);


    var theForm = linkInForm.parents('form');

    theForm.find('.selectQualityId').val(qualityId);

    var setToTxt = theForm.find('.set-exactly');
    var levelTxt = theForm.find('.set-by');
    var ifAtLeastText = theForm.find('.ifAtLeast');
    var ifNoMoreThanText = theForm.find('.ifNoMoreThan');

    if (setTo != -1) {
        setToTxt.children('input').val(setTo);
        levelTxt.children('input').val('');
        levelTxt.hide();
        setToTxt.show();
    }
    else {
        levelTxt.children('input').val(level);
        setToTxt.children('input').val('');
        setToTxt.hide();
        levelTxt.show();
    }
    ifAtLeastText.val(ifAtLeast);
    ifNoMoreThanText.val(ifNoMoreThan);

}

function switchSetToAndLevel(eventId) {
    //toggle between Set To Exactly and Change By
    var setToTxt = getSetToTxtForEventId(eventId);
    var levelTxt = getLevelTxtForEventId(eventId);

    if (levelTxt.is(":visible")) {
        levelTxt.hide();
        levelTxt.children('input').val('');
        setToTxt.show();
    }
    else {
        setToTxt.hide();
        setToTxt.children('input').val('');
        levelTxt.show();
    }
}
function setQualityRequiredOnBranchControls(branchId, qualityId, min, difficulty, max, branchVisibleWhenRequirementFailed) {

    var divId = "div#branch" + branchId + "QualitiesRequired";

    $(divId + " > .selectQualityId").attr("form").reset();

    $(divId + " > .selectQualityId").val(qualityId);
    if (min != -1) { $(divId + " > .min").val(min); }
    if (difficulty != -1) { $(divId + " > .difficulty").val(difficulty); }
    if (max != -1) { $(divId + " > .max").val(max); }

    if (branchVisibleWhenRequirementFailed) {
        $(divId + " .branchVisibleWhenRequirementFailed").attr("checked", "checked");
    }
    else {
        $(divId + " .branchVisibleWhenRequirementFailed").removeAttr('checked');
     }

    
}
function setQualityRequiredOnEventControls(eventId, qualityId, min, max) {

    var divId = "div#event" + eventId + "QualitiesRequired";

    $(divId + " > .selectQualityId").val(qualityId);
    if (min != -1) { $(divId + " > .min").val(min); }
    if (max != -1) { $(divId + " > .max").val(max); }
}
function switchEditable(eventControlForm, makeEditable) {
    var nameDisplay = $(eventControlForm).find('.contentNameDisplay');
    var descDisplay = $(eventControlForm).find('.contentDescDisplay');

    var nameEdit = $(eventControlForm).find('.contentNameEdit');
    var descEdit = $(eventControlForm).find('.contentDescEdit');

    if (makeEditable) {
        nameDisplay.hide();
        descDisplay.hide();
        nameEdit.show();
        descEdit.show();
    }
    else {

        nameDisplay.html(nameEdit.val());
        descDisplay.html(descEdit.val());
        nameDisplay.show();
        descDisplay.show();
        nameEdit.hide();
        descEdit.hide();
    }
}
function eventSaved(eventControlForm) {
    var eventSavedSpan = $(eventControlForm).find(".eventSavedSpan");
    eventSavedSpan.fadeIn('slow');
    eventSavedSpan.html('<strong>Saved!</strong>');
    eventSavedSpan.fadeOut('slow');
    switchEditable(eventControlForm, false);
}

