﻿var markerGift = null;

function load() {
  if (GBrowserIsCompatible()) {
    mapdiv = document.getElementById("map");
    map = new google.maps.Map2(mapdiv);
    map.setUIToDefault();


    var lat = parseFloat($get('C_mapLatitude').value);
    var lng = parseFloat($get('C_mapLongitude').value);
    var zoom = parseInt($get('C_mapZoomLevel').value);
    var mapType = parseInt($get('C_mapMapType').value);
    var giftMarkerText = $get('C_giftMarkerText').value;

    var start = new GLatLng(lat, lng);
    switch (mapType) {
      case 2:
        map.setCenter(start, zoom, G_SATELLITE_MAP);
        break;
      case 3:
        map.setCenter(start, zoom, G_HYBRID_MAP);
        break;
      case 4:
        map.setCenter(start, zoom, G_PHYSICAL_MAP);
        break;
      default:
        map.setCenter(start, zoom, G_NORMAL_MAP);
        break;
    }

    addMarker($get('C_icon').value);

    if (giftMarkerText != '') {
      var bounds = map.getBounds();
      var southWest = bounds.getSouthWest();
      markerGift = createGiftMarker(southWest, giftMarkerText);
      map.addOverlay(markerGift);
    }

    var map2 = new GMap2($get('map2'));
    map2.setCenter(start, 3, G_NORMAL_MAP);
    map2.addOverlay(createMarker2(start));
    map2.disableDragging();
    map2.disableDoubleClickZoom();


    //  GEvent.addDomListener(mapdiv, "DOMMouseScroll", map.wheelZoom); // Firefox 
    //  GEvent.addDomListener(mapdiv, "mousewheel", map.wheelZoom); // IE 

    GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) {
      if (markerGift != null) {
        if (newLevel < 14) {
          map.removeOverlay(markerGift);
        }
        else {
          map.addOverlay(markerGift);
        }
      }
    });
  }
}

function addMarker(icon) {
  var lat = parseFloat($get('C_mapLatitude').value);
  var lng = parseFloat($get('C_mapLongitude').value);
  var start = new GLatLng(lat, lng);
  var id = parseInt($get('C_id').value);
  var markerText = $get('C_mapMarkerText').value;
  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 marker;
  if (icon == "") {
    marker = createMarker(start, markerText);
  }
  else {
    marker = createPlaceIconMarker(map, start, id, icon, markerText, isMapEdit, isLocal);
  }
  map.addOverlay(marker);
  marker.openInfoWindowHtml(markerText);
}


function createMarker(point, markerText) 
{  
  var icon = new GIcon();
  icon.image = "http://i.ittvoltam.hu/icon/green.png";
  icon.shadow = "http://i.ittvoltam.hu/icon/shadow.png";
  icon.iconSize = new GSize(20, 34);
  icon.shadowSize = new GSize(37, 34);
  icon.iconAnchor = new GPoint(9, 34);
  icon.infoWindowAnchor = new GPoint(9, 2);
  icon.infoShadowAnchor = new GPoint(18, 25);
  
  var marker = new GMarker(point, icon);  
  GEvent.addListener(marker, "click", function() 
  {
    marker.openInfoWindowHtml(markerText);  
  });
  return marker;
}

function createMarker2(point) 
{  
  var icon = new GIcon();
  icon.image = "http://i.ittvoltam.hu/icon/crossR.png";
  icon.iconSize = new GSize(11, 11);
  icon.iconAnchor = new GPoint(5, 5);
  icon.infoWindowAnchor = new GPoint(5, 0);
  return new GMarker(point, icon);
}

function createGiftMarker(point, markerText) 
{  
  var icon = new GIcon();
  icon.image = "http://i.ittvoltam.hu/icon/jheremiosz.png";
  icon.iconSize = new GSize(60, 114);
  icon.iconAnchor = new GPoint(0, 114);
  icon.infoWindowAnchor = new GPoint(30, 2);
  
  var marker = new GMarker(point, icon);  
  GEvent.addListener(marker, "click", function() 
  {
    marker.openInfoWindowHtml(markerText);  
  });
  return marker;
}

function buttonCommentClick()
{
  var c = $get('commentText');
  var placeGuid = $get('C_placeGuid').value;
  var comment = c.value;
  c.value = "";
  document.documentElement.style.cursor = "wait";
  $get('up1').style.display = 'block'; 
  Animare.IttVoltam.Web.WebService.Command.AddNewComment(placeGuid, comment, OnSucceeded, OnFailed);
}

function OnSucceeded(result)
{
  document.documentElement.style.cursor = "default";
  $get('up1').style.display = 'none'; 
  if (result.code != 0)
  {
    alert(result.message); 
  }
  else
  {
    $get('commentList').innerHTML = result.content;
    $get('statistics').innerHTML = result.content2;
    var a2 = new AjaxControlToolkit.Animation.ColorAnimation($get("firstComment"), 0.3, 25, "style", "backgroundColor", "#FFFFF8", "#FFCC00");
    var a3 = new AjaxControlToolkit.Animation.ColorAnimation($get("firstComment"), 1.0, 25, "style", "backgroundColor", "#FFCC00", "#FFFFF8");
    AjaxControlToolkit.Animation.SequenceAnimation.play($get("firstComment"), null, 30, [a2, a3], 1); 
  }
}

