//
// EasyMap JavaScript Library
// Copyright (c) Yuriy Honek, 2003
//

// SelectBox
  var SelBox
  var ObjHint
  var Selecting = false
  var Resising = false


  var wndX1=wndY1=wndX2=wndY2=mapX1=mapY1=mapX2=mapY2=0
//  document.write('<fieldset id="selbox" style="margin:0;padding:0;position:absolute;top:0;left:0;width:2;height:2;'+
//            'border-color:red;border-width:1px;border-style:solid;visibility:hidden"></fieldset>')

//  document.write('<div id="mapselrect" style="position:absolute;z-index:98;left:0;top:0;'+
//                 'border-color:red;border-width:1;border-style:solid;visibility:hidden;width:1;height:1"></div>')
// End SelectBox

  var mapLeft=mapTop=mapRight=mapBottom=0
  var mapImage
  var mapWidth = 500
  var mapHeight = 400

var AutorunString = ''

var NavigatorWidth = 150
var NavigatorHeight = 130
var NavigatorLayer = ''

var ft = false
var newMap
var mapUserID = ''
var mapProfile = ''
var mapServerURL = '/scripts/easymap211.dll'
var mapAction = ''
var mapActionParams = ''
var mapHideLayers = ''
var mapSelectQuery = ''
var mapClickX = 0
var mapClickY = 0
var mapTool = 'move'   //  move|zoomin|zoomout|region|identify|selreg|addobj|delobj|edtobj
var isIE = false
var isNav = false

var isOpera = false
var uAgent = navigator.userAgent
if (document.all) isIE = true
if (uAgent.indexOf("Opera") != (-1)) isOpera = true
if (uAgent.indexOf("Firefox") != (-1)) isNav = true

var PreloadActionName = ''

var ObjectCount = 0
var ObjectX = new Array()
var ObjectY = new Array()
var ObjectName = new Array()
var ObjectImage = new Array()




function GetLayer(name){
  return document.getElementById(name).style;

//    if (document.all[name])
//      return document.all[name].style
//    else
//      return null
}

function GetObject(name){
  return document.getElementById(name);

//  if (isIE)
//    return document.all[name]
//  else if (isNav)
//    return document.layers[name]
//  else
//    return null
}

function mapInit(){
//  SelBox = document.all.selbox.style
  SelBox = GetLayer('selbox')
  ObjHint = GetLayer('objhint')

  mapImage = document.images['mapImage']
  mapImage.ondragstart = new Function("return false")
  mapImage.onload = mapOnLoad
  mapImage.onmousemove = mapOnMouseMove
  document.onmousemove = doc_OnMouseMove
  document.onmouseup = doc_OnMouseUp
  if (document.images['size']) document.images['size'].onmousedown = ResizeBox_OnMouseDown
  mapSetTool('move')
  mapFullExtent()

}

function mapOnMouseMove(e){
  var tpx, tpy
  var s
  if (isNav)
  {
    tpx = e.pageX - mapLeft
    tpy = e.pageY - mapTop
  }
  else
  {
    tpx = event.offsetX
    tpy = event.offsetY
  }

  s = ''
  for (i=0;i<ObjectCount;i++)
    if ( (tpx>=(ObjectX[i]-7))&(tpx<=(ObjectX[i]+7))&(tpy>=(ObjectY[i]-7))&(tpy<=(ObjectY[i]+7)) ) {
      s = s + '<img src="/easymap/images/'+ObjectImage[i]+'" align="absmiddle"> ' + ObjectName[i] + '<br>'
    }
  if (s=='')
    ObjHint.visibility = 'hidden'
  else{
    GetObject('objhint').innerHTML = s
//    ObjHint.left = event.clientX + 6
//    ObjHint.top = event.clientY + 6
    ObjHint.left = mapLeft + tpx + 6
    ObjHint.top = mapTop +tpy + 6




//    ObjHint.left = this.style.pixelLeft + 6 + tpx
//    ObjHint.top = this.style.pixelTop + 6 + tpy
//    dx = this.style.left + 6 + tpx
//    dy = this.style.top + 6 + tpy
//  status = 'x: ' + dx + '  y: ' + dy

    ObjHint.visibility = 'visible'
  }
}


function mapOnLoad(){
  getMapPos()
  if (!isOpera) HidePreloader()
  mapGetNavigatorImage()
  mapGetDataScript()

  if (AutorunString != '')
  {
//   status = AutorunString
   eval(AutorunString);
   AutorunString = ''
  } 
}


