﻿

function initDragPan() {
    document.getElementById('DragBar1').onmousedown = handleMouseDown;
    document.getElementById('DragBar2').onmousedown = handleMouseDown;
}

function setDescriptionState(state) {
    document.getElementById('ctl00_contentMain_mapControl_hfDescriptionState').value = state;
    showDescription();
}

function showDescription() {
    if (document.getElementById('ctl00_contentMain_mapControl_hfDescriptionState').value == '1') {
        document.getElementById('panelSmall').style.display = 'none';
        document.getElementById('panelBig').style.display = 'block';
        document.getElementById('hidebt').style.display = 'block';
        document.getElementById('showbt').style.display = 'none';
    } else {
        document.getElementById('panelSmall').style.display = 'block';
        document.getElementById('panelBig').style.display = 'none';
        document.getElementById('hidebt').style.display = 'none';
        document.getElementById('showbt').style.display = 'block';
    }
}

var iDiffx = 0;
var iDiffy = 0;
function handleMouseDown(e) {
    var oEvent = EventUtil.getEvent(e);
    var oDiv = document.getElementById("descriptionPanel");

    if (oEvent) {
        iDiffx = oEvent.clientX - oDiv.offsetLeft;
        iDiffy = oEvent.clientY - oDiv.offsetTop;
    }

    EventUtil.addEventHandler(document.body, "mousemove", handleMouseMove);
    EventUtil.addEventHandler(document.body, "mouseup", handleMouseUp);
    document.getElementById("descriptionPanel").style.cursor = "move";
}
function handleMouseMove(e) {
    var oEvent = EventUtil.getEvent(e);
    var oDiv = document.getElementById("descriptionPanel");
    oDiv.style.left = parseInt(oEvent.clientX - iDiffx) + 'px';
    oDiv.style.top = parseInt(oEvent.clientY - iDiffy) + 'px';
}
function handleMouseUp(e) {
    EventUtil.removeEventHandler(document.body, "mousemove", handleMouseMove);
    EventUtil.removeEventHandler(document.body, "mouseup", handleMouseUp);
    document.getElementById("descriptionPanel").style.cursor = "default";
}


var EventUtil = new Object;

EventUtil.addEventHandler = function(oTarget, sEventType, fnHandler) {
    if (oTarget.addEventListener) {//firefox   
        oTarget.addEventListener(sEventType, fnHandler, false);
    } else if (oTarget.attachEvent) {//IE   
        oTarget.attachEvent("on" + sEventType, fnHandler);
    } else {//others   
        oTarget["on" + sEventType] = fnHandler;
    }
};
EventUtil.removeEventHandler = function(oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {//firefox   
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {//IE   
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else {//others   
        oTarget["on" + sEventType] = null;
    }
};

EventUtil.formatEvent = function(oEvent) {
    oEvent.charCode = (oEvent.type == "keypress") ? oEvent.keycode : 0;
    oEvent.eventPhase = 2;
    oEvent.isChar = (oEvent.charCode > 0);
    oEvent.pageX = oEvent.clientX + document.body.scrollLeft;
    oEvent.pageY = oEvent.clientY + document.body.scrollTop;

    oEvent.preventDefault = function() {
        this.returnValue = false;
    };

    if (oEvent.type == "mouseout") {
        oEvent.relatedTarget = oEvent.toElement;
    } else if (oEvent.Type == "mouseover") {
        oEvent.relatedTarget = oEvent.fromElement;
    }

    oEvent.stopPropagation = function() {
        this.cancelBubble = true;
    };

    oEvent.target = oEvent.srcElement;
    oEvent.time = (new Date()).getTime();
    return oEvent;
};

EventUtil.getEvent = function() {
    if (window.event) {
        return this.formatEvent(window.event);
    } else {
        return EventUtil.getEvent.caller.arguments[0];
    }
};

function visibilityDynamicLayer(idLayer) {
    visibilityDynamicLayer(idLayer, null);
}

function visibilityDynamicLayer(idLayer, visibility) {
    var layer = g_map.getLayer(idLayer);
    if (layer != null) {
        if (visibility == null) {
            layer.display(!layer.visibility);
            layer.visibility = !layer.visibility;

        }
        else {
            layer.display(visibility);
            layer.visibility = visibility;
        }

        if (layer.visibility == true) {
            layer.redraw();
        }

        try {
            document.getElementById('ctl00_contentMain_mapControl_button' + idLayer).className = layer.visibility == true ? 'btn_bold' : 'btn';
        }
        catch (e) {
        }
    }
}

function setGoogleMap(type) {
    getMap().SetCurrentBackgroundMapType(type);
    document.getElementById('ctl00_contentMain_mapControl_buttonG_NORMAL_MAP').className = type == G_NORMAL_MAP ? 'btn_bold' : 'btn';
    document.getElementById('ctl00_contentMain_mapControl_buttonG_SATELLITE_MAP').className = type == G_SATELLITE_MAP ? 'btn_bold' : 'btn';
    document.getElementById('ctl00_contentMain_mapControl_buttonG_PHYSICAL_MAP').className = type == G_PHYSICAL_MAP ? 'btn_bold' : 'btn';
    document.getElementById('ctl00_contentMain_mapControl_buttonG_HYBRID_MAP').className = type == G_HYBRID_MAP ? 'btn_bold' : 'btn';
}


function getStationInfo(featureId) {
    if (featureId != null)
        PageMethods.GetStationInfo(featureId, onStationInfoLoaded, onClientCallFailed);
}

function onStationInfoLoaded(result) {
    document.getElementById('divInfoContainer').style.display = 'block';
    document.getElementById('divInfoContent').innerHTML = result;
}

function onClientCallFailed(ex) {
    alert(ex.get_message());
}


