﻿/* jquery plugin to load google map */
(function (jQuery) {
    /* default options for google map */
    var defaultMapOptions = {
        zoom: 2,
        panControl: false,
        mapTypeControl: false,
        streetViewControl: false,
        zoomControl: true,
        center: new google.maps.LatLng(32.249974, -4.921875),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    /* default plugin options */
    var defaultPluginOptions = {
        "countryId": 0,
        "destinationId": 0,
        "loadObjects": true
    }

    /* instance of google maps */
    var _map = null;

    /* indexes of respective entities in marker managers array */
    var _COMPANY = 1;
    var _ARTICLE = 0;
    var _DESTINATION = 2;
    var _VIDEO = 3;

    /* array of marker managers */
    var _managers = null;

    /* plugin entry point ----------------------------------------------- */
    jQuery.fn.RtMap = function (mapOptions, pluginOptions) {
        /* merge default and custom options */
        var options = $.extend(defaultMapOptions, mapOptions);
        var optionsPlugin = $.extend(defaultPluginOptions, pluginOptions);

        this.each(function () {
            /* create map */
            _map = new google.maps.Map(this, options);

            // if plugin should load all entities to layers
            if (optionsPlugin.loadObjects) {
                var qs = "";
                if (optionsPlugin.countryId != null)
                    qs += "?country={0}".format(parseInt(optionsPlugin.countryId));
                if (optionsPlugin.destinationId != null)
                    qs += "&destination={0}".format(parseInt(optionsPlugin.destinationId));

                /* set markers */
                $.ajax({
                    "url": "/Ajax/MapPositions" + qs,
                    "success": function (data) {
                        var markers = [];
                        markers[_COMPANY] = [];
                        markers[_ARTICLE] = [];
                        markers[_DESTINATION] = [];
                        markers[_VIDEO] = [];

                        /* add markers to respective markers collection */
                        for (var i = 0; i < data.length; i++) {
                            markers[data[i].Type].push(AddMarker(data[i].Type, data[i].Title, data[i].Coords, data[i].Url));
                        }

                        /* init managers */
                        _managers = [];
                        for (var i = 0; i < markers.length; i++) {
                            _managers[i] = new MarkerManager(_map);

                            /* bind load event to manager */
                            google.maps.event.addListener(_managers[i], 'loaded', function () {
                                var idx = _managers.indexOf(this);
                                this.addMarkers(markers[idx], 1);
                                this.refresh();
                            });
                        }
                    }
                });

                // add panoramio
                var panoramioLayer = new google.maps.panoramio.PanoramioLayer();
                panoramioLayer.setMap(_map);
            }
        });
    };

    /* return single marker of certain type with onclick event hooked on it */
    function AddMarker(type, title, position, url) {
        /* lat, lon position of marker */
        var posn = position.split(",");

        /* create icon for marker */
        var icon = new google.maps.MarkerImage();
        icon.size = new google.maps.Size(21, 21);
        icon.anchor = new google.maps.Point(0, 0);
        icon.url = "{0}images/mapicon{1}.gif".format(RTweb.ContentPath, type);

        /* create options for marker */
        var markerOptions = {
            icon: icon,
            position: new google.maps.LatLng(posn[0], posn[1]),
            title: title,
            shadow: ""
        };

        /* create marker */
        var marker = new google.maps.Marker(markerOptions);
        /* add url to marker */
        marker.url = url;
        /* register onclick event on marker */
        google.maps.event.addListener(marker, 'click', OnMarkerClick);

        return marker;
    };

    /* when marker is clicked */
    function OnMarkerClick() {
        var type;
        if (this.url.length > 4) {
            switch (this.url.substr(0, 4)) {
                case "vid:":
                    type = "video";
                    break;
                default:
                    type = "url";
                    break;
            }
        }

        switch (type) {
            /* standard url */ 
            case "url":
                location.href = this.url;
                break;
            /* play video */ 
            case "video":
                var options = $.extend(RTweb.colorboxVideoOptions, { "href": "http://www.youtube.com/embed/" + this.url.substr(4) });
                $.colorbox(options);
                break;
        }
    };

    /* showing and hiding layers */
    jQuery.fn.RtMap.HideLayer = function (idx) {
        _managers[idx].hide();
    };
    jQuery.fn.RtMap.ShowLayer = function (idx) {
        _managers[idx].show();
    };

    /* adds single marker to map */
    jQuery.fn.RtMap.AddSimpleMarker = function (coords) {
        var marker = new google.maps.Marker({
            position: coords,
            map: _map
        });
    }

    /* centers map on specified coordinatas */
    jQuery.fn.RtMap.Center = function (latlng, zoom) {
        _map.setCenter(latlng);
        _map.setZoom(zoom);
    };
})(jQuery);