function mapOnClick(e){

  if (isNav)
  {
    mapClickX = e.pageX - mapLeft
    mapClickY = e.pageY - mapTop
  }
  else
  {
    mapClickX = event.offsetX
    mapClickY = event.offsetY
  }

  if (mapTool == 'move')
    mapMove(mapClickX, mapClickY)
  else if (mapTool == 'zoomin')
    mapZoomInMove(mapClickX, mapClickY)
  else if (mapTool == 'zoomout')
    mapZoomOutMove(mapClickX, mapClickY)
  else if (mapTool == 'measure')
    mapMeasure(mapClickX, mapClickY)
  else if (mapTool == 'identify')
    mapIdentify(mapClickX, mapClickY)
  else if (mapTool == 'addobj')
    mapAddObjectWindow(mapClickX, mapClickY, document.all['editlayer'].value)
    // mapAddObject(mapClickX, mapClickY, document.all['editlayer'].value, '')    
  else if (mapTool == 'delobj')
    mapIdentify(mapClickX, mapClickY)
  else if (mapTool == 'edtobj')
    mapIdentify(mapClickX, mapClickY)
}


function mapGetPointInfo(){
  imgMap = document.images['mapImage']
  if (imgMap != null){
    srcString = mapServerURL + '/mapinfo?'+
      'uid=' + mapUserID +
      '&mw=' + mapWidth +
      '&mh=' + mapHeight +
      '&x=' + mapClickX +
      '&y=' + mapClickY +
      '&act=' + 'info' +              // mapAction +
      '&param=' + mapActionParams +
      '&hidelayers=' + mapHideLayers +
      '&dt='+Date()
    // imgMap.src = srcString
    wnd = open(srcString, 'InfoWindow', 'height=400,width=330,status=no,toolbar=no,menubar=no,location=no, top=200, left=200, resizable=yes, scrollbars=yes')
    wnd.focus()
  }else{
    alert('Error: Can\'t find mapImage element.')
  }
  mapAction = ''
}



function mapGetMapImage(){
  if (GetObject('preload_action'))
    GetObject('preload_action').innerHTML = PreloadActionName
  if (!isOpera) ShowPreloader()
  imgMap = document.images['mapImage']
  if (imgMap != null){
    srcString = mapServerURL + '/map?'+
      'uid=' + mapUserID +
      '&mw=' + mapWidth +
      '&mh=' + mapHeight +
      '&x=' + mapClickX +
      '&y=' + mapClickY +
      '&act=' + mapAction +
      '&param=' + mapActionParams +
      '&hidelayers=' + mapHideLayers +
      '&' + mapSelectQuery +
      '&dt='+Date()

//     imgMap.src = prompt('Map src', srcString)
    imgMap.src = srcString
  }else{
    alert('Error: Can\'t find mapImage element.')
  }
  mapAction = ''
  PreloadActionName = ''
}

function mapGetMapImageNav(){
  if (GetObject('preload_action'))
    GetObject('preload_action').innerHTML = PreloadActionName
  ShowPreloader()
  imgMap = document.images['mapImage']
  if (imgMap != null){
    srcString = mapServerURL + '/map?'+
      'uid=' + mapUserID +
      '&mw=' + mapWidth +
      '&mh=' + mapHeight +
      '&x=' + mapClickX +
      '&y=' + mapClickY +
      '&act=' + mapAction +
      '&param=' + mapActionParams +
      '&hidelayers=' + mapHideLayers +

      '&width=' + NavigatorWidth +
      '&height=' + NavigatorHeight +
      '&layer=' + NavigatorLayer +

      '&dt='+Date()

//     imgMap.src = prompt('Map src', srcString)
    imgMap.src = srcString
  }else{
    alert('Error: Can\'t find mapImage element.')
  }
  mapAction = ''
  PreloadActionName = ''
}





function mapSetNavigatorData(layer, w, h){
  NavigatorWidth = w
  NavigatorHeight = h
  NavigatorLayer = layer
}



function mapGetNavigatorImage(){
  imgNavigator = document.images['NavigatorImage']
  if (imgNavigator != null){
    srcString = mapServerURL + '/navigator?'+
      'uid=' + mapUserID +
      '&width=' + NavigatorWidth +
      '&height=' + NavigatorHeight +
      '&layer=' + NavigatorLayer +
      '&dt='+Date()

    imgNavigator.src = srcString
  }
}


function mapGetDataScript(){
  var DataScript = document.getElementById('DataScript');
  if (DataScript) {
    srcString = mapServerURL + '/data?'+
      'uid=' + mapUserID +
      '&mw=' + mapWidth +
      '&mh=' + mapHeight +
      '&hidelayers=' + mapHideLayers +
      '&dt='+Date()
//          srcString = '/Data.js'
    DataScript.setAttribute('src', srcString);

  }
}



