﻿//////////////  VERSION 2   ///////////////////////
// JScript File
var map = null;
var mapSupplements = null;
var allmarkers = [];
var markerBounds = null;
//We use this to know if the map should be centered - If we switch the first time to the Map tab than do it. We 
var zoomToCenter = 2;

var defSuppLat;
var defSuppLong
var defLat;
var defLong


function loadMaps() {
    //google.load("maps", "2", { "callback": mapsLoaded });
}

function CallbackMap(HotelPkey) {
    var mapDivID = document.getElementById('MapComponentDivClientID').value;
    var dropdownIndex = document.getElementById('RadiusDDL').selectedIndex;
    var RadiusKm = document.getElementById('RadiusDDL')[dropdownIndex].value;

    TransitionForCallback(mapDivID, 'in', 'map');

    var strUrlParams = 'cb=CallbackMap&h=' + HotelPkey + '&mapDivID=' + mapDivID + '&RadiusKm=' + RadiusKm;

    Ajax_Send_W(strUrlParams, afterCallbackMap);

}

function afterCallbackMap() {
    var str = Ajax_Receive();

    if (str != null && str != "") {
        var Res = str.split('&|&|&');
        if (Res.length >= 0) {
            // alert('Res[0] - ' + Res[0]);
            // alert('Res[1] - ' + Res[1]);

            //Res[0],Res[1] - Map
            //Res[2] - JS for repopilating the map with markers
            //Res[3] - Radius


            document.getElementById(Res[0]).innerHTML = Res[1];
            document.getElementById('RadiusDDL').selectedValue = Res[3];

            TransitionForCallback(Res[0], 'out', 'map');

            map.clearOverlays();
            //testMap();
            AddAllMarkers();
            //                    InitilizeMapOnCallback();
            ResetMapToDefaulHotel(true);
            //ResetMapToDefaulHotel(false);
        }
    }

}

function LoadMapHotelPage() 
{
    //alert('Load');
    if (document.getElementById('divHotelMap')) {
        if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("divHotelMap"));
            map.setUIToDefault();
            //map.setZoom(15);
            GLOBALMAP = map;
        }
    }
}

function LoadMapSupplementsPage() 
{
    if (GBrowserIsCompatible()) {
        mapSupplements = new GMap2(document.getElementById("divSupplementMap"));
        mapSupplements.setUIToDefault();
       // GLOBALMAP = mapSupplements;
    }
}


function AddMarkerToSupplementsMap(theTitle, lat, lng, icon, html, isDefault) {
    //alert('Add Marker');
    if (GBrowserIsCompatible()) {
        var tinyIcon = new GIcon();
        tinyIcon.image = icon;
        tinyIcon.iconSize = new GSize(30, 30);
        tinyIcon.shadowSize = new GSize(30, 30);
        tinyIcon.iconAnchor = new GPoint(30, 30);
        tinyIcon.infoWindowAnchor = new GPoint(5, 1);

        markerOptions = { icon: tinyIcon, id: theTitle };

        var point = new GLatLng(parseFloat(lat), parseFloat(lng));
        var marker = new GMarker(point, markerOptions);

        marker.id = theTitle;

        GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });

        mapSupplements.addOverlay(marker);

        if (isDefault == 'true') {
            defSuppLat = lat;
            defSuppLong = lng;
            mapSupplements.setCenter(lat, lng, 13);
        }
    }
}

function ResetMapToDefaultSupplement() 
{
    var point = new GLatLng(parseFloat(defSuppLat), parseFloat(defSuppLong));    
    mapSupplements.setCenter(point, 10);
    mapSupplements.checkResize();
    mapSupplements.checkResize();
    mapSupplements.setCenter(point, 10);
}

function GoToMarker(markerTitle, html) {
    //alert(allmarkers.length);

    for (x in allmarkers) {
        var title = allmarkers[x].id;
        if (title == markerTitle) {
            var point = allmarkers[x].getLatLng();
            map.panTo(point, 10);
            allmarkers[x].openInfoWindow(html);            
        }
//        if (x > 0 && x < 5) {
//            var point = allmarkers[x].getLatLng();
//            var title = allmarkers[x].title;
//            alert(title);
//            //alert(x + " - " + point);
//        }
    }
}


