﻿var piIconSize = "1";
var piBackgroundColor = "000000";
var piTextColor = "ffffff";
var piFontFamily = "3";
var piFontSize = "15";
var piFontStyle = "1";
var piIconText = "";


var selectedPlaceID = 0;
var selectedIcon = "";



function processPlaceIconXml(map, xmlData) {
  var xml = GXml.parse(xmlData);
  if (xml.documentElement) {
    var isMapEdit = false;
    var mapEdit = $get('C_mapEdit');
    if (mapEdit != null) {
      isMapEdit = parseInt(mapEdit.value) == 1;
    }

    var isLocal = false;
    var local = $get('C_local');
    if (local != null) {
      isLocal = parseInt(local.value) == 1;
    }
    var points = xml.documentElement.getElementsByTagName("point");
    var point;
    var item;
    for (var i = 0; i < points.length; i++) {
      item = points[i];
      point = new GLatLng(parseFloat(item.getAttribute("lat")), parseFloat(item.getAttribute("lng")));
      map.addOverlay(createPlaceIconMarker(map, point, item.getAttribute("id"), item.getAttribute("img"), item.getAttribute("text"), isMapEdit, isLocal));
    }
  }
}

function createPlaceIconMarker(map, point, id, icon, text, isMapEdit, isLocal) {
  var gicon = new GIcon();
  gicon.shadow = id;
  if (isLocal) {
    gicon.image = 'http://127.0.0.1:3516/Animare.IttVoltam.Web/ic.aspx?i=' + icon;
  }
  else {
    gicon.image = 'http://www.ittvoltam.hu/ic.aspx?i=' + icon;
  }

  var markerType;
  if (icon.length > 0) {
    markerType = parseInt(icon.substring(0, 1));
  }
  else {
    markerType = 1;
  }
  switch (markerType) {
    case 1:  
      gicon.iconSize = new GSize(32, 37);
      gicon.iconAnchor = new GPoint(16, 37);
      gicon.infoWindowAnchor = new GPoint(32, 0);
      break;
    case 2:
      gicon.iconSize = new GSize(32, 29);
      gicon.iconAnchor = new GPoint(16, 29);
      gicon.infoWindowAnchor = new GPoint(32, 0);
      break;
  }

  var marker = new GMarker(point, { "icon": gicon, "draggable": isMapEdit });
  /*
  if (isMapEdit) {
    marker = new GMarker(point, { "icon": gicon, "draggable": true });
  }
  else {
    marker = new GMarker(point, gicon);
  }  */     
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(text);
  });
  GEvent.addListener(marker, "dragstart", function() {
    map.closeInfoWindow();
  });
  GEvent.addListener(marker, "dragend", function() {
    var p = marker.getPoint();
    var id = parseInt(marker.getIcon().shadow);
    document.documentElement.style.cursor = "wait";
    $get('up1').style.display = 'block';
    Animare.IttVoltam.Web.WebService.Command.ChangePlaceCoordinate(id, p.lat(), p.lng(), OnSucceededChangePlaceCoordinate, OnFailedChangePlaceCoordinate);
  });
  return marker;
}



function OnSucceededChangePlaceCoordinate(ro) {
  document.documentElement.style.cursor = "default";
  $get('up1').style.display = 'none';
  if (ro.code != 0) {
    alert(ro.message);
  }
}

function OnFailedChangePlaceCoordinate(error) {
  Animare.IttVoltam.Web.WebService.Command.Error(
    error.get_exceptionType(),
    error.get_message(),
    error.get_statusCode(),
    error.get_stackTrace(),
    error.get_timedOut()
  );
  $get('up1').style.display = 'none';
  alert(errorMessage);
}   



function piW(placeID, icon) {
  selectedPlaceID = placeID;
  selectedIcon = icon;


  var placeIcon = $get('placeIcon');
  if (placeIcon != null) {
    var l = 0;
    var local = $get('C_local');
    if (local != null) {
      l = parseInt(local.value);
    }
    if (l == 0) {
      placeIcon.src = 'http://www.ittvoltam.hu/ic.aspx?i=' + selectedIcon;
    }
    else {
      placeIcon.src = 'http://127.0.0.1:3516/Animare.IttVoltam.Web/ic.aspx?i=' + selectedIcon;
    }

    if (selectedIcon.length >= 16) {
      piBackgroundColor = selectedIcon.substring(1, 7);
      piTextColor = selectedIcon.substring(7, 13);
      piFontFamily = selectedIcon.substring(13, 14);
      piFontSize = selectedIcon.substring(14, 16);
      piFontStyle = selectedIcon.substring(16, 17);
      piIconText = UrlCode.decode(selectedIcon.substring(17));

      var fs;
      if (piFontSize.substring(0, 1) == '0') {
        fs = piFontSize.substring(1, 2);
      }
      else {
        fs = piFontSize;
      }

      var panel = $get('T_pic_sampleBox');
      panel.style.backgroundColor = "#" + piBackgroundColor;
      pIcA(panel);

      $get('placeIconChar').style.color = "#" + piTextColor;
      
      var fontopt = $get('placeIconFont');
      fontopt.value = piFontFamily;
      pIcD(fontopt);

      $get('placeIconChar').innerHTML = piIconText;
      $get('placeIconFontSize').value = fs;
      $get('placeIconChar').style.fontSize = piFontSize + 'pt';
      
      var fontsty = $get('placeIconFontStyle');
      fontsty.value = piFontStyle;
      pIcE(fontsty);
      
      $get('placeIconText').value = piIconText;
    }
  }     
  var mpe = $find('T_pic_mpe');
  mpe.show();
}



