// уровени персонажа
var levels = {
    battle : 'Боевой',
    ekonom : 'Экономический',
    work   : 'Производственный',
    sind   : 'Синдикатный'
}
    
    // статы персонажа
var stats = {
    str : 'Сила',
    mark : 'Меткость',
    life : 'Здоровье',
    end  : 'Выносливость'
}

// бонусы
var bonuses = {
    saper : {
        title : 'Бонус Сапера',
        desc : ''
    },
    
    stability : {
        title : 'Бонус Устойчивости',
        desc : ''
    },
    
    exp : {
        title : 'Бонус Опыта',
        desc : ''
    },
    
    double_step : {
        title : 'Второй шаг',
        desc : ''
    },
    
    fury : {
        title : 'Бонус Ярости',
        desc : ''
    },
    
    die_hard : {
        title : 'Крепкий Орешек',
        desc : ''
    },
    
    seflhealth : {
        title : 'Самолечение',
        desc : ''
    },
    
    adrenalin : {
        title : 'Адреналин',
        desc : ''
    },
    
    maklaud : {
        title : 'Бонус Маклауда',
        desc : ''
    },
    
    regeneration : {
        title : 'Бонус выздоровления',
        desc : ''
    },
    
    men : {
        title : 'Мужской бонус',
        desc : ''
    },
    
    antikiller : {
        title : 'Бонус антикиллера',
        desc : ''
    },
    
    blood : {
        title : 'Жажда крови',
        desc : ''
    },
    
    skills : {
        title : 'Бонус умений',
        desc : ''
    },
    
    nightmasking : {
        title : 'Ночная маскировка',
        desc : ''
    }
}

// синдикатные звания
var sind_levels = {
    
    0 : { 
        title : 'Private', 
        min : 0, 
        pts_cost : 0
    },
    
    1 : { 
        title : 'Lieutenant', 
        min : 15, 
        pts_cost : 75,
        thermovision : 4,
        mark : 2,
        end : 2,
        bonus_saper : 1
    },
    
    2 : { 
        title : 'Captain',
        min: 18, 
        pts_cost : 150,
        thermovision : 6,
        masking : 4,
        mark : 4,
        life : 2,
        bonus_saper : 2,
        bonus_stability : 1
    },
    
    3 : { 
        title : 'Major',
        min: 24, 
        pts_cost : 300,
        thermovision : 8,
        masking : 6,
        mark : 4,
        end : 4,
        life : 2,
        bonus_saper : 3,
        bonus_exp : 1
    },
    
    4 : { 
        title : 'Colonel', 
        min: 30, 
        pts_cost : 400,
        thermovision : 9,
        masking : 9,
        mark : 6,
        end : 6,
        life : 4,
        bonus_saper : 4,
        bonus_double_step : 1
    },
    
    5 : { 
        title : 'Brigadier',
        min: 34, 
        pts_cost : 400,
        thermovision : 10,
        masking : 9,
        mark : 6,
        end : 6,
        life : 6,
        bonus_saper : 5,
        bonus_double_step : 1,
        bonus_fury : 1,
        bonus_die_hard : 1
    }
}


function gwPers() {
    // ник
    this.nickname = 'Имя персонажа';
    
    // уровни
    this.battle_level = 0;
    this.ekonom_level = 0;
    this.work_level = 0;
    this.sind_level = 0;
    
    // синдикатное звание
    this.sind_rank = 0;
    
    // статы
    this.stat_str = 10;
    this.stat_mark = 10;
    this.stat_life = 10;
    this.stat_end = 10;
    
    this.str = 10;
    this.mark = 10;
    this.life = 10;
    this.end = 10;
    
    this.init = function() {
        $('#skills_bonuses > table').empty();
        
        // количество жизней персонажа
        this.hp = 0;
        
        // стоимость
        this.cost = 0;
        this.cost_eun = 0;
    
        // видимость
        this.thermovision = 0;
        // маскировка
        this.masking = 0;
        // броня
        this.arm_head = 0;
        this.arm_body = 0;
        this.arm_legs = 0;
        this.arm_act_head = 0;
        this.arm_act_body = 0;
        this.arm_act_legs = 0;
        
        this.free_stats = 0;
        
        // скорость
        this.speed_earth = 0;
        this.speed_water = 0;
    
        // вес
        this.weight = 0;
        this.add_weight = 10;
                
        // бонусы
        for (i in bonuses) {
            this['bonus_'+ i] = 0;
        }
    }
}

// проверка на диапазон
gwPers.prototype.checkVal = function(n, min, max) {
    n = parseInt(n);
    if (isNaN(n) || n < min) {
        return min;
    } else if (n > max) {
        return max;
    } else {
        return n;
    }
}

// изменений значение для уровней и статов
gwPers.prototype.setVal = function(id, val) {
    if ($('#'+ id).length) {
        if ($('#'+ id +'2').length) {
            $('#'+ id).val(this['stat_'+ id]);
            $('#'+ id +'2').text(val);
        } else {
            $('#'+ id).text(val);
        }
        return true;
    } else {
        return false;
    }    
}


