//=============================================================================
// MUR Enhanced Equips v2.0
// by MUR (https:var github.com/murlab)
// BSD 3-Clause License
// Free for use with both free and commercial RPG Maker games.
//=============================================================================

var Imported = Imported || {};
Imported.MUR_Equips = true;

var MUR = MUR || {};				// MUR's main object
MUR.EQUIPS = MUR.EQUIPS || {};		// Plugins's stuff

/* :en
 * @plugindesc v2.0 Enhanced Equip
 * @author Mur
 * @help 
 *
 /
 /*:ru
 * @plugindesc v2.0 Расширенный выбор снаряжения
 * @author Mur
 * @help 
 *
 */

MUR.EQUIPS.sprite = new Sprite(0,0);
MUR.EQUIPS.sprite.x = 31;
MUR.EQUIPS.sprite.y = 41-17;

MUR.EQUIPS.bgBitmap = ImageManager.loadBitmap('img/equips/', 'bg', null, true);

MUR.EQUIPS.itemsPos = [
	{x:31,	y:41},		// 03 Голова
	{x:305,	y:41},		// 06 Бижутерия 
	{x:31,	y:108},		// 07 Уши
	{x:305,	y:108},		// 05 Шея
	{x:31,	y:175},		// 08 Руки
	{x:305,	y:175},		// 09 Спина
	{x:31,	y:242},		// 13 Пальцы
	{x:305,	y:242},		// 04 Тело	
	{x:31,	y:309},		// 10 Пояс
	{x:305,	y:309},		// 14 Запястье
	{x:31,	y:376},		// 11 Бёдра
	{x:305,	y:376},		// 12 Ноги
	{x:51,	y:447},		// 01 В правой руке
	{x:285,	y:447}		// 02 В левой руке
];

MUR.EQUIPS.slotIndexSequence = [
	  3-1,				// 03 Голова
	  6-1,				// 06 Бижутерия
	  7-1,				// 07 Уши
	  5-1,				// 05 Шея
	  8-1,				// 08 Руки
	  9-1,				// 09 Спина
	 13-1,				// 13 Пальцы
	  4-1,				// 04 Тело
	 10-1,				// 10 Пояс
	 14-1,				// 14 Запястье
	 11-1,				// 11 Бёдра
	 12-1,				// 12 Ноги
	  1-1,				// 01 В правой руке
	  2-1				// 02 В левой руке
];

MUR.EQUIPS.menuPos = [];
MUR.EQUIPS.menuCursorHeight = 35;
MUR.EQUIPS.menuCursorPadding = 10;

MUR.EQUIPS.prepareStats = function(actor, equipInfoWindow, equips) {
	// Задаём все значения в 0
	for (var i=0; i<28; i++) { equipInfoWindow._stats[i]=0; }
	for (var i=0; i<equips.length; i++ ) {
		var item = undefined;
		if (equips[i]._dataClass == "weapon") {
			item = $dataWeapons[equips[i]._itemId];
		} else if (equips[i]._dataClass == "armor") {
			item = $dataArmors[equips[i]._itemId];
		} else {
			// предмет в левой руке?
		}
		if (item != undefined) {
			// основные 8 параметров
			for (var j=0; j<8; j++) { equipInfoWindow._stats[j] += item.params[j]; }
			for (var j=0; j<item.traits.length; j++) {
				var trait = item.traits[j];
				/*
				code: 31
				dataId: 1
				value: 1
				*/
				switch (trait.code) {
					case 21: // parameter / Основные параментры *% 
						// 0 - Max XP
						// 1 - Max MP
						// 2 - Attack
						// 3 - Defence
						// 4 - M.Attack
						// 5 - M.Defence
						// 6 - Agility
						// 7 - Luck
						break;
					case 22: // ex-parameter / Дополнительные параментры +% 
						// 0 - hit rate
						// 1 - evasion rate
						// 2 - critical rate
						// 3 - critucal evasion
						// 4 - magic evasion
						// 5 - magic reflection
						// 6 - counter attack
						// 7 - hp regeneration
						// 8 - mp regeneration
						// 9 - tp regeneration
						equipInfoWindow._stats[8 + trait.dataId] += trait.value;
						break;
					case 23: //sp-parameter / Специальные параментры *%
						// 0 - target rate
						// 1 - guard effect
						// 2 - recovery effect
						// 3 - pharmacalogy
						// 4 - mp cost rate
						// 5 - tp charge rate
						// 6 - physical damage
						// 7 - magical damage
						// 8 - floor damage
						// 9 - experience
						equipInfoWindow._stats[18 + trait.dataId] += trait.value;
						break;
				}
			}
		}
	}

	var tActor = JsonEx.makeDeepCopy(actor);
	tActor.clearEquipments();

	for (var j=0; j<8; j++) {
		equipInfoWindow._stats[j] += actor.param(j) - tActor.param(j);
	}
	
}

