﻿// display_mapmarker.js

var prevtool= null;

// Set up Marker
function MapMarker(divid) 
{
    map = Maps[divid];
    
	map.tempMode = map.mode;
	map.tempAction = map.actionType;
	map.tempCursor = map.cursor;
    
    MapPoint(map.controlName, "MapMarker", false);
    map.divObject.onmousedown = MapIdClick;
}

// Event handler
function MapIdClick(e) {

    if (getSessionLapse() < maximumLapseTime) 
    {

    map.cursor = map.divObject.style.cursor;
	//map.divObject.style.cursor = "wait";
	
	markerText = "";
	
	getXY(e);
	var box = calcElementPosition(map.containerDivId);
	zleft = mouseX - box.left;
	ztop = mouseY - box.top;

	map.xMin=zleft;
	map.yMin=ztop;
	
	addMarkerLocation();
	showLayer("MapMarkerLocation");
	editMarkerText();
	
	map.getTopLeftTile();
	
	var argument = "ControlID=Map1&ControlType=Map&EventArg=MapMarker&x=" + zleft + "&y=" + ztop;
	var context = map.controlName;
	eval(markerCallbackFunctionString);
	
	var div = document.getElementById("MapMarkerLocation");
	var cWidth = div.clientWidth;
	var cHeight = div.clientHeight;
	
	//alert(cWidth + " x " + cHeight); // width and height might not be available on first time.... :-(
	//if (cWidth==0) cWidth = 160;
	//if (cHeight==0) cHeight = 120;
	
	var idLeft = zleft - parseInt( map.divObject.style.left ) - 14;
	var idTop = ztop - parseInt( map.divObject.style.top ) - cHeight + 2;

	window.setTimeout('moveLayer("MapMarkerLocation", ' + idLeft + ', ' + idTop + '); showLayer("MapMarkerLocation");', 0 );
	map.mode = map.tempMode;
	map.actionType = map.tempAction;
	map.cursor = map.tempCursor;
	
	}
	else
	{
	    showLapseAlert();
	}
	return false;
	
}

function enableMap( state )
{
    var map = Maps["Map1"];
    if ( map == null ) return;

    if ( state == true )
    {
        if (isIE) { 
        // if IE, use the map's container object div 
        map.containerObject.onkeydown = keyDown;
        map.containerObject.onkeyup = keyUp;
        //map.divObject.focus();
        } else {
        //  else use the map's link tag
        map.mozillaLinkObject.onkeydown = keyDown;
        map.mozillaLinkObject.onkeyup = keyUp;        
        }
        
        map.divObject.onmousemove = MapMouseMove;                
    }
    else
    {
        if (isIE) { 
        // if IE, use the map's container object div 
        map.containerObject.onkeydown = null;
        map.containerObject.onkeyup = null;
        } else {
        //  else use the map's link tag
        map.mozillaLinkObject.onkeydown = null;
        map.mozillaLinkObject.onkeyup = null;
        }
        
        map.divObject.onmousemove = null;
    }
    map.navigationEnabled = state;
}

function addHandler(target, eventName, handler) {
 if (target.attachEvent) {
  target.attachEvent('on'+eventName, handler);
 } else {
  target.addEventListener(eventName, handler, false);
 }
}

var oH = 0;
function checkStuff(e) 
{
    //alert( event.keyCode );
    
 if (e.which?e.which==13:event.keyCode==13) 
 {
  //var d = document.getElementById('c').style.height.replace(/[^\d]/g,"");
  //if(!d)d=oH;var ex=parseInt(d)+parseInt(oH);
  //document.getElementById('c').style.height = ex+"px";
    // ENTER key handling
    saveMarkerText();
 } 
 if (e.which?e.which==27:event.keyCode==27) 
 {
    // ESC key handling
    abortMarkerLocation();
 } 
 else 
 {
    // others
    //document.getElementById('c').style.height = "0px"; // hack to handle backspace, not ideal, causes flicker on firefox
    //document.getElementById('c').style.height = document.getElementById('c').scrollHeight + "px";
 }
 // vertical scrollbar
 // if (document.getElementById('c').scrollHeight > oH) document.getElementById('c').style.overflowY = "scroll";
}

function checkStuffKeyChange(myevent) 
{
 if (myevent.which && !myevent.ctrlKey && !myevent.ctrlKey)
  checkStuff(myevent);
}

function MapMarkerInput_onfocusout()
{
    focus("MapMarkerInput");
}

function getMarkerText()
{
    if ( markerText == null || markerText == "" )
    {
        //markerText = "Здесь можно оставить комментарий...";
        return "<textarea tabindex='1' onfocusout='MapMarkerInput_onfocusout()' id='MapMarkerInput'>Здесь можно оставить комментарий...</textarea>";
    }
    return markerText;
}

