
var GAllMaps = []; // key = uniqid, value = data
GAllMaps.__indices = [];

function MapData() { }

// SOAP data.
MapData.map_id = null;
MapData.map_guid = null;

MapData.isLoaded = false;
MapData.forceLoading = false;

MapData.map_name = "";
MapData.map_comments = "";

// Left top corner in geo coords.
MapData.map_west = 0;
MapData.map_north = 0;

// Right bottom corner in geo coords.
MapData.map_east = 0;
MapData.map_south = 0;

MapData.map_server = "";
MapData.map_slash = "/"; // Default.

MapData.markers = [];           // marker`s heap (only data).

// Markers type.
MapData.NULL_MARKERS = 0;
MapData.USER_MARKERS = 1;
MapData.FAVORITE_MARKERS = 2;
MapData.FINDER_MARKERS = 4;

MapData.markers[MapData.NULL_MARKERS] = [];
MapData.markers[MapData.USER_MARKERS] = [];
MapData.markers[MapData.FAVORITE_MARKERS] = [];
MapData.markers[MapData.FINDER_MARKERS] = [];

MapData.markers[MapData.NULL_MARKERS].filter = "*";
MapData.markers[MapData.USER_MARKERS].filter = "*";
MapData.markers[MapData.FAVORITE_MARKERS].filter = "!*";
MapData.markers[MapData.FINDER_MARKERS].filter = "*";

MapData.markers[MapData.FAVORITE_MARKERS].gid_list = "!*";

MapData.markers[MapData.NULL_MARKERS].scale = MapData.curr_scale;
MapData.markers[MapData.USER_MARKERS].scale = MapData.curr_scale;
MapData.markers[MapData.FAVORITE_MARKERS].scale = MapData.curr_scale;
MapData.markers[MapData.FINDER_MARKERS].scale = MapData.curr_scale;

MapData.groups = new Array;     // key = Uid:Gid, value = [Mid].
MapData.groups.__indices = new Array;

MapData.classes = [];           // key = Cuid, value = class data.

// Scale data.
MapData.scales = [ 2, 5, 10, 20, 50, 100 ];
MapData.curr_scale = 5;
MapData.curr_scale_id = MapData.scales.indexOf(MapData.curr_scale);

MapData.scale_kx = [];
MapData.scale_ky = [];
MapData.scale_rkx = [];
MapData.scale_rky = [];

MapData.checkGroupOnGlobalByGid = function(gid) { return (this.groups["0:" + gid] == undefined) ? false : true; };

MapData.checkGroupOnGlobal = function(group) {
          var gid = group.elmtGid;
          if(gid + "" == "-1" || gid + "" == "0") return false;
          return (group.elmtUid == 0) ? true : false;
};

MapData.findMarkerByMid = function(markers, mid) {
          for(var i=0; i<markers.length; i++) {
                  if(markers[i].elmtMid == mid)
                           return i;
          }
          return -1;
};

MapData.findGroupByUidGid = function(uid, gid) {
          var group = this.groups[uid + ":" + gid];
          if(group == undefined) return false;
          return group;
};

MapData.setSplash = function(str) {

          var reg = /http:/i;
          if(reg.test(str) == true) {
                 this.map_slash = "/";
                 return;
          }

          var reg = /[a-zA-Z]{1}:/;
          if(reg.test(str) == true)
                 this.map_slash = "\\";
          else
                 this.map_slash = "/";
};

MapData.initMap = function(map_data, callback) {

          this.isLoaded = false;

          this.map_id = map_data.elmtId;
          this.map_guid = map_data.elmtUniqid;
          this.map_name = map_data.elmtName;
          this.map_comments = map_data.elmtComments;
          this.map_west = map_data.elmtWest;
          this.map_north = map_data.elmtNorth;
          this.map_east = map_data.elmtEast;
          this.map_south = map_data.elmtSouth;
          
          // Setup current map server.
          var link = Profile.getMapServer(this.map_id);
          this.map_server = link != "" ? link : map_static_data;
          //this.setSplash(this.map_server);

          this.forceLoading = false;

          //GMode.setLabel("");
          if(map_data.elmtFlags.indexOf("fm") != -1) { this.forceLoading = true; /*GMode.setLabel("force mode");*/ }

          var id = MapData.scales.indexOf(100);
          if(map_data.elmtFlags.indexOf("top") != -1) {
                   if(id != -1) MapData.scales.splice(id, 1);
          } else {
                   if(id == -1) MapData.scales.push(100);
          }

          // Clear markers.
          MapData.markers[MapData.NULL_MARKERS].length = 0;
          MapData.markers[MapData.USER_MARKERS].length = 0;
          MapData.markers[MapData.FAVORITE_MARKERS].length = 0;


          for(var i=0; i<this.scales.length; i++) {
                  var scale = this.scales[i];
                  this.scale_kx[scale] = (this.map_east-this.map_west)/(scale*GCellWidth);
                  this.scale_ky[scale] = (this.map_south-this.map_north)/(scale*GCellHeight);
                  this.scale_rkx[scale] = 1/this.scale_kx[scale];
                  this.scale_rky[scale] = 1/this.scale_ky[scale];
          }

          var groups = this.getGroups();

          callback(groups);
          
          this.isLoaded = true;
};