//////////////
//////////////    Map Control
//////////////

function mapSetTool(ATool){
  mapTool = ATool
  if (mapTool=='selreg'){
    //    mapImage.onclick = null
    mapImage.onmousedown = map_OnMouseDown
    mapImage.style.cursor = 'crosshair'
  }
  else{
    mapImage.onclick = mapOnClick
    mapImage.onmousedown = null
    mapImage.onmouseup = null
//    mapImage.onmousemove = null
    if (mapTool=='identify')
      mapImage.style.cursor = 'help'
    else
      mapImage.style.cursor = 'hand'
  }
}

function mapSetHideLayers(ALayers){
  mapHideLayers = ALayers
}

function mapMoveNavigator(x, y){
  PreloadActionName = 'Локалізація'
  mapClickX = x
  mapClickY = y
  mapAction = 'movenavigator'+
    '&width=' + NavigatorWidth +
    '&height=' + NavigatorHeight +
    '&layer=' + NavigatorLayer
  mapGetMapImage()
}

function mapFullExtent(){
  PreloadActionName = 'Показати всю карту'
  mapAction = 'fullextent'
  mapGetMapImage()
}

function mapZoomIn(){
  PreloadActionName = 'Збільшити масштаб'
  mapAction = 'zoomin'
  mapGetMapImage()
}

function mapZoomOut(){
  PreloadActionName = 'Зменшити масштаб'
  mapAction = 'zoomout'
  mapGetMapImage()
}

function mapClear(){
  PreloadActionName = 'Очистити карту'
  mapAction = 'clear'
  mapSelectQuery = ''  
  mapGetMapImage()
}

function mapMove(x, y){
  PreloadActionName = 'Центрування'
  mapClickX = x
  mapClickY = y
  mapAction = 'move'
  mapGetMapImage()
  mapClickX = 0
  mapClickY = 0
}

function mapZoomInMove(x, y){
  PreloadActionName = 'Збільшення масштабу'
  mapClickX = x
  mapClickY = y
  mapAction = 'zoomin_move'
  mapGetMapImage()
  mapClickX = 0
  mapClickY = 0
}

function mapZoomOutMove(x, y){
  PreloadActionName = 'Зменшення масштабу'
  mapClickX = x
  mapClickY = y
  mapAction = 'zoomout_move'
  mapGetMapImage()
  mapClickX = 0
  mapClickY = 0
}

function mapMeasure(x, y){
  PreloadActionName = 'Довжина маршруту'
  mapClickX = x
  mapClickY = y
  mapAction = 'measure'
  mapGetMapImage()
  mapClickX = 0
  mapClickY = 0
}

function mapIdentify(x, y){
//  PreloadActionName = 'Довідка'
//  mapClickX = x
//  mapClickY = y
  mapAction = 'identify'
//  mapGetMapImage()
  mapGetPointInfo()
  mapClickX = 0
  mapClickY = 0
}

function mapAddObjectWindow(x, y, aLayer){
  wnd = open('/AddObj'+aLayer+'.html?'+x+','+y+",'"+aLayer+"'", 'AddObjectWindow', 'height=400,width=330,status=no,toolbar=no,menubar=no,location=no, top=200, left=200, resizable=yes, scrollbars=yes')
  wnd.focus()
  mapAction = ''
}

function mapAddObject(x, y, aLayer, aParams){
  PreloadActionName = "Додати об'єкт"
  mapAction = 'addobj'
  mapActionParams = 'Layer%3D'+aLayer+';'+aParams
  mapGetMapImage()
}



function mapSelectRegion(X1, Y1, X2, Y2){
  PreloadActionName = 'Вибір регіону'
  mapAction = 'selreg'
  mapActionParams = X1+';'+Y1+';'+X2+';'+Y2
  mapGetMapImage()
}

function mapGoNorth(){
  PreloadActionName = 'Перемістити на північ'
  mapAction = 'up'
  mapGetMapImage()
}

function mapGoSouth(){
  PreloadActionName = 'Перемістити на південь'
  mapAction = 'down'
  mapGetMapImage()
}

function mapGoWest(){
  PreloadActionName = 'Перемістити на захід'
  mapAction = 'left'
  mapGetMapImage()
}

function mapGoEast(){
  PreloadActionName = 'Перемістити на схід'
  mapAction = 'right'
  mapGetMapImage()
}

function mapSetMapSize(AWidth, AHeight){
  PreloadActionName = 'Встановити розмір карти: ' + AWidth + 'x' + AHeight
  mapWidth = AWidth
  mapHeight = AHeight
  imgMap.width = AWidth
  imgMap.height = AHeight
  mapAction = 'setmapsize'
  mapGetMapImage()
}

