/*global pmmi, jQuery, google */
Object.setToDotRef("pmmi.GMap", function ($) {
    var defaultCfg;

    /**
     * @constructor
     */
    var GMap = function (cfg) {
        this.cfg = cfg;
        this.map = null;
        this.markers = [];
        this.initAPI();
    };

    GMap.prototype = {
        /**
         * @private
         */
        init: function () {
            GMap.prototype.init = (this.init = $.noop);
            defaultCfg = {
                canvas: "#map",
                options: {
                    zoom: 8,
                    center: new google.maps.LatLng(47.15, 19.5),
                    mapTypeId: google.maps.MapTypeId.ROADMAP // TERRAIN
                },
                pins: []
            };
        },

        initCfg: function (cfg) {
            this.cfg = $.extend(true, {}, defaultCfg, cfg);
            this.cfg.options = $.extend({}, defaultCfg.options, cfg.options);
        },

        /**
         * @private
         */
        initAPI: function () {
            this.init();
            this.initCfg(this.cfg);

            var i, pins = this.cfg.pins, opts = this.cfg.options, canvas = $(this.cfg.canvas)[0];

            opts.center = new google.maps.LatLng(opts.lat, opts.lng);
            delete opts.lat;
            delete opts.lng;

            this.map = new google.maps.Map(canvas, this.cfg.options);

            for (i = 0; i < pins.length; i++) {
                this.addPin.apply(this, pins[i]);
            }
        },

        /**
         * @private
         */
        addPin: function (lat, lng, title) {
            this.markers.push(new google.maps.Marker({
                position: new google.maps.LatLng(lat, lng),
                title: title,
                map: this.map
            }));
        }
    };

    return GMap;
}(jQuery));

