/*
  Eon Aton: a RPG-like game system in JavaScript
  Copyright (C) 2008-2009  Niels Serup
  
	This file is part of Eon Aton.

	Eon Aton is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Eon Aton is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Eon Aton.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
  This file contains various functions to be used together with the classes.
*/
function style_bg_img(background) {
  var cssurl
  if (background.indexOf(".")!=-1) cssurl=["url(",")"]
  else cssurl=["",""]
  return cssurl[0]+background+cssurl[1]
}

function remove(obj) {
  if (obj.type="character") take_life(obj)
  obj.remove()
  delete obj
}

function getkeycode(e) {
  var keycode
  if (window.event) keycode=window.event.keyCode
  else if (e) keycode=e.which
  else keycode=0
  return keycode
}

function START_systems() {
  key_up=false
  key_right=false
  key_down=false
  key_left=false
  key_shift=false
  key_control=false
  
  key_order=new Array()
  for (var i=0;i<4;i++) {
    key_order[i]=0
  }
  
  direction=7
  character_state=1
  
  document.onkeydown=keydown
  document.onkeyup=keyup
  
  row_num=new Array()
  col_num=new Array()
  col_length=new Array()
  
  maxwidth=0
  maxheight=0
  
  define_pixel_movement(5,10)
  define_character_distance(-5)
  
  direction_changing_speed=3
  
  frame_showing_speed=150
  movement_speed=50
  frame_showing()
  movement()
}

function define_pixel_movement(walk,run) {
  pixel_movement=[0,walk,run]
  diagonal_pixel_movement=[0,Math.sqrt(walk*walk/2),Math.sqrt(run*run/2)]
}

function define_character_distance(dist) {
  character_distance=dist
  diagonal_character_distance=Math.sqrt(dist*dist*2)
  if (dist<0) diagonal_character_distance=-diagonal_character_distance
}

function show_frame(cha_num,row,col) {
  characters[cha_num].elem.style.backgroundPosition="-"+(col*(characters[cha_num].width+1)+1)+"px -"+(characters[cha_num].dirs[row][0]*(characters[cha_num].height+1)+1)+"px"
}

function frame_showing() {
  for (var i=0;i<characters.length;i++) {
    if (col_num[i]<col_length[i]) col_num[i]++
    else col_num[i]=0
    if (characters[i].visible) show_frame(i,row_num[0],characters[i].dirs[row_num[i]][character_state][col_num[i]])
  }
  setTimeout(frame_showing,frame_showing_speed)
}