function mapSearchShape(ALayer, AExpression){
  PreloadActionName = "Пошук об'єкту"
  mapAction = 'searchshape'
  mapActionParams = ALayer+';'+AExpression
  mapGetMapImage()
  mapAction = ''
  focus()
}

function mapZoomToShape(ALayer, AExpression){
  PreloadActionName = "Пошук об'єкту"
  mapAction = 'zoomshape'
  mapActionParams = ALayer+';'+AExpression
  mapGetMapImage()
  mapAction = ''
  focus()
}

function mapSearchSQLShape(ALayer, AExpression, Radius){
  PreloadActionName = "Пошук об'єкту"
  mapAction = 'searchsqlshape'
  mapActionParams = ALayer+';'+AExpression+';'+Radius
  mapGetMapImage()
  mapAction = ''
}

function mapSearchAbonShape(AExpression, Radius){
  PreloadActionName = "Пошук об'єкту"
  mapAction = 'searchabonshape'
  mapActionParams = AExpression+';'+Radius
  mapGetMapImage()
  mapAction = ''
}


function mapGetTable(ATable){
  s = mapServerURL + '/table?'+ 'name=' + ATable
  pleft = screen.availWidth - 340
  ptop = screen.availHeight - 450
  wnd = open(s, ATable, 'left='+pleft+', top='+ptop+', height=400,width=330,status=no,toolbar=no,menubar=no,location=no, resizable=yes, scrollbars=yes')
  wnd.focus()
}

function mapGetSQLTable(ATable){
  s = mapServerURL + '/sqltable?'+ 'name=' + ATable
  pleft = screen.availWidth - 340
  ptop = screen.availHeight - 450
  wnd = open(s, ATable, 'left='+pleft+', top='+ptop+', height=400,width=330,status=no,toolbar=no,menubar=no,location=no, resizable=yes, scrollbars=yes')
  wnd.focus()
}

function mapGetAbonTable(ATable){
  s = mapServerURL + '/abontable?'+ 'name=' + ATable
  pleft = screen.availWidth - 340
  ptop = screen.availHeight - 450
  wnd = open(s, ATable, 'left='+pleft+', top='+ptop+', height=400,width=330,status=no,toolbar=no,menubar=no,location=no, resizable=yes, scrollbars=yes')
  wnd.focus()
}

function mapSearchAbon(AQuery){
  s = mapServerURL + '/searchabon?'+ AQuery
  pleft = screen.availWidth - 340
  ptop = screen.availHeight - 450
  wnd = open(s, 'searchabon', 'left='+pleft+', top='+ptop+', height=400,width=330,status=no,toolbar=no,menubar=no,location=no, resizable=yes, scrollbars=yes')
  wnd.focus()
}

function mapLocatePath(AQuery){
  s = mapServerURL + '/locatepath?'+ AQuery
  pleft = screen.availWidth - 340
  ptop = screen.availHeight - 450
  wnd = open(s, 'PathSearch', 'left='+pleft+', top='+ptop+', height=400,width=330,status=no,toolbar=no,menubar=no,location=no, resizable=yes, scrollbars=yes')
  wnd.focus()
}

function mapFindPaths(AQuery){
  s = mapServerURL + '/findpaths?'+ AQuery
  pleft = screen.availWidth - 340
  ptop = screen.availHeight - 450
  wnd = open(s, 'PathSearch', 'left='+pleft+', top='+ptop+', height=400,width=330,status=no,toolbar=no,menubar=no,location=no, resizable=yes, scrollbars=yes')
  wnd.focus()
}

function mapShowPath(AParam){
  alert(AParam);
}


function mapPrintMap(){
  wnd = window.open('', 'wndPrintMap')
  with (wnd.document) {
    open()
    srst = mapServerURL + '/map?'+
      'uid=' + mapUserID +
      '&mw=' + mapWidth +
      '&mh=' + mapHeight +
      '&hidelayers=' + mapHideLayers +
      '&dt='+Date()
    write('<img src="'+srst+'" onload="print()">')
    close()
  }
}


//////////// Select Region Functions /////////////

function getMapPos(){
  mapLeft = mapTop = 0
  mapWidth = mapImage.width
  mapHeight = mapImage.height
  var obj = mapImage
  while (obj != document.body){
    mapLeft = mapLeft + obj.offsetLeft
    mapTop = mapTop + obj.offsetTop
    obj = obj.offsetParent
  }
  mapRight = mapLeft + mapWidth
  mapBottom = mapTop + mapHeight
}