function ChangeZoomToShowAllMarkers() 
{
    var point = new GLatLng(parseFloat(defLat), parseFloat(defLong));
    
    if (allmarkers.length > 1) {
        markerBounds = new GLatLngBounds();
                
        for (var i = 0; i < allmarkers.length; i++) {
            markerBounds.extend(allmarkers[i].getLatLng());
        }

        //map.setCenter(markerBounds.getCenter(), map.getBoundsZoomLevel(markerBounds) + 1);
        
        map.setCenter(point, map.getBoundsZoomLevel(markerBounds) + 1);
    }
    else
        map.setCenter(point, 13);
}
function ResetMapToDefaulHotel(forceReset) {
    if (forceReset == true || zoomToCenter == 2 || zoomToCenter == 1) 
    {
        var point = new GLatLng(parseFloat(defLat), parseFloat(defLong));
        map.setCenter(point, 13);
        map.checkResize();
        ChangeZoomToShowAllMarkers();
        zoomToCenter--;
    }
}



function AddAllMarkers() 
{
    var theHtml = document.getElementById('HiddenMarkersJS').value;
    //alert(theHtml);
    eval(theHtml);
}

function AddMarkerToMap(theTitle, lat,lng,icon ,html, isDefault) 
{
    //alert('Add Marker');
   if (GBrowserIsCompatible()) 
   {
        var tinyIcon = new GIcon();
        tinyIcon.image =  icon;
        tinyIcon.iconSize = new GSize(30, 30);
        tinyIcon.shadowSize = new GSize(30, 30);
        tinyIcon.iconAnchor = new GPoint(30, 30);
        tinyIcon.infoWindowAnchor = new GPoint(5, 1);

        //var theTitle = 'some title - ' + html;
        // Set up our GMarkerOptions object literal
        markerOptions = { icon:tinyIcon, id:theTitle };       
        
        var point = new GLatLng(parseFloat(lat), parseFloat(lng));
        var marker = new GMarker(point, markerOptions);
        
        //marker.tooltip = new Tooltip(marker, 'aaa', 5);

        marker.id = theTitle;
               
        GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
        //GEvent.addListener(marker, 'mouseover', function() { this.tooltip.show(); });
        //GEvent.addListener(marker, 'mouseout', function() { this.tooltip.hide(); });

        map.addOverlay(marker);
        //map.addOverlay(marker.tooltip);

        
        allmarkers.push(marker);
        
        //Used for centering the map - Center only for the specified marker        
        if (isDefault == 'true') 
        {
            defLat = lat;
            defLong = lng;          
        }            
    }

}



/**
* @author Marco Alionso Ramirez, marco@onemarco.com
* @url http://onemarco.com
* @version 1.0
* This code is public domain
*/

/**
* The Tooltip class is an addon designed for the Google Maps GMarker class. 
* @constructor
* @param {GMarker} marker
* @param {String} text
* @param {Number} padding
*/
function Tooltip(marker, text, padding) {
    this.marker_ = marker;
    this.text_ = text;
    this.padding_ = padding;
}

Tooltip.prototype = new GOverlay();

Tooltip.prototype.initialize = function(map) {
    var div = document.createElement("div");
    div.appendChild(document.createTextNode(this.text_));
    div.className = 'tooltip';
    div.style.position = 'absolute';
    div.style.visibility = 'hidden';
    map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
    this.map_ = map;
    this.div_ = div;
}

Tooltip.prototype.remove = function() {
    this.div_.parentNode.removeChild(this.div_);
}

Tooltip.prototype.copy = function() {
    return new Tooltip(this.marker_, this.text_, this.padding_);
}

Tooltip.prototype.redraw = function(force) {
    if (!force) return;
    var markerPos = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
    var iconAnchor = this.marker_.getIcon().iconAnchor;
    var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
    var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
    this.div_.style.top = yPos + 'px';
    this.div_.style.left = xPos + 'px';
}

Tooltip.prototype.show = function() {
    this.div_.style.visibility = 'visible';
}

Tooltip.prototype.hide = function() {
    this.div_.style.visibility = 'hidden';
}
