﻿
//GLOBAL VARIABLE DECLARATION
//#region

var map = null;
var mapType = google.maps.MapTypeId.ROADMAP;
var point = null;
var domain = null;
var infoString = '';
var infoTitle = '';
var mapZoom = null;
var tempIcon = null;
var otherIcon = null;
var meIcon = null;
var customIcon = null;
var infowindow = null;
var markers = new Array();
var data = new Array();
var hasDirections = false;
var hasCustomMarker = false;
var geocoder;
var markerCounter = 0;
var directionRenders = null;
var directionsService = new google.maps.DirectionsService();

google.maps.Map.prototype.clearDirections = function () {
    if (directionRenders != null) {
        directionRenders.setMap(null);
        directionRenders = null;
        $("#googleDirections").html("");
        $("#googleDirectionsManager").hide();
        $("#googleDirections").hide();
        hasDirections = false;
        $('a[id^="ucsGoogleMapControl_rptDirectionLinks_hlDirectionLink_"]').css({ 'color': '#04A2FF', 'background': 'url(https://www.giannoulishotels.com/Images/route.png) no-repeat left top' });
        try {
            var mainContainerHeight = document.getElementById("mainContainer").clientHeight + 150; //Known margin and paddings
            FB.Canvas.setSize({ width: 520, height: mainContainerHeight });
        } catch (e) {}
    }
};

google.maps.Map.prototype.reset = function () {
    this.setCenter(point);
    this.setZoom(mapZoom);
};

//#endregion

//SIMPLE MAP INITIALIZATION
//#region

function LoadMap() {
    tempIcon = new google.maps.MarkerImage('https://www.giannoulishotels.com/Images/HotelMarker.png', new google.maps.Size(34, 40), new google.maps.Point(0, 0), new google.maps.Point(17, 40));
    
    var options = {
        center: point,
        mapTypeId: mapType,
        zoom: mapZoom,
        disableDefaultUI: false,
        disableDoubleClickZoom: true,
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
        },
        streetViewControl: true
    };
    map = new google.maps.Map(document.getElementById('map'), options);

    infowindow = new google.maps.InfoWindow({
        content: infoString
    });

    marker = new google.maps.Marker({
        position: point,
        map: map,
        title: infoTitle,
        icon: tempIcon
    });

    google.maps.event.addListener(map, 'dblclick', function () {
        map.panTo(point);
    });

    google.maps.event.addListener(marker, 'mouseover', function () {
        infowindow.open(map, marker);
    });
    google.maps.event.addListener(marker, 'mouseout', function () {
        infowindow.close();
    });
}

//#endregion

//DIRECTIONS MAP INITIALIZATION
//#region

function LoadDirectionsMap() {
    geocoder = new google.maps.Geocoder();
    tempIcon = new google.maps.MarkerImage('https://www.giannoulishotels.com/Images/HotelMarker.png', new google.maps.Size(34, 40), new google.maps.Point(0, 0), new google.maps.Point(17, 40));
    otherIcon = new google.maps.MarkerImage('https://www.giannoulishotels.com/Images/OtherHotelMarker.png', new google.maps.Size(17, 21), new google.maps.Point(0, 0), new google.maps.Point(8, 21));
    meIcon = new google.maps.MarkerImage('../../Images/me.png', new google.maps.Size(34, 40), new google.maps.Point(0, 0), new google.maps.Point(17, 40));

    point = new google.maps.LatLng(data[0].latitude, data[0].longitude);
    var options = {
        center: point,
        mapTypeId: mapType,
        zoom: mapZoom,
        disableDefaultUI: false,
        disableDoubleClickZoom: true,
        mapTypeControlOptions: {
            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
        },
        streetViewControl: true
    };
    map = new google.maps.Map(document.getElementById('map'), options);

    var tempPoint = new google.maps.LatLng(data[0].latitude, data[0].longitude);

    marker = new google.maps.Marker({
        position: tempPoint,
        map: map,
        title: data[0].info,
        icon: tempIcon,
        zIndex: 0
    });
    markers.push(marker);

    for (markerCounter = 1; markerCounter < data.length; markerCounter++) {
       
        var tempPoint = new google.maps.LatLng(data[markerCounter].latitude, data[markerCounter].longitude);
        
        marker = new google.maps.Marker({
            position: tempPoint,
            map: map,
            title: data[markerCounter].info,
            icon: otherIcon,
            zIndex: markerCounter
        });

        google.maps.event.addListener(marker, 'click', function () {
            GetDirections(this.zIndex);
        });

        google.maps.event.addListener(marker, 'mouseover', function () {
            if (hasDirections == false) {
                var thisLink = "#ucsGoogleMapControl_rptDirectionLinks_hlDirectionLink_" + (this.zIndex - 1);
                $(thisLink).css({ 'color': '#225391', 'background': 'url(https://www.giannoulishotels.com/Images/route.png) no-repeat left -17px' });
            }
        });

        google.maps.event.addListener(marker, 'mouseout', function () {
            if (hasDirections == false) {
                var thisLink = "#ucsGoogleMapControl_rptDirectionLinks_hlDirectionLink_" + (this.zIndex - 1);
                $(thisLink).css({ 'color': '#04A2FF', 'background': 'url(https://www.giannoulishotels.com/Images/route.png) no-repeat left top' });
            }
        });

        markers.push(marker);
    }

}

