﻿var map;
var marker; //Đánh dấu bds

var mapRE;
var marketRE;

//Biến giữ sự kiện của map
var myListener=null;

//Trong js nếu muốn một biến được khởi tạo trong hàm theo kiểm tham chiếu (ra
//khỏi hàm bị thay đổi giá trị) thì biến đó cần được khởi tạo toàn cục và không truyền vào
//hàm như một tham số đầu vào, mà dùng trực tiếp biến toàn cục này luôn.

//Khởi tạo map
//select=0: Google Map
//select=1: Bing Map

//divId: Id của thẻ div chưa map. vd: divId="#mapContainer"

function InitialMap(select,divId) {
    switch (select) {
        case 0:
            var myLatlng = new google.maps.LatLng(21.02867698539203, 105.84148857421877);
            var myOptions = {
                zoom: 15,
                center: myLatlng,
                mapTypeControl: true,
                mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
                    position: google.maps.ControlPosition.TOP_LEFT
                },
                navigationControl: false,

                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map($(divId)[0], myOptions);
            break;
        case 1:
            break
    }
}

var geocoder;
function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
        zoom: 8,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function codeAddress() {
    var address = document.getElementById("address").value + ', ' + $('#ProvinceOnMapId > option:selected').text();
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

function codeAddressWithKeyDow(e) {
        var keycode;
        if (window.event) keycode = window.event.keyCode;
        else if (e) keycode = e.which;
        if (keycode == 13) {
            codeAddress();
        }
}

function MapToolbarContainer(controlDiv, map) {

    // Set CSS styles for the DIV containing the control
    // Setting padding to 5 px will offset the control
    // from the edge of the map
    controlDiv.style.padding = '5px 0px 5px 0px';
    //controlDiv.style.height = '20px';
    controlDiv.style.width = '485px';
    //controlDiv.style.border = '1px solid #678AC7';
    controlDiv.id='mapToolbarDiv';
}

function MapPagingContainer(controlDiv, map) {

    // Set CSS styles for the DIV containing the control
    // Setting padding to 5 px will offset the control
    // from the edge of the map
    controlDiv.style.padding = '0px 0px 0px 0px';
    //controlDiv.style.height = '20px';
    controlDiv.style.width = '200px';
    //controlDiv.style.border = '1px solid #678AC7';
    controlDiv.id = 'mapPagingDiv';
}



function ZoomUtilityContainer(controlDiv, map) {

    // Set CSS styles for the DIV containing the control
    // Setting padding to 5 px will offset the control
    // from the edge of the map
    controlDiv.style.padding = '5px 0px 5px 3px';
    controlDiv.style.height = '210px';
    controlDiv.style.width = '80px';
    controlDiv.style.left = '40px';
    controlDiv.style.top = '146px';
    controlDiv.id = 'zoomUtilityDiv';
    controlDiv.style.border = 'none';
}

function InitialMapUser(select, divId) {
    switch (select) {
        case 0:
            var myLatlng = new google.maps.LatLng(21.02867698539203, 105.84148857421877);
            var myOptions = {
                zoom: 15,
                center: myLatlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: true,
                mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                    position: google.maps.ControlPosition.TOP_LEFT
                },
                zoomControl: false,
                zoomControlOptions: {
                    style: google.maps.ZoomControlStyle.LARGE,
                    position: google.maps.ControlPosition.LEFT_CENTER
                },
                panControl: false,
                scaleControl: false,
                streetViewControl: false


            };
            geocoder = new google.maps.Geocoder();
            map = new google.maps.Map($(divId)[0], myOptions);

            var toolbarControlDiv = document.createElement('DIV');
            var toolbarControl = new MapToolbarContainer(toolbarControlDiv, map);

            toolbarControlDiv.index = 1;
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(toolbarControlDiv);

            //Paging control
            var pagingControlDiv = document.createElement('DIV');
            var pagingControl = new MapPagingContainer(pagingControlDiv, map);

            pagingControlDiv.index = 1;
            map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(pagingControlDiv);

            var zoomControlDiv = document.createElement('DIV');
            var zoomControl = new ZoomUtilityContainer(zoomControlDiv, map);

            zoomControlDiv.index = 1;
            map.controls[google.maps.ControlPosition.LEFT_CENTER].push(zoomControlDiv);

            break;
        case 1:
            break
    }

}