//-----------------------------------------------------------------------------
// Scene_Equip
//-----------------------------------------------------------------------------

Scene_Equip.prototype.create = function() {
	Scene_MenuBase.prototype.create.call(this);
	// Параметры окон
	MUR.EQUIPS.win = [
		// 0: меню
		{x:Graphics.boxWidth - 588, y:Graphics.boxHeight-62, w:561, h:36},
		// 1: экипировка
		{x:0, y:0, w:370, h:Graphics.boxHeight},
		// 2: статы
		{x:370+12, y:0, w:Graphics.boxWidth-370-12-30, h:Graphics.boxHeight-62},
		// 3: инвентарь
		{x:27, y:40, w:370-27, h:Graphics.boxHeight-40-25}
	];
	this._spr = new Sprite()
	this._spr.bitmap = MUR.EQUIPS.bgBitmap;
	this._spr.x = 0;
	this._spr.y = 0;
	this.addChildAt(this._spr, 1);
	this.createWindow();
	this.createSlotWindow();
	this._slotWindow.setActor(this.actor());
};

Scene_Equip.prototype.createWindow = function() {
	this._commandWindow = new Window_EquipCommand(MUR.EQUIPS.win[0].x, MUR.EQUIPS.win[0].y, MUR.EQUIPS.win[0].w, MUR.EQUIPS.win[0].h);

	this._commandWindow.setHandler('equip',    this.commandEquip.bind(this));
	this._commandWindow.setHandler('optimize', this.commandOptimize.bind(this));
	this._commandWindow.setHandler('clear',    this.commandClear.bind(this));
	this._commandWindow.setHandler('cancel',   this.popScene.bind(this));
	this._commandWindow.setHandler('pagedown', this.nextActor.bind(this));
	this._commandWindow.setHandler('pageup',   this.previousActor.bind(this));
	this.addWindow(this._commandWindow);
};

Scene_Equip.prototype.createSlotWindow = function() {
	this._slotWindow = new Window_EquipSlot(MUR.EQUIPS.win[1].x, MUR.EQUIPS.win[1].y, MUR.EQUIPS.win[1].w, MUR.EQUIPS.win[1].h);
	this._slotWindow.setHandler('ok',       this.onSlotOk.bind(this));
	this._slotWindow.setHandler('cancel',   this.onSlotCancel.bind(this));
	this.addWindow(this._slotWindow);
	
	this._equipInfoWindow = new WindowInfo(MUR.EQUIPS.win[2].x, MUR.EQUIPS.win[2].y, MUR.EQUIPS.win[2].w, MUR.EQUIPS.win[2].h);
	this._equipInfoWindow._itemName = '';
	this._equipInfoWindow._itemType = '';
	this._equipInfoWindow._itemGrade = '';
	this._equipInfoWindow._itemCost = undefined;
	this._equipInfoWindow._weaponId1 = undefined;
	this._equipInfoWindow._weaponId2 = undefined;
	this._equipInfoWindow._damage = undefined;
	this._equipInfoWindow._statMode = true;
	this._equipInfoWindow._invMode = false;
	this._equipInfoWindow._errMode = false;
	this.prepareStats();
	this.addWindow(this._equipInfoWindow);
	
	this._slotWindow.equipInfoWindow = this._equipInfoWindow;
};

