﻿//var editor = null;
var currentPage = 'step6';
var markerNewPlace = null;
var markerGoto = null;

function load() 
{
  if (GBrowserIsCompatible()) 
  {
    var lat = parseFloat($get('m_C_mapLatitude').value);
    var lng = parseFloat($get('m_C_mapLongitude').value);
    var zoom = parseInt($get('m_C_mapZoomLevel').value);
    var mapType = parseInt($get('m_C_mapMapType').value);
    var markerNumber = parseInt($get('m_C_mapMarker').value);
    var markerText = $get('m_C_mapMarkerText').value;
    
  //  editor = document.getElementById("editor");
    mapdiv = document.getElementById("map");
    map = new google.maps.Map2(mapdiv);
    map.setUIToDefault(); 
    /*
    map.addMapType(G_SATELLITE_3D_MAP);    
    
    map.addControl(new GLargeMapControl());
    map.addControl(new GScaleControl());
    map.addControl(new TextualZoomControlEarth());
     
    map.enableContinuousZoom();
    map.enableScrollWheelZoom();
    */
    
    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;
    }


    
    
    showLatLng(start);
    showZoom();
    
    if (markerNumber > 0)
    {
      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);
      markerGoto = new GMarker(start, icon);  
      map.addOverlay(markerGoto);
      if (markerText != "")
      {
        GEvent.addListener(markerGoto, "click", function() 
        {
          markerGoto.openInfoWindowHtml(markerText);  
        });
        markerGoto.openInfoWindowHtml(markerText);  
      }
    }
    
    updateSmallMap();

    //GEvent.addDomListener(mapdiv, "DOMMouseScroll", map.wheelZoom); // Firefox 
    //GEvent.addDomListener(mapdiv, "mousewheel", map.wheelZoom); // IE 
   
    GEvent.addListener(map, "mousemove", function(point) { 
      if (!markerNewPlace)
      {
        showLatLng(point);
      }            
    });


    GEvent.addListener(map, "click", function(marker, point) {
      if (marker) {
        editorCancel();
      }
      else {
        if (!markerNewPlace) {
          var userLogin = $get('m_userLogin').value;

          if (userLogin == "0") {
            alert("Új helyszín felvétele előtt be kell jelentkezned!");
          }
          else {
            if (map.getZoom() < 16) {
              alert("A hely kijelölése előtt nagyítsd fel a térképet legalább 16-os szintre annak érdekében, hogy pontosan tudd megjelölni a helyszínt!");
            }
            else {
              $get("bOK6").disabled = false;
              $get("bCancel6").disabled = false;

              map.disableScrollWheelZoom();
              map.disableDragging();
              markerNewPlace = new GMarker(point);
              //var gp = markerNewPlace.getPoint();
              map.addOverlay(markerNewPlace);

              var CDivPixel = map.fromLatLngToDivPixel(map.getCenter());
              var pointDivPixel = map.fromLatLngToDivPixel(point);
              var fromCenter = subGPoints(pointDivPixel, CDivPixel);
              /*
              editor.style.left = fromCenter.x + mapdiv.offsetLeft + mapdiv.offsetWidth / 2 + 8;
              editor.style.top = fromCenter.y  + mapdiv.offsetTop + mapdiv.offsetHeight / 2 + 8;
              editor.style.display = "block";
              AjaxControlToolkit.Animation.FadeInAnimation.play(editor, 0.15, 30, 0, 1, true);
              */

              //pcnStep('step1');

              var mpe = $find('m_T_mpe');
              mpe.show();

              var pcnPanel = $get('m_T_pcnPanel');
              pcnPanel.style.left = (fromCenter.x + mapdiv.offsetLeft + mapdiv.offsetWidth / 2 + 3) + "px";
              pcnPanel.style.top = (fromCenter.y + mapdiv.offsetTop + mapdiv.offsetHeight / 2 + 3 - f_scrollTop()) + "px";

              //alert(f_scrollTop());

              var latlng = markerNewPlace.getPoint();
              $get('m_T_pcn_tbLatitude4').value = latlng.lat();
              $get('m_T_pcn_tbLongitude4').value = latlng.lng();

              //$get("m_C_TextBoxPlaceName").focus();
            }
          }
        }
      }
    });
            
    GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) {
      showZoom();
    });
    
  }
}