function movement() {
  var pixmove=pixel_movement[character_state-1]
  var dia_pixmove=diagonal_pixel_movement[character_state-1]
  var li, x, y, s_x, s_y, cy
  for (var i=0;i<characters.length;i++) {
    if (characters[i].visible) {
      switch(direction) {
        case 1:
          if (i==0) {
            x=characters[i].posX
            y=characters[i].posY-pixmove
          }
          else {
            x=characters[i].posX+(characters[li].posX-characters[i].posX)/direction_changing_speed
            y=characters[i].posY+(characters[li].posY-characters[i].posY+character_distance+characters[li].height)/direction_changing_speed
          }
          break;
        case 2:
          if (i==0) {
            x=characters[i].posX+dia_pixmove
            y=characters[i].posY-dia_pixmove
          }
          else {
            x=characters[i].posX+(characters[li].posX-characters[i].posX-diagonal_character_distance-characters[i].width)/direction_changing_speed
            y=characters[i].posY+(characters[li].posY-characters[i].posY+diagonal_character_distance+characters[li].height)/direction_changing_speed
          }
          break;
        case 3:
          if (i==0) {
            x=characters[i].posX+pixmove
            y=characters[i].posY
          }
          else {
            x=characters[i].posX+(characters[li].posX-characters[i].posX-character_distance-characters[i].width)/direction_changing_speed
            y=characters[i].posY+(characters[li].posY-characters[i].posY)/direction_changing_speed
          }
          break;
        case 4:
          if (i==0) {
            x=characters[i].posX+dia_pixmove
            y=characters[i].posY+dia_pixmove
          }
          else {
            x=characters[i].posX+(characters[li].posX-characters[i].posX-diagonal_character_distance-characters[i].width)/direction_changing_speed
            y=characters[i].posY+(characters[li].posY-characters[i].posY-diagonal_character_distance-characters[i].height)/direction_changing_speed
          }
          break;
        case 5:
          if (i==0) {
            x=characters[i].posX
            y=characters[i].posY+pixmove
          }
          else {
            x=characters[i].posX+(characters[li].posX-characters[i].posX)/direction_changing_speed
            y=characters[i].posY+(characters[li].posY-characters[i].posY-character_distance-characters[i].height)/direction_changing_speed
          }
          break;
        case 6:
          if (i==0) {
            x=characters[i].posX-dia_pixmove
            y=characters[i].posY+dia_pixmove
          }
          else {
            x=characters[i].posX+(characters[li].posX-characters[i].posX+diagonal_character_distance+characters[li].width)/direction_changing_speed
            y=characters[i].posY+(characters[li].posY-characters[i].posY-diagonal_character_distance-characters[i].height)/direction_changing_speed
          }
          break;
        case 7:
          if (i==0) {
            x=characters[i].posX-pixmove
            y=characters[i].posY
          }
          else {
            x=characters[i].posX+(characters[li].posX-characters[i].posX+character_distance+characters[li].width)/direction_changing_speed
            y=characters[i].posY+(characters[li].posY-characters[i].posY)/direction_changing_speed
          }
          break;
        case 8:
          if (i==0) {
            x=characters[i].posX-dia_pixmove
            y=characters[i].posY-dia_pixmove
          }
          else {
            x=characters[i].posX+(characters[li].posX-characters[i].posX+diagonal_character_distance+characters[li].width)/direction_changing_speed
            y=characters[i].posY+(characters[li].posY-characters[i].posY+diagonal_character_distance+characters[li].height)/direction_changing_speed
          }
          break;
      }
      
      if (x>-1 && x<current_screen.width-characters[i].width+1) characters[i].posX=x
      if (y>-1 && y<current_screen.height-characters[i].height+1) characters[i].posY=y
      
      if (direction==1 || direction==5) s_x=(maxwidth-characters[i].width)/2
      else s_x=0
      if (direction==3 || direction==7) s_y=maxheight-characters[i].height
      else s_y=0
          
      characters[i].elem.style.left=characters[i].posX+s_x+"px"
      characters[i].elem.style.top=characters[i].posY+s_y+"px"
      
      cy=characters.length+1
      for (ii=0;ii<characters.length;ii++) {
        if (characters[ii].posY>characters[i].posY) cy--
      }
      characters[i].elem.style.zIndex=cy
      li=i
    }
    
    if (characters.length>0) {
      if (characters[0].posX>(current_screen.viswidth-characters[0].width)/2 && characters[0].posX<current_screen.width-(current_screen.viswidth+characters[0].width)/2) current_screen.elem.style.left=-(characters[0].posX-(current_screen.viswidth-characters[0].width)/2)+"px"
      if (characters[0].posY>(current_screen.visheight-characters[0].height)/2 && characters[0].posY<current_screen.height-(current_screen.visheight+characters[0].height)/2) current_screen.elem.style.top=-(characters[0].posY-(current_screen.visheight-characters[0].height)/2)+"px"
    }
  }
  
  setTimeout(movement,movement_speed)
}

function get_character_state() {
  if (key_order[0]==0) character_state=1
  else if (!key_shift && key_order[0]>0) character_state=2
  else if (key_shift && key_order[0]>0) character_state=3
}

function get_row_num() {
  for (var i=0;i<characters.length;i++) {
    if (!characters[i].dirs[direction-1]) {
      if (direction==2 && key_order[1]==1) row_num[i]=0
      else if (direction==2 && key_order[1]==2) row_num[i]=2
      else if (direction==4 && key_order[1]==2) row_num[i]=2
      else if (direction==4 && key_order[1]==3) row_num[i]=4
      else if (direction==6 && key_order[1]==3) row_num[i]=4
      else if (direction==6 && key_order[1]==4) row_num[i]=6
      else if (direction==8 && key_order[1]==4) row_num[i]=6
      else if (direction==8 && key_order[1]==1) row_num[i]=0
    }
    else row_num[i]=direction-1
  }
}

