
var offset_x = 25;
var offset_y = 41;
var margin_right = 25;
var margin_down = 25; // 60;
var min_w = 652;
var min_h = 80;
var default_w = 868;
var default_h = 680; 
var first_loops = 0;
var do_send = 0;

function first() {
    var center_x = 385;
    var center_y = 475;
    var max_w = 868;
    var max_h = 680;
    var w = max_w;
    var h = max_h;
    var x = 0;
    var y = 0;
    if (typeof(window.innerWidth) == "number") {
        // Non-IE
        w = window.innerWidth;
        h = window.innerHeight;
    } 
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight )) {
        // IE 6+ in 'standards compliant mode'
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
    } 
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        // IE 4 compatible
        w = document.body.clientWidth;
        h = document.body.clientHeight;
    }
    if ((w==0 && h==0) || first_loops<1) {
        first_loops++;
        setTimeout('first()',100);
        return;
    }
    //alert("w: "+w+"\nh: "+h);
    w -= offset_x;
    w -= margin_right;
    w = Math.min(w,max_w);
    w = Math.max(w,min_w);
    h -= offset_y;
    h -= margin_down;
    h = Math.min(h,max_h);
    h = Math.max(h,min_h);
    x = Math.max(center_x-Math.max(Math.round(w/2), w-(max_w-center_x)), 0);
    y = Math.max(center_y-Math.max(Math.round(h/2), h-(max_h-center_y)), 0);
    document.send.tbx.value = x;
    document.send.tby.value = y;
    document.send.tbw.value = w;
    document.send.tbh.value = h;
    //alert("w: "+w+"\nh: "+h+"\nx: "+x+"\ny: "+y);
    document.send.submit();
}

function map(dummy) {
    //alert("dummy");
    if (do_send==1) {
        document.send.submit();
        do_send = 0;
    }
}

function send(shortcut, zoom, x, y, w, h) {
    document.send.tbx.value = x;
    document.send.tby.value = y;
    document.send.tbw.value = w;
    document.send.tbh.value = h;
    document.send.tbzoom.value = zoom;
    document.send.tbplus.value = document.f.plus.value;
    document.send.tbminus.value = document.f.minus.value;
    document.send.tbcenter.value = document.f.center.value;
    document.send.tbshortcut.value = shortcut;
    do_send = 1; // litet knep fšr att lura vissa weblŠsare att godta href som inte gŒr nŒgonstans
    //document.send.submit();
}

function newmap(shortcut, zoom, center_x, center_y, max_w, max_h, max_x, max_y) {
    //alert("x: "+center_x+"   y: "+center_y);
    var w = default_w;
    var h = default_h;
    
    if (typeof(window.innerWidth) == "number") {
        // Non-IE
        w = window.innerWidth;
        h = window.innerHeight;
    } 
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight )) {
        // IE 6+ in 'standards compliant mode'
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
    } 
    else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        // IE 4 compatible
        w = document.body.clientWidth;
        h = document.body.clientHeight;
    }
    if (w==0 && h==0) {
        setTimeout('newmap(shortcut, zoom, center_x, center_y, max_w, max_h, max_x, max_y)',100);
        return;
    }
    w -= offset_x;
    w -= margin_right;
    w = Math.min(w,max_w);
    w = Math.max(w,min_w);
    h -= offset_y;
    h -= margin_down;
    h = Math.min(h,max_h);
    h = Math.max(h,min_h);
    var x = Math.max(center_x-Math.max(Math.round(w/2), w-(max_x-center_x)), 0);
    var y = Math.max(center_y-Math.max(Math.round(h/2), h-(max_y-center_y)), 0);

    send(shortcut, zoom, x, y, w, h);
}