Scene_Equip.prototype.commandEquip = function() {
	this._slotWindow.activate();
	this._slotWindow.equipInfoWindow._statMode = false;
	this._slotWindow.select(0);
	this._slotWindow.refresh();
};

//--------------------------------------------------------
// Окно с выбором предметов
//--------------------------------------------------------
Scene_Equip.prototype.onSlotOk = function() {
	this._selectItemWindow = new SlotEquip_Window(MUR.EQUIPS.win[3].x, MUR.EQUIPS.win[3].y, MUR.EQUIPS.win[3].w, MUR.EQUIPS.win[3].h);
	this._selectItemWindow.setHandler('ok',     this.selectItemOk.bind(this));
	this._selectItemWindow.setHandler('cancel', this.closeSelectItemWindow.bind(this));
	this.addWindow(this._selectItemWindow);

	this._selectItemWindow._activeSlot = this._slotWindow.activeSlot;
	this._selectItemWindow._activeSlotId = this._slotWindow.activeSlotId;
	this._selectItemWindow._actor = this._actor;
	this._selectItemWindow.equipInfoWindow = this._equipInfoWindow;
	this._selectItemWindow.refresh();
	this._selectItemWindow.activate();
	this._selectItemWindow.select(0);
};
 
Scene_Equip.prototype.selectItemOk = function() {
	var item = this._selectItemWindow._data[this._selectItemWindow._index];
	var slotId = MUR.EQUIPS.slotIndexSequence[this._slotWindow._index];
	if (item.id != 0) {
		this.actor().changeEquip(slotId, item);
		
	} else {
		this.actor().changeEquip(slotId, null);
	}
	
	this.closeSelectItemWindow();
}

Scene_Equip.prototype.closeSelectItemWindow = function() {
	if (this._selectItemWindow) {
		this._selectItemWindow.close();
	}
	this._equipInfoWindow._invMode = false;
	this._equipInfoWindow._errMode = false;
	this._slotWindow.refresh();
	this._slotWindow.activate();
}

Scene_Equip.prototype.commandOptimize = function() {
	SoundManager.playEquip();
	this.actor().optimizeEquipments();
	this._commandWindow.activate();
	this.prepareStats();
	this._slotWindow.refresh();
};

Scene_Equip.prototype.onSlotCancel = function() {
	this.prepareStats();
	if (this._equipInfoWindow) {
		this._equipInfoWindow._statMode = true;
		this._equipInfoWindow.refresh();
	}
	this._slotWindow.deselect();
	this._slotWindow.setCursorRect(0, 0, 0, 0);
	this._slotWindow.activeSlot = '';
	this._slotWindow.refresh();
	this._commandWindow.activate();
};
 
Scene_Equip.prototype.commandClear = function() {
	SoundManager.playEquip();
	this.actor().clearEquipments();
	this._commandWindow.activate();
	this.prepareStats();
	this._slotWindow.refresh();
};

Scene_Equip.prototype.prepareStats = function() {
	MUR.EQUIPS.prepareStats(this.actor(), this._equipInfoWindow, this.actor()._equips);
}

//-----------------------------------------------------------------------------
// Window_EquipCommand
//-----------------------------------------------------------------------------
var _Mur_Window_EquipCommand_makeCommandList = Window_EquipCommand.prototype.makeCommandList;
Window_EquipCommand.prototype.makeCommandList = function() {
	_Mur_Window_EquipCommand_makeCommandList.call(this);
	this.addCommand(MUR.dic['equipFinishMsg'], 'cancel');
};