function map_OnMouseDown(e){
  if (isNav)
  {
    mapClickX = e.pageX
    mapClickY = e.pageY

    e.preventDefault();
  }
  else
  {
    mapClickX = document.body.scrollLeft + event.clientX - 2
    mapClickY = document.body.scrollTop + event.clientY - 2
  }

  if(mapTool=='selreg'){
    getMapPos()
//    wndX1 = document.body.scrollLeft + event.clientX - 2
//    wndY1 = document.body.scrollTop + event.clientY - 2

    wndX1 = mapClickX
    wndY1 = mapClickY
    Selecting = true
    SelBox.visibility = 'visible'
    SelBox.left = wndX1
    SelBox.top = wndY1
    SelBox.width = 1
    SelBox.height = 1
    mapX1 = wndX1 - mapLeft
    mapY1 = wndY1 - mapTop
  }
}

function doc_OnMouseMove(e){
  if (Selecting){
    if (isNav)
    {
      mapClickX = e.pageX
      mapClickY = e.pageY
    }
    else
    {
      mapClickX = document.body.scrollLeft + event.clientX - 2
      mapClickY = document.body.scrollTop + event.clientY - 2
    }

    wndX2 = mapClickX
    wndY2 = mapClickY
    if(wndX2 > mapRight) wndX2 = mapRight
    if(wndY2 > mapBottom) wndY2 = mapBottom
    if(wndX2 < mapLeft) wndX2 = mapLeft
    if(wndY2 < mapTop) wndY2 = mapTop
    SelBox.width = Math.abs(wndX1 - wndX2)
    SelBox.height = Math.abs(wndY1 - wndY2)
    SelBox.left = Math.min(wndX1, wndX2)
    SelBox.top = Math.min(wndY1, wndY2)
  }

  if (Resising){
    if (isNav)
    {
      mapClickX = e.pageX
      mapClickY = e.pageY
    }
    else
    {
      mapClickX = event.clientX - 2
      mapClickY = event.clientY - 2

    }
    mapImage.width=srW-srX+mapClickX
    mapImage.height=srH-srY+mapClickY
  }
}

function doc_OnMouseUp(e){
  if (Selecting){
    if (isNav)
    {
      mapClickX = e.pageX
      mapClickY = e.pageY
    }
    else
    {
      mapClickX = document.body.scrollLeft + event.clientX - 2
      mapClickY = document.body.scrollTop + event.clientY - 2
    }

    Selecting = false
    SelBox.visibility = 'hidden'
    wndX2 = mapClickX
    wndY2 = mapClickY
    if(wndX2 > mapRight) wndX2 = mapRight
    if(wndY2 > mapBottom) wndY2 = mapBottom
    if(wndX2 < mapLeft) wndX2 = mapLeft
    if(wndY2 < mapTop) wndY2 = mapTop
    mapX2 = wndX2 - mapLeft
    mapY2 = wndY2 - mapTop
    mapSelectRegion(mapX1, mapY1, mapX2, mapY2)
  }
  if (Resising){
    Resising=false 
    mapSetMapSize(mapImage.width, mapImage.height); 
  }

}

function ResizeBox_OnMouseDown(e){
  if (isNav)
  {
    srX = e.pageX
    srY = e.pageY

    e.preventDefault();
  }
  else
  {
    srX=event.clientX;
    srY=event.clientY;
  }
  Resising=true; 
  srW=mapImage.width;
  srH=mapImage.height;
}


//////////////////////////////////////   Preloader

function ShowPreloader(){

  imgMap = document.images['mapImage']
  if (!ft) {ft = true; return}
  pl = GetLayer('preloader')
  if (pl) {
//    pl.pixelTop = mapTop
//    pl.pixelLeft = mapLeft
//    pl.pixelWidth = imgMap.width
//    pl.pixelHeight = imgMap.height

    pl.top = mapTop
    pl.left = mapLeft
    pl.width = imgMap.width
    pl.height = imgMap.height


    pl.visibility = 'visible'
  }
}

function HidePreloader(){
  pl = GetLayer('preloader')
  if (pl) {
    pl.visibility = 'hidden'
  }
}

function mapSetZoomScale(AZoomScale){
  PreloadActionName = "Встановити масштаб"
  mapAction = 'setscale'
  mapActionParams = AZoomScale.toString()
//  alert(mapActionParams)
  mapGetMapImage()
  mapAction = ''
}




/////////////////////////
////////////  /////////////
////////////  /////////////



//  mapPan(intX, intY)
//  mapSelectRect(intX1, intY1, intX2, intY2)

