var com = com ? com : {}
com.sc2bc = {}

var mishak = {
    browser: function () {
        
    },
    fileSizeToHuman: function (size) {
        var units = ['B', 'kB', 'MB', 'GB'],
            unitSize = 1024,
            unit = '',
            dSize = size
        while (units.length) {
            unit = units.shift()
            if (dSize <= unitSize && dSize <= unitSize * 0.9) {
                break
            }
            dSize /= unitSize
        }
        return Math.round(dSize * 10) / 10 + unit
    },

    request: function (url, data, onSuccess, onCancel)
    {
        $.post(url, data, function (data, textStatus) {
            switch (data.status) {
                case 'ok':
                    onSuccess(data)
                    if (data.message) {
                        mishak.showSuccessMessage(data.message)
                    }
                    break;
                case 'login':
                    mishak.showLoginForm({
                        url: url,
                        data: data,
                        onSuccess: onSuccess,
                        onCancel: onCancel
                    })
                    break
                case 'error':
                    mishak.showErrorMessage(data.message)
                    break
            }
        }, 'json')
    },
    showSuccessMessage: function (message) {
        alert(message)
    },
    showLoginForm: function (request) {
        var self = this;
        var name = 'quick_signin'
        var prefix = name + '_'
        var login = $('#quick_signin')
        if (login.length) {
            login.dialog('open')
        } else {
            login = $('<div id="' + name + '" class="quick_form"><div class="ui-widget flash-message" id="' + prefix + 'error_box"><div class="ui-state-error ui-corner-all"><p><span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span><span id="' + prefix + 'error"></span></p></div></div><label for="' + prefix + 'username">Username:</label><input type="text" name="username" id="' + prefix + 'username" /><label for="' + prefix + 'password">Password:</label><input type="password" name="password" id="' + prefix + 'password" /><a href="/reset-password">Reset Password&hellip;</a></div>')
            login.find('#' + prefix + 'error_box').hide()
        }
        $('.button, #' + prefix + 'error_box', login).hide()
        login.dialog({
            autoOpen: true,
            modal: true,
            draggable: false,
            resizable: false,
            width: 200,
            title: "Sign In",
            buttons: {
                "Sign In": function () {
                    var data = {
                        'do': 'signInForm-submit',
                        username: login.find('#' + prefix + 'username').val(),
                        password: login.find('#' + prefix + 'password').val()
                    }
                    login.dialog('disable')
                    $.ajax({
                        url: '',
                        data: data,
                        type: 'post',
                        dataType: 'json',
                        success: function (payload) {
                            if (payload.error) {
                                login.find('#' + prefix + 'error').html(payload.error)
                                login.find('#' + prefix + 'error_box').show()
                                login.dialog('enable')
                            } else {
                                $.nette.success(payload)
                                login.dialog('close')
                            }
                        },
                        error: function () {
                            login.dialog('enable')
                            login.find('#' + prefix + 'error').html('Something went wrong while singing in.')
                            login.find('#' + prefix + 'error_box').show()
                        }
                    })
                },
                "Cancel": function () {
                    $(this).dialog('close')
                }
            }
        })
    },
    showErrorMessage: function (message) {
        alert(message)
    }
}

function _(string, count) {
    var value = string
    if (translations[string]) {
        var translation = translations[string]
        if (count > 1 && count < 5 && translation.length > 2) {
            value = translation[2]
        } else if (count > 1 && translation.length > 1) {
            value = translation[1]
        } else {
            value = translation[0]
        }
        
    }
    return value.replace('%d', count)
}
com.sc2bc.charToRace = function (chr) {
    switch (chr) {
        case 'Z':
            return 'zerg';
        case 'T':
            return 'terran';
        case 'P':
            return 'protoss';
    }
}
$(function(){
    $('.matchup').not('th').each(function(index, node){
        node = $(node)
        var value = node.text(),
            html = ''
        
        for (var i = 0; i < value.length; i++) {
            var chr = value.charAt(i),
                race = com.sc2bc.charToRace(chr)
            if (race) {
                html += '<span class="race race-' + race + '">' + chr + '</span>'
            } else {
                html += chr
            }
        }
        node.html(html)
    })
})