function GetGeoCode(index) {
    var geoPos =  markers[index].getPosition();
    geocoder.geocode({ 'latLng': geoPos }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[1]) {
                map.panTo(geoPos);
                var tempTitle = results[1].formatted_address;

                infowindow = new google.maps.InfoWindow({
                    content: tempTitle
                });

                infowindow.open(map, markers[index]);
                markers[index].setTitle(tempTitle);
            }
        }
    });
}


function AddCustomMarker() {

    if (hasCustomMarker) {
        var tempCenter = markers[markers.length - 1].getPosition();
        map.panTo(tempCenter);        

    } else {
        customIcon = new google.maps.MarkerImage('https://www.giannoulishotels.com/Images/customMarker.png', new google.maps.Size(17, 21), new google.maps.Point(0, 0), new google.maps.Point(8, 21));

        var currentCenter = map.getCenter();

        marker = new google.maps.Marker({
            position: currentCenter,
            map: map,
            draggable: true,
            icon: customIcon,
            zIndex: markers.length
        });

        google.maps.event.addListener(marker, 'click', function () {
            try {
                infowindow.close();
            } catch (e) {}
            GetDirections(this.zIndex);
        });

        google.maps.event.addListener(marker, 'dragend', function () {
            GetGeoCode(this.zIndex);
        });

        google.maps.event.addListener(marker, 'dragstart', function () {
            try {
                map.clearDirections();
                infowindow.close();
            } catch (e) { }
        });

        markers.push(marker);
        hasCustomMarker = true;

        GetGeoCode(marker.zIndex);
    }
}

function GetDirections(index) {
    
    map.clearDirections();
    try {
        infowindow.close();
    } catch (e) { }
    
    hasDirections = true;
    var request = {
        origin: markers[index].getPosition(),
        destination: markers[0].getPosition(),
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

    directionRenders = new google.maps.DirectionsRenderer({ suppressMarkers: true });
    directionRenders.setMap(map);
    directionRenders.setPanel(document.getElementById('googleDirections'))


    google.maps.event.addListener(directionRenders, 'directions_changed', function () {
        try {
            var mainContainerHeight = document.getElementById("mainContainer").clientHeight + 150; //Known margin and paddings
            FB.Canvas.setSize({ width: 520, height: mainContainerHeight });
        } catch (e) { }
    });

    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionRenders.setDirections(response);
        }
    });

    var thisLink = "#ucsGoogleMapControl_rptDirectionLinks_hlDirectionLink_" + (index - 1);
    $(thisLink).css({ 'color': '#225391', 'background': 'url(https://www.giannoulishotels.com/Images/route.png) no-repeat left -17px' });
    $("#googleDirectionsManager").show();
    $("#googleDirections").show();

}

//#endregion


