
var maximised = false;
var container = document.createElement("div");
var maximiseDiv = document.createElement("div");
var map_width;
var map_h_borders;
var map_v_borders;
var map_height;

function makeMaximiseButton()
{
    map_width = mapDiv.offsetWidth;
    map_height = mapDiv.offsetHeight;
    map_h_borders = mapDiv.offsetWidth - mapDiv.clientWidth;
    map_v_borders = mapDiv.offsetHeight - mapDiv.clientHeight;
    var controlpos = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 36));
    container.appendChild(maximiseDiv);
    maximiseDiv.appendChild(document.createTextNode("Maximise"));
    GEvent.addDomListener(maximiseDiv, "click", function() { fullScreen() });
    controlpos.apply(container);
    map.getContainer().appendChild(container);

    maximiseDiv.style.backgroundColor = "white";
    maximiseDiv.style.fontSize = "12px";
    maximiseDiv.style.border = "1px solid #b0b0b0";
    container.style.border = '1px solid black';
    maximiseDiv.style.borderColor = 'white #b0b0b0 #b0b0b0 white';
    maximiseDiv.style.padding = "0 2px";
    maximiseDiv.style.textAlign = "center";
    maximiseDiv.style.width = "5.5em";
    maximiseDiv.style.cursor = "pointer";
}

function fullScreen()
{
    var center = map.getCenter();
    if (!maximised)
    {
        mapDiv.style.width = (f_clientWidth()-map_h_borders)+'px';
        mapDiv.style.height = (f_clientHeight()-map_v_borders)+'px';
        mapDiv.style.position = 'fixed';
        if (navigator.appVersion.toLowerCase().indexOf('msie 6')!=-1)
        {
            // IE6 doesn't handle position: fixed...
            // still needs to move mapdiv to top left, somehow :(
            // perhaps move div to HTML root in DOM?
            document.body.style.height = '100%';
            document.body.style.overflow = 'auto';
            mapDiv.style.position = 'absolute';
        }
        mapDiv.style.zIndex = 99;
        mapDiv.style.top = 0;
        mapDiv.style.left = 0;
        maximised = true;
        map.checkResize();
        map.setCenter(center);
        maximiseDiv.removeChild(maximiseDiv.childNodes[0])
        maximiseDiv.appendChild(document.createTextNode("Restore"));
    }
    else
    {
        mapDiv.style.position = 'relative';
        mapDiv.style.width = (map_width-2)+'px';
        mapDiv.style.height = (map_height-2)+'px';
        mapDiv.style.zIndex = '0';
        maximised = false;
        map.checkResize();
        map.setCenter(center);
        maximiseDiv.removeChild(maximiseDiv.childNodes[0])
        maximiseDiv.appendChild(document.createTextNode("Maximise"));
    }
}

// The following derived from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html

function f_clientWidth() {
    var n_win = window.innerWidth ? window.innerWidth : 0;
    var n_docel = document.documentElement ? document.documentElement.clientWidth : 0;
    var n_body = document.body ? document.body.clientWidth : 0;
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
    n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_clientHeight() {

    // From http://www.quirksmode.org/viewport/compatibility.html
    
    if (self.innerHeight) // all except Explorer
    {
        return self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
    {
        return document.documentElement.clientHeight;
    }
    else if (document.body) // other Explorers (needs scroll-bar width added?)
    {
        return document.body.clientHeight;
    }

    /*
    // The following derived from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
    GLog.write('self.innerHeight = '+self.innerHeight);
    GLog.write('window.innerHeight = '+window.innerHeight);
    var n_win = window.innerHeight ? window.innerHeight : 0;
    var n_docel = document.documentElement ? document.documentElement.clientHeight : 0;
    GLog.write('document.documentElement.clientHeight = '+n_docel);
    var n_body = document.body ? document.body.clientHeight : 0;
    GLog.write('document.body.clientHeight = '+n_body);
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result < n_docel)))
    n_result = n_docel;
    return n_body && (!n_result || (n_result < n_body)) ? n_body : n_result;
    */
}