// aX = (cX - GPortX - GAbsX), cX - port coord.
// aY = (cY - GPortY - GAbsY), cY - port coord.

MapData.Px2Geo = function(aX, aY) { // aX, aY - absolute coord in pixels.

         var ret = [];

         var deg_lon = this.map_west + aX * this.scale_kx[this.curr_scale];
         var deg_lat = this.map_north + aY * this.scale_ky[this.curr_scale];

         ret.lon = deg_lon; ret.lat = deg_lat;

         var min_lon = (deg_lon - ( deg_lon = Math.floor(deg_lon))) * 60;
         var min_lat = (deg_lat - ( deg_lat = Math.floor(deg_lat))) * 60;
         var sec_lon = (min_lon - ( min_lon = Math.floor(min_lon))) * 60;
         var sec_lat = (min_lat - ( min_lat = Math.floor(min_lat))) * 60;

         ret.str = " N " + deg_lat + "\u00B0 " + min_lat + "' " + Math.floor(sec_lat) + "\" , " +
                    " E " + deg_lon + "\u00B0 " + min_lon + "' " + Math.floor(sec_lon) + "\"";

         return ret;
}

MapData.Geo2Px = function(deg_lon, deg_lat) {

         var aX = (deg_lon-this.map_west)*this.scale_rkx[this.curr_scale];
         var aY = (deg_lat-this.map_north)*this.scale_rky[this.curr_scale];

         return { aX : aX, aY : aY }; // Absolute coords in pixels.
}

MapData.fillClasses = function() { this.classes = GetAllMarkerClass(); };

MapData.getGroups = function() {
          var groups = GetAllMarkerGroup(this.map_guid, 0, "*", "");

          this.groups = [];
          this.groups.__indices = [];

          MapData.markers[MapData.FAVORITE_MARKERS].length = 0;

          for(var i=0; i<groups.length; i++) {
                  this.groups.__indices[i] = groups[i].elmtUid + ":" + groups[i].elmtGid;
                  groups[i].__isFill = false;
                  groups[i].__isCheck = false;
                  groups[i].__ugid = this.groups.__indices[i];
                  this.groups[this.groups.__indices[i]] = groups[i];
          }
          return groups;
};

MapData.getFavoriteGroups = function() {

};

MapData.addGroup = function(group) {
          group.__isFill = false;
          group.__isCheck = false;
          group.__ugid = group.elmtUid + ":" + group.elmtGid;
          this.groups.__indices.push(group.__ugid);
          this.groups[group.__ugid] = group;
          return group;
};

MapData.getMarkerForGroup = function(group) {

/*          GDebugPort.debug("Group: " + group);
          var soap_data = [];
          soap_data["elmtUid"] = "*";
          soap_data["elmtLogicFlag"] = "strong";
          soap_data["elmtName"] = "*";
          soap_data["elmtGroup"] = group;
          soap_data["elmtComments"] = "*";
          soap_data["elmtPicsComments"] = "*";
          GetMarker(soap_data, true, function(arr) {
                 for(var i=0; i<arr.length; i++) {
                     arr[i] = SetIconGroup(arr[i]);
                     arr[i] = SetTextGroup(arr[i]);
                     MapData.markers[MapData.FAVORITE_MARKERS].push(arr[i]);
                 }
          });*/
};

MapData.removeGroup = function(group) {
          var key = group.elmtUid + ":" + group.elmtGid;
          this.groups.__indices.splice(this.groups.__indices.indexOf(key), 1);
          this.groups[key] = undefined;

          // Remove markers.
          for(var i=0; i<this.markers[MapData.FAVORITE_MARKERS].length; i++) {
                  if(this.markers[MapData.FAVORITE_MARKERS][i].elmtGroup != group.elmtGid) continue;
                  if(this.markers[MapData.FAVORITE_MARKERS][i].elmtUid != group.elmtUid) continue;
                  this.markers[MapData.FAVORITE_MARKERS].splice(i, 1);
                  i--;
          }
};

MapData.removeAllUserGroups = function() {
          var group;
          for(var i=0; i<this.groups.__indices.length; i++) {
                  group = this.groups[this.groups.__indices[i]];
                  if(this.checkGroupOnGlobal(group) == false) {
                         this.removeGroup(group);
                         i--;
                  }
          }
};

