var imap_current_x = 0;
var imap_current_y = 0;

function imap_set_xy(x, y)
{
    imap_current_x = x;
    imap_current_y = y;
}

function imap_move(direction)
{
    if (direction == 0)
    {
        //North
        imap_set_xy(imap_current_x, imap_current_y + 256);
    } else if (direction == 1)
    {
        //East
        imap_set_xy(imap_current_x + 256, imap_current_y);
    } else if (direction == 2)
    {
        //South
        imap_set_xy(imap_current_x, imap_current_y - 256);
    } else
    {
        //West
        imap_set_xy(imap_current_x - 256, imap_current_y);
    }

    var imap_img = document.getElementById("imap_img");

    imap_img.src = "/media/galaxy_map.php?x=" + imap_current_x + "&y=" + imap_current_y + "&line=1";
}

/* Angles around Circle
 *         270
 *      315 | 225
 *    000---+---180
 *      045 | 135
 *         090
 */


function nav_sol_move(event)
{
    pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("pointer_div").offsetLeft;
    pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("pointer_div").offsetTop;

    var remove = document.getElementById("movement_tile");
    while (remove != null) {
        remove.parentNode.removeChild(remove);
        remove = document.getElementById("movement_tile");
    }


    current_grid_x = 7;
    current_grid_y = 6;

    end_grid_x = Math.floor(pos_x / 48);
    end_grid_y = Math.floor(pos_y / 48);

    end_x = end_grid_x * 48 + 24;
    end_y = end_grid_y * 48 + 24;

    start_x = (current_grid_x * 48) + 24;
    start_y = (current_grid_y * 48) + 24;

    //alert("sx: " + start_x + " sy: " + start_y + " ex: " + end_x + " ey: " + end_y);

    while ((current_grid_x != end_grid_x) || (current_grid_y != end_grid_y)) {
        start_x = (current_grid_x * 48) + 24;
        start_y = (current_grid_y * 48) + 24;

        angle = (Math.atan2(end_y - start_y, start_x - end_x) * (180/Math.PI));
        if (angle < 0)
        {
            angle += 360;
        }

        d = 360;
        mov = 0;

        for (i=0; i<8; i++)
        {
            if (Math.abs(angle - (45 * i)) < d)
                mov = i;
                d = Math.abs(angle - (45 * i));
        }

        //alert("Pos: " + mov);

        switch (mov)
        {
            case 0:
                x_diff = -1;
                y_diff = 0;
                break;
            case 1:
                x_diff = -1;
                y_diff = 1;
                break;
            case 2:
                x_diff = 0;
                y_diff = 1;
                break;
            case 3:
                x_diff = 1;
                y_diff = 1;
                break;
            case 4:
                x_diff = 1;
                y_diff = 0;
                break;
            case 5:
                x_diff = 1;
                y_diff = -1;
                break;
            case 6:
                x_diff = 0;
                y_diff = -1;
                break;
            case 7:
                x_diff = -1;
                y_diff = -1;
                break;
        }

        var new_div = document.createElement('div');
        new_div.setAttribute('id', 'movement_tile');
        new_div.style.width = 47;
        new_div.style.height = 47;
        new_div.style.position = "absolute";
        new_div.style.opacity = 0.25;
        new_div.style.background = "#ff0000";
        new_div.style.left = (current_grid_x + x_diff) * 48 + 1;
        new_div.style.top = (current_grid_y + y_diff) * 48 + 1;

        document.getElementById("pointer_div").appendChild(new_div);

        current_grid_x += x_diff;
        current_grid_y += y_diff;
    }
}
