﻿
var UxCultureValues = [];
var UxStartingCityValues = [];
var UxSexValues = [];
var UxWealthValues = [];
var LastCultureId = null;

function QuickFill() {
    //$('#UxCulture').selectOptions("African", true)
    if (!doQuickFill && $('#' + UxLastNameClientId).html() != "Surname") {
        return; 
    }

    $('#UxCulture option').each(function (i, selected) { UxCultureValues.push(parseInt($(selected).val())); });
    $('#' + UxStartingCityClientId + ' option').each(function (i, selected) { UxStartingCityValues.push(parseInt($(selected).val())); });
    $('#UxSex option').each(function (i, selected) { UxSexValues.push(parseInt($(selected).val())); });
    $('#UxWealth option').each(function (i, selected) { UxWealthValues.push(parseInt($(selected).val())); });

    SelectRandomOption("UxWealth", UxWealthValues[Math.floor(Math.random() * UxWealthValues.length)]);
    SelectRandomOption("UxSex", UxSexValues[Math.floor(Math.random() * UxSexValues.length)]);
    SelectRandomOption(UxStartingCityClientId, UxStartingCityValues[Math.floor(Math.random() * UxStartingCityValues.length)]);

    ChangeName(3);
    SelectRandomOption("UxCulture", LastCultureId);
    ChangeImage(LastCultureId);

}
function ChangeName(which) {
    if (LastCultureId === null || which == 3) {
        LastCultureId = GetCulture();
    }
    switch (which) {
        case 1: ChangeFirstName(LastCultureId); break;
        case 2: ChangeLastName(LastCultureId); break;
        case 3: ChangeFirstName(LastCultureId); ChangeLastName(LastCultureId); break;
    }
}
function SelectRandomOption(name, value) {
    $("#" + name + " option[selected]").removeAttr("selected");
    $("#" + name + " option[value='" + value + "']").attr("selected", "selected");
}
function GetCulture() {
    if (parseInt($('#UxCulture').val()) == 0) {
        return UxCultureValues[Math.floor(Math.random() * UxCultureValues.length)];
    } else {
        return parseInt($('#UxCulture').val());
    }
}
function ChangeFirstName(eth) {
    if ($('#UxCulture').val() == "" || $('#UxSex').val() == "") {
        $('#tip').html("<span style='color:red'>First choose your character's culture and sex</span>");
    } else {
        var male = false;
        if ($('#UxSex').val() == 1) { male = true; }
        $.ajax({ type: "POST", url: "WebService.asmx/GetForename", contentType: "application/json; charset=utf-8", data: "{'cultureId':" + eth + ",'Male':'" + male + "'}", dataType: "json", success: ForenameRecieved, error: NameError });
    }
}
function ForenameRecieved(result) {
    if (result.d == "") {
        return;
    } else {
        $('#' + UxFirstNameClientId).html(result.d);
    }
}
function ChangeLastName(eth) {
    if ($('#UxCulture').val() == "" || $('#UxSex').val() == "") {
        $('#tip').html("<span style='color:red'>First choose your character's culture and sex</span>");
    } else {
        $.ajax({ type: "POST", url: "WebService.asmx/GetSurname", contentType: "application/json; charset=utf-8", data: "{'cultureId':" + eth + "}", dataType: "json", success: SurnameRecieved, error: NameError });
    }
}
function SurnameRecieved(result) {
    if (result.d == "") {
        return;
    } else {
        $('#' + UxLastNameClientId).html(result.d);
    }
}
function ChangeImage(eth) {
    var male = false;
    if ($('#UxSex').val() == 1) { male = true; }

    if ($('#UxCulture').val() == "" || $('#UxSex').val() == "") {
        $('#tip').html("<span style='color:red'>First choose your character's culture and sex</span>");
    } else {
        $.ajax({ type: "POST", url: "WebService.asmx/GetImage", contentType: "application/json; charset=utf-8", data: "{'cultureId':" + eth + ",'Male':'" + male + "'}", dataType: "json", success: ImageRecieved, error: NameError });
    }
}
function ImageRecieved(result) {
    if (result.d == "") {
        return;
    } else {
        $('#' + UxImageClientId).attr('src', 'GetThumbnail2.ashx?w=50&h=50&img=Library/' + result.d);
    }
}
function NameError(result) {
    var x = result;
}
$(function () {
    QuickFill();
    $('#' + UxImageClientId).click(function () {
        ChangeImage(GetCulture()); 
    });
    $('#UxCulture').change(function () {
        ChangeName(3); 
        ChangeImage(GetCulture()); 
    });
    $('#UxSex').change(function () {
        ChangeName(1);
        ChangeImage(GetCulture()); 
    });
});