function iconClick(event) {
    var w = parseInt(document.f.w.value);
    var cx = offset_x+w-25; // default
    var cy = 20;
    
    if (!isNaN(event.x)) { // OK: Safari, Navigator, Explorer, Opera
        cx = event.x;
        cy = event.y;
    }
    else if (!isNaN(event.clientX)) { // OK: Safari, Explorer, Opera, Netscape
        cx = event.clientX;
        cy = event.clientY;
    }
    else if (!isNaN(event.layerX)) { // OK: Safari, Netscape, Navigator
        cx = event.layerX;
        cy = event.layerY;
    }
    else if (!isNaN(event.pageX)) { // OK: Safari, Netscape, Navigator
        cx = event.pageX;
        cy = event.pageY;
    }
    
    cx -= (offset_x+w-50);
    //alert("cx: "+cx+"\ncy: "+cy);
    
    cx = Math.max(0,Math.min(51, cx));
    cy = Math.max(0,Math.min(40, cy));
    
    if (document.f.zoom.value == "-1") {
        cx = Math.round(cx*17.0196);
        cy *= 17;
        newmap(0, -1, cx, cy, 868, 680, 868, 680);
    }
    else if (document.f.zoom.value=="0") {
        cx = Math.round(cx*17.0196);
        cy *= 17;
        newmap(0, 0, cx, cy, 868, 680, 868, 680);
    }
    else if (document.f.zoom.value == "1") {
        cx = Math.round(cx*34.039);
        cy *= 34;
        newmap(0, 1, cx, cy, 1736, 1360, 1736, 1360);
    }
    else if (document.f.zoom.value == "2") {
        cx = Math.round(cx*68.078);
        cy *= 68;
        newmap(0, 2, cx, cy, 1736, 1360, 3472, 2720);
    }
    else if (document.f.zoom.value == "3") {
        cx = Math.round(cx*136.1568);
        cy *= 136;
        newmap(0, 3, cx, cy, 1736, 1360, 6944, 5440);
    }
}

function mapClick(event) {
    var cx = 434+offset_x; // default
    var cy = 340+offset_y;
    
    if (!isNaN(event.x)) { // OK: Safari, Navigator, Explorer, Opera
        cx = event.x;
        cy = event.y;
    }
    else if (!isNaN(event.clientX)) { // OK: Safari, Explorer, Opera, Netscape
        cx = event.clientX;
        cy = event.clientY;
    }
    else if (!isNaN(event.layerX)) { // OK: Safari, Netscape, Navigator
        cx = event.layerX;
        cy = event.layerY;
    }
    else if (!isNaN(event.pageX)) { // OK: Safari, Netscape, Navigator
        cx = event.pageX;
        cy = event.pageY;
    }
    
    //alert("cx: "+cx+"\ncy: "+cy); return;
    
    cx -= offset_x;
    cy -= offset_y;
    
    var x = parseInt(document.f.x.value);
    var y = parseInt(document.f.y.value);
    
    if (document.f.zoom.value == "-1") {
        if (document.f.plus.value == "1") {
            var x1 = 339-x;
            var y1 = 437-y;
            var x2 = 432-x;
            var y2 = 512-y;
            if (cx<x1 || cx>x2 || cy<y1 || cy>y2) {
                return; // ej klick i rutan
            }
            cx -= x1;
            cy -= y1;
            var center_x = Math.max(Math.min(Math.round(cx*9.333),868),0);
            var center_y = Math.max(Math.min(Math.round(cy*9.067),680),0);
            newmap(0, 0, center_x, center_y, 868, 680, 868, 680);
        }
        else if (document.f.center.value == "1") {
            newmap(0, -1, x+cx, y+cy, 868, 680, 868, 680);
        }
    }
    else if (document.f.zoom.value == "0") {
        if (document.f.plus.value == "1") {
            newmap(0, 1, (x+cx)*2, (y+cy)*2, 1736, 1360, 1736, 1360);
        }
        else if (document.f.minus.value == "1") {
            newmap(0, -1, 385, 475, 868, 680, 868, 680);
        }
        else if (document.f.center.value == "1") {
            newmap(0, 0, x+cx, y+cy, 868, 680, 868, 680);
        }
    }
    else if (document.f.zoom.value == "1") {
        if (document.f.plus.value == "1") {
            newmap(0, 2, (x+cx)*2, (y+cy)*2, 1736, 1360, 3472, 2720);
        }
        else if (document.f.minus.value == "1") {
            newmap(0, 0, Math.round((x+cx)/2), Math.round((y+cy)/2), 868, 680, 868, 680);
        }
        else if (document.f.center.value == "1") {
            newmap(0, 1, x+cx, y+cy, 1736, 1360, 1736, 1360);
        }
    }
    else if (document.f.zoom.value == "2") {
        if (document.f.plus.value == "1") {
            newmap(0, 3, (x+cx)*2, (y+cy)*2, 1736, 1360, 6944, 5440);
        }
        else if (document.f.minus.value == "1") {
            newmap(0, 1, Math.round((x+cx)/2), Math.round((y+cy)/2), 1736, 1360, 1736, 1360);
        }
        else if (document.f.center.value == "1") {
            newmap(0, 2, x+cx, y+cy, 1736, 1360, 3472, 2720);
        }
    }
    else if (document.f.zoom.value == "3") {
        if (document.f.minus.value == "1") {
            newmap(0, 2, Math.round((x+cx)/2), Math.round((y+cy)/2), 1736, 1360, 3472, 2720);
        }
        else if (document.f.center.value == "1") {
            newmap(0, 3, x+cx, y+cy, 1736, 1360, 6944, 5440);
        }
    }
}

