// just rough. could maybe do this better.
var MAX_PERCENT = 130;
var MAX_PIXELS = 700;

var TRANSITION_MS = 1000;

var inTransition;

var mapHTML;

var debug = 0;
var pages = top.pages;

var loadedOnce = {};

var name_prefix = '';

var imgSrcStore = {};

var collapsed = false;
var collapsedHeight = 65;

var curSize;

var funPrompt = -1;

function imgMouseOver(s) {
    var img = document.getElementById("img_" + s);
    var state = 'strong';
    var prefix = '/' + top.W + "_images/map";
    imgSrcStore[img] = img.src;
    img.src=prefix + '/' + s + '.' + state + '.jpg';
}
function imgMouseOut(s) {
    var img = document.getElementById("img_" + s);
    img.src=imgSrcStore[img];
    imgSrcStore[img] = '';
}

// this also gets called when you change the .src e.g. rollover! (of course)
/*
function imgLoad(imageAction) {
}
*/

// store is usually omitted and defaults to 1. if it's 0, we are restoring
// the map to a former state, so don't change top.mapScale
function map_resize_old(scale, store) {
    map_uncollapse();

    if (store == null) store = 1;

    var table = document.getElementById("map_table2");
    //var table = document.getElementById("map_map_cell");
    curSize = table.style.width;

    var re = /(.+)(px|%)/;
    var size;
    var suf;
    var dummy = re.exec(curSize);
    size = dummy[1];
    suf = dummy[2];

    curSize = size;
        
    var newSize = curSize * scale;

    if (suf == "%") {
        if (newSize >= MAX_PERCENT) {
            return;
        }
    } 
    else if (suf == "px") {
        if (newSize >= MAX_PIXELS) {
            return;
        }
    }

    table.style.width = newSize + suf;
    table.style.textAlign = 'center';

    //frameResize('scale', scale, true);

    collapsed = false;

    if (! top.mapScale) alert ("error: top.mapScale null");

    if (store) top.mapScale *= scale;

    return top.mapScale;
}

// store is usually omitted and defaults to 1. if it's 0, we are restoring
// the map to a former state, so don't change top.mapScale
function map_resize(scale, store) {
    map_uncollapse();

    if (store == null) store = 1;

    var table = $("map_table");
    var splashPic = $$(".splash_pics")[0];
    var quickTable = $('quicklink_cell');

    // PT
    var HARDCODED_FONT = '8';
    var fontSize = top.globals.currentFontsize;
    if (! fontSize) fontSize = HARDCODED_FONT;

    var ok = 0;
    [table, splashPic].each(function(s) {
        ok = 0;
        var c = s.getCoordinates();
        var newWidth = c.width * scale;
        var newHeight = c.height * scale;
        if (newWidth >= MAX_PIXELS) return;
        ok = 1;
        s.style.width = newWidth + 'px';
        s.style.height = newHeight + 'px';
    });

    if (! ok) return;

    var newFontSize = fontSize * scale;
    quickTable.style.fontSize = newFontSize + 'pt';

    collapsed = false;

    if (! top.mapScale) alert ("error: top.mapScale null");

    if (store) top.mapScale *= scale;
    top.globals.currentFontsize = newFontSize;

    return top.mapScale;
}

function map_resize_abs (abs) {
    map_uncollapse();
    var table = document.getElementById("map_table");
    table.style.width = abs;
}
function map_collapse() {
    var div = document.getElementById('map_div');
    top.storeMapHTML = div.innerHTML;
    
    var f = function() {
        inTransition = 0;
    };
    inTransition = 1;
    setTimeout(f, TRANSITION_MS * 1.1);
    blindUp(div, { duration: TRANSITION_MS / 1000 });
    collapsed = true;
}
function map_uncollapse() {
    if (top.storeMapHTML) {
        var div = document.getElementById('map_div');
        div.style.visibility = 'visible';

        var f = function() {
            inTransition = 0;
        };
        inTransition = 1;
        setTimeout(f, TRANSITION_MS * 1.1);

        appear(div, { duration: TRANSITION_MS / 1000 });
        div.innerHTML = top.storeMapHTML;
        top.storeMapHTML = '';
    }
}

function minimise(nav_frame_height) {
    //var td = document.getElementById('zoom_image_cell');

    if (inTransition) return;

    top.globals.textNavForJsDisplay = 'block';
    updateTextNavDisplay();

    map_collapse();

    top.mapVisible = 0;

}

function restore() {

    if (inTransition) return;

    map_uncollapse();

    top.globals.textNavForJsDisplay = 'none';
    updateTextNavDisplay();

    top.mapVisible = 1;
}
    
function zoom_out() {
    var size = map_resize(0.91);
    if (size < .2) {
        if (funPrompt < 1) {
            funPrompt = 1;
            prompt("Don't you think you're having too much fun?");
        }
    }
    if (size < .1) {
        if (funPrompt < 2) {
            funPrompt = 2;
            prompt("Still not?");
        }
    }
}

function zoom_in() {
    map_resize(1.1);
}