Window_EquipCommand.prototype.initialize = function(x, y, width, height) {
	this._windowWidth = width;
	this._windowHeight = height;
	Window_HorzCommand.prototype.initialize.call(this, x, y);
};

Window_EquipCommand.prototype.windowWidth = function() { return this._windowWidth; };
Window_EquipCommand.prototype.windowHeight = function() { return this._windowHeight; };

Window_EquipCommand.prototype.maxCols = function() { return 4; };
Window_EquipCommand.prototype.numVisibleRows = function() { return 1; };

Window_EquipCommand.prototype.drawItem = function(index) {
	var rect = this.itemRectForText(index);
	this.changePaintOpacity(this.isCommandEnabled(index));
	MUR.setfont(this.contents, 'm', 'grey');
	var name = this.commandName(index);

	MUR.EQUIPS.menuPos[index] = {};
	var p = MUR.EQUIPS.menuPos[index];
	p.w = this.textWidth(name, 0, 0);
	if (index == 0) {
		p.x =  10;
	} else {
		p.x = MUR.EQUIPS.menuPos[index-1].x + MUR.EQUIPS.menuCursorPadding*2 + MUR.EQUIPS.menuPos[index-1].w;
	}
	this.drawText(name, p.x, 0, rect.width, 'left');
};

Window_EquipCommand.prototype.standardPadding = function() { return 0; };
Window_EquipCommand.prototype._refreshFrame = function() {}
Window_EquipCommand.prototype._refreshBack = function() {}
 
Window_EquipCommand.prototype.updateCursor = function() {
	if (this.index()!= -1) {
		var p = MUR.EQUIPS.menuPos[this.index()];
		this.setCursorRect(p.x-MUR.EQUIPS.menuCursorPadding, MUR.EQUIPS.menuY, p.w+MUR.EQUIPS.menuCursorPadding*2, MUR.EQUIPS.menuCursorHeight);
	}
};

Window_EquipCommand.prototype._refreshCursor = function() {
	this._dashOffset = 0;
	var pad = this._padding;
	var x = this._cursorRect.x + pad - this.origin.x;
	var y = this._cursorRect.y + pad - this.origin.y;
	var w = this._cursorRect.width;
	var h = this._cursorRect.height;
	var m = 4;
	var x2 = Math.max(x, pad);
	var y2 = Math.max(y, pad);
	var ox = x - x2;
	var oy = y - y2;
	this._w2 = Math.min(w, this._width - pad - x2);
	this._h2 = Math.min(h, this._height - pad - y2);	
	this._windowCursorSprite.setFrame(0, 0, this._w2, this._h2);
	this._windowCursorSprite.move(x2, y2);
};

Window_EquipCommand.prototype._updateCursor = function() {
	if (this.active) {
		var bitmap = new Bitmap(this._w2, this._h2);
		bitmap.context.setLineDash([6,3]);
		bitmap.context.lineDashOffset = this._dashOffset;
		bitmap.context.lineWidth = 2;
		bitmap.context.strokeStyle = '#4d3014';
		bitmap.context.strokeRect(0, 0, this._w2, this._h2);
		this._dashOffset++;
		if (this._dashOffset > 9) this._dashOffset = 0;
		this._windowCursorSprite.bitmap = bitmap;
	}
	this._windowCursorSprite.visible = this.isOpen();
};

//-----------------------------------------------------------------------------
// Window_EquipSlot
//-----------------------------------------------------------------------------

Window_EquipSlot.prototype.initialize = function(x, y, width, height) {
	Window_Selectable.prototype.initialize.call(this, x, y, width, height);
	this.addChildToBack(MUR.EQUIPS.sprite);
	this.activeSlot = '';
	this.refresh();
};

Window_EquipSlot.prototype.standardPadding = function() { return 0; };
Window_EquipSlot.prototype.windowWidth = function() { return this._width; };
Window_EquipSlot.prototype.windowHeight = function() { return this._height; };

