aboutsummaryrefslogtreecommitdiffstats
path: root/view/theme/diabook/js/jquery.mapquery.core.js
blob: b9dd16c8bf1603d7a7a6b3cee945416654b25ce3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
/* Copyright (c) 2011 by MapQuery Contributors (see AUTHORS for
 * full list of contributors). Published under the MIT license.
 * See https://github.com/mapquery/mapquery/blob/master/LICENSE for the
 * full text of the license. */
(function ($) {
/**
# jquery.mapquery.core.js
The main MapQuery file. It contains the MapQuery constructor, the MapQuery.Map
constructor and the MapQuery.Layer constructor.


### *$('selector')*.`mapQuery([options])`
_version added 0.1_
####**Description**: initialise MapQuery and associate it with
the matched element

**options**  an object of key-value pairs with options for the map. Possible
pairs are:

 * **layers** (array of MapQuery.Layer *or* MapQuery.Layer): Either an array 
 or a single layer that should be added to the map
 * **center** ({position: [x,y], zoom: z(int), box: [llx,lly,urx,ury]}):
 Initially go to a certain location. At least one layer (in the `layers`
 option) needs to be specified.

> Returns: $('selector') (jQuery object)


We can initialise MapQuery without any options, or for instance pass in a layer
object. The mapQuery function returns a jQuery object, to access the mapQuery object retrieve
the 'mapQuery' data object.

     var map = $('#map').mapQuery(); //create an empty map
     var map = $('#map').mapQuery({layers:[{type:'osm'}]); //create a map with osm

     var mq = map.data('mapQuery'); //get the MapQuery object
 */
$.MapQuery = $.MapQuery || {};

/**

---

#MapQuery.Map

The MapQuery.Map object. It is automatically constructed from the options
given in the `mapQuery([options])` constructor. The Map object is refered
to as _map_ in the documentation.
 */
$.MapQuery.Map = function(element, options) {
    var self = this;
    //If there are a maxExtent and a projection other than Spherical Mercator
    //automagically set maxResolution if it is not set
    //TODO smo 20110614: put maxExtent and maxResolution setting in the
    //proper option building routine
    if(options){
    if(!options.maxResolution&&options.maxExtent&&options.projection){
        options.maxResolution = (options.maxExtent[2]-options.maxExtent[0])/256;
    }}
    this.options = $.extend({}, new $.fn.mapQuery.defaults.map(), options);

    this.element = element;
    // TODO vmx 20110609: do proper options building
    // TODO SMO 20110616: make sure that all projection strings are uppercase
    // smo 20110620: you need the exact map options in the overviewmap widget
    // as such we need to preserve them
    this.olMapOptions = $.extend({}, this.options);
    delete this.olMapOptions.layers;
    delete this.olMapOptions.maxExtent;
    delete this.olMapOptions.zoomToMaxExtent;
    delete this.olMapOptions.center;

    //TODO SMO20110630 the maxExtent is in mapprojection, decide whether or
    //not we need to change it to displayProjection
    this.maxExtent = this.options.maxExtent;
    this.olMapOptions.maxExtent = new OpenLayers.Bounds(
    this.maxExtent[0],this.maxExtent[1],this.maxExtent[2],this.maxExtent[3]);


    this.projection = this.options.projection;
    this.displayProjection = this.options.displayProjection;

    OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
    OpenLayers.Util.onImageLoadErrorColor = "transparent";

    // create the OpenLayers Map
    this.olMap = new OpenLayers.Map(this.element[0], this.olMapOptions);

    //OpenLayers doesn't want to return a maxExtent when there is no baselayer
    //set (eg on an empty map, so we create a fake baselayer
    this.olMap.addLayer(new OpenLayers.Layer('fake', {baseLayer: true}));

    // Keep IDs of vector layer for select feature control
    this.vectorLayers = [];
    this.selectFeatureControl = null;
    // Counts up to create unique IDs
    this.idCounter = 0;

    element.data('mapQuery', this);
    this.layersList = {};

    // To bind and trigger jQuery events
    this.events = $({});

    this.handlers = {
        // Triggers the jQuery events, after the OpenLayers events
        // happened without any further processing
        simple: function(data) {
            this.trigger(data.type);
        }
    };

    // MapQuery doesn't bind all OpenLayers events automatically,
    // but just the ones that make sense.
    // Events that are left out intensionally are:
    //  - changebaselayer: MapQuery doesn't have the concept of base layers
    //  - mouseover, mouseout, mousemove: Handle those with jQuery on the
    //        DOM level
    // Some events can be triggered by MapQuery without listening to the
    // OpenLayers events. This only works for events that are triggered
    // by functionality that MapQuery implements in some custom way, e.g.
    // (pre)addlayer, (pre)removelayer, changelayer.
    // TODO vmx 20120309: Proper docs for the events, here's some quickly
    // written info:
    //  - generally spoken, the map events follow the OpeLayer events
    //  - preaddlayer, movestart, move, moveend, zoomend: no additional
    //        argument
    //  - addlayer, preremovelayer, removelayer: layer as additional argument
    //  - changelayer: layer and the property that changed as additional
    //        argument. Possible values for the property are: position (in
    //        the layer stack), opacity, visibility
    //        Currently this event is always fired, even if the property
    //        was only meant to be changed, but wasn't exctually changed.
    //        I.e. that the event is fired even if you call
    //        `layer.visible(true)` although the layer is already visible.
    //        I'm (vmx) not sure if we want to change that :)
    this.olMap.events.on({
        scope: this,
        movestart: this.handlers.simple,
        move: this.handlers.simple,
        moveend: this.handlers.simple,
        zoomend: this.handlers.simple
    });

    // Add layers to the map
    if (this.options.layers!==undefined) {
        this.layers(this.options.layers);
        // You can only go to some location if there were layers added
        if (this.options.center!==undefined) {
            this.center(this.options.center);
        }
    }

    // zoom to the maxExtent of the map if no precise location was specified
    if (this.options.zoomToMaxExtent && this.options.center===undefined) {
        this.olMap.zoomToMaxExtent();
    }
};

$.MapQuery.Map.prototype = {
 /**
###*map*.`layers([options])`
_version added 0.1_
####**Description**: get/set the layers of the map

**options** an object of key-value pairs with options to create one or
more layers

>Returns: [layer] (array of MapQuery.Layer) _or_ false


The `.layers()` method allows us to attach layers to a mapQuery object. It takes
an options object with layer options. To add multiple layers, create an array of
layer options objects. If an options object is given, it will return the
resulting layer(s). We can also use it to retrieve all layers currently attached
to the map.

When adding layers, those are returned. If the creation is cancled by returning
`false` in the `preaddlayer` event, this function returns `false` to
intentionally break the chain instead of hiding errors subtly).


     var osm = map.layers({type:'osm'}); //add an osm layer to the map
     var layers = map.layers(); //get all layers of the map

     */
    layers: function(options) {
        //var o = $.extend({}, options);
        var self = this;
        switch(arguments.length) {
        case 0:
            return this._allLayers();
        case 1:
            if (!$.isArray(options)) {
                return this._addLayer(options);
            }
            else {
                return $.map(options, function(layer) {
                    return self._addLayer(layer);
                }).reverse();
            }
            break;
        default:
            throw('wrong argument number');
        }
    },
    // Returns all layers as an array, sorted by there order in the map. First
    // element in the array is the topmost layer
    _allLayers: function() {
        var layers = [];
        $.each(this.layersList, function(id, layer) {
            var item = [layer.position(), layer];
            layers.push(item);
        });
        var sorted = layers.sort( function compare(a, b) {
            return a[0] - b[0];
        });
        var result = $.map(sorted, function(item) {
            return item[1];
        });
        return result.reverse();
    },
    _addLayer: function(options) {
        var id = this._createId();
        var layer = new $.MapQuery.Layer(this, id, options);
        // NOTE vmx 20120305: Not sure if this is a good idea, or if it would
        //     be better to include `options` with the preaddlayer event
        if (this._triggerReturn('preaddlayer', [layer])===false) {
            return false;
        }
        this.olMap.addLayer(layer.olLayer);

        this.layersList[id] = layer;
        if (layer.isVector) {
            this.vectorLayers.push(id);
        }
        this._updateSelectFeatureControl(this.vectorLayers);

        layer.trigger('addlayer');
        return layer;
    },
    // Creates a new unique ID for a layer
    _createId: function() {
        return 'mapquery_' + this.idCounter++;
    },
    _removeLayer: function(id) {
        var layer = this.layersList[id];
        if (this._triggerReturn('preremovelayer', [layer])===false) {
            return false;
        }

        // remove id from vectorlayer if it is there list
        this.vectorLayers = $.grep(this.vectorLayers, function(elem) {
            return elem != id;
        });
        this._updateSelectFeatureControl(this.vectorLayers);
        this.olMap.removeLayer(layer.olLayer);

        // XXX vmx: shouldn't the layer be destroyed() properly?
        delete this.layersList[id];

        layer.trigger('removelayer');
        return this;
    },
/**
 ###*map*.`center([options])`
_version added 0.1_
####**Description**: get/set the extent, zoom and position of the map

 * **position** the position as [x,y] in displayProjection (default EPSG:4326)
to center the map at
 * **zoom** the zoomlevel as integer to zoom the map to
 * **box** an array with the lower left x, lower left y, upper right x,
upper right y to zoom the map to,
this will take precedent when conflicting with any of the above values
 * **projection** the projection the coordinates are in, default is
the displayProjection

>Returns: {position: [x,y], zoom: z(int), box: [llx,lly,urx,ury]}


The `.center()` method allows us to move to map to a specific zoom level,
specific position or a specific extent. We can specify the projection of the
coordinates to override the displayProjection. For instance you want to show
the coordinates in 4326, but you have a dataset in EPSG:28992
(dutch projection). We can also retrieve the current zoomlevel, position and
extent from the map. The coordinates are returned in displayProjection.


     var center = map.center(); //get the current zoom, position and extent
     map.center({zoom:4}); //zoom to zoomlevel 4
     map.center({position:[5,52]}); //pan to point 5,52
     map.center(box:[-180,-90,180,90]); //zoom to the box -180,-900,180,90
     //pan to point 125000,485000 in dutch projection
     map.center({position:[125000,485000],projection:'EPSG:28992'});
 */
    center: function (options) {
        var position;
        var mapProjection = new OpenLayers.Projection(this.projection);
        // Determine source projection
        var sourceProjection = null;
        var zoom;
        var box;
        if(options && options.projection) {
            sourceProjection = options.projection.CLASS_NAME ===
            'OpenLayers.Projection' ? options.projection :
            new OpenLayers.Projection(options.projection);
        } else {
            var displayProjection = this.displayProjection;
            if(!displayProjection) {
                // source == target
                sourceProjection = new OpenLayers.Projection('EPSG:4326');
            } else {
                sourceProjection = displayProjection.CLASS_NAME ===
            'OpenLayers.Projection' ? displayProjection :
            new OpenLayers.Projection(displayProjection);
            }
        }

        // Get the current position
        if (arguments.length===0) {
            position = this.olMap.getCenter();
            zoom = this.olMap.getZoom();
            box = this.olMap.getExtent();

            if (!mapProjection.equals(sourceProjection)) {
                position.transform(mapProjection, sourceProjection);
            }
            box.transform(mapProjection,sourceProjection);
            box = box!==null ? box.toArray() : [];
            return {
                position: [position.lon, position.lat],
                zoom: this.olMap.getZoom(),
                box: box
            };
        }

        // Zoom to the extent of the box
        if (options.box!==undefined) {
            box = new OpenLayers.Bounds(
                options.box[0], options.box[1],options.box[2], options.box[3]);
            if (!mapProjection.equals(sourceProjection)) {
                box.transform(sourceProjection,mapProjection);
            }
            this.olMap.zoomToExtent(box);

        }
        // Only zoom is given
        else if (options.position===undefined) {
            this.olMap.zoomTo(options.zoom);
        }
        // Position is given, zoom maybe as well
        else {
            position = new OpenLayers.LonLat(options.position[0],
                                             options.position[1]);
            if (!mapProjection.equals(sourceProjection)) {
                position.transform(sourceProjection, mapProjection);
            }
            // options.zoom might be undefined, so we are good to
            // pass it on
            this.olMap.setCenter(position, options.zoom);
        }
    },
    _updateSelectFeatureControl: function(layerIds) {
        var vectorLayers = [];
        var layersList = this.layersList;
        if (this.selectFeatureControl!==null) {
            this.selectFeatureControl.deactivate();
            this.selectFeatureControl.destroy();
        }
        $.each(layerIds, function() {
            vectorLayers.push(layersList[this].olLayer);
        });
        this.selectFeatureControl = new OpenLayers.Control.SelectFeature(
            vectorLayers);
        this.olMap.addControl(this.selectFeatureControl);
        this.selectFeatureControl.activate();
    },
    // This function got a bit too clever. The reason is, that jQuery's
    // bind() is overloaded with so many possible combinations of arguments.
    // And, of course, MapQuery wants to support them all
    // The essence of the function is to wrap the original callback into
    // the correct scope
    bind: function(types, data, fn) {
        var self = this;

        // A map of event/handle pairs, wrap each of them
        if(arguments.length===1) {
            var wrapped = {};
            $.each(types, function(type, fn) {
                wrapped[type] = function() {
                    return fn.apply(self, arguments);
                };
            });
            this.events.bind.apply(this.events, [wrapped]);
        }
        else {
            var args = [types];
            // Only callback given, but no data (types, fn), hence
            // `data` is the function
            if(arguments.length===2) {
                fn = data;
            }
            else {
                if (!$.isFunction(fn)) {
                    throw('bind: you might have a typo in the function name');
                }
                // Callback and data given (types, data, fn), hence include
                // the data in the argument list
                args.push(data);
            }

            args.push(function() {
                return fn.apply(self, arguments);
            });

            this.events.bind.apply(this.events, args);
        }

        //this.events.bind.call(this.events, types, function() {
        //    data.apply(self, arguments);
        //});
        //this.events.bind.call(this.events, types, function() {
        //    data.apply(self, arguments);
        //});

        //this.events.bind.apply(this.events, arguments);
        //this.events.bind.call(this.events, types, $.proxy(data, self));
        //this.events.bind.apply(this.events, arguments);//.bind(this);
        //this.events.bind.apply(this.events, $.proxy(arguments));//.bind(this);
        //this.events.bind.apply(this.events, $.proxy(arguments));//.bind(this);
        //this.events.bind(types, data, fn);//.bind(this);
        //this.events.bind.call(this.events, types, data, fn);//.bind(this);
        return this;
    },
/**
###*map*.`trigger(name [, parameters])`
_version added 0.2_
####**Description**: triggers an event on the map

 * **name** the name of the event
 * **parameters** additional parameters that will be passed on with the event

>Returns: map (MapQuery.Map)

To subscribe to the triggered events, you need to bind to the mapuuu.

     map.bind('myEvent', function(evt) {
         console.log('the values are: ' + evt.data[0] + ' and ' + evt.data[1])
     });
     map.trigger('myEvent', 'some', 'values');
*/
    trigger: function() {
        // There is no point in using trigger() insted of triggerHandler(), as
        // we don't fire native events
        this.events.triggerHandler.apply(this.events, arguments);
        return this;
    },
    // Basically a trigger that returns the return value of the last listener
    _triggerReturn: function() {
        return this.events.triggerHandler.apply(this.events, arguments);
    },
    destroy: function() {
        this.olMap.destroy();
        this.element.removeData('mapQuery');
    }
};
/**

---

#MapQuery.Layer

The MapQuery.Layer object. It is constructed with layer options object in the
map.`layers([options])` function or by passing a `layer:{options}` object in
the `mapQuery()` constructor. The Layer object is refered to as _layer_ in the
documentation.
 */
$.MapQuery.Layer = function(map, id, options) {

    var self = this;
    // apply default options that are not specific to a layer

    this.id = id;
    this.label = options.label || this.id;
    // a reference to the map object is needed as it stores e.g. the list
    // of all layers (and we need to keep track of it, if we delete a
    // layer)
    this.map = map;

    // true if this layer is a vector layer
    this.isVector = false;

    // to bind and trigger jQuery events
    this.events = $({});

    this.handlers = {
        // Triggers the jQuery events, after the OpenLayers events
        // happened without any further processing
        simple: function(data) {
            this.trigger(data.type);
        },
        // All OpenLayers events that are triggered by user interaction,
        // like clicking somewhere or selecting a feature, need to be
        // handled in a special way. Those OpenLayers events will then be
        // triggered by MapQuery as well
        // In case of the "featureselected" event, this means that the
        // logic of handling the event is completely within the event
        // handler. When ".select()" on a feature is called, it will just
        // trigger the OpenLayers "featureselected" event, whose handler
        // will then trigger the corresponding jQuery event.
        includeFeature: function(data) {
            var feature = new $.MapQuery.Feature(this, {olFeature:
                                                        data.feature});
            this.trigger(data.type, [feature]);
        },
        prependLayer: function(data) {
            this.trigger('layer' + data.type);
        }
    };


    // create the actual layer based on the options
    // Returns layer and final options for the layer (for later re-use,
    // e.g. zoomToMaxExtent).
    var res = $.MapQuery.Layer.types[options.type.toLowerCase()].call(
        this, options);
    this.olLayer = res.layer;
    this.options = res.options;

    // Some good documentation for the events is needed. Here is a short
    // description on how the current events compare to the OpenLayer
    // events on the layer:
    // - added, remove: not needed, there's addlayer and removelayer
    // - visibilitychanged: not needed, there's the changelayer event
    // - move, moveend: not needed as you get them from the map, not the layer
    // - loadstart, loadend: renamed to layerloadstart, layerloadend
    this.olLayer.events.on({
        scope: this,
        loadstart: this.handlers.prependLayer,
        loadend: this.handlers.prependLayer,
        featureselected: this.handlers.includeFeature,
        featureunselected: this.handlers.includeFeature,
        featureremoved: this.handlers.includeFeature
    });

    // To be able to retreive the MapQuery layer, when we only have the
    // OpenLayers layer available. For example on the layeradded event.
    // NOTE vmx 2012-02-26: Any nicer solution is welcome
    this.olLayer.mapQueryId = this.id;
};

$.MapQuery.Layer.prototype = {
/**
###*layer*.`down([delta])`
_version added 0.1_
####**Description**: move the layer down in the layer stack of the map

 * **delta** the amount of layers the layer has to move down in the layer
stack (default 1)

>Returns layer (MapQuery.Layer)


The `.down()` method is a shortcut method for `.position(pos)` which makes
it easier to move a layer down in the layerstack relative to its current
position. It takes an integer and will try to move the layer down the number of
places given. If delta is bigger than the current position in the stack, it
will put the layer at the bottom.


     layer.down();  //move layer 1 place down
     layer.down(3); //move layer 3 places down

 */
    down: function(delta) {
        delta = delta || 1;
        var pos = this.position();
        pos = pos - delta;
        if (pos<0) {pos = 0;}
        this.position(pos);
        return this;
    },
    // NOTE vmx: this would be pretty cool, but it's not easily possible
    // you could use $.each($.geojq.layer())) instead, this is for pure
    // convenience.
    each: function () {},
/**
###*layer*.`remove()`
_version added 0.2_
####**Description**: remove the layer from the map

>Returns: map (MapQuery.Map) or false


The `.remove()` method allows us to remove a layer from the map.
It returns the `map` object if the layer was removed, or `false` if the
removal was prevented in the preremovelayer event.

     var id = layer.remove(); //remove this layer


 */
    remove: function() {
        // remove references to this layer that are stored in the
        // map object
        return this.map._removeLayer(this.id);
    },
/**
###*layer*.`position([position])`
_version added 0.1_
####**Description**: get/set the `position` of the layer in the layer
stack of the map

 * **position** an integer setting the new position of the layer in the layer stack

>Returns: position (integer) _or_ layer (MapQuery.Layer)


The `.position()` method allows us to change the position of the layer in the
layer stack. It will take into account the hidden baselayer that is used by
OpenLayers. The lowest layer is position 0. If no position is given, it will
return the current postion.


     var pos =  layer.position(); //get position of layer in the layer stack
     layer.position(2); //put layer on position 2 in the layer stack

 */
    position: function(pos) {
        if (pos===undefined) {
            return this.map.olMap.getLayerIndex(this.olLayer)-1;
        }
        else {
            this.map.olMap.setLayerIndex(this.olLayer, pos+1);
            this.trigger('changelayer', ['position']);
            return this;
        }
    },
/**
###*layer*.`up([delta])`
_version added 0.1_
####**Description**: move the layer up in the layer stack of the map

 * **delta** the amount of layers the layer has to move up in the layer
stack (default 1)

>Returns: layer (MapQuery.Layer)


The `.up()` method is a shortcut method for `.position(pos)` which makes
it easier to move a layer up in the layerstack relative to its current
position. It takes an integer and will move the layer up the number of places
given.



     layer.up();  //move layer 1 place up
     layer.up(3); //move layer 3 places up
*/
    up: function(delta) {
        delta = delta || 1;
        var pos = this.position();
        pos = pos + delta;
        this.position(pos);
        return this;
    },
/**
###*layer*.`visible([visible])`
_version added 0.1_
####**Description**: get/set the `visible` state of the layer

 * **visible** a boolean setting the visibility of the layer

>Returns: visible (boolean)


The `.visible()` method allows us to change the visibility of the layer.
If no visible is given, it will return the current visibility.


     var vis =  layer.visible(); //get the visibility of layer
     layer.visible(true); //set visibility of layer to true

 */
    visible: function(vis) {
        if (vis===undefined) {
            return this.olLayer.getVisibility();
        }
        else {
            this.olLayer.setVisibility(vis);
            this.trigger('changelayer', ['visibility']);
            return this;
        }
    },
/**
###*layer*.`opacity([opacity])`
_version added 0.1_
####**Description**: get/set the `opacity` of the layer

 * **position** a float [0-1] setting the opacity of the layer

>Returns: opacity (float) _or_ layer (MapQuery.Layer)


The `.opacity()` method allows us to change the opacity of the layer.
If no opacity is given, it will return the current opacity.


     var opac =  layer.opacity(); //get opacity of layer
     layer.opacity(0.7); //set opacity of layer to 0.7

 */
    opacity: function(opac) {
        if (opac===undefined) {
            // this.olLayer.opacity can be null if never
            // set so return the visibility
            var value = this.olLayer.opacity ?
            this.olLayer.opacity : this.olLayer.getVisibility();
            return value;
        }
        else {
            this.olLayer.setOpacity(opac);
            this.trigger('changelayer', ['opacity']);
            return this;
        }
    },
    // every event gets the layer passed in
    bind: function() {
        // Use the same bind function as for the map
        this.map.bind.apply(this, arguments);
        return this;
    },
/**
###*layer*.`trigger(name [, parameters])`
_version added 0.2_
####**Description**: triggers an event on the layer and map

 * **name** the name of the event
 * **parameters** additional parameters that will be passed on with the event

>Returns: layer (MapQuery.Layer)

The events get triggered on the layer as well as on the map. To subscribe to
the triggered events, you can either bind to the layer or the map. If bound
to the map, the second argument in the bind will be the layer the event
came from

     layer.bind('myEvent', function(evt) {
         console.log('the values are: ' + evt.data[0] + ' and ' + evt.data[1])
     });
     map.bind('myEvent', function(evt, layer) {
         console.log('the values are: ' + evt.data[0] + ' and ' + evt.data[1])
     });
     layer.trigger('myEvent', 'some', 'values');
*/
    trigger: function() {
        var args = Array.prototype.slice.call(arguments);
        this.events.triggerHandler.apply(this.events, args);

        this._addLayerToArgs(args);

        this.map.events.triggerHandler.apply(this.map.events, args);
        return this;
    },
    // Basically a trigger that returns the return value of the last listener
    _triggerReturn: function() {
        var args = Array.prototype.slice.call(arguments);
        var ret = this.events.triggerHandler.apply(this.events, args);
        if (ret !== undefined) {
            return ret;
        }

        this._addLayerToArgs(args);
        return this.events.triggerHandler.apply(this.map.events, args);
    },
    // Adds the current layer to the event arguments, so that it is included
    // in the event on the map
    _addLayerToArgs: function(args) {
        // Add layer for the map event
        if (args.length===1) {
            args.push([this]);
        }
        else {
            args[1].unshift(this);
        }
    },
/**
###*layer*.`features([options])`
_version added 0.2.0_
####**Description**: get/set the features of a (vector) layer

**options** an object of key-value pairs with options to create one or
more features

>Returns: [features] (array of MapQuery.Feature)


The `.features()` method allows us to attach features to a mapQuery layer
object. It takes an options object with feature options. To add multiple
features, create an array of feature options objects. If an options object
is given, it will return the resulting feature(s). We can also use it to
retrieve all features currently attached to the layer.


     // add an (vector) json layer to the map
     var jsonlayer = map.layers({type:'json'});
     // add a feature to the layer
     jsonlayer.features({geometry: {type: "Point", coordinates: [5.3, 7.4]}});
     // get all features of a layer (sorted with first added feature at the beginning
     var features = jsonlayer.features();
*/
    features: function(options) {
        var self = this;
        switch(arguments.length) {
        // return all features
        case 0:
            return this._allFeatures();
        // add new feature(s)
        case 1:
            if (!$.isArray(options)) {
                return this._addFeature(options);
            }
            else {
                return $.map(options, function(feature) {
                    return self._addFeature(feature);
                });
            }
            break;
        default:
            throw('wrong argument number');
        }
    },
    _allFeatures: function() {
        var layer = this;
        return $.map(layer.olLayer.features, function(feature) {
            return new $.MapQuery.Feature(layer, {olFeature: feature});
        });
    },
    _addFeature: function(options) {
        var feature = new $.MapQuery.Feature(this, options);
        // NOTE vmx 2012-04-19: Not sure if this is a good idea, or if it would
        //     be better to include `options` with the preaddfeature event
        if (this._triggerReturn('preaddfeature', [feature])===false) {
            return false;
        }
        this.olLayer.addFeatures(feature.olFeature);
        this.trigger('addfeature', [feature]);
        return feature;
    }
};

/**
#MapQuery.Feature

The MapQuery.Feature object. It is constructed with a feature options object
in the layer.`features([options])` function. The Feautre object is refered to
as _feature_ in the documentation.

TODO vmx 20110905: Support other geometry types than GeoJSON
options:
 * geometry: A GeoJSON geometry
 * properties: Properties for the feature
*/
// Not in the pulic API docs: You can pass in as options:
//  * olFeature: This will wrap the olFeature in a MapQuery feature
$.MapQuery.Feature = function(layer, options) {
    // The ID is the
    this._id = layer.map._createId();
    this.layer = layer;

    // Feature already exists on the layer, it just needs to be wrapped
    // to an MapQuery feature
    if (options.olFeature) {
        this.olFeature = options.olFeature;
    }
    else {
        // XXX vmx 20110905: Different feature types might make sense:
        //     (Geo)JSON, KML, WKT
        // vmx 2012-04-14: I changed my mind quite some time ago. We should onlu
        //     support GeoJSON and let the user easily transfrom their format
        //     (e.g. KML) to GeoJSON, before they add a feature to the layer
        var GeoJSON = new OpenLayers.Format.GeoJSON();
        var geometry = GeoJSON.parseGeometry(options.geometry);
        geometry.transform(
            new OpenLayers.Projection(this.layer.map.displaProjection),
            new OpenLayers.Projection(this.layer.map.projection));

        this.olFeature = new OpenLayers.Feature.Vector(geometry,
            options.properties);
    }

    // Modify the features to be more practical
    // e.g. copy properties that should be easily accessed from the
    // outside, out of the olLayer and to the feature level
    this.properties = $.extend(true, {}, this.olFeature.attributes);
    this.geometry = $.parseJSON(
        new OpenLayers.Format.GeoJSON().write(this.olFeature.geometry));

    return this;
};

$.MapQuery.Feature.prototype = {
/**
###*feature*.`remove()`
_version added 0.2.0_
####**Description**: remove the feature from the layer

>Returns: layer (layer) or false


The `.remove()` method allows us to remove a feature from the layer.
It returns the `layer` object if the feature was removed, or `false` if the
removal was prevented in the preremovefeature event.

     // add a feature to a layer
     var feature = layer.features({geometry: {type: "Point", coordinates: [5.3, 7.4]}});
     // remove the feature again
     feature.remove();
*/
    remove: function() {
        if (this.layer._triggerReturn('preremovefeature', [this])===false) {
            return false;
        }
        this.layer.olLayer.removeFeatures(this.olFeature);
        // The `removefeature` event is triggered by an OpenLayes event handler
        return this.layer;
    },
/**
###*feature*.`select(exclusive)`
_version added 0.2.0_
####**Description**: select a feature

**exclusive** (boolean, default: true) True means that all other features get
deselectd

>Returns: layer (layer)


The `.select()` method allows us to select a feature from the layer.
A `featureselected` will be fired.

     // add a feature to a layer
     var feature = layer.features({geometry: {type: "Point", coordinates: [5.3, 7.4]}});
     // select the feature again
     feature.select();
*/
    select: function(exclusive) {
        if (exclusive===undefined || exclusive===true) {
            this.layer.map.selectFeatureControl.unselectAll();
        }
        this.layer.map.selectFeatureControl.select(this.olFeature);
    },
/**
###*feature*.`unselect()`
_version added 0.2.0_
####**Description**: unselect a feature

>Returns: layer (layer)


The `.unselect()` method allows us to unselect a feature from the layer.
A `featureunselected` will be fired.

     // add a feature to a layer
     var feature = layer.features({geometry: {type: "Point", coordinates: [5.3, 7.4]}});
     // select the feature
     feature.select();
     // unselect the feature again
     feature.unselect();
*/
    unselect: function() {
        this.layer.map.selectFeatureControl.unselect(this.olFeature);
    }
};

$.fn.mapQuery = function(options) {
    return this.each(function() {
        var instance = $.data(this, 'mapQuery');
        if (!instance) {
            $.data(this, 'mapQuery', new $.MapQuery.Map($(this), options));
        }
    });
};

$.extend($.MapQuery.Layer, {
    types: {
/**
###*layer* `{type:bing}`
_version added 0.1_
####**Description**: create a Bing maps layer

 * **view** a string ['road','hybrid','satellite'] to define which Bing maps
layer to use (default road)   
 * **key** Bing Maps API key for your application. Get you own at
http://bingmapsportal.com/ 
 * **label** string with the name of the layer


      layers:[{
            type:'bing',      //create a bing maps layer
            view:'satellite', //use the bing satellite layer
            key:'ArAGGPJ16xm0RX' //the Bing maps API key
            }]

*/
        bing: function(options) {
            var o = $.extend(true, {}, $.fn.mapQuery.defaults.layer.all,
                $.fn.mapQuery.defaults.layer.bing,
                options);
            var view = o.view;
            switch(view){
                case 'road':
                    view = 'Road'; break;
                case 'hybrid':
                    view = 'AerialWithLabels'; break;
                case 'satellite':
                    view = 'Aerial'; break;
            }
            return {
                layer: new OpenLayers.Layer.Bing({type:view,key:o.key}),
                options: o
            };
        },
        //Not sure this one is worth pursuing works with ecwp:// & jpip:// urls
        //See ../lib/NCSOpenLayersECWP.js
        ecwp: function(options) {
            var o = $.extend(true, {}, $.fn.mapQuery.defaults.layer.all,
                    $.fn.mapQuery.defaults.layer.raster,
                    options);
            return {
                layer: new OpenLayers.Layer.ECWP(o.label, o.url, o),
                options: o
            };
        },
/**
###*layer* `{type:google}`
_version added 0.1_
####**Description**: create a Google maps layer

 * **view** a string ['road','hybrid','satellite'] to define which Google maps
layer to use (default road)
 * **label** string with the name of the layer


*Note* you need to include the google maps v3 API in your application by adding
`<script src="http://maps.google.com/maps/api/js?v=3.5&amp;sensor=false"type="text/javascript"></script>`


      layers:[{
            type:'google',      //create a google maps layer
            view:'hybrid' //use the google hybridlayer
            }]

*/
        google: function(options) {
            var o = $.extend(true, {}, $.fn.mapQuery.defaults.layer.all,
                    $.fn.mapQuery.defaults.layer.google,
                    options);
            var view = o.view;
            switch(view){
                case 'road':
                    view = google.maps.MapTypeId.ROADMAP; break;
                case 'terrain':
                    view = google.maps.MapTypeId.TERRAIN; break;
                case 'hybrid':
                    view = google.maps.MapTypeId.HYBRID; break;
                case 'satellite':
                    view = google.maps.MapTypeId.SATELLITE; break;
            }
            return {
                layer: new OpenLayers.Layer.Google({type:view}),
                options: o
            };
        },
/**
###*layer* `{type:vector}`
_version added 0.1_
####**Description**: create a vector layer

 * **label** string with the name of the layer


      layers:[{
            type:'vector'     //create a vector layer
            }]

*/
        vector: function(options) {
            var o = $.extend(true, {}, $.fn.mapQuery.defaults.layer.all,
                    $.fn.mapQuery.defaults.layer.vector,
                    options);
            this.isVector = true;
            return {
                layer: new OpenLayers.Layer.Vector(o.label),
                options: o
            };
        },
/**
###*layer* `{type:json}`
_version added 0.1_
####**Description**: create a JSON layer

 * **url** a string pointing to the location of the JSON data
 * **strategies** a string ['bbox','cluster','filter','fixed','paging','refresh','save']
stating which update strategy should be used (default fixed)
(see also http://dev.openlayers.org/apidocs/files/OpenLayers/Strategy-js.html)
 * **projection** a string with the projection of the JSON data (default EPSG:4326)
 * **styleMap** {object} the style to be used to render the JSON data    
 * **label** string with the name of the layer


      layers:[{
            type: 'JSON',
            url: 'data/reservate.json',
            label: 'reservate'
            }]

*/
        json: function(options) {
            var o = $.extend(true, {}, $.fn.mapQuery.defaults.layer.all,
                $.fn.mapQuery.defaults.layer.vector,
                options);
            this.isVector = true;
            var strategies = [];
            for (var i in o.strategies) {
                if(o.strategies.hasOwnProperty(i)) {
                    switch(o.strategies[i].toLowerCase()) {
                    case 'bbox':
                        strategies.push(new OpenLayers.Strategy.BBOX());
                   break;
                    case 'cluster':
                        strategies.push(new OpenLayers.Strategy.Cluster());
                   break;
                    case 'filter':
                        strategies.push(new OpenLayers.Strategy.Filter());
                   break;
                    case 'fixed':
                        strategies.push(new OpenLayers.Strategy.Fixed());
                   break;
                    case 'paging':
                        strategies.push(new OpenLayers.Strategy.Paging());
                   break;
                    case 'refresh':
                        strategies.push(new OpenLayers.Strategy.Refresh());
                   break;
                    case 'save':
                        strategies.push(new OpenLayers.Strategy.Save());
                   break;
                    }
                }
            }
            var protocol;

            var params = {
                strategies: strategies,
                projection: o.projection || 'EPSG:4326',
                styleMap: o.styleMap
            };

            if (o.url) {
                // only use JSONP if we use http(s)
                if (o.url.match(/^https?:\/\//)!==null &&
                    !$.MapQuery.util.sameOrigin(o.url)) {
                    protocol = 'Script';
                }
                else {
                    protocol = 'HTTP';
                }
                params.protocol = new OpenLayers.Protocol[protocol]({
                    url: o.url,
                    format: new OpenLayers.Format.GeoJSON()
                });
            };

            var layer = new OpenLayers.Layer.Vector(o.label, params);
            return {
                layer: layer,
                options: o
            };
        },
/**
###*layer* `{type:osm}`
_version added 0.1_
####**Description**: create an OpenStreetMap layer

 
 * **label** string with the name of the layer   
 * **url** A single URL (string) or an array of URLs to OSM-like server like 
Cloudmade   
 * **attribution** A string to put some attribution on the map

      layers:[{
        type: 'osm',
        url: [
          'http://a.tile.cloudmade.com/<yourapikey>/999/256/${z}/${x}/${y}.png',
          'http://b.tile.cloudmade.com/<yourapikey>/999/256/${z}/${x}/${y}.png',
          'http://c.tile.cloudmade.com/<yourapikey>/999/256/${z}/${x}/${y}.png'
        ],
        attribution: "Data &copy; 2009 <a href='http://openstreetmap.org/'>
          OpenStreetMap</a>. Rendering &copy; 2009 
          <a href='http://cloudmade.com'>CloudMade</a>."
        }]

*/
        osm: function(options) {
            var o = $.extend(true, {}, $.fn.mapQuery.defaults.layer.all,
                $.fn.mapQuery.defaults.layer.osm,
                options);
            var label = options.label || undefined;
            var url = options.url || undefined;
            return {
                layer: new OpenLayers.Layer.OSM(label, url, o),
                options: o
            };
        },
/**
###*layer* `{type:tms}`
_version added 0.1_
####**Description**: create an OpenStreetMap layer

 
 * **label** string with the name of the layer   
 * **url** A single URL (string) or an array of URLs to the TMS end point
 * **layer** The identifier for the <TileMap> as advertised by the service. 
 For example, if the service advertises a <TileMap> with ‘href=”http://tms.osgeo.org/1.0.0/vmap0”’, 
 the layer property would be set to “vmap0”.
 * **format** The image format (default png)

      layers:[{
        type: 'tms',
        url: 'http://tilecache.osgeo.org/wms-c/Basic.py/',
        layer: 'basic'
        }]

*/        
        tms: function(options) {
            var o = $.extend(true, {}, $.fn.mapQuery.defaults.layer.all,
                $.fn.mapQuery.defaults.layer.tms,
                options);
            var label = options.label || undefined;
            var url = options.url || undefined;
            var params = {
                layername: o.layer,
                type: o.format
            };
            return {
                layer: new OpenLayers.Layer.TMS(label, url, params),
                options: o
            };
        },
/**
###*layer* `{type:wms}`
_version added 0.1_
####**Description**: create a WMS layer

 * **url** a string pointing to the location of the WMS service
 * **layers** a string with the name of the WMS layer(s)
 * **format** a string with format of the WMS image (default image/jpeg)
 * **transparent** a boolean for requesting images with transparency
 * **label** string with the name of the layer
 * **wms_parameters** an hashtable of extra GetMap query string parameters and parameter values


      layers:[{
            type:'wms',
            url:'http://vmap0.tiles.osgeo.org/wms/vmap0',
            layers:'basic'
            }]

*/
        wms: function(options) {
            var o = $.extend(true, {}, $.fn.mapQuery.defaults.layer.all,
                    $.fn.mapQuery.defaults.layer.raster,
                    options);
            var params = {
                layers: o.layers,
                transparent: o.transparent,
                format: o.format
            };
            if(typeof o.wms_parameters != "undefined"){
                params = $.extend(params, o.wms_parameters);
            }
            return {
                layer: new OpenLayers.Layer.WMS(o.label, o.url, params, o),
                options: o
            };
        },
//TODO complete this documentation
/**
###*layer* `{type:wmts}`
_version added 0.1_
####**Description**: create a WMTS (tiling) layer

 * **url** a string pointing to the location of the WMTS service
 * **layer** a string with the name of the WMTS layer
 * **matrixSet** a string with one of the advertised matrix set identifiers
 * **style** a string with one of the advertised layer styles    
 * **label** string with the name of the layer


      layers:[{
            type:'wmts'
            }]

*/
        wmts: function(options) {
            var o = $.extend(true, {}, $.fn.mapQuery.defaults.layer.all,
                    $.fn.mapQuery.defaults.layer.wmts);
            //smo 20110614 the maxExtent is set here with OpenLayers.Bounds
            if (options.sphericalMercator===true) {
                $.extend(true, o, {
                    maxExtent: new OpenLayers.Bounds(
                        -128 * 156543.0339, -128 * 156543.0339,
                        128 * 156543.0339, 128 * 156543.0339),
                    maxResolution: 156543.0339,
                    numZoomLevels: 19,
                    projection: 'EPSG:900913',
                    units: 'm'
                });
            }
            $.extend(true, o, options);
            // use by default all options that were passed in for the final
            // openlayers layer consrtuctor
            var params = $.extend(true, {}, o);

            // remove trailing slash
            if (params.url.charAt(params.url.length-1)==='/') {
                params.url = params.url.slice(0, params.url.length-1);
            }
            // if no options that influence the URL where set, extract them
            // from the given URL
            if (o.layer===undefined && o.matrixSet===undefined &&
                    o.style===undefined) {
                var url = $.MapQuery.util.parseUri(params.url);
                var urlParts = url.path.split('/');
                var wmtsPath = urlParts.slice(urlParts.length-3);
                params.url = url.protocol ? url.protocol + '//' : '';
                params.url += url.authority +
                    // remove WMTS version (1.0.0) as well
                    urlParts.slice(0, urlParts.length-4).join('/');
                params.layer = wmtsPath[0];
                params.style = wmtsPath[1];
                params.matrixSet = wmtsPath[2];
            }
            return {
                layer: new OpenLayers.Layer.WMTS(params),
                options: o
            };
        }
    }
});

// default options for the map and layers
$.fn.mapQuery.defaults = {
    // The controls for the map are per instance, therefore it need to
    // be an function that can be initiated per instance
    map: function() {
        return {
            // Remove quirky moveTo behavior, probably not a good idea in the
            // long run
            allOverlays: true,
            controls: [
                // Since OL2.11 the Navigation control includes touch navigation as well
                new OpenLayers.Control.Navigation({
                    documentDrag: true,
                    dragPanOptions: {
                        interval: 1,
                        enableKinetic: true
                    }
                }),
                new OpenLayers.Control.ArgParser(),
                new OpenLayers.Control.Attribution(),
                new OpenLayers.Control.KeyboardDefaults()
            ],
            format: 'image/png',
            maxExtent: [-128*156543.0339,
                -128*156543.0339,
                128*156543.0339,
                128*156543.0339],
            maxResolution: 156543.0339,
            numZoomLevels: 19,
            projection: 'EPSG:900913',
            displayProjection: 'EPSG:4326',
            zoomToMaxExtent: true,
            units: 'm'
        };
    },
    layer: {
        all: {
            isBaseLayer: false,
        //in general it is kinda pointless to load tiles outside a maxextent
            displayOutsideMaxExtent: false
        },
        bing: {
            transitionEffect: 'resize',
            view: 'road',
            sphericalMercator: true
        },
        google: {
            transitionEffect: 'resize',
            view: 'road',
            sphericalMercator: true
        },
        osm: {
            transitionEffect: 'resize',
            sphericalMercator: true
        },
        tms: {
            transitionEffect: 'resize',
            format: 'png'
        },
        raster: {
            // options for raster layers
            transparent: true
        },
        vector: {
            // options for vector layers
            strategies: ['bbox']
        },
        wmts: {
            format: 'image/jpeg',
            requestEncoding: 'REST',
            sphericalMercator: false
        }
    }
};

// Some utility functions

$.MapQuery.util = {};
// http://blog.stevenlevithan.com/archives/parseuri (2010-12-18)
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
// Edited to include the colon in the protocol, just like it is
// with window.location.protocol
$.MapQuery.util.parseUri = function (str) {
    var o = $.MapQuery.util.parseUri.options,
        m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
        uri = {},
        i = 14;

    while (i--) {uri[o.key[i]] = m[i] || "";}

    uri[o.q.name] = {};
    uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
        if ($1) {uri[o.q.name][$1] = $2;}
    });

    return uri;
};
$.MapQuery.util.parseUri.options = {
    strictMode: false,
    key: ["source", "protocol", "authority", "userInfo", "user",
            "password", "host", "port", "relative", "path", "directory",
            "file", "query", "anchor"],
    q: {
        name: "queryKey",
        parser: /(?:^|&)([^&=]*)=?([^&]*)/g
    },
    parser: {
        strict: /^(?:([^:\/?#]+:))?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
        loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+:))?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
        }
};
// Checks whether a URL conforms to the same origin policy or not
$.MapQuery.util.sameOrigin = function(url) {
    var parsed = $.MapQuery.util.parseUri(url);
    parsed.protocol = parsed.protocol || 'file:';
    parsed.port = parsed.port || "80";

    var current = {
        domain: document.domain,
        port: window.location.port,
        protocol: window.location.protocol
    };
    current.port = current.port || "80";

    return parsed.protocol===current.protocol &&
        parsed.port===current.port &&
        // the current domain is a suffix of the parsed domain
        parsed.host.match(current.domain + '$')!==null;
};
})(jQuery);