function OnFailed(error)
{
  document.documentElement.style.cursor = "default";
  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 placeRateAddNew()
{
  var c = $get('C_Rating1_RatingExtender_ClientState');
  var placeGuid = $get('C_placeGuid').value;
  document.documentElement.style.cursor = "wait";
  $get('up1').style.display = 'block'; 
  Animare.IttVoltam.Web.WebService.Command.PlaceRateAddNew(placeGuid, c.value, OnSucceededPlaceRateAddNew, OnFailedPlaceRateAddNew);
}

function OnSucceededPlaceRateAddNew(result)
{
  document.documentElement.style.cursor = "default";
  $get('up1').style.display = 'none'; 
  if (result.code != 0)
  {
    alert(result.message); 
  }
  else
  {
    $get('statistics').innerHTML = result.content2;
    var c = $get('C_PanelRating');
    var a2 = new AjaxControlToolkit.Animation.FadeOutAnimation(c, 0.4, 30, 0, 1, true);
    var a3 = new AjaxControlToolkit.Animation.HideAction(c, 0.05, 30);
    AjaxControlToolkit.Animation.SequenceAnimation.play(c, null, 30, [a2, a3], 1); 
   // $get('commentList').innerHTML = result.content;
  }
}

function OnFailedPlaceRateAddNew(error)
{
  document.documentElement.style.cursor = "default";
  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 placePhotoRemove(photoGuid)
{
  document.documentElement.style.cursor = "wait";
  $get('up1').style.display = 'block'; 
  Animare.IttVoltam.Web.WebService.Command.PlacePhotoRemove(photoGuid, OnSucceededPlacePhotoRemove, OnFailedPlacePhotoRemove);
}  
 
function OnSucceededPlacePhotoRemove(result)
{
  document.documentElement.style.cursor = "default";
  $get('up1').style.display = 'none'; 
  if (result.code != 0)
  {
    alert(result.message); 
  }
  else
  {
    $get('photoDiv').innerHTML = result.content;
  }
}

function OnFailedPlacePhotoRemove(error)
{
  document.documentElement.style.cursor = "default";
  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 modifyZoom(change)
{
  var placeGuid = $get('C_placeGuid').value;
  document.documentElement.style.cursor = "wait";
  $get('up1').style.display = 'block'; 
  Animare.IttVoltam.Web.WebService.Command.ModifyPlace(placeGuid, 1, change, OnSucceededModifyPlace, OnFailedModifyPlace);
}

function modifyMapType(id)
{
  var placeGuid = $get('C_placeGuid').value;
  document.documentElement.style.cursor = "wait";
  $get('up1').style.display = 'block'; 
  Animare.IttVoltam.Web.WebService.Command.ModifyPlace(placeGuid, 2, id, OnSucceededModifyPlace, OnFailedModifyPlace);
}

function OnSucceededModifyPlace(result) {
  document.documentElement.style.cursor = "default";
  $get('up1').style.display = 'none'; 
  if (result.code < 0)
  {
    alert(result.message); 
  }
  else
  {
    if (result.code == 1) {
      map.setZoom(result.zoomLevel);
    }
    else
    {
      if (result.code == 2)
      {   
        switch (result.mapTypeID)
        {
          case 2:
            map.setMapType(G_SATELLITE_MAP);
            break;
          case 3:
            map.setMapType(G_HYBRID_MAP);
            break;
          case 4:
            map.setMapType(G_PHYSICAL_MAP);
            break;
          default:
            map.setMapType(G_NORMAL_MAP);
            break;
        }
      }
    } 
  } 
}

function OnFailedModifyPlace(error)
{
  Animare.IttVoltam.Web.WebService.Command.Error(
    error.get_exceptionType(),
    error.get_message(),
    error.get_statusCode(),
    error.get_stackTrace(),
    error.get_timedOut()
  );
  document.documentElement.style.cursor = "default";
  $get('up1').style.display = 'none'; 
  alert(errorMessage);
}    



function copyLink(permaLink)
{
  window.clipboardData.setData('Text', permaLink.value);
}








function placeIconReplace(placeID, icon) {
  document.documentElement.style.cursor = "wait";
  $get('up1').style.display = 'block';
  Animare.IttVoltam.Web.WebService.Command.PlaceIconReplace(2, placeID, icon, OnSucceededPlaceIconReplace, OnFailedPlaceIconReplace);
}

function OnSucceededPlaceIconReplace(ro) {
  document.documentElement.style.cursor = "default";
  if (ro.code != 0) {
    $get('up1').style.display = 'none';
    alert(ro.message);
  }
  else {
    map.clearOverlays();
    addMarker(ro.content);
    var img = $get('iconImg');
    if (img != null) {
      img.src = ro.content2;
    }
    $get('up1').style.display = 'none';
  }
}

function OnFailedPlaceIconReplace(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 pageLoad()
{
  load();
}   
  
function pageUnload()
{
  GUnload();
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();