Window_EquipSlot.prototype.setActor = function(actor) {
	if (this._actor !== actor) {
		this._actor = actor;
		var bgImage = 'unknown';
		var notedata = $dataActors[this._actor._actorId].note.split(/[\r\n]+/);
		var actorGenderNote = /<(?:gender):[ ](.*)>/i;                
		for (var i = 0; i < notedata.length; i++) {
		  var line = notedata[i];
		  if (line.match(actorGenderNote)) {
			bgImage = String(RegExp.$1);
		  }
		}
		var b = ImageManager.loadBitmap('img/equips/', bgImage, null, true);      
		b.addLoadListener(function(b) {
			MUR.EQUIPS.sprite.bitmap = new Bitmap(b.width, b.height);
			MUR.EQUIPS.sprite.width = b.width;
			MUR.EQUIPS.sprite.height = b.height;
			MUR.EQUIPS.sprite.bitmap.blt(b, 0, 0, b.width, b.height, 0, 0);

			for (var i=0; i<MUR.EQUIPS.itemsPos.length; i++) {
				var item = MUR.EQUIPS.itemsPos[i];
				MUR.EQUIPS.sprite.bitmap.fillRect(item.x - MUR.EQUIPS.sprite.x, item.y - MUR.EQUIPS.sprite.y, 64, 64, "rgba(0,0,0,0.2)");
			}

			this.refresh();

		}.bind(this, b));

		if (this.equipInfoWindow) {
			this.equipInfoWindow.setActor(actor);
			this.equipInfoWindow.show();
			this.equipInfoWindow.refresh();
		}
	}
};

Window_EquipSlot.prototype.drawItem = function(index) {
	if (this._actor) {
		var newIndex = MUR.EQUIPS.slotIndexSequence[index];
		var item = this._actor.equips()[newIndex];
		var iconWidth = Window_Base._iconWidth + 4;
		if (item) {
			var itemPath = 'items';			
			if (item.wtypeId) {
				itemPath = 'weapons';
			} else if (item.atypeId) {
				itemPath = 'armors';
			}
			var pX = MUR.EQUIPS.itemsPos[index].x;
			var pY = MUR.EQUIPS.itemsPos[index].y;

			var iconsBitmap = ImageManager.loadBitmap('img/icons/' + itemPath + '/64x64/', item.id, null, true);
			iconsBitmap.addLoadListener(function(iconsBitmap, pX, pY) {
				this.contents.blt(iconsBitmap, 0, 0, 64, 64, pX, pY);
			}.bind(this, iconsBitmap, pX, pY));
		}
		this.changePaintOpacity(true);
	}
};
 
Window_EquipSlot.prototype.updateCursor = function() {
	if (this.index()!= -1) {
		var pX = MUR.EQUIPS.itemsPos[this.index()].x;
		var pY = MUR.EQUIPS.itemsPos[this.index()].y;
		this.setCursorRect(pX-1, pY-1, 64+2, 64+2);
	}
};

Window_EquipSlot.prototype.refresh = function() {
	if (this.contents) {
		this.contents.clear();
		this.drawAllItems();
		if (this.activeSlot != '') {
			MUR.setfont(this.contents, 's', 'brown');
			var w = this.textWidth(MUR.dic['slot'], 0, 0);
			this.drawText(MUR.dic['slot'], 31+(338-w)/2, 435+9);

			MUR.setfont(this.contents, 'm', 'brown');
			var text = this.activeSlot;
			var w = this.textWidth(text, 0, 0);
			this.drawText(text, 31+(338-w)/2, 435+26+6);
		}
	}
};