// вывод результата
gwPers.prototype.update = function() {
    
    // ----------------- ПОДСЧЕТ СТАТОВ ----------------
    this.recalc();
    
    
    
    
    // -----------------------  ВЫВОД -------------------------
    
    // сила
    this.setVal('str', this.str);
    // метка
    this.setVal('mark', this.mark);
    // жизни
    this.setVal('life', this.life);
    // вынос
    this.setVal('end', this.end);
    
    // свободные статы
    this.setVal('free_stats', this.free_stats);
    
    // хп
    this.setVal('hp', this.hp +' / '+ this.hp);
    
    // вес
    this.setVal('weight', this.weight +' ('+ (this.add_weight > 0 ? '+' : '')  + this.add_weight +')');
    
    // тепловизор
    this.setVal('thermovision', this.thermovision);
    // маскировка
    this.setVal('masking', this.masking);
    
    // стоимость
    this.setVal('cost', this.cost);
    this.setVal('cost_eun', this.cost_eun);
    
    // скорость
    this.setVal('speed_earth', 180 - (this.speed_earth == 0 ? 0 : 180 - this.speed_earth));
    this.setVal('speed_water', this.speed_water);
    
    //броня
    this.setVal('arm_head', this.arm_head);
    this.setVal('arm_body', this.arm_body);
    this.setVal('arm_legs', this.arm_legs);
    this.setVal('arm_act_head', this.arm_act_head);
    this.setVal('arm_act_body', this.arm_act_body);
    this.setVal('arm_act_legs', this.arm_act_legs);
    
    
    // бонусы
    var table = $('#skills_bonuses > table');
    for (i in bonuses) {
        if (this['bonus_'+ i] > 0) {
            table.append('<tr><td style="width: 200px" class="right">'+ bonuses[i].title +': </td><td><span>'+ this['bonus_'+ i] +'</span></td></tr></tr>');
        }
    }
}

// подсчет статов
gwPers.prototype.recalc = function() {
    // очистка некоторых значений
    this.init();
    
    // сила
    this.str = this.stat_str + this.ekonom_level;
    // меткость
    this.mark = this.stat_mark;
    // здоровье
    this.life = this.stat_life;
    // вынос
    this.end = this.stat_end + this.work_level;
    
    // свободных статов
    this.free_stats = this.battle_level + 40 - (this.stat_str + this.stat_mark + this.stat_life + this.stat_end);
    
    // шмот
    for (i = 1; i <= 12; i++) {
        
        var item = $('#items'+ i).val();
        if (item != '') {
            for (m in items[item]) {
                if (typeof this[m] != 'undefined') {
                    if (m == 'battle_level' || m == 'ekonom_level' || m == 'work_level' || m == 'sind_level') {
                        
                    } else {
                        if (i == 3 && 
                            ((this.getSlot(2) != '' && items[this.getSlot(2)].slots[0] == 2) && (this.getSlot(3) != '' && items[this.getSlot(3)].slots[0] == 2)) || (this.getSlot(2) == '' && this.getSlot(3) != '')) {
                            this[m] += items[item][m];
                        } else if (i != 3) {
                            this[m] += items[item][m];
                        }
                    }
                }
            }
        }
    }
    
    
    // подсчет дополнительно хп от синда и бонусов от званий
    var temp = this.sind_level - 15;
    if (temp > 0) {
        this.hp += temp * 6;
    }
    
    for (i in sind_levels[this.sind_rank]) {
        if (typeof this[i] != 'undefined') {
            this[i] += sind_levels[this.sind_rank][i];
        }
    }
    
    
    // хп
    this.hp += this.battle_level + this.life * 6;
    // вес
    this.add_weight = this.str - this.weight;
}


// изменение уровней и статов
gwPers.prototype.changeLevels = function(id, val) {
    
    // минимальное значение для статов 10
    if (id == 'str' || id == 'mark' || 
        id == 'life' || id == 'end') {
        
        val = val < 10 ? 10 : val;
        this['stat_'+ id] = val;
    } else {
        this[id] = val;
    }
    
    this.update();
}

// обновление картинок
gwPers.prototype.updateImgs = function() {
    $('#weapons_img').empty();
    
    if ($('#items2').val() != '') {
        this._printImg(2);
    }
    
    if ($('#items2').val() == '' && $('#items3').val() != '') {
        this._printImg(3);
    } else if (($('#items2').val() != '' && items[$('#items2').val()].slots[0] == 2) && 
        ($('#items3').val() != '' && items[$('#items3').val()].slots[0] == 2)) {
        this._printImg(3);
    }
    
    if ($('#items6').val() != '') {
        this._printImg(6);
    }
    
    if ($('#items1').val() != '') {
        this._printImg(1);
    }
    
    if ($('#items4').val() != '') {
        this._printImg(4);
    }
    
    if ($('#items5').val() != '') {
        this._printImg(5);
    }
    
    if ($('#items7').val() != '') {
        this._printImg(7);
    }
    
    if ($('#items8').val() != '') {
        this._printImg(8);
    }
    
    if ($('#items9').val() != '') {
        this._printImg(9);
    }
    
    if ($('#items10').val() != '') {
        this._printImg(10);
    }
    
    if ($('#items11').val() != '') {
        this._printImg(11);
    }
    
    if ($('#items12').val() != '') {
        this._printImg(12);
    }
}

// получение id предмета в слоте
gwPers.prototype.getSlot = function (slot_id) {
    if (typeof $('#items'+ slot_id).val() != 'undefined') {
        return $('#items'+ slot_id).val();
    }
    return false;
}

// получение свойства предмета по его id
gwPers.prototype.getItem = function (item_id) {
    if (typeof items[item_id] != 'undefined') {
        return items[item_id];
    }
    return false;
}

// добавление картинки
gwPers.prototype._printImg = function (id) {
    if (id) {
        $('#weapons_img').append('<a href="http://www.ganjawars.ru/item.php?item_id='+ $('#items'+ id).val() +'" target="_blank"><img height="51" width="51" src="http://images.ganjawars.ru/img/items/'+ $('#items'+ id).val() +'_s.jpg" alt="" /></a>');
    }
}

var pers = new gwPers();
pers.init();