function f_filterResults(n_win, n_docel, n_body) {
  var n_result = n_win ? n_win : 0;
  if (n_docel && (!n_result || (n_result > n_docel)))
    n_result = n_docel;
  return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function f_scrollTop() {
  return f_filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function resetPlace()
{
  map.removeOverlay(markerNewPlace);
  markerNewPlace = null;
  $get('newplace').style.display = "none";
  $get('ButtonReset').style.display = "none";
}

function editorCancel() {
  var mpe = $find('m_T_mpe');
  mpe.hide();

  $get("bOK6").disabled = false;
  $get("bCancel6").disabled = false;

  /*

  var a1 = new AjaxControlToolkit.Animation.FadeOutAnimation(editor, 0.15, 30, 0, 1, true);
  var a2 = new AjaxControlToolkit.Animation.StyleAction(editor, 0.05, 30, "display", "none");
  AjaxControlToolkit.Animation.SequenceAnimation.play(editor, null, 30, [a1, a2], 1); 
  */
  map.removeOverlay(markerNewPlace);
  markerNewPlace = null;
  map.enableScrollWheelZoom();
  map.enableDragging();

  document.documentElement.style.cursor = "default";
  
}

function editorOK6()
{
  $get("bOK6").disabled = true;
  $get("bCancel6").disabled = true;

  var minLen = parseInt($get('m_T_pcn_minLen').value);
  var latlng = markerNewPlace.getPoint();
  var visibilityID = $get('m_T_pcn_ddlVisibility6').value;
  var placeTypeID = $get('m_T_pcn_ddlType6').value;
  var country = $get('m_T_pcn_tbCountry6').value;
  var placeName = $get('m_T_pcn_tbPlaceName6').value;
  var description = $get('m_T_pcn_tbDescription6').value;
  var tags = $get('m_T_pcn_tbTags6').value;
  var startDate = $get('m_T_pcn_tbStartDate6').value;
  var endDate = $get('m_T_pcn_tbEndDate6').value;
  var url = $get('m_T_pcn_tbUrl6').value;
  
  if (placeName == "")
  {
    alert("A hely nevét kötelező megadni!");
    $get("m_T_pcn_tbPlaceName6").focus();
    $get("bOK6").disabled = false;
    $get("bCancel6").disabled = false;
  }
  else
  {
    if (description.length < minLen) {
      alert("A hely leírását kötelező megadni!\nKérjük, írj pár sort erről a helyszínről!");
      $get("m_T_pcn_tbDescription6").focus();
      $get("bOK6").disabled = false;
      $get("bCancel6").disabled = false;
    }
    else {
      document.documentElement.style.cursor = "wait";
      $get('m_up1').style.display = 'block';
      Animare.IttVoltam.Web.WebService.Command.StoreNewPlace(latlng.lat(), latlng.lng(), 14, visibilityID, placeTypeID, country, placeName, description, tags, startDate, endDate, url, OnSucceeded6, OnFailed);
    }
  }    
}
    
function OnSucceeded6(result)
{
  document.documentElement.style.cursor = "default";
  if (result.code != 0)
  {
    $get('m_up1').style.display = 'none'; 
    alert(result.message); 
    $get("bOK6").disabled = false;
    $get("bCancel6").disabled = false;
  }
  else
  {
    document.documentElement.scrollTop = 182;
    editorCancel();
    $get('m_T_pcn_tbPlaceName6').value = "";
    $get('m_T_pcn_tbDescription6').value = "";
    $get('m_T_pcn_tbTags6').value = "";
    $get('m_T_pcn_tbStartDate6').value = "";
    $get('m_T_pcn_tbEndDate6').value = "";
    $get('m_T_pcn_tbUrl6').value = "";
    $get('m_C_LabelPlaceList').innerHTML = result.content;
    
    updateSmallMap(); 
    $get('m_up1').style.display = 'none'; 
    
    //AjaxControlToolkit.Animation.ColorAnimation.play($get("firstPlace"), 0.8, 25, "style", "backgroundColor", "#000000", "#FFFFFF");
    //AjaxControlToolkit.Animation.FadeInAnimation.play($get("firstPlace"), 1, 30, 0, 1, true);
    //var a1 = new AjaxControlToolkit.Animation.FadeInAnimation($get("firstPlace"), 0.3, 30, 0, 1, true);
    
    var a2 = new AjaxControlToolkit.Animation.ColorAnimation($get("firstPlace"), 0.3, 25, "style", "backgroundColor", "#FFFFFF", "#FFCC00");
    var a3 = new AjaxControlToolkit.Animation.ColorAnimation($get("firstPlace"), 1.0, 25, "style", "backgroundColor", "#FFCC00", "#FFFFFF");
    AjaxControlToolkit.Animation.SequenceAnimation.play($get("firstPlace"), null, 30, [a2, a3], 1);

  }
}

function OnFailed(error)
{
  editorCancel();
  Animare.IttVoltam.Web.WebService.Command.Error(
    error.get_exceptionType(),
    error.get_message(),
    error.get_statusCode(),
    error.get_stackTrace(),
    error.get_timedOut()
  );
  $get('m_up1').style.display = 'none'; 
  alert(errorMessage);
}    
    
/*
function getDirections()
{
  directions.load("from: " + $get('m_R_tbFrom').value + " to: " + $get('m_R_tbTo').value);
}

function clearDirections()
{
  directions.clear();
}
 */
function pcnStep(page) {
  var o = $get(currentPage);
  if (o != null) {
    o.style.display = 'none';
  }
  o = $get(page);
  if (o != null) {
    o.style.display = 'block';
  }
  currentPage = page;
}


function onKeyP6(evt, frm) {
  var keyCode = null;
  if (evt.which) {
    keyCode = evt.which;
  }
  else {
    if (evt.keyCode) {
      keyCode = evt.keyCode;
    }
  }
  if (13 == keyCode) {
    editorOK6();
    return false;
  }
  return true;
}

function textCounter() {                        
  var minLen = parseInt($get('m_T_pcn_minLen').value);
  var c = minLen - $get('m_T_pcn_tbDescription6').value.length;
  if (c > 0) {
    $get("counter").innerHTML = "Írj még legalább <b>" + c + "</b> karaktert!";
  }
  else {
    $get("counter").innerHTML = "";
  }
}

 function pageLoad()
 {
   load();
 }   
    
 function pageUnload()
 {
   GUnload();
 }

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