Window_EquipSlot.prototype.callUpdateHelp = function() {
	var itemImage = 'unknown';
	var iName = MUR.dic['equipStatMsg'];
	var iType = "";
	var iDescription = "";
	var iTpCost = undefined;
	var iRequiredWtypeId1 = undefined;
	var iRequiredWtypeId2 = undefined;
	var iDamage = undefined;
	var iEffects = undefined;
	var iParams = undefined;
	var iTraits = undefined;
	var iGrade = "";
	
	if (this._index != -1) {
		var itemName = MUR.dic['equipEmptyMsg'];
		var newIndex = MUR.EQUIPS.slotIndexSequence[this._index];
		var item = this._actor.equips()[newIndex];
		if (this.isEnabled(newIndex)) {
			this.activeSlot = this.slotName(newIndex);
			this.activeSlotId = newIndex;
		} else {
			this.activeSlot = equipNotAvailableMsg;
		}

		if (item) {           
			iName = item.name;
			var subType = 0;
			if (item.wtypeId) {
				iType = $dataSystem.weaponTypes[item.wtypeId];			
			} else if (item.atypeId){
				iType = $dataSystem.armorTypes[item.atypeId];
			} else {
				 // для предметов?
			}

			var notes = item.note.split("\n");
			for (var i=0; i < notes.length; i++) {
				var note = notes[i];
				if (note != "" && note.match(/<grade:\s*(.*)>/im)) {
					iGrade = note.match(/grade:\s*(.*)>/im)[1];
					break;
				}
			}

			iDescription = item.description;
			iTpCost = item.tpCost;
			iRequiredWtypeId1 = item.requiredWtypeId1;
			iRequiredWtypeId2 = item.requiredWtypeId2;
			iDamage = item.damage;
			iEffects = item.effects;
			iParams = item.params;
			iTraits = item.traits;
		} else {
			iName = MUR.dic['equipEmptyMsg'];
		}
		this.refresh();
	}

	if (this.equipInfoWindow) {
		this.equipInfoWindow._itemName = iName;
		this.equipInfoWindow._itemType = iType;
		this.equipInfoWindow._itemGrade = iGrade;
		this.equipInfoWindow._itemDis = iDescription;
		this.equipInfoWindow._itemCost = iTpCost;
		this.equipInfoWindow._weaponId1 = iRequiredWtypeId1;
		this.equipInfoWindow._weaponId2 = iRequiredWtypeId2;
		this.equipInfoWindow._damage = iDamage;
		this.equipInfoWindow._effects = iEffects;
		this.equipInfoWindow._params = iParams;
		this.equipInfoWindow._traits = iTraits;

		this.equipInfoWindow.refresh();

	}
}

Window_EquipSlot.prototype.maxCols = function() { return 2; };
Window_EquipSlot.prototype._refreshFrame = function() { }
Window_EquipSlot.prototype._refreshBack = function() { }

Window_EquipSlot.prototype._refreshCursor = function() {
	this._dashOffset = 0;
	var pad = this._padding;
	var x = this._cursorRect.x + pad - this.origin.x;
	var y = this._cursorRect.y + pad - this.origin.y;
	var w = this._cursorRect.width;
	var h = this._cursorRect.height;
	var m = 4;
	var x2 = Math.max(x, pad);
	var y2 = Math.max(y, pad);
	var ox = x - x2;
	var oy = y - y2;
	this._w2 = Math.min(w, this._width - pad - x2);
	this._h2 = Math.min(h, this._height - pad - y2);	
	this._windowCursorSprite.setFrame(0, 0, this._w2, this._h2);
	this._windowCursorSprite.move(x2, y2);
};

Window_EquipSlot.prototype._updateCursor = function() {
	if (this.active) {
		var bitmap = new Bitmap(this._w2, this._h2);
		bitmap.context.setLineDash([6,3]);
		bitmap.context.lineDashOffset = this._dashOffset;
		bitmap.context.lineWidth = 2;
		bitmap.context.strokeStyle = '#fff';
		bitmap.context.strokeRect(0, 0, this._w2, this._h2);
		this._dashOffset++;
		if (this._dashOffset > 9) this._dashOffset = 0;
		this._windowCursorSprite.bitmap = bitmap;
	}
	this._windowCursorSprite.visible = this.isOpen();
};