// Add marker object
function addMarkerLocation() 
{
    var div = document.getElementById("MapMarkerLocation");
	if ( div == null ) 
	{
        var content = '<div id="MapMarkerLocation" class="MapMarkerLocation">';
        content += '<div class="pop-up-box">';
        //content += '<h5>Поставленная отметка</h5>';
        content += '<div id="MapMarkerText">';
        content += getMarkerText();
        content += '</div>'; // <div id="MapMarkerText">
        if ( markerText == null || markerText == "" )
        {
            content += '<div id="MapMarkerButtons"><span class="edit-link" onclick="saveMarkerText()">сохранить</span><span class="edit-link" onclick="abortMarkerLocation()">удалить</span></div>';
        }
        else
        {
            content += '<div id="MapMarkerButtons"><span class="edit-link" onclick="editMarkerText()">редактировать</span><span class="edit-link" onclick="abortMarkerLocation()">удалить</span></div>';
        }
        content += '</div>';
        content += '<div class="flag-corner crop"></div>';
        content += '</div>';
        map.overlayObject.insertAdjacentHTML("BeforeEnd", content);        
    }

    AddKeyHandler();    
}

function AddKeyHandler()
{
    var text = document.getElementById("MapMarkerInput");
    if ( text != null )
    {
        if (text.attachEvent) 
        {
          addHandler(text, 'keypress', checkStuff);
        } 
        else 
        {
          addHandler(text, 'keypress', checkStuffKeyChange);
        }
    }    
}

function selectText()
{
    var text = document.getElementById("MapMarkerInput");
    if ( text != null )
    {
        var tt = text.createTextRange();
        if ( tt != null )
            tt.select();
        text.focus();
    }        
}

function saveMarkerText()
{
    //alert( getText("MapMarkerText") );
    markerText = getText("MapMarkerText");
     
    putInnerHtml( "MapMarkerText", markerText );
    putInnerHtml( "MapMarkerButtons", '<span class="edit-link" onclick="editMarkerText()">редактировать</span><span class="edit-link" onclick="abortMarkerLocation()">удалить</span>' ); 
 
    enableMap(true);
    
    // send marker text to the server
	var argument = "ControlID=Map1&ControlType=Map&EventArg=MapMarkerText&text=" + markerText;
	var context = map.controlName;
	eval(markerCallbackFunctionString);
}

function editMarkerText()
{
    enableMap(false);
    
    //alert( getText("MapMarkerText") );
    markerText = getText("MapMarkerText");
     
    putInnerHtml( "MapMarkerText", '<textarea id="' + "MapMarkerInput" + '" onfocusout="MapMarkerInput_onfocusout()" tabindex="1">' + markerText + '</textarea>' );
    putInnerHtml( "MapMarkerButtons", '<span class="edit-link" onclick="saveMarkerText()">сохранить</span><span class="edit-link" onclick="abortMarkerLocation()">удалить</span>' ); 
    
    AddKeyHandler();
    selectText();
    
    // send marker text to the server
	//var message = "ControlID=Map1&ControlType=Map&EventArg=MapMarkerText&text=" + markerText;
	//var context = map.controlName;
	//eval(markerCallbackFunctionString);
}

function abortMarkerLocation()
{
    hideLayer("MapMarkerLocation");
    
    markerText = "";
    enableMap(true);
    
    // send notification to the server 
	var argument = "ControlID=Map1&ControlType=Map&EventArg=MapMarkerText&text=" + markerText;
	var context = map.controlName;
	eval(markerCallbackFunctionString);  
}

function moveMarkerLocation( x, y )
{
    moveMarkerLocation( x, y, "" );
}

function moveMarkerLocation( x, y, text ) 
{ 
    if ( text != null && text != "" ) 
        markerText = text;
    
    if ( markerText == null || markerText == "" )
        return false;
        
    if ( map == null )
        return;
        
	var box = calcElementPosition( map.containerDivId );
	zleft = x;// - box.left;
	ztop = y;// - box.top;
    
	//map.xMin=zleft;
	//map.yMin=ztop;
    addMarkerLocation();
	map.getTopLeftTile();
	
	var div = document.getElementById("MapMarkerLocation");
	var cWidth = div.clientWidth;
	var cHeight = div.clientHeight;
	
	//alert(cWidth + " x " + cHeight); // width and height might not be available on first time.... :-(
	//if (cWidth==0) cWidth = 160;
	//if (cHeight==0) cHeight = 120;
	
	var idLeft = zleft /*- parseInt( map.divObject.style.left )*/ - 14; // flag offset
	var idTop = ztop - parseInt( map.divObject.style.top ) - cHeight - 2; // flag offset

	window.setTimeout('addMarkerLocation(); moveLayer("MapMarkerLocation", ' + idLeft + ', ' + idTop + '); showLayer("MapMarkerLocation");', 500 );    
	
	return false;
}