function up() {
    var zoom = parseInt(document.f.zoom.value);
    var x = parseInt(document.f.x.value);
    var y = parseInt(document.f.y.value);
    var w = parseInt(document.f.w.value);
    var h = parseInt(document.f.h.value);
    x = Math.round(x+(w*0.5));
    y = Math.max(0, Math.round(y-(h*0.25)));
    if (zoom==-1 || zoom==0) {
        newmap(0, zoom, x, y, 868, 680, 868, 680);
    }
    else if (zoom==1) {
        newmap(0, zoom, x, y, 1736, 1360, 1736, 1360);
    }
    else if (zoom==2) {
        newmap(0, zoom, x, y, 1736, 1360, 3472, 2720);
    }
    else if (zoom==3) {
        newmap(0, zoom, x, y, 1736, 1360, 6944, 5440);
    }
    map("");
}

function down() {
    var zoom = parseInt(document.f.zoom.value);
    var x = parseInt(document.f.x.value);
    var y = parseInt(document.f.y.value);
    var w = parseInt(document.f.w.value);
    var h = parseInt(document.f.h.value);
    x = Math.round(x+(w*0.5));
    if (zoom==-1 || zoom==0) {
        y = Math.min(680, Math.round(y+(h*1.25)));
        newmap(0, zoom, x, y, 868, 680, 868, 680);
    }
    else if (zoom==1) {
        y = Math.min(1360, Math.round(y+(h*1.25)));
        newmap(0, zoom, x, y, 1736, 1360, 1736, 1360);
    }
    else if (zoom==2) {
        y = Math.min(2720, Math.round(y+(h*1.25)));
        newmap(0, zoom, x, y, 1736, 1360, 3472, 2720);
    }
    else if (zoom==3) {
        y = Math.min(5440, Math.round(y+(h*1.25)));
        newmap(0, zoom, x, y, 1736, 1360, 6944, 5440);
    }
    map("");
}

function left() {
    var zoom = parseInt(document.f.zoom.value);
    var x = parseInt(document.f.x.value);
    var y = parseInt(document.f.y.value);
    var w = parseInt(document.f.w.value);
    var h = parseInt(document.f.h.value);
    x = Math.max(0, Math.round(x-(w*0.25)));
    y = Math.round(y+(h*0.5));
    if (zoom==-1 || zoom==0) {
        newmap(0, zoom, x, y, 868, 680);
    }
    else if (zoom==1) {
        newmap(0, zoom, x, y, 1736, 1360, 1736, 1360);
    }
    else if (zoom==2) {
        newmap(0, zoom, x, y, 1736, 1360, 3472, 2720);
    }
    else if (zoom==3) {
        newmap(0, zoom, x, y, 1736, 1360, 6944, 5440);
    }
    map("");
}

function right() {
    var zoom = parseInt(document.f.zoom.value);
    var x = parseInt(document.f.x.value);
    var y = parseInt(document.f.y.value);
    var w = parseInt(document.f.w.value);
    var h = parseInt(document.f.h.value);
    y = Math.round(y+(h*0.5));
    if (zoom==-1 || zoom==0) {
        x = Math.min(868, Math.round(x+(w*1.25)));
        newmap(0, zoom, x, y, 868, 680, 868, 680);
    }
    else if (zoom==1) {
        x = Math.min(1736, Math.round(x+(w*1.25)));
        newmap(0, zoom, x, y, 1736, 1360, 1736, 1360);
    }
    else if (zoom==2) {
        x = Math.min(3472, Math.round(x+(w*1.25)));
        newmap(0, zoom, x, y, 1736, 1360, 3472, 2720);
    }
    else if (zoom==3) {
        x = Math.min(6944, Math.round(x+(w*1.25)));
        newmap(0, zoom, x, y, 1736, 1360, 6944, 5440);
    }
    map("");
}