//------------------------------------------------------
// Создаём окно выбора экипировки
//------------------------------------------------------
function SlotEquip_Window() {
	this.initialize.apply(this, arguments);
}

SlotEquip_Window.prototype = Object.create(Window_ItemList.prototype);
SlotEquip_Window.prototype.constructor = SlotEquip_Window;

SlotEquip_Window.prototype.initialize = function(x, y, width, height) {
	Window_ItemList.prototype.initialize.call(this, x, y, width, height);
	this._activeSlot = '';
	this._activeSlotId = null;
	this._data = [];
	this._actor = null;

	this._spr = new Sprite();
	this._spr.bitmap = new Bitmap(width, height);
	for (var y=0; y<7; y++) {
		var pY = y * 67+1;
		for (var x=0; x<5; x++) {
			var pX = x * 67;
			this._spr.bitmap.fillRect(pX, pY, 64, 64, "rgba(0,0,0,0.2)");
		}
	}
	this._spr.x = 0;
	this._spr.y = 0;
	this.addChildAt(this._spr, 1);
};

SlotEquip_Window.prototype.standardPadding = function() { return 0; };
SlotEquip_Window.prototype._refreshFrame = function() {}
SlotEquip_Window.prototype._refreshBack = function() {}

SlotEquip_Window.prototype.makeItemList = function() {
	this._data = [];
	var tItem = [];
	tItem.id = 0;
	tItem.name = MUR.dic['equipEmptyMsg'];
	this._data.push(tItem);
	var iAll = $gameParty.armors().concat($gameParty.weapons());
	for (var i=0; i<iAll.length; i++) {
		var tItem = iAll[i];
		if ($dataSystem.equipTypes[tItem.etypeId] == this._activeSlot) {
			this._data.push(tItem);
		}
	}
}

SlotEquip_Window.prototype.maxCols = function() {
	return 5;
};

SlotEquip_Window.prototype.isEnabled = function(item) {
	return item.id == 0 ? true : this._actor.canEquip(item);
};

SlotEquip_Window.prototype.drawItem = function(index) {
	var item = this._data[index];
	if (item) {
		var itemPath = 'items';			
		if (item.wtypeId) {
			itemPath = 'weapons';
		} else if (item.atypeId) {
			itemPath = 'armors';
		}
		var pX = Math.floor(index%5) * 67;
		var pY = Math.floor(index/5) * 67+1;

		var iconsBitmap = ImageManager.loadBitmap('img/icons/' + itemPath + '/64x64/', item.id, null, true);
		iconsBitmap.addLoadListener(function(iconsBitmap, pX, pY, itemCount, itemEnabled) {
			if (!itemEnabled) {
				this.contents.fillRect(pX, pY, 64, 64, "rgba(255,0,0,0.1)");
			} else {
				this.contents.fillRect(pX, pY, 64, 64, "rgba(0,0,0,0.1)");
			}
			this.contents.blt(iconsBitmap, 0, 0, 64, 64, pX, pY);
			if (itemCount > 1) {
				MUR.setfont(this.contents, 's', 'white');
				this.drawText(itemCount, pX+2, pY-8, 60, 'right');
			}
		}.bind(this, iconsBitmap, pX, pY, $gameParty.numItems(item), this.isEnabled(item)));
	}
};

SlotEquip_Window.prototype.updateCursor = function() {
	if (this.index()!= -1) {
		var pX = Math.floor(this.index()%5) * 67;
		var pY = Math.floor(this.index()/5) * 67+1;
		this.setCursorRect(pX-1, pY-1, 64+2, 64+2);
	}
};

SlotEquip_Window.prototype._refreshCursor = function() {
	this._dashOffset = 0;
	var pad = this._padding;
	var x = this._cursorRect.x + pad - this.origin.x;
	var y = this._cursorRect.y + pad - this.origin.y;
	var w = this._cursorRect.width;
	var h = this._cursorRect.height;
	var m = 4;
	var x2 = Math.max(x, pad);
	var y2 = Math.max(y, pad);
	var ox = x - x2;
	var oy = y - y2;
	this._w2 = Math.min(w, this._width - pad - x2);
	this._h2 = Math.min(h, this._height - pad - y2);	
	this._windowCursorSprite.setFrame(0, 0, this._w2, this._h2);
	this._windowCursorSprite.move(x2, y2);
};

