﻿/// <reference path="TranceSoft.js" />
/// <reference path="../jquery-1.4.2-vsdoc.js" />
/// <reference path="../jquery.tools.min.js" />
/// <reference path="../MicrosoftAjax.debug.js" />
/// <reference path="../MicrosoftMvcAjax.debug.js" />
/// <reference path="../MicrosoftMvcValidation.debug.js" />
/// <reference path="http://maps.google.pl/maps/api/js?sensor=false&language=pl" />

if (typeof TranceSoft == 'undefined')
{
    TranceSoft = function () { }
}
if (!TranceSoft.Google)
{
    TranceSoft.Google = function () { } 
}
TranceSoft.Google.Maps = function (config)
{
    Context = {
        map: null,
        directionSvc: null,
        mapDrawer: null,
        lastDirs: null,
        addressMarker: null,
        fromID: null,
        locale: null
    };

    Config = {
        ContainerID: "map",
        DirectionsID: "directions",
        LinkDirectionsId: "menu-dirs",
        LinkRouteId: "menu-route",
        LinkCentreId: "menu-centre",
        PanelErrorId: "panel-error",
        DefaultTarget: "Poznań",
        DefaultLocale: "pl_PL",
        DefaultPosition: null,
        CurrentLocale: null,
        MarkerTitle: "Tu jesteśmy"
    };

    Messages =
    {
        ErrorLocation: "Serwis Google Maps nie był w stanie odnaleźć podanej lokalizacji lub wytyczyć poprawnej drogi. Proszę zmodyfikować podany adres i spróbować ponownie.",
        Exception: "Wystąpił nieoczekiwany wyjątek podczas pobierania danych z serwisu Google Maps. Proszę spróbować ponownie."
    };

    function Init()
    {
        setTimeout(function ()
        {
            ShowStartMap();
            Context.fromID = "txtFrom";
            Context.locale = Config.DefaultLocale;
            Context.directionSvc = new google.maps.DirectionsService();
            Context.mapDrawer = new google.maps.DirectionsRenderer();
            Context.mapDrawer.setMap(Context.map);
            Context.mapDrawer.setPanel(document.getElementById(Config.DirectionsID));

            google.maps.event.addListener(Context.mapDrawer, 'directions_changed', function ()
            {
                //alert("Direction changed");
            });

            $('#' + Config.LinkRouteId).bind('click', '', ShowRoute);
            $('#' + Config.LinkCentreId).bind('click', '', CantreMap);
        }, 10);
    };

    function CantreMap()
    {
        Context.map.setCenter(Config.DefaultPosition);
    }

    function ShowStartMap()
    {
        function PlaceMarker(map)
        {
            var marker = new google.maps.Marker({
                position: map.getCenter(),
                map: map,
                title: config.MarkerTitle
            });
        }

        Config.DefaultPosition = new google.maps.LatLng(config.Longitude, config.Latitude);

        var mapOptions =
        {
            zoom: 14,
            center: Config.DefaultPosition,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: false,
            navigationControl: true,
            navigationControlOptions:
            {
                style: google.maps.NavigationControlStyle.SMALL,
                position: google.maps.ControlPosition.TOP_RIGHT
            },
            scaleControl: true,
            scaleControlOptions:
            {
                position: google.maps.ControlPosition.TOP_LEFT
            }
        };
        Context.map = new google.maps.Map(document.getElementById(Config.ContainerID), mapOptions);
        PlaceMarker(Context.map);
    }

    function ShowRoute()
    {
        var from = document.getElementById(Context.fromID);
        if (from && from.value)
        {
            //$('#' + Config.DirectionsID).hide();
            HideDirections();
            //rocl$('#' + Config.LinkDirectionsId).hide();

            setTimeout(function ()
            {
                var request = {
                    origin: from.value,
                    destination: Config.DefaultTarget,
                    travelMode: google.maps.DirectionsTravelMode.DRIVING,
                    region: 'pl'
                };
                Context.directionSvc.route(request, NewRouteLoaded);

                $("#" + Config.LinkDirectionsId).click(ShowDirections);
            }, 1);
        }
    };

    function ShowDirections()
    {
        var dir = $('#' + Config.DirectionsID);
        dir.css("height", 'auto');
        var clientHeight = dir.height();

        dir.css("height", '0px');
        dir.css("opacity", "0");
        dir.show();
        dir.animate
        ({
            opacity: 1,
            height: clientHeight + 10
        }, 500, '', function ()
        {
            $("#" + Config.LinkDirectionsId).
						unbind("click", ShowDirections).
						bind("click", HideDirections).text('Ukryj wskazówki');
        });
    }

    function HideDirections()
    {
        var dir = $('#' + Config.DirectionsID);
        dir.animate({ opacity: 0, height: 0 }, 500, '', function ()
        {
            $("#" + Config.LinkDirectionsId).
						unbind("click", HideDirections).
						bind("click", ShowDirections).text('Pokaż wskazówki');
        });
    };

    function HandleErrors(result, status)
    {
        var message = Messages.Exception;
        if (status == google.maps.DirectionsStatus.NOT_FOUND || status == google.maps.DirectionsStatus.ZERO_RESULTS)
        {
            message = Messages.ErrorLocation;
        }

        $('#' + Config.PanelErrorId + ' .message-area').html(message).slideDown(500);
        $("#" + Config.LinkDirectionsId).hide().text('Pokaż wskazówki');

    };

    function NewRouteLoaded(result, status)
    {
        if (status == google.maps.DirectionsStatus.OK)
        {
            Context.mapDrawer.setDirections(result);
            $("#" + Config.LinkDirectionsId).show();
            Context.lastDirs = result;
            setTimeout(ShowDirections, 1);
            $('#' + Config.PanelErrorId + ' .message-area').slideUp(500).html('');
        }
        else
        {
            HandleErrors(result, status);
        }
    };

    $(document).ready(Init);

    if (config)
    {
        Config = config;
    }

    return {
        Version: function () { return "1.2"; }
    }
};