function setPlaceIconBackground() {
  var s1 = $get('T_pic_tbR');
  var s2 = $get('T_pic_tbG');
  var s3 = $get('T_pic_tbB');
  var panel = $get('T_pic_sampleBox');
  if (s1 != null && s2 != null && s3 != null && panel != null) {
    var r = s1.value;
    var g = s2.value;
    var b = s3.value;
    piBackgroundColor = intToHex(r) + intToHex(g) + intToHex(b);
    panel.style.backgroundColor = '#' + piBackgroundColor;
  }
}



function intToHex(dec) {
  var hexArray = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f");
  var code1 = Math.floor(dec / 16);
  var code2 = dec - code1 * 16;
  var decToHex = hexArray[code1];
  decToHex += hexArray[code2]; 
  return (decToHex);
}

function pISave() {
  pICancel(); 
  selectedIcon = piIconSize + piBackgroundColor + piTextColor + piFontFamily + piFontSize + piFontStyle + UrlCode.encode(piIconText);
  placeIconReplace(selectedPlaceID, selectedIcon);
}

function pISave2(c) {
  pICancel();
  selectedIcon = "1" + c + "ffffff1162";
  placeIconReplace(selectedPlaceID, selectedIcon);
}

function pICancel() {
  var mpe = $find('T_pic_mpe');
  mpe.hide();
}








function pIcA(obj) {
  var s1 = $get('T_pic_tbRL');
  var s2 = $get('T_pic_tbGL');
  var s3 = $get('T_pic_tbBL');
  var tbR = $get('tbR_railElement');
  var tbG = $get('tbG_railElement');
  var tbB = $get('tbB_railElement');
  var panel = $get('T_pic_sampleBox');

  panel.style.backgroundColor = obj.style.backgroundColor;

  var color = new RGBColor(panel.style.backgroundColor);
  if (color.ok) {

    var r = color.r;
    var g = color.g;
    var b = color.b;

    s1.value = r;
    s2.value = g;
    s3.value = b;

    var width = 140;
    tbR.children[0].style.left = (width / 255 * r) + 'px';
    tbG.children[0].style.left = (width / 255 * g) + 'px';
    tbB.children[0].style.left = (width / 255 * b) + 'px';

    piBackgroundColor = color.toHex();
  }
}


function pIcB(obj) {
  var color = new RGBColor(obj.style.backgroundColor);
  if (color.ok) {
    var a = $get('placeIconChar');
    a.style.color = color.color;
    piTextColor = color.toHex();
  }
}


function pIcC(c) {
  var pic = $get('placeIconChar');
  pic.innerHTML = c;
  piIconText = c;
}


function pIcD(opt) {
  piFontFamily = opt.value;
  var ch = $get('charMap');
  var pic = $get('placeIconChar');
  var tb = $get('placeIconText');
  switch (parseInt(opt.value)) {
    case 1:
      ch.style.fontFamily = 'Arial';
      pic.style.fontFamily = 'Arial';
      tb.style.fontFamily = 'Arial';
      break;
    case 2:
      ch.style.fontFamily = 'Verdana';
      pic.style.fontFamily = 'Verdana';
      tb.style.fontFamily = 'Verdana';
      break;
    case 3:
      ch.style.fontFamily = 'Webdings';
      pic.style.fontFamily = 'Webdings';
      tb.style.fontFamily = 'Webdings';
      break;
    case 4:
      ch.style.fontFamily = 'Wingdings';
      pic.style.fontFamily = 'Wingdings';
      tb.style.fontFamily = 'Wingdings';
      break;
  }
}

function pIcE(opt) {
  piFontStyle = opt.value;
  var pic = $get('placeIconChar');
  var tb = $get('placeIconText');
  switch (parseInt(opt.value)) {
    case 1:
      pic.style.fontWeight = 'normal';
      tb.style.fontWeight = 'normal';
      break;
    case 2:
      pic.style.fontWeight = 'bold';
      tb.style.fontWeight = 'bold';
      break;
  }
}

function pIcF(opt) {
  var pic = $get('placeIconChar');
  pic.style.fontSize = opt.value + 'pt';
  piFontSize = opt.value;
  if (piFontSize.length == 1) piFontSize = '0' + piFontSize;
}

function pIcG(tb) {
  var pic = $get('placeIconChar');
  pic.innerHTML = tb.value;
  piIconText = tb.value;
}

function pIcNot(e) {
  if (e.keyCode == 13) {
    e.preventDefault();
    return false;
  }
  else {
    return true;
  }
}