function InitialMapCreateRE(select, divId) {
    switch (select) {
        case 0:
            var myLatlng = new google.maps.LatLng(21.02867698539203, 105.84148857421877);
            var myOptions = {
                zoom: 15,
                center: myLatlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: true,
                mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                    position: google.maps.ControlPosition.TOP_LEFT
                },
                zoomControl: true,
                zoomControlOptions: {
                    style: google.maps.ZoomControlStyle.LARGE,
                    position: google.maps.ControlPosition.LEFT_CENTER
                },
                panControl: false,
                scaleControl: false,
                streetViewControl: false
            };
            geocoder = new google.maps.Geocoder();
            map = new google.maps.Map($(divId)[0], myOptions);

            var toolbarControlDiv = document.createElement('DIV');
            var toolbarControl = new MapToolbarContainer(toolbarControlDiv, map);

            toolbarControlDiv.index = 1;
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(toolbarControlDiv);

            break;
        case 1:
            break
    }
}

function getLevelZoom() {

    alert(map.getZoom());
}

function InitialMapRE(select, divId) {
    switch (select) {
        case 0:
            var myLatlng = new google.maps.LatLng(21.02867698539203, 105.84148857421877);
            var myOptions = {
                zoom: 15,
                center: myLatlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: true,
                mapTypeControlOptions: {
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
                    position: google.maps.ControlPosition.TOP_LEFT
                },
                zoomControl: true,
                zoomControlOptions: {
                    style: google.maps.ZoomControlStyle.LARGE,
                    position: google.maps.ControlPosition.LEFT_CENTER
                },
                panControl: false,
                scaleControl: false,
                streetViewControl: false
            };
            mapRE = new google.maps.Map($(divId)[0], myOptions);
            break;
        case 1:
            break
    }
}

function MouseMove(select, latId, lngId) {
    /// <summary> Xử lý sự kiện chuột di chuyển trên bản đồ, hiển thị kinh độ, vĩ độ của chuột trên map
    /// <para>select: select=0: Google Map, select=1: Bing Map  </para>
    /// <para>latId: id của label hiển thị kinh độ. vd: latId="#lat"</para>
    /// <para>lngId: id của label hiển thị vĩ độ. vd: lngId="#lng" </para>
    /// </summary>
    switch (select) {
        case 0:
            google.maps.event.addListener(map, 'mousemove', function(event) {
                $(latId)[0].innerHTML = event.latLng.lat();
                $(lngId)[0].innerHTML = event.latLng.lng();
            });
            break;

        case 1:
            break;
    }
}

//Đánh dấu bất đông sản vừa được đăng ký ở bước 5
//latReal: Vĩ độ của BĐS
//lngReal: Kinh độ của BĐS
function addMarketRE(latReal,lngReal) {

    var myLatlng = new google.maps.LatLng(latReal, lngReal);
    marker = new google.maps.Marker({
        position: myLatlng,
        map: map
    });
    map.setZoom(17);
    map.setCenter(myLatlng);
}

function addMarketRE_RegisterREM(latReal, lngReal) {
    var myLatlng = new google.maps.LatLng(latReal, lngReal);
    marker = new google.maps.Marker({
        position: myLatlng,
        map: map
    });
    map.setZoom(15);
    map.setCenter(myLatlng);
}

function addMarketREdetail(latReal, lngReal) {

    var myLatlng = new google.maps.LatLng(latReal, lngReal);
    markerRE = new google.maps.Marker({
        position: myLatlng,
        map: mapRE
    });
    mapRE.setZoom(15);
    mapRE.setCenter(myLatlng);
}

