// Medkit

if(HealthTimer > 0)
{
	--HealthTimer
}

if(keyboard.isKeyDown(44)) // 44 - 'H' key
{
	if(medkit_first)
	{
		UseMedKit();
		medkit_first = false;
	}
}
else
{
	medkit_first = true;
}

if(HealthTimer != 0 && player.getHealth() != 1)
{
	player.setHealth(player.getHealth() - removeHealth)
	time.sleep(4)
}

//*******************************//
//  Start of Code for            //
//  Item Management v2.1         //
//                               //
//*******************************//

  //////////
 // Init //
//////////
screen.clear();

  //////////
 // Vars //
//////////
h = ui.getHeight();

  //////////
 // Code //
//////////
try
{
	if (ui.getHeight() != im_sh) {
		var ne = ui.getHeight();
		screen.clear();
		ne = im_sh;
	}

	var rIndx = 0;
	// We have no currency. Show zero and call it quits.
	for (var indx in im_db) {
		if (im_pinv.getSlotContainingItem(im_db[indx][0]) == -1)
		{
			if (im_db[indx][5] == true)  {
				renderCurrency(0,indx,rIndx);
				rIndx++;
			}
		} 
		else 
		{
			try 
			{
				// Initialize our item counter at 0
				iCount = 0; iMax = 0;
			
				// Array of positions
				iPos = new Array();
		
				// New version
				for (ii = 0; ii <= 39; ii++) {
					iItem = im_pinv.getItemInSlot(ii);
			
					if (iItem != null) if (iItem.getItemID() == im_db[indx][0]) {
						if (iItem.getQuantity() <= 0) {
							im_pinv.emptySlot(ii);
						} else {
							if (im_db[indx][2] == true) {
								iCount += iItem.getMaxDamage()-iItem.getDamage();
								iMax += iItem.getMaxDamage();
							} else {
								iCount += iItem.getQuantity();
							}
						}
					}
				}

				//If forced to stack, stack to which of the 4 slots?
				//if (im_db[indx][2] != false) {
				//	im_pinv.setSlot(35+im_db[indx][2],new Item(im_db[indx][0],iCount,5));
				//}

				// Ensure top limit is not exceeded.	
				if (iCount > im_db[indx][1]) {
					iDif = iCount - im_db[indx][1];
					iCount = im_db[indx][1];
					for (var jj = iDif; jj > 0; jj--) {
						dItem(im_db[indx][0]);
                                        }
				}

				// Do render
				if (im_db[indx][2] == true) {renderCurrency(iCount+"/"+iMax,indx,rIndx);}
				else {renderCurrency(iCount,indx,rIndx);}
				rIndx++;
			}
			catch(e) { chat.print(e) }
		}
	}
	
}
catch(e) { chat.print(e) }


  ///////////////
 // Functions //
///////////////
function renderCurrency(q,i,r) {
	UISprite(im_db[i][3],5,h-20-(17*r),15,15,im_db[i][4][0],im_db[i][4][1])
	lText = UILabel(q,23,h-15-(17*r))
	if (q >= im_db[i][1]) {
		lText.red = lText .blue = 0;
	}
}

function dItem(id) {
	im_pinv.decrementItem(im_pinv.getSlotContainingItem(id),1);
}

//*******************************//
//  End of Code for              //
//  Item Management v2.1         //
//                               //
//*******************************//

if(pinv.getItemInSlot(25) != null)
{
        if(pinv.getItemInSlot(25).getItemID() != 488)
        {
                for(i=0;i<25;i++)
                {
                        if(pinv.getItemInSlot(i) == null)
                        {
                                var pitem = pinv.getItemInSlot(25).copy();
                                pinv.emptySlot(25);
                                pinv.setSlot(i, item);
                        }
                }
        }
}
if (pinv.getSlotContainingItem(488) != -1)
{
    if (pinv.getSlotContainingItem(488) != 25)
    {
        try
        {
            var count = pinv.getItemInSlot(25).getQuantity();
        }
        catch(e) { var count = 0; }
        var item = pinv.getItemInSlot(pinv.getSlotContainingItem(488)).copy();
        pinv.emptySlot(pinv.getSlotContainingItem(488));
        if (count < 70)
        {
            if (item.getQuantity() + count <= 70)
            {
                item.setQuantity(item.getQuantity() + count);
            }
            else
            {
                item.setQuantity(70);
            }
        }
        else
        {
            item.setQuantity(70);
        }
        pinv.setSlot(25, item);
    }
}

if (pinv.getItemInSlot(39) != null && pinv.getItemInSlot(39).getItemID() == 323)
{
	effect.setOverlay("coverlay")
}
else
{
	effect.clearOverlay()
}