SlotEquip_Window.prototype._updateCursor = function() {
	if (this.active) {
		var bitmap = new Bitmap(this._w2, this._h2);
		bitmap.context.setLineDash([6,3]);
		bitmap.context.lineDashOffset = this._dashOffset;
		bitmap.context.lineWidth = 2;
		if (!this.isEnabled(this._data[this._index])) {
			bitmap.context.strokeStyle = '#a90000';
		} else {
			bitmap.context.strokeStyle = '#fff';
		}

		bitmap.context.strokeRect(0, 0, this._w2, this._h2);
		this._dashOffset++;
		if (this._dashOffset > 9) this._dashOffset = 0;
		this._windowCursorSprite.bitmap = bitmap;
	}
	this._windowCursorSprite.visible = this.isOpen();
};

 SlotEquip_Window.prototype.callUpdateHelp = function() {
	 if (this._index != -1 && this.equipInfoWindow) {
		var item = this._data[this._index];
		var iType = '';
		if (item.wtypeId) {
			iType = $dataSystem.weaponTypes[item.wtypeId];

		} else if (item.atypeId){
			iType = $dataSystem.armorTypes[item.atypeId];
		} else {
			// для предметов?
		}

		if (this.equipInfoWindow) {

			var iGrade = "";
			if (item.note) {
				var notes = item.note.split("\n");
				for (var i=0; i < notes.length; i++) {
					var note = notes[i];
					if (note != "" && note.match(/<grade:\s*(.*)>/im)) {
						iGrade = note.match(/grade:\s*(.*)>/im)[1];
						break;
					}
				}
			}
			this.equipInfoWindow._itemName = item.name;
			this.equipInfoWindow._itemType = iType;
			this.equipInfoWindow._itemGrade = iGrade;
			this.equipInfoWindow._itemDis = item.description;
			this.equipInfoWindow._itemCost = item.tpCost;
			this.equipInfoWindow._weaponId1 = item.requiredWtypeId1;
			this.equipInfoWindow._weaponId2 = item.requiredWtypeId2;
			this.equipInfoWindow._damage = item.damage;
			this.equipInfoWindow._effects = item.effects;
			this.equipInfoWindow._params = item.params;
			this.equipInfoWindow._traits = item.traits;

			this.equipInfoWindow._invMode = true;
			this.equipInfoWindow._errMode = !this.isEnabled(item);
			if (this.equipInfoWindow._errMode) {
				var errMsg = [];
				if (DataManager.isWeapon(item)) {
					if (!this._actor.isEquipWtypeOk(item.wtypeId)) {
						errMsg.push(MUR.dic['equipReqWeaponMsg'].replace('%s', $dataSystem.weaponTypes[item.wtypeId]));
					}
				} else if (DataManager.isArmor(item)) {
					if (!this._actor.isEquipAtypeOk(item.atypeId)) {
						errMsg.push(MUR.dic['equipReqArmorMsg'].replace('%s', $dataSystem.armorTypes[item.atypeId]));
					}
				}
				if (this._actor.isEquipTypeSealed(item.etypeId)) {
					errMsg.push(MUR.dic['equipReqSlotMsg'].replace('%s', $dataSystem.equipTypes[item.etypeId]));
				}

				this.equipInfoWindow._errMsg = errMsg;
			}

			if (this._activeSlot && item) {
				var actor = JsonEx.makeDeepCopy(this._actor);
				actor.forceChangeEquip(this._activeSlotId, item);
				this.equipInfoWindow.setTempActor(actor);
			} else {
				this.equipInfoWindow.setTempActor(null);			
			}
		}
	 }
 };