function Society() {
    this.IdSoci=null
    this.type = null
    this.name = null
    this.lat = null
    this.lng = null
    this.index = null

    this.IdRoad = null
    this.DestructorOfSociety=DestructorOfSociety
}

function DestructorOfSociety() {
    this.type = null
    this.name = null
    this.lat = null
    this.lng = null
    this.index = null

}

function RoadLinkSociety(marker_Society, id_Society, polyline, info_Window, coordinates_Road) {

    //Properties

    //Tọa độ đường đi Polyline khi chuyển sang chuỗi
    this.coordinates = coordinates_Road
    //Id trong cơ sở dữ liệu của hạ tầng xã hội
    this.idSociety = id_Society
    
    //Lưu trữ thông tin về htxh
    this.SocietyInfo=null

    //Marker đánh dấu HTXH
    this.society = marker_Society

    this.infoWindow = info_Window

    this.polyline = polyline

    //Methods

    this.Invisible = Invisible

    this.Destructor = Destructor
}

//Ẩn những marker được đánh dấu xóa
function Invisible() {
    if (this.society != null) this.society.setMap(null)
    if (this.polyline != null) this.polyline.setMap(null)
    if (this.infoWindow != null) this.infoWindow.close()

}

//Giải phóng bộ nhớ được các thuộc tính tham chiếu đến
function Destructor() {
    this.coordinates = null
    this.idSociety = null
    this.society = null
    this.infoWindow = null
    this.polyline = null
    this.SocietyInfo.type = ""
    this.SocietyInfo.name = ""
    this.SocietyInfo.lat = ""
    this.SocietyInfo.lng =""
    this.SocietyInfo.index = ""
    
}

//Không cần đến đối tượng này
function InfoSociety(markerSociety, idSociety) {
    this.marker = markerSociety
    this.id = idSociety
}

/*-------------------Show/Hide pleasewait------------------*/
function showPleasewait(element) {
    /// <summary> Hiển thị màn hình pleasewait khi xử lý ajax
    /// <para>element: theo cú pháp selector của jquery </para>
    /// </summary>
    $.blockUI({
        message: '<img alt="Đang tải..." src="http://rs.golden.net.vn/icon/pleasewait.gif"/>',
        css: {
            padding: 0,
            margin: 0,
            textAlign: 'center',
            color: '#000',
            border: '0px solid #aaa',
            backgroundColor: 'Transparent',
            opacity: '0.6',
            cursor: 'wait'
        },
        overlayCSS: {
            backgroundColor: '#e6e0d5',
            opacity: '0.6'
        }
    });
}

function hidePleasewait(element) {
    /// <summary> Ẩn màn hình pleasewait khi xử lý ajax
    /// <para>element: theo cú pháp selector của jquery </para>
    /// </summary>
    $.unblockUI();
}

//lấy lại index  một marker được click trong mảng dựa vào ID của HTXH
Array.prototype.isOfArray = function(value) {
    var i;
    for (i = 0; i < this.length; i++) {
        if (this[i].SocietyInfo != null) {
            var id = this[i].SocietyInfo.index;
            if (id == value) { return i; }
        }

    }
    return -1;
}

function getCookie(Name) { //get cookie value
    var re = new RegExp(Name + "=[^;]+", "i"); //construct RE to search for target name/value pair
    if (document.cookie.match(re)) //if cookie found
        return document.cookie.match(re)[0].split("=")[1] //return its value
    return ""
}

function setCookie(name, value, days) { //set cookie value
    var expireDate = new Date()
    //set "expstring" to either future or past date, to set or delete cookie, respectively
    var expstring = expireDate.setDate(expireDate.getDate() + parseInt(days))
    document.cookie = name + "=" + value + "; expires=" + expireDate.toGMTString() + "; path=/";
}

// This function deletes a cookie
function delCookie(name) {
    document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
} 
