keys = {
  { "C", "sin", "cos", "tan" },
  { "1/x", "x^2", "sqr", "/" },
  { "7", "8", "9", "*" },
  { "4", "5", "6", "-" },
  { "1", "2", "3", "+" },
  { "0", "+/-", ".", "=" } }

-- PSysPInfo
--created by Glynnder - PSPro
cont = false
purple =  Color.new(167, 29, 226)

dblue = Color.new(0, 0, 255)

red = Color.new(230, 0, 0)

pink = Color.new(255, 0 , 153)

blue = Color.new(0 , 153, 255)

color = Color.new(0 , 153, 255)

orange = Color.new(255, 102, 0)

gray = Color.new(0, 0, 0)

black = Color.new(0 , 0 , 0)

white = Color.new(255, 255, 255)
green = Color.new(0 , 255, 0)
pagestatus=0
infostatus=1

--Loading bg items
bg = Image.load("mainbg2.png")
bg2 = Image.load("mainbg.png")
bg3 = Image.load("mainbg3.jpg")
bg1 = Image.load("mainbg1.png")
bg4 = Image.load("mainbg4.png")
bg5 = Image.load("mainbg5.png")
bg6 = Image.load("mainbg6.png")

controls = Image.load("controls.png")


screen.waitVblankStart(60)

screen.flip()

function drawRect(x0, y0, w, h)
  screen:drawLine(x0, y0, x0+w, y0, color)
  screen:drawLine(x0, y0, x0, y0+h, color)
  screen:drawLine(x0+w, y0, x0+w, y0+h, color)
  screen:drawLine(x0+w, y0+h, x0, y0+h, color)
        screen:fillRect(x0+1, y0+1, w-1, h-1, gray)
end

exPad = Controls.read()
oldPad = Controls.read()
x = 1
y = 1
text = "0"
lastNumber = 0
deleteOnKey = true
lastOperation = ""
value = ""
number = 0

while true do

  pad = Controls.read()
  if pad ~= oldPad then
    if pad:left() then
      x = x - 1
      if x == 0 then
        x = 4
      end
    end
    if pad:right() then
      x = x + 1
      if x == 5 then
        x = 1
      end
    end
    if pad:up() then
      y = y - 1
      if y == 0 then
        y = 6
      end
    end
    if pad:down() then
      y = y + 1
      if y == 7 then
        y = 1
      end
    end
    if pad:cross() then
      value = keys[y][x]
      number = tonumber(text)
      if value == "C" then
        text = "0"
        lastNumber = 0
        lastOperation = ""
        deleteOnKey = true
      elseif value == "1/x" then
        text = tostring(1/number)
      elseif value == "sin" then
        text = tostring(math.sin(number))
      elseif value == "cos" then
        text = tostring(math.cos(number))
      elseif value == "tan" then
        text = tostring(math.tan(number))
      elseif value == "x^2" then
        text = tostring(number*number)
      elseif value == "sqr" then
        text = tostring(math.sqrt(number))
      elseif value == "/" or value == "*" or value == "-" or value == "+" or value == "=" then
        if lastOperation == "/" then
          text = tostring(lastNumber / number)
        elseif lastOperation == "*" then
          text = tostring(lastNumber * number)
        elseif lastOperation == "-" then
          text = tostring(lastNumber - number)
        elseif lastOperation == "+" then
          text = tostring(lastNumber + number)
        end
        number = tonumber(text)
        if value == "=" then
          text = tostring(number)
          lastOperation = ""
        else
          lastNumber = number
          lastOperation = value
        end
        deleteOnKey = true
      elseif value == "+/-" then
        text = tostring(-number)
      else
        if deleteOnKey then
          text = value
          deleteOnKey = false
        else
          text = text .. value
        end
      end
    end
    oldPad = pad
  end
  
  screen:clear()
  yk = 0
  w = 40
  h = 20
  w2 = 160
  h2 = 84
--blit images
--skins
if pagestatus==0
then
          screen:blit(0, 0, bg, false)
          elseif pagestatus==1 then
          screen:blit(0, 0, bg2, false)
          elseif pagestatus==2 then
          screen:blit(0, 0, bg3, false)
          elseif pagestatus==4 then
          screen:blit(0, 0, bg1, false)
          elseif pagestatus==3 then
          screen:blit(0, 0, bg4, false)          
          elseif pagestatus==5 then
          screen:blit(0, 0, bg5, false)
      elseif pagestatus==6 then
          screen:blit(0, 0, bg6, false)
          end
      
if pagestatus==7
then pagestatus=pagestatus-7
end
         if pagestatus==-1
then pagestatus=pagestatus+7
end
  drawRect(w2, 84, w * 4, h-2)
  screen:print(162, 88, text, color)
  for yindex,line in keys do
    for xindex,key in line do
      x0 = xindex * w + 120
      y0 = yindex * h + h2
      drawRect(x0, y0, w, h)
      if xindex == x and yindex == y then
        screen:fillRect(x0, y0, w, h, color)
        foreground = Color.new(0, 0, 0)
      else
        foreground = color
      end
      screen:print(x0 + 5, y0 + 5, key, foreground)
    end
  end

        time = os.time()

          dateString = os.date("%c")

      screen:print(1, 1, os.date(), red)

      dateFields = os.date("*t")

      hour = dateFields.hour

      if hour < 10 then

        hour = "0" .. hour

      end
      min = dateFields.min

      if min < 10 then

      min = "0" .. min

      end
  sec = dateFields.sec

  if sec < 10 then

    sec = "0" .. sec

  end

      screen:print(30, 260, "Press 'Start' to quit", blue)

      if  (System.powerIsLowBattery()) == true then

    screen:print(410, 1, tostring("Batt:".. System.powerGetBatteryLifePercent()) .. "%", red)

    else screen:print(410, 1, tostring("Batt:".. System.powerGetBatteryLifePercent()) .. "%", red)

    end

    if (System.powerIsBatteryCharging()) == true then

    screen:print(472, 1, "+", red) end 

                screen:print(300, 260, "'Select' = Screenshot", white)

          screen.flip()


          if pad:start() then

                    break

          end          


screen.flip()
  if pad ~= exPad then
  
    if pad:l() then
    pagestatus = pagestatus - 1
    exPad = pad
        else
    exPad = pd
    end
else if pad:r() then
    pagestatus = pagestatus
    end
    end
    
    if pad:triangle() then
    dofile("index.lua")
    end

  if pad ~= exPad then
    if pad:r() then
    pagestatus = pagestatus + 1
    exPad = pad
        else
    exPad = pd
    end
else if pad:l() then
    pagestatus = pagestatus
    end
    end


  screen.waitVblankStart()
  screen.flip()
end