var ie=document.all;
var nn6=document.getElementById&&!document.all;

var isdrag=false;
var x, y;
var dobj;
var dropFunc;

function movemouse(e) {
  if (isdrag)
  {
    dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
    dobj.style.top  = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
    return false;
  }
}



function selectmouse(e) {
  var fobj       = nn6 ? e.target : event.srcElement;
  var topelement = nn6 ? "HTML" : "BODY";

  while (fobj.tagName != topelement && fobj.className != "dragimage")
  {
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }

  if (fobj.className=="dragimage")
  {
    isdrag = true;
    dobj = fobj;
    tx = parseInt(dobj.style.left+0);
    ty = parseInt(dobj.style.top+0);
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;

    document.onmousemove=movemouse;
    return false;
  }
}

function dropId2GeoId(id){
    return id + "_geo";// to fix cursor "pointer", Ingemar Nilsson
}

function drop(e) {
    if (isdrag) {
	var x = nn6 ? e.clientX : event.clientX;
	var y = nn6 ? e.clientY : event.clientY;
	var point = map.getPosition(x, y);
	var geoId; 
	geoId = dropId2GeoId(dobj.id);
	
	var go = map.getGeoObject(geoId, "startStopLayer");
	if (typeof(go) == "undefined" || go == null)  //* added 'undefined' check /krsa
	{	    
	    go = map.createGeoObject(point.x, point.y, geoId, "startStopLayer", "point", null);    
	    //go.setAttribute("info", "Startpunt. Dra <br />och släpp i kartan"
        go.setAttribute("type", dobj.id);	    
	    go.activate();

	    if (dropFunc) {
		dropFunc(go);
	    }
	} else {
	    go.setPosition(point.x, point.y, null);
	    startStopLayer.refresh();
	}
	// Once the objects are created, we reset the icon position and hide it.
	dobj.style.visibility = "hidden";
	dobj.style.left = "";
	dobj.style.top = "";
    }
    isdrag=false;
}



// Resets the drag operation by removing the geoobjects 
// and enabling the drag icons again.
function resetDragAll() {
    resetDragStart();
    resetDragEnd();
}
// Resets the start drag operation
function resetDragStart() {
    resetDrag('travelStart');
}
// Resets the end drag operation
function resetDragEnd() {
    resetDrag('travelEnd');
}

// Resets the drag operation by removing a geoobject 
// and enabling the drag icon again.
// ...redesigned by Ingemar Nilsson to just reset one object
function resetDrag(elementId) {

    // Get the icon images and enable them
    var element = document.getElementById(elementId);        
    element.style.visibility = "visible";    
    
    var geoId = dropId2GeoId(elementId); //to fix cursor pointer problem, Ingemar Nilsson
    map.removeGeoObject(geoId, "startStopLayer");

    startStopLayer.refresh();

    // In Firefox, I need to get the icon images and enable them
    // again, after I've done startStopLayer.refresh.  I have no
    // idea why.  A browser bug, perhaps?
    element  = document.getElementById(elementId);
    element.style.visibility = "visible";    

}

document.onmousedown=selectmouse;
document.onmouseup=drop;