function shortcut(nr) {
    // newmap(shortcut, zoom, center_x, center_y, max_w, max_h, max_x, max_y);
    if (nr==1) { // SkŒne
        newmap(1, -1, 385, 475, 868, 680, 868, 680);
    }
    else if (nr==2) { // Dressinbanan med omnejd
        newmap(2, 0, 409, 288, 868, 680, 868, 680);
    }
    else if (nr==3) { // Dressinbanan
        newmap(3, 1, 904, 562, 1736, 1360, 1736, 1360);
    }
    else if (nr==4) { // Starten i Bj&ouml;rnstorp
        newmap(4, 3, 2770, 2084, 1736, 1360, 6944, 5440);
    }
    else if (nr==5) { // Rastplatsen
        newmap(5, 3, 4015, 2049, 1736, 1360, 6944, 5440);
    }
    else if (nr==6) { // G&aring;ngv&auml;g fr&aring;n bussh&aring;llplats
        newmap(6, 2, 1391, 866, 1736, 1360, 6944, 5440);
    }
    map("");
}

var p0,p1,p2,m0,m1,m2,c0,c1,c2;
var preloaded_buttons = 0;

function preload_buttons() {
    // borde vara synchronized, men va fan...
    if (preloaded_buttons==1) {
        return;
    }
    preloaded_buttons = 1;
    p0 = new Image();  p0.src = "icon/p0.gif";
    p1 = new Image();  p1.src = "icon/p1.gif";
    p2 = new Image();  p2.src = "icon/p2.gif";
    m0 = new Image();  m0.src = "icon/m0.gif";
    m1 = new Image();  m1.src = "icon/m1.gif";
    m2 = new Image();  m2.src = "icon/m2.gif";
    c0 = new Image();  c0.src = "icon/c0.gif";
    c1 = new Image();  c1.src = "icon/c1.gif";
    c2 = new Image();  c2.src = "icon/c2.gif";
}

var load_cnt = 0;
function loaded() {
    load_cnt++;
    if (load_cnt==2) {
        document.loading.src = "img/pixel.gif";
        setTimeout("preload_buttons()",200);
    }
}

var l0 = new Image();  l0.src = "icon/loading0.gif";
var l1 = new Image();  l1.src = "icon/loading1.gif";
var l2 = new Image();  l2.src = "icon/loading2.gif";
var l3 = new Image();  l3.src = "icon/loading3.gif";
var l4 = new Image();  l4.src = "icon/loading4.gif";
var l5 = new Image();  l5.src = "icon/loading5.gif";
var l6 = new Image();  l6.src = "icon/loading6.gif";
var loading_images = new Array(l0,l1,l2,l3,l4,l5,l6);

var loading_cnt = 0;
function loading() {
    //alert("loading");
    if (load_cnt<2) {
        document.loading.src = loading_images[loading_cnt].src;
        loading_cnt++;
        if (loading_cnt>6) {
            loading_cnt = 0;
        }
        setTimeout("loading()",400);
    }
    else {
        document.loading.src = "img/pixel.gif";
    }
}

function zoom(src, info) {
    preload_buttons();
    if (src==0) { // plus
        if (document.f.plus.value == 2) {
            return;
        }
        document.plus.src = p1.src;  document.f.plus.value = 1;
        if (document.f.minus.value == 1) {
            document.minus.src = m0.src;  document.f.minus.value = 0;
        }
        if (document.f.center.value == 1) {
            document.center.src = c0.src;  document.f.center.value = 0;
        }
    }
    else if (src==1) { // minus
        if (document.f.minus.value == 2) {
            return;
        }
        document.minus.src = m1.src;  document.f.minus.value = 1;
        if (document.f.plus.value == 1) {
            document.plus.src = p0.src;  document.f.plus.value = 0;
        }
        if (document.f.center.value == 1) {
            document.center.src = c0.src;  document.f.center.value = 0;
        }
    }
    else if (src==2) { // center
        if (document.f.center.value == 2) {
            return;
        }
        document.center.src = c1.src;  document.f.center.value = 1;
        if (document.f.plus.value == 1) {
            document.plus.src = p0.src;  document.f.plus.value = 0;
        }
        if (document.f.minus.value == 1) {
            document.minus.src = m0.src;  document.f.minus.value = 0;
        }
    }
    else {
        alert("Error 5");
    }
}



