﻿window.addEvent('domready', function() { s_GMap.setup(); });
window.addEvent('unload', function() { GUnload(); });

var s_GMap = {
    setup: function() {

        $('gmapLink').addClass('hidden');
        $('gmapLink').addEvent('click', function(e) {

            $('gmap').removeClass('hidden');
            e = new Event(e);
            e.stop();
            Page.MainGallery.hide();
            if ($('flashintro') != null)
                $('flashintro').addClass('hidden');

            if (GBrowserIsCompatible()) {
                if (s_GMap.map == null) {
                    s_GMap.map = new GMap2(document.getElementById("gmap"));
                    s_GMap.map.setCenter(new GLatLng(1, 1), 1);
                    s_GMap.map.addMapType(G_PHYSICAL_MAP)


                    s_GMap.tooltip = new Element('div', { 'class': 'tooltip' });
                    s_GMap.map.getPane(G_MAP_FLOAT_PANE).appendChild(s_GMap.tooltip);
                    s_GMap.tooltip.setOpacity(0);


                    var req = new Request({ url: 'dest/', onComplete: function(i) { s_GMap.SetupPoints(i); } }).post();

                    var mapNav = new Element('div', { 'id': 'gmapnav', 'class': 'hidden' });
                    mapNav.inject($('gmap'));

                    s_GMap.locInfo = new Element('div', { 'class': 'mapinfo' });
                    s_GMap.locInfo.inject($('gmapnav'));

                    var inner = new Element('div', { 'class': 'map_content' });
                    inner.inject(s_GMap.locInfo);

                    s_GMap.collapse = new Element('div', { 'class': 'collapse' });
                    s_GMap.collapse.inject(s_GMap.locInfo);
                    s_GMap.collapse.addEvent('click', function(e) { e = new Event(e); e.stop(); s_GMap.ToggleInfo(); });
                }
                else {
                    s_GMap.PlotPoints();
                }
            }


        });
    },
    ToggleInfo: function() {
        var myEffect = new Fx.Morph($$('#gmapnav .mapinfo')[0], { duration: 1000, transition: Fx.Transitions.Sine.easeOut, onStart: function() { s_GMap.map.removeControl(s_GMap.mapControl); }, onComplete: function() { s_GMap.ToggleComplete(); } });
        if ($('gmapnav').hasClass('collapsed')) {
            myEffect.start({ 'right': '0px' });
        }
        else {

            myEffect.start({ 'right': '-196px' });
        }
    },
    ToggleComplete: function() {
        $('gmapnav').toggleClass('collapsed');
        if ($('gmapnav').hasClass('collapsed')) {
            s_GMap.map.removeControl(s_GMap.mapControl);
            s_GMap.mapControl = new GMenuMapTypeControl();
            s_GMap.map.addControl(s_GMap.mapControl);
            if ($('menumtctl_main') != null) {
                $('menumtctl_main').setStyle('z-index', '900');
            }
        }
        else {
            s_GMap.map.removeControl(s_GMap.mapControl);
        }
    },
    SetupMarkers: function() {
        s_GMap.tinyIcon = new google.maps.Icon();
        var tinyIcon = s_GMap.tinyIcon;
        tinyIcon.image = "http://seasonsnew.strattons.com/images/map_pointer.png";
        tinyIcon.shadow = "http://seasonsnew.strattons.com/images/map_shadow.png";
        tinyIcon.iconSize = new google.maps.Size(12, 20);
        tinyIcon.shadowSize = new google.maps.Size(22, 20);
        tinyIcon.iconAnchor = new google.maps.Point(6, 20);
        tinyIcon.tooltipAnchor = new google.maps.Point(4, 8);
        tinyIcon.infoWindowAnchor = new google.maps.Point(5, 1);


        s_GMap.markerOptions = { icon: tinyIcon };
    },
    SetupPoints: function(r) {
        // need to clear any existing points first
        var req = r.split('|');
        var num = req[0] * 1;

        s_GMap.DestPoints = [];

        s_GMap.SetupMarkers();

        for (var i = 1; i <= num; i++) {
            var items = req[i].split(',')
            s_GMap.DestPoints[i] = items;

        }

        s_GMap.PlotPoints();
    }
    ,
    PlotPoints: function() {
        s_GMap.map.clearOverlays();
        s_GMap.mapControl = new GMenuMapTypeControl();
        s_GMap.map.addControl(s_GMap.mapControl);
        $('gmapnav').addClass('hidden');

        var bounds = new google.maps.LatLngBounds;

        for (var i = 1; i < s_GMap.DestPoints.length; i++) {
            var items = s_GMap.DestPoints[i];
            bounds.extend(s_GMap.AddMarker(items[4] * 1, items[3] * 1, i, 'dest'));
        }
        s_GMap.map.setCenter(new GLatLng(0, 0));
        s_GMap.map.setZoom(s_GMap.map.getBoundsZoomLevel(bounds));

    }
    ,
    AddMarker: function(lat, lng, i, type) {
        var ll = new google.maps.LatLng(lat, lng);
        var GM = new google.maps.Marker(ll, s_GMap.markerOptions);
        GM.seasons_id = i;
        if (type == 'dest') {
            GM.tooltip = unescape(s_GMap.DestPoints[GM.seasons_id][5]);
            GEvent.addListener(GM, "click", function(o) {

                s_GMap.map.clearOverlays();
                s_GMap.tooltip.setStyle('visibility', 'hidden');
                s_GMap.tooltip.fade(0);
                s_GMap.DestLat = s_GMap.DestPoints[GM.seasons_id][0] * 1;
                s_GMap.DestLng = s_GMap.DestPoints[GM.seasons_id][1] * 1;
                s_GMap.DestZoom = s_GMap.DestPoints[GM.seasons_id][2] * 1;

                s_GMap.map.setCenter(new GLatLng(s_GMap.DestLng, s_GMap.DestLat), s_GMap.DestZoom);
                $('gmapnav').removeClass('hidden');
                $$('#gmapnav .map_content')[0].set('html', '<h1>' + unescape(s_GMap.DestPoints[GM.seasons_id][5]) + '</h1>');
                var post = s_GMap.DestPoints[GM.seasons_id][6];
                var req = new Request({ url: 'dest/', onComplete: function(i) { s_GMap.SetupDestination(i); } }).post({ 'dest': post });
            });
        } else if (type == 'loc') {
            GM.tooltip = unescape(s_GMap.LocPoints[GM.seasons_id][5]);
            GEvent.addListener(GM, "click", function(o) {

                s_GMap.map.clearOverlays();
                s_GMap.tooltip.setOpacity(0);
                var lat = s_GMap.LocPoints[GM.seasons_id][0] * 1;
                var lng = s_GMap.LocPoints[GM.seasons_id][1] * 1;
                var zoom = s_GMap.LocPoints[GM.seasons_id][2] * 1;

                s_GMap.map.setCenter(new GLatLng(lng, lat), zoom);
                $('gmapnav').removeClass('hidden');
                $$('#gmapnav .map_content')[0].set('html', '<h1>' + unescape(s_GMap.LocPoints[GM.seasons_id][5]) + '</h1>');
                var post = s_GMap.LocPoints[GM.seasons_id][6];
                var req = new Request({ url: 'loc/', onComplete: function(i) { s_GMap.SetupLocation(i); } }).post({ 'loc': post });
            });
        } else if (type == 'hot') {

            GM.tooltip = unescape(s_GMap.HotPoints[GM.seasons_id][2]);
            GEvent.addListener(GM, "click", function(o) {
                location.href = unescape(s_GMap.HotPoints[GM.seasons_id][3]);
            });
        }

        google.maps.Event.addListener(GM, 'mouseover', function() { s_GMap.showTooltip(this) });
        google.maps.Event.addListener(GM, 'mouseout', function() { s_GMap.tooltip.fade(0); });
        s_GMap.map.addOverlay(GM);
        return ll;
    },
    SetupDestination: function(r) {
        var req = r.split('|');
        s_GMap.DestText = unescape(req[0]);
        var num = req[1] * 1;

        s_GMap.LocPoints = [];

        for (var i = 1; i <= num; i++) {
            var items = req[i + 1].split(',')
            s_GMap.LocPoints[i] = items;

        }



        s_GMap.PlotLocations();
    },
    SetupLocation: function(r) {

        var req = r.split('|');
        var num = req[1] * 1;



        s_GMap.HotPoints = [];

        for (var i = 1; i <= num; i++) {
            var items = req[i + 1].split(',')
            s_GMap.HotPoints[i] = items;
        }

        $$('#gmapnav .map_content')[0].set('html', unescape(req[0]));
        $$('.returnMap').addEvent('click', function(e) { e = new Event(e); e.stop(); s_GMap.map.setCenter(new GLatLng(s_GMap.DestLng, s_GMap.DestLat), s_GMap.DestZoom); s_GMap.PlotLocations(); });

        s_GMap.PlotHotels();
    },
    PlotLocations: function() {
        s_GMap.map.clearOverlays();
        s_GMap.map.removeControl(s_GMap.mapControl);
        s_GMap.tooltip.setOpacity(0);
        $$('#gmapnav .map_content')[0].set('html', s_GMap.DestText);
        $$('.returnMap').addEvent('click', function(e) { e = new Event(e); e.stop(); s_GMap.PlotPoints(); });

        for (var i = 1; i < s_GMap.LocPoints.length; i++) {
            var items = s_GMap.LocPoints[i];
            s_GMap.AddMarker(items[4] * 1, items[3] * 1, i, 'loc');
        }

    },
    PlotHotels: function() {
        s_GMap.map.clearOverlays();
        s_GMap.tooltip.setOpacity(0);
        for (var i = 1; i < s_GMap.HotPoints.length; i++) {
            var items = s_GMap.HotPoints[i];
            s_GMap.AddMarker(items[1] * 1, items[0] * 1, i, 'hot');
        }
    },
    showTooltip: function(marker) {
        s_GMap.tooltip.set('html', marker.tooltip);
        var top = $$('#gmap div div')[0];
        top.setStyle('z-index', '');
        s_GMap.tooltip.getParent().setStyle('z-index', '507');
        s_GMap.tooltip.setStyle('width', 'auto');
        s_GMap.tooltip.setStyle('white-space', 'nowrap');
        var size = s_GMap.tooltip.getSize();

        s_GMap.tooltip.setStyle('width', size.x > 100 ? '100px' : 'auto');
        s_GMap.tooltip.setStyle('white-space', size.x > 100 ? 'normal' : 'nowrap');

        var point = s_GMap.map.getCurrentMapType().getProjection().fromLatLngToPixel(s_GMap.map.fromDivPixelToLatLng(new GPoint(0, 0), true), s_GMap.map.getZoom());
        var offset = s_GMap.map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(), s_GMap.map.getZoom());

        var anchor = marker.getIcon().tooltipAnchor;

        var width = marker.getIcon().iconSize.width;
        var height = s_GMap.tooltip.clientHeight;

        var x = offset.x - point.x - anchor.x + width;
        var y = offset.y - point.y - anchor.y - height;

        var g = new GPoint(x, y);

        var cWidth = s_GMap.map.fromLatLngToContainerPixel(s_GMap.map.fromDivPixelToLatLng(g)).x;

        if ($('gmap').getSize().x < (cWidth + s_GMap.tooltip.getSize().x))
            x = x - s_GMap.tooltip.getSize().x - width - 5;

        var pos = new google.maps.ControlPosition(G_ANCHOR_TOP_LEFT, new GSize(x, y));
        pos.apply(s_GMap.tooltip);

        s_GMap.tooltip.fade(0.8);
    }

}