function get_col_length() {
  get_character_state()
  get_row_num()
  for (var i=0;i<characters.length;i++) {
    col_length[i]=characters[i].dirs[row_num[i]][character_state].length-1
  }
}

function get_maxs() {
  maxwidth=0
  maxheight=0
  for (var i=0;i<characters.length;i++) {
    if (characters[i].width>maxwidth) maxwidth=characters[i].width
    if (characters[i].height>maxheight) maxheight=characters[i].height
  }
}

function find_direction() {
  if (key_order[0]==1 && key_order[1]!=2 && key_order[1]!=4) direction=1
  else if ((key_order[0]==1 && key_order[1]==2) || (key_order[0]==2 && key_order[1]==1)) direction=2
  else if (key_order[0]==2 && key_order[1]!=1 && key_order[1]!=3) direction=3
  else if ((key_order[0]==2 && key_order[1]==3) || (key_order[0]==3 && key_order[1]==2)) direction=4
  else if (key_order[0]==3 && key_order[1]!=2 && key_order[1]!=4) direction=5
  else if ((key_order[0]==3 && key_order[1]==4) || (key_order[0]==4 && key_order[1]==3)) direction=6
  else if (key_order[0]==4 && key_order[1]!=3 && key_order[1]!=1) direction=7
  else if ((key_order[0]==4 && key_order[1]==1) || (key_order[0]==1 && key_order[1]==4)) direction=8
  else direction=0
}

function keydown(e) {
  var keycode=getkeycode(e)
    
  if (keycode==38 && !key_up) {
    key_up=true
    key_order.splice(0,0,1)
    key_order.splice(3,1)
  }
  else if (keycode==39 && !key_right) {
    key_right=true
    key_order.splice(0,0,2)
    key_order.splice(3,1)
  }
  else if (keycode==40 && !key_down) {
    key_down=true
    key_order.splice(0,0,3)
    key_order.splice(3,1)
  }
  else if (keycode==37 && !key_left) {
    key_left=true
    key_order.splice(0,0,4)
    key_order.splice(3,1)
  }
  else if (keycode==16) key_shift=true
  else if (keycode==17) key_control=true
  
  if (keycode==38 || keycode==39 || keycode==40 || keycode==37) find_direction()
  if (keycode==38 || keycode==39 || keycode==40 || keycode==37 || keycode==16) get_col_length()
}

function keyup(e) {
  var keycode=getkeycode(e)
  
  if (keycode==38 && key_up) {
    key_up=false
    for (var i=0;i<4;i++) {
      if (key_order[i]==1) key_order.splice(i,1)
    }
    key_order.splice(3,0,0)
  }
  else if (keycode==39 && key_right) {
    key_right=false
    for (var i=0;i<4;i++) {
      if (key_order[i]==2) key_order.splice(i,1)
    }
    key_order.splice(3,0,0)
  }
  else if (keycode==40 && key_down) {
    key_down=false
    for (var i=0;i<4;i++) {
      if (key_order[i]==3) key_order.splice(i,1)
    }
    key_order.splice(3,0,0)
  }
  else if (keycode==37 && key_left) {
    key_left=false
    for (var i=0;i<4;i++) {
      if (key_order[i]==4) key_order.splice(i,1)
    }
    key_order.splice(3,0,0)
  }
  else if (keycode==16) key_shift=false
  else if (keycode==17) key_control=false
  
  if (key_order[0]>0 && (keycode==38 || keycode==39 || keycode==40 || keycode==37)) find_direction()  
  if (keycode==38 || keycode==39 || keycode==40 || keycode==37 || keycode==16) get_col_length()
}

function exchange_characters(a,b) {
  var temp=characters[a]
  characters[a]=characters[b]
  characters[b]=temp
}

function give_life(cha) {
  col_num[characters.length]=0
  cha.visible=true
  cha.elem.style.display="block"
  get_col_length()
  get_maxs()
}

function take_life(cha) {
  cha.visible=false
  cha.elem.style.display="none"
  get_maxs()
}