﻿var g_isDisplaiedRightPanel = true;
var g_map;

window.onresize = function() { HandleResize(); };
window.onload = function() {

    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    SetTimeZone();
};


function HandleResize() {
    var height = windowHeight() - (document.getElementById('toolbar').offsetHeight + document.getElementById('head').offsetHeight + document.getElementById('menu').offsetHeight) - 3;

    var width = windowWidth() - 423;
    if (g_isDisplaiedRightPanel != null) {
        if (!g_isDisplaiedRightPanel)
            width = windowWidth() - 20;
    }

    if (height >= 400 && width >= 500) {
        document.getElementById('arrow').style.paddingTop = (height / 2) + 'px';
        try {
            document.getElementById('ctl00_contentMain_mapControl_mapControlContainer').style.height = height + 'px';
            document.getElementById('ctl00_contentMain_mapControl_mapControl').style.height = height + 'px';

            document.getElementById('ctl00_contentMain_mapControl_mapControlContainer').style.width = width + 'px';
            document.getElementById('ctl00_contentMain_mapControl_mapControl').style.width = width + 'px';
        }
        catch (e) {
            alert(e);
        }
    }
    if (g_map != null) {
        g_map.updateSize();
    }
}

function ResizeMe(idDivElement, minusHeight) {
    var height = windowHeight() - (document.getElementById('toolbar').offsetHeight + document.getElementById('head').offsetHeight + document.getElementById('menu').offsetHeight) - 3;
    try {
        document.getElementById(idDivElement).style.height = (height - minusHeight) + 'px';
    }
    catch (e) {
    }
    return true;
}


var OnMapCreating = function(map) {
    HandleResize();
}


var OnMapCreated = function(map) {
    g_map = map;

    showScale();
    g_map.events.register("zoomend", null, showScale);
    buttonStationLayerShowHide();
    g_map.events.register("zoomend", null, buttonStationLayerShowHide);
}

function EndRequestHandler(sender, args) {
    SetLoadingVisible(false);
}

function BeginRequestHandler(sender, args) {
    SetLoadingVisible(true);
}

function windowHeight() {
    // Standard browsers (Mozilla, Safari, etc.)
    if (self.innerHeight) {
        return self.innerHeight;
    }
    // IE 6
    if (document.documentElement && document.documentElement.clientHeight) {
        return document.documentElement.clientHeight;
    }
    // IE 5
    if (document.body) {
        return document.body.clientHeight;
    }
    // Just in case. 
    return 0;
}

function windowWidth() {
    // Standard browsers (Mozilla, Safari, etc.)
    if (self.innerWidth) {
        return self.innerWidth;
    }
    // IE 6
    if (document.documentElement && document.documentElement.clientWidth) {
        return document.documentElement.clientWidth;
    }
    // IE 5
    if (document.body) {
        return document.body.clientWidth;
    }
    // Just in case. 
    return 0;
}

function SetTimeZone() {
    var visitortime = new Date();
    PageMethods.SetTimeZoneOffSet(visitortime.getTimezoneOffset() / 60);
}

function DisableEnterKey(e) {
    var key;
    if (window.event)
        key = window.event.keyCode; //IE
    else
        key = e.which; //firefox      

    return (key != 13);
}

function SetLoadingVisible(value) {
    if (value == true) {
        if (document.getElementById('divLoading').style.display == true)
            return;
    }
    else
        document.getElementById('divLoading').style.display = value ? '' : 'none';
}

function showScale() {
    if (document.all) {
        document.getElementById('scaleInfo').innerText = "1 : " + formatNumber(g_map.getScale(), 0);
    } else {
        document.getElementById('scaleInfo').textContent = "1 : " + formatNumber(g_map.getScale(), 0);
    }
}

var stationLayerZoomVisible = 73000;

function buttonStationLayerShowHide() {
    var buttonStationOverlay = document.getElementById('ctl00_contentMain_mapControl_buttonStationOverlay');
    if (buttonStationOverlay != null && g_map != null) {
        if (g_map.getScale() < stationLayerZoomVisible) {
            buttonStationOverlay.style.display = "inline";
        }
        else {
            buttonStationOverlay.style.display = "none";
        }
    }
}

function formatNumber(value, dec) {
    //decimal  - the number of decimals after the digit from 0 to 3
    //-- Returns the passed number as a string in the xxx,xxx.xx format.
    anynum = eval(value);
    divider = 10;
    switch (dec) {
        case 0:
            divider = 1;
            break;
        case 1:
            divider = 10;
            break;
        case 2:
            divider = 100;
            break;
        default:  	 //for 3 decimal places
            divider = 1000;
    }

    workNum = Math.abs((Math.round(anynum * divider) / divider));

    workStr = "" + workNum

    if (workStr.indexOf(".") == -1) { workStr += "." }

    dStr = workStr.substr(0, workStr.indexOf(".")); dNum = dStr - 0
    pStr = workStr.substr(workStr.indexOf("."))

    while (pStr.length - 1 < dec) { pStr += "0" }

    if (pStr == '.') pStr = '';

    //--- Adds a comma in the thousands place.    
    if (dNum >= 1000) {
        dLen = dStr.length
        dStr = parseInt("" + (dNum / 1000)) + " " + dStr.substring(dLen - 3, dLen)
    }

    //-- Adds a comma in the millions place.
    if (dNum >= 1000000) {
        dLen = dStr.length
        dStr = parseInt("" + (dNum / 1000000)) + " " + dStr.substring(dLen - 7, dLen)
    }
    retval = dStr + pStr
    //-- Put numbers in parentheses if negative.
    if (anynum < 0) { retval = "(" + retval + ")"; }


    //You could include a dollar sign in the return value.
    //retval =  "$"+retval

    return retval.toString();
}

function CenterMapHere() {
    var divContextMenu = $get('divContextMenu');
    divContextMenu.style.display = 'none';
    var pixel = new OpenLayers.Pixel(parseInt(divContextMenu.style.left) - 5, parseInt(divContextMenu.style.top) - 120);
    var lonlat = g_map.getLonLatFromViewPortPx(pixel);
    var x = lonlat.lon;
    var y = lonlat.lat;
    getMap().SetCenter(x, y);
}

function ZoomIn() {
    g_map.zoomIn();
}

function ZoomOut() {
    g_map.zoomOut();
}

function getMap() {
    return ctl00_contentMain_mapControl_mapControl;
}

function SetTimeZone() {
    var visitortime = new Date();
    PageMethods.SetTimeZoneOffSet(visitortime.getTimezoneOffset() / 60);
}



