// Configuration

// Image name pattern, %l, %z, %z and %y will be replaced with layer, zoom, x and y values
var map_img_name = 'layer_%l/zoom_%z/augs_%l_%z_%x_%y.jpg';
var map_lvl_name = 'layer_%l/zoom_%z_pg/augs_%l_pg_%z_%x_%y.png';

// Tile size
var map_tile_size_x = 800;
var map_tile_size_y = 800;

// Number of digits in the hex coordinates in file names
var map_img_x_digits = 4;
var map_img_y_digits = 4;

// Margins for the auto-resize of map after resizing the window, depends on layout
var map_margin_x = 270;
var map_margin_y = 148;

var map_padding = -2;    // Map will move this many pixels into surrounding white area, negative value crops the map clean

// Movement settings
var move_speed_fixed    = 00;       // fixed speed will override other speed settings
var move_divider        = 3;        // speed = distance / divider
var move_accel_max      = 20;       // limit acceleration to this value
var move_delay          = 40;       // msec to wait before next move-step for window.setTimeout()
var move_fixed          = 000;      // fixed value of pixels to be moved per click, must be multiple of move_speed_fixed
var move_relative       = 0.8;     // movement per click relative to canvas size, will be ignored if move_fixed != 0

/***************/
/* Init values */
/***************/

// Main values
var layer   = 0;
var zoom    = 2;
//var op_low  = 0.8;
var op_low  = 1;

// Starting position in tile coordinates
var cur_x   = 0;
var cur_y   = 0;
var map_w   = 200;
var map_h   = 200;

// Focus position, size and dimensions of mini_map
var foc_x, foc_y, foc_w, foc_h, foc_offs_x, foc_offs_y, mini_map_w, mini_map_h;

// Map moving destination
var dst_x   = 0;
var dst_y   = 0;

// Current speed of movement
var spd_x = move_speed_fixed;
var spd_y = move_speed_fixed;

// Starting szenario and criteria
var szenario = 0;
var kriterium = 0;

var viewport = { t:0, l: 0, b: 0, r: 0 };           // top, left, bottom and right of viewport in tiles
var viewport_px = { t:0, l: 0, b: 0, r: 0 };        // viewport in pixels

var zoom_max = 4;

// How to get zoom_max_limits:
// - Use maximum zoom layer
// - Left and Top: Count empty tiles (or non existing tiles) multiply with tile size and add the offset of the first colored pixel
// example first tile: muc_c_f_000c_0002.jpg fist pixel at 192|81: Left= 12*600+192 Top: 2*600+81
// - Rechts/Unten x/y-Coordinate der letzten Kachel (aus dem Dateinamen) * Kachelbreite + Restpixels auf der Kachel
// example last tile: muc_c_f_0033_001d.jpg, last pixel at 413|494: Right: 51*600+413, Bottom: 29*600+494

//var zoom_max_limits = { l: 400, r: 9200, t: 1600, b: 14400 };      // Exact boundaries of map body on tiles
//var zoom_max_limits = { l: 3905, r: 27300, t: 8624, b: 22513 };      // Exact boundaries of map body on tiles
var zoom_max_limits = { l: 3900, r: 21700, t: 6288, b: 32112 };      // Exact boundaries of map body on tiles
var coords = { l: 4411800, r: 4423100, t: 5367300, b: 5350900 };    // Map coordinates from DB
coords.w = coords.r - coords.l;
coords.h = coords.b - coords.t;

var mouse_mode = 0;
var client_width = 0;
var client_height = 0;

// Half of width/height of target image
var target_offset_x = 23;
var target_offset_y = 23;

var level_info_width = 150;
var last_jump = { x: 0, y: 0};


// Get pixel offset for different browsers
var pixel_offset = 0;
if (navigator.userAgent.search(/Safari/) != -1)     pixel_offset = 1;
if (navigator.userAgent.search(/Firefox/) != -1)    pixel_offset = 2;
if (navigator.userAgent.search(/Camino/) != -1)     pixel_offset = 2;
if (navigator.userAgent.search(/MSIE/) != -1)       pixel_offset = 3;

// Variables for address selection
var selectfield = null;

