if (shop_over == true) {player.setStunned(1); helpShop.alpha = (help) ? 1 : 0;
if (keyboard.isKeyDown(1) || keyboard.isKeyDown(18)) {shop_over = false; effect.clearOverlay();}
shopMoney.text = Math.floor(traderMoney); shopMoney.x = width/2 + 103 - ui.getStringWidth(shopMoney.text);
if (shopMain.alpha == 0) {shopMain.alpha = shopMoney.alpha = shopDisplay.alpha = 1;}

shopMain.texture = "gui/NPC/shopMain"+shopState+".png";

if (shopState == "Buy") {	
	//DRAW SHOPCASES
	for (i = 0; shopCases [i] != undefined; i++) {for (ii = 0; ii < 4; ii++) {shopCases [i] [ii].alpha = 0;}}
	for (i = Math.floor(shopActive/16)*16; shopValues [i] != undefined && shopValues [i] [0] != undefined && 
	shopValues [i] [3] != 0 && i < Math.floor(shopActive/16)*16+16; i++) {
		
		ia = i%16; for (ii = 0; ii < 4; ii++) {shopCases [ia] [ii].alpha = 1;}
		k = shopValues [i] [0]; j = shopValues [i] [1];
		shopCases [ia] [1].u = 16*prices [k] [j] [3]; shopCases [ia] [1].v = 16*prices [k] [j] [4]; 
		shopCases [ia] [1].texture = prices [k] [j] [5]; shopCases [ia] [3].text = shopValues [i] [3];
		
		kk = getPrice(shopValues [i] [3], shopValues [i] [0], shopValues [i] [1], 1, 1, 3, 5);
		shopValues [i] [2] = (kk < 5) ? Math.ceil(kk*10)/10 : Math.ceil(kk);
		if (keyboard.isKeyDown(42) && shopActive == i) for (ii = 1; ii < shopValues [i] [3]; ii++)
			kk += getPrice(shopValues [i] [3] - ii, shopValues [i] [0], shopValues [i] [1], 1, 1, 3, 5);
			
		shopCases [ia] [2].text = (kk < 5) ? Math.ceil(kk*10)/10 : Math.ceil(kk);
		if (shopActive == i) {shopDisplay.text = prices [shopValues [shopActive] [0]] [shopValues [shopActive] [1]] [2];}
		shopCases [ia] [0].texture = (shopActive == i) ? "gui/NPC/shopCaseUp.png" : "gui/NPC/shopCaseDown.png";
	} 
	
	for (i = 0; shopValues [i] != undefined && shopValues [i] [0] != undefined && shopValues [i] [3] != 0; i++) {} shopMax = i - 1;
	
	if (wasKeyPressed(57)) {
		if (Math.floor(coins) >= shopValues [shopActive] [2]) {
			i = (keyboard.isKeyDown(42)) ? shopValues [shopActive] [3] : 1;
			sound.playSoundUI("sound.click"); collectPayment = 0;
			k = shopValues [shopActive] [0]; j = shopValues [shopActive] [1];
			for (ii = 0; i > 0 && Math.floor(coins) >= shopValues [shopActive] [2] && ii < i; ii++) {
				coins -= shopValues [shopActive] [2]; collectPayment += shopValues [shopActive] [2]; pinv.addItem(Item(k, 1, j));
				shopValues [shopActive] [2] = getPrice(shopValues [i] [3] - ii, shopValues [i] [0], shopValues [i] [1], 1, 1, 3, 5);
			}
			shopChange = [collectPayment, shopActive, -ii];
		} else {chat.print("You'll need more money to buy it " + shopValues [shopActive] [2]);}
	}
}

if (shopState == "Sell") {
pVIP = []; ii = 0; 

for (i = 0; i < 36; i++) {
	itemChk = pinv.getItemInSlot(i);
	if (itemChk != null && itemChk.itemID != 468) {
		pVIP [ii] = [itemChk.itemID, itemChk.damage, itemChk.quantity]; 
		if (itemChk.maxDamage != 0) {pVIP [ii] [4] = 1 - itemChk.damage/itemChk.maxDamage; 
		pVIP [ii] [5] = itemChk.damage; pVIP [ii] [1] = 0;}
		if (pVIP [ii] != undefined && prices [pVIP [ii] [0]] [pVIP [ii] [1]] [1] != undefined) {ii++;}
	}
}

if (pVIP [0] != undefined) {
	if (pVIP [ii] == undefined || prices [pVIP [ii] [0]] [pVIP [ii] [1]] [1] == undefined) {pVIP [ii] = []; ii -= 1;}
	if (pVIP [shopActive] == undefined || pVIP [shopActive] [0] == undefined) {shopActive -= 1;}
	
	for (i = Math.floor(shopActive/16)*16; i < Math.floor(shopActive/16)*16+16; i++) {
		if (pVIP [i] != undefined && pVIP [i] [0] != undefined &&
			prices [pVIP [i] [0]] [pVIP [i] [1]] != undefined && prices [pVIP [i] [0]] [pVIP [i] [1]] [1] != undefined) {
			ia = i%16; for (ii = 0; ii < 4; ii++) {shopCases [ia] [ii].alpha = 1;} k = pVIP [i] [0]; j = pVIP [i] [1];
			shopCases [ia] [1].u = 16*prices [k] [j] [3]; shopCases [ia] [1].v = 16*prices [k] [j] [4]; 
			shopCases [ia] [1].texture = prices [k] [j] [5]; 
			shopCases [ia] [0].texture = (shopActive == i) ? "gui/NPC/shopCaseUp.png" : "gui/NPC/shopCaseDown.png";
			
			for (jj = 0; shopValues [jj] != undefined && (shopValues [jj] [0] != pVIP [i] [0] || shopValues [jj] [1] != pVIP [i] [1]); jj++) {}
			kk = pVIP [i] [3] = getPrice((shopValues [jj] != undefined) ? shopValues [jj] [3] : 0, k, j, (pVIP [i] [4] > 0) ? pVIP [i] [4] : 1, 0.1, 0.7, 20);
			if (keyboard.isKeyDown(42) && shopActive == i) for (ii = 1; ii < pVIP [i] [2]; ii++)
				{kk += getPrice(((shopValues [jj] != undefined) ? shopValues [jj] [3] : 0) + ii, k, j, 1, 0.1, 0.7, 20);}
				
			shopCases [ia] [3].text = pVIP [i] [2]; shopCases [ia] [2].text = (kk < 5) ? Math.ceil(kk*10)/10 : Math.ceil(kk);
		} else {for (ii = 0; ii < 4; ii++) {shopCases [i] [ii].alpha = 0;}}
	}
	shopDisplay.text = prices [pVIP [shopActive] [0]] [pVIP [shopActive] [1]] [2];
	
	for (i = 0; pVIP [i] != undefined && pVIP [i] [0] != undefined; i++) {} i++; shopMax = i - 1; 
	
	if (wasKeyPressed(57)) {
		if (Math.floor(traderMoney) >= pVIP [shopActive] [3]) {
			i = (keyboard.isKeyDown(42)) ? pVIP [shopActive] [2] : 1;
			sound.playSoundUI("sound.click"); collectPayment = 0;
			for (jj = 0; shopValues [jj] != undefined && (shopValues [jj] [0] != pVIP [shopActive] [0] 
				|| shopValues [jj] [1] != pVIP [shopActive] [1]); jj++) {} 
			k = pVIP [shopActive] [0]; j = pVIP [shopActive] [1];
			for (ii = 0; Math.floor(traderMoney) >= collectPayment && ii < i; ii++) {
				coins += pVIP [shopActive] [3]; collectPayment += pVIP [shopActive] [3];
				pinv.consumeItemAmount(k, pVIP [shopActive] [(pVIP [shopActive] [5] == undefined) ? 1 : 5], 1);
				pVIP [shopActive] [3] = getPrice(((shopValues [jj] != undefined) ? shopValues [jj] [3] : 0) + ii, k, j, 1, 0.1, 0.7, 20);			
			}
			shopChange = [-collectPayment, -k, ii, j]; 
		} else {chat.print("Trader doesn't have money to buy it");}
	}
} else {for (i = 0; i < 16; i++) for (ii = 0; ii < 4; ii++) {shopCases [i] [ii].alpha = 0;}}
}

if (shopState == "Sell" || shopState == "Buy") {
	pageCount = Math.floor(shopActive/16);
	shopPage [0].text = (pageCount != 0) ? "<" + pageCount : "";
	shopPage [1].text = (pageCount != Math.floor(shopMax/16)) ? (pageCount + 2) + ">" : "";
	shopPage [2].text = (pageCount + 1) + "/" + (Math.floor(shopMax/16) + 1); 
	
	if (keyboard.isKeyDown(42)) {
		if (wasKeyPressed(32) && actionTime(1)) {shopState = (shopState == "Buy") ? "Sell" : "In"; shopActive = 0;}
		if (wasKeyPressed(30) && actionTime(1)) {shopState = (shopState == "Buy") ? "In" : "Buy"; shopActive = 0;}
	} else {
		if (shopActive < shopMax && isKeyActive(32, 5)) {shopActive += 1;} 
		if (shopActive > 0 && isKeyActive(30, 5)) {shopActive -= 1;} 
	}
	if (isKeyActive(31, 5)) {shopActive = Math.min(shopActive + 4, shopMax);}
	if (isKeyActive(17, 5)) {shopActive = Math.max(shopActive - 4, 0);}
}

if (shopState == "In") {
	if (wasKeyPressed(57)) {
		sound.playSoundUI("sound.click");
		if (coins >= shopInvestment) {
			shopChange = [shopInvestment, shopActiveIcon, 0]; 
			coins -= shopInvestment; investment += shopInvestment;
		}
		else {chat.print("You don't enough money to invest that much");} shopInvestment = 0;
	}
	
	shopInCase.alpha = 1; shopInv.text = shopInvestment; shopInv.alpha = 1; shopInIcon.alpha = 1;
	shopInv.x = width/2 + 60 - ui.getStringWidth(shopInv.text); shopDisplay.text = "";
	k = shopValues [shopActiveIcon] [0]; j = shopValues [shopActiveIcon] [1];
	shopInIcon.u = 16*prices [k] [j] [3]; shopInIcon.v = 16*prices [k] [j] [4]; shopInIcon.texture = prices [k] [j] [5];
	
	if (shopActive == 6) {
		shopInCase.x = width/2 - 66.5;
		if (shopValues [shopActiveIcon - 1] != undefined && isKeyActive(31, 5)) {shopActiveIcon -= 1}
		if (shopValues [shopActiveIcon + 1] != undefined && isKeyActive(17, 5)) {shopActiveIcon += 1}
	} else {
		shopInCase.x = width/2 + 48 - shopActive*6;
		if (isKeyActive(31, 5)) {shopInvestment -= Math.pow(10, shopActive);}
		if (isKeyActive(17, 5)) {shopInvestment += Math.pow(10, shopActive);}
	}
	
	if (keyboard.isKeyDown(42)) {
		if (wasKeyPressed(32) && actionTime(1)) {shopState = "Buy"; shopActive = 0;}
		if (wasKeyPressed(30) && actionTime(1)) {shopState = "Sell"; shopActive = 0;}
	} else {
		if (isKeyActive(30, 5)) {shopActive += (shopActive == 6) ? -6 : 1;} 
		if (isKeyActive(32, 5)) {shopActive -= (shopActive == 0) ? -6 : 1;} 
	}
	if (shopInvestment < 0) {shopInvestment = 0;}
	shopPage [0].text = shopPage [1].text = shopPage [2].text = "";
	for (i = 0; shopCases [i] != undefined; i++) {for (ii = 0; ii < 4; ii++) {shopCases [i] [ii].alpha = 0;}}
}
else {shopInCase.alpha = 0; shopInvestment = 0; shopInv.alpha = 0; shopInIcon.alpha = 0;}}

else {helpShop.alpha = 0; shopMain.alpha = 0; shopMoney.alpha = 0; shopDisplay.alpha = 0; 
shopInCase.alpha = 0; shopInvestment = 0; shopInv.alpha = 0; shopInIcon.alpha = 0;
shopPage [0].text = shopPage [1].text = shopPage [2].text = "";
for (i = 0; shopCases [i] != undefined; i++) {for (ii = 0; ii < 4; ii++) {shopCases [i] [ii].alpha = 0;}}}