﻿function MapWrapper() 
{
    var _this = this;
    this.map = null;
    this.setMarker = null;
    this.geocoder = null;
    this.markerClusterer = null;

    this.centerX = document.getElementById('pos_x').value;
    this.centerY = document.getElementById('pos_y').value;

    var retAddress = "";
    this.init = function () {       
        if (_this.geocoder == null) {
            _this.geocoder = new google.maps.Geocoder();
        }      
        if (_this.map == null) {
            var latlng = new google.maps.LatLng(_this.centerX, _this.centerY);
            var myOptions = {
                zoom: 8,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            _this.map = new google.maps.Map(document.getElementById("Map"),
            myOptions);
        }     
    }
  
    this.getZoom = function (bounds) {

        var width = $(".map").width();
        var height = $(".map").height();

        var dlat = Math.abs(bounds.getNorthEast().lat() - bounds.getSouthWest().lat());
        var dlon = Math.abs(bounds.getNorthEast().lng() - bounds.getSouthWest().lng());

        var max = 0;
        if (dlat > dlon) {
            max = dlat;
        } else {
            max = dlon;
        }

        // Center latitude in radians
        var clat = Math.PI * Math.abs(bounds.getSouthWest().lat() + bounds.getNorthEast().lat()) / 360.;

        var C = 0.0000107288;
        var z0 = Math.ceil(Math.log(dlat / (C * height)) / Math.LN2);
        var z1 = Math.ceil(Math.log(dlon / (C * width * Math.cos(clat))) / Math.LN2);

        return 18 - ((z1 > z0) ? z1 : z0);
    }

  
    this.toggleBounceMarker = function() {
        if (_this.setMarker.getAnimation() != null) {
            _this.setMarker.setAnimation(null);
        } else {
            _this.setMarker.setAnimation(google.maps.Animation.BOUNCE);
        }
    }
   
    this.markerPositionChanged = function () {
        var latlng = _this.setMarker.getPosition();
        _this.GetInfo(latlng);
    }
    
    this.GetInfo = function (latlng) 
    {
        _this.geocoder.geocode({ 'latLng': latlng, 'language' : document.getElementById('pos_lang').value }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                _this.map.setCenter(results[0].geometry.location);
                _this.SetAddresses(results);
            } else {
                alert("Error: " + status);
            }
        });
    }
   
    this.ComposeAddress = function (item) {
        retAddress = "";
        $.each(item.address_components, function (i, address_item) {
            var isOk = false;
            $.each(address_item.types, function (j, typeName) {
                
                if (typeName != "street_address" && typeName != "locality") {
                    isOk = true;
                }
            });
            if (isOk) {
                if (retAddress == "") {
                    retAddress = address_item.long_name;
                } else {
                    retAddress = retAddress + ", " + address_item.long_name;
                }
            }
        });
        return retAddress;
    }   
}


$().ready(function () {
    var mapWrapper = new MapWrapper();
    mapWrapper.init();
});
