92112044ef4b7fbeab1cb356fa319b486c73f879
[rails.git] / vendor / assets / leaflet / leaflet.js
1 /*
2  Copyright (c) 2010-2012, CloudMade, Vladimir Agafonkin
3  Leaflet is an open-source JavaScript library for mobile-friendly interactive maps.
4  http://leafletjs.com
5 */
6 (function (window, undefined) {
7
8 var L, originalL;
9
10 if (typeof exports !== undefined + '') {
11         L = exports;
12 } else {
13         originalL = window.L;
14         L = {};
15
16         L.noConflict = function () {
17                 window.L = originalL;
18                 return this;
19         };
20
21         window.L = L;
22 }
23
24 L.version = '0.4.4';
25
26
27 /*
28  * L.Util is a namespace for various utility functions.
29  */
30
31 L.Util = {
32         extend: function (dest) { // (Object[, Object, ...]) ->
33                 var sources = Array.prototype.slice.call(arguments, 1),
34                     i, j, len, src;
35
36                 for (j = 0, len = sources.length; j < len; j++) {
37                         src = sources[j] || {};
38                         for (i in src) {
39                                 if (src.hasOwnProperty(i)) {
40                                         dest[i] = src[i];
41                                 }
42                         }
43                 }
44                 return dest;
45         },
46
47         bind: function (fn, obj) { // (Function, Object) -> Function
48                 var args = arguments.length > 2 ? Array.prototype.slice.call(arguments, 2) : null;
49                 return function () {
50                         return fn.apply(obj, args || arguments);
51                 };
52         },
53
54         stamp: (function () {
55                 var lastId = 0, key = '_leaflet_id';
56                 return function (/*Object*/ obj) {
57                         obj[key] = obj[key] || ++lastId;
58                         return obj[key];
59                 };
60         }()),
61
62         limitExecByInterval: function (fn, time, context) {
63                 var lock, execOnUnlock;
64
65                 return function wrapperFn() {
66                         var args = arguments;
67
68                         if (lock) {
69                                 execOnUnlock = true;
70                                 return;
71                         }
72
73                         lock = true;
74
75                         setTimeout(function () {
76                                 lock = false;
77
78                                 if (execOnUnlock) {
79                                         wrapperFn.apply(context, args);
80                                         execOnUnlock = false;
81                                 }
82                         }, time);
83
84                         fn.apply(context, args);
85                 };
86         },
87
88         falseFn: function () {
89                 return false;
90         },
91
92         formatNum: function (num, digits) {
93                 var pow = Math.pow(10, digits || 5);
94                 return Math.round(num * pow) / pow;
95         },
96
97         splitWords: function (str) {
98                 return str.replace(/^\s+|\s+$/g, '').split(/\s+/);
99         },
100
101         setOptions: function (obj, options) {
102                 obj.options = L.extend({}, obj.options, options);
103                 return obj.options;
104         },
105
106         getParamString: function (obj) {
107                 var params = [];
108                 for (var i in obj) {
109                         if (obj.hasOwnProperty(i)) {
110                                 params.push(i + '=' + obj[i]);
111                         }
112                 }
113                 return '?' + params.join('&');
114         },
115
116         template: function (str, data) {
117                 return str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
118                         var value = data[key];
119                         if (!data.hasOwnProperty(key)) {
120                                 throw new Error('No value provided for variable ' + str);
121                         }
122                         return value;
123                 });
124         },
125
126         emptyImageUrl: 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
127 };
128
129 (function () {
130
131         // inspired by http://paulirish.com/2011/requestanimationframe-for-smart-animating/
132
133         function getPrefixed(name) {
134                 var i, fn,
135                     prefixes = ['webkit', 'moz', 'o', 'ms'];
136
137                 for (i = 0; i < prefixes.length && !fn; i++) {
138                         fn = window[prefixes[i] + name];
139                 }
140
141                 return fn;
142         }
143
144         var lastTime = 0;
145
146         function timeoutDefer(fn) {
147                 var time = +new Date(),
148                     timeToCall = Math.max(0, 16 - (time - lastTime));
149
150                 lastTime = time + timeToCall;
151                 return window.setTimeout(fn, timeToCall);
152         }
153
154         var requestFn = window.requestAnimationFrame ||
155                 getPrefixed('RequestAnimationFrame') || timeoutDefer;
156
157         var cancelFn = window.cancelAnimationFrame ||
158                 getPrefixed('CancelAnimationFrame') ||
159                 getPrefixed('CancelRequestAnimationFrame') ||
160                 function (id) { window.clearTimeout(id); };
161
162
163         L.Util.requestAnimFrame = function (fn, context, immediate, element) {
164                 fn = L.bind(fn, context);
165
166                 if (immediate && requestFn === timeoutDefer) {
167                         fn();
168                 } else {
169                         return requestFn.call(window, fn, element);
170                 }
171         };
172
173         L.Util.cancelAnimFrame = function (id) {
174                 if (id) {
175                         cancelFn.call(window, id);
176                 }
177         };
178
179 }());
180
181 // shortcuts for most used utility functions
182 L.extend = L.Util.extend;
183 L.bind = L.Util.bind;
184 L.stamp = L.Util.stamp;
185 L.setOptions = L.Util.setOptions;
186
187
188 /*
189  * Class powers the OOP facilities of the library. Thanks to John Resig and Dean Edwards for inspiration!
190  */
191
192 L.Class = function () {};
193
194 L.Class.extend = function (/*Object*/ props) /*-> Class*/ {
195
196         // extended class with the new prototype
197         var NewClass = function () {
198                 if (this.initialize) {
199                         this.initialize.apply(this, arguments);
200                 }
201         };
202
203         // instantiate class without calling constructor
204         var F = function () {};
205         F.prototype = this.prototype;
206
207         var proto = new F();
208         proto.constructor = NewClass;
209
210         NewClass.prototype = proto;
211
212         //inherit parent's statics
213         for (var i in this) {
214                 if (this.hasOwnProperty(i) && i !== 'prototype') {
215                         NewClass[i] = this[i];
216                 }
217         }
218
219         // mix static properties into the class
220         if (props.statics) {
221                 L.extend(NewClass, props.statics);
222                 delete props.statics;
223         }
224
225         // mix includes into the prototype
226         if (props.includes) {
227                 L.Util.extend.apply(null, [proto].concat(props.includes));
228                 delete props.includes;
229         }
230
231         // merge options
232         if (props.options && proto.options) {
233                 props.options = L.extend({}, proto.options, props.options);
234         }
235
236         // mix given properties into the prototype
237         L.extend(proto, props);
238
239         return NewClass;
240 };
241
242
243 // method for adding properties to prototype
244 L.Class.include = function (props) {
245         L.extend(this.prototype, props);
246 };
247
248 L.Class.mergeOptions = function (options) {
249         L.extend(this.prototype.options, options);
250 };
251
252
253 /*
254  * L.Mixin.Events adds custom events functionality to Leaflet classes
255  */
256
257 var key = '_leaflet_events';
258
259 L.Mixin = {};
260
261 L.Mixin.Events = {
262
263         addEventListener: function (types, fn, context) { // (String, Function[, Object]) or (Object[, Object])
264                 var events = this[key] = this[key] || {},
265                         type, i, len;
266
267                 // Types can be a map of types/handlers
268                 if (typeof types === 'object') {
269                         for (type in types) {
270                                 if (types.hasOwnProperty(type)) {
271                                         this.addEventListener(type, types[type], fn);
272                                 }
273                         }
274
275                         return this;
276                 }
277
278                 types = L.Util.splitWords(types);
279
280                 for (i = 0, len = types.length; i < len; i++) {
281                         events[types[i]] = events[types[i]] || [];
282                         events[types[i]].push({
283                                 action: fn,
284                                 context: context || this
285                         });
286                 }
287
288                 return this;
289         },
290
291         hasEventListeners: function (type) { // (String) -> Boolean
292                 return (key in this) && (type in this[key]) && (this[key][type].length > 0);
293         },
294
295         removeEventListener: function (types, fn, context) { // (String[, Function, Object]) or (Object[, Object])
296                 var events = this[key],
297                         type, i, len, listeners, j;
298
299                 if (typeof types === 'object') {
300                         for (type in types) {
301                                 if (types.hasOwnProperty(type)) {
302                                         this.removeEventListener(type, types[type], fn);
303                                 }
304                         }
305
306                         return this;
307                 }
308
309                 types = L.Util.splitWords(types);
310
311                 for (i = 0, len = types.length; i < len; i++) {
312
313                         if (this.hasEventListeners(types[i])) {
314                                 listeners = events[types[i]];
315
316                                 for (j = listeners.length - 1; j >= 0; j--) {
317                                         if (
318                                                 (!fn || listeners[j].action === fn) &&
319                                                 (!context || (listeners[j].context === context))
320                                         ) {
321                                                 listeners.splice(j, 1);
322                                         }
323                                 }
324                         }
325                 }
326
327                 return this;
328         },
329
330         fireEvent: function (type, data) { // (String[, Object])
331                 if (!this.hasEventListeners(type)) {
332                         return this;
333                 }
334
335                 var event = L.extend({
336                         type: type,
337                         target: this
338                 }, data);
339
340                 var listeners = this[key][type].slice();
341
342                 for (var i = 0, len = listeners.length; i < len; i++) {
343                         listeners[i].action.call(listeners[i].context || this, event);
344                 }
345
346                 return this;
347         }
348 };
349
350 L.Mixin.Events.on = L.Mixin.Events.addEventListener;
351 L.Mixin.Events.off = L.Mixin.Events.removeEventListener;
352 L.Mixin.Events.fire = L.Mixin.Events.fireEvent;
353
354
355 (function () {
356
357         var ie = !!window.ActiveXObject,
358             // http://tanalin.com/en/articles/ie-version-js/
359             ie6 = ie && !window.XMLHttpRequest,
360             ie7 = ie && !document.querySelector,
361
362             // terrible browser detection to work around Safari / iOS / Android browser bugs
363             // see TileLayer._addTile and debug/hacks/jitter.html
364
365             ua = navigator.userAgent.toLowerCase(),
366             webkit = ua.indexOf("webkit") !== -1,
367             chrome = ua.indexOf("chrome") !== -1,
368             android = ua.indexOf("android") !== -1,
369             android23 = ua.search("android [23]") !== -1,
370
371             mobile = typeof orientation !== undefined + '',
372             msTouch = (window.navigator && window.navigator.msPointerEnabled && window.navigator.msMaxTouchPoints),
373             retina = (('devicePixelRatio' in window && window.devicePixelRatio > 1) ||
374                       ('matchMedia' in window && window.matchMedia("(min-resolution:144dpi)").matches)),
375
376             doc = document.documentElement,
377             ie3d = ie && ('transition' in doc.style),
378             webkit3d = ('WebKitCSSMatrix' in window) && ('m11' in new window.WebKitCSSMatrix()),
379             gecko3d = 'MozPerspective' in doc.style,
380             opera3d = 'OTransition' in doc.style,
381             any3d = !window.L_DISABLE_3D && (ie3d || webkit3d || gecko3d || opera3d);
382
383
384         var touch = !window.L_NO_TOUCH && (function () {
385
386                 var startName = 'ontouchstart';
387
388                 // IE10+ (We simulate these into touch* events in L.DomEvent and L.DomEvent.MsTouch) or WebKit, etc.
389                 if (msTouch || (startName in doc)) {
390                         return true;
391                 }
392
393                 // Firefox/Gecko
394                 var div = document.createElement('div'),
395                     supported = false;
396
397                 if (!div.setAttribute) {
398                         return false;
399                 }
400                 div.setAttribute(startName, 'return;');
401
402                 if (typeof div[startName] === 'function') {
403                         supported = true;
404                 }
405
406                 div.removeAttribute(startName);
407                 div = null;
408
409                 return supported;
410         }());
411
412
413         L.Browser = {
414                 ie6: ie6,
415                 ie7: ie7,
416                 webkit: webkit,
417
418                 android: android,
419                 android23: android23,
420
421                 chrome: chrome,
422
423                 ie3d: ie3d,
424                 webkit3d: webkit3d,
425                 gecko3d: gecko3d,
426                 opera3d: opera3d,
427                 any3d: any3d,
428
429                 mobile: mobile,
430                 mobileWebkit: mobile && webkit,
431                 mobileWebkit3d: mobile && webkit3d,
432                 mobileOpera: mobile && window.opera,
433
434                 touch: touch,
435                 msTouch: msTouch,
436
437                 retina: retina
438         };
439
440 }());
441
442
443 /*
444  * L.Point represents a point with x and y coordinates.
445  */
446
447 L.Point = function (/*Number*/ x, /*Number*/ y, /*Boolean*/ round) {
448         this.x = (round ? Math.round(x) : x);
449         this.y = (round ? Math.round(y) : y);
450 };
451
452 L.Point.prototype = {
453
454         clone: function () {
455                 return new L.Point(this.x, this.y);
456         },
457
458         // non-destructive, returns a new point
459         add: function (point) {
460                 return this.clone()._add(L.point(point));
461         },
462
463         // destructive, used directly for performance in situations where it's safe to modify existing point
464         _add: function (point) {
465                 this.x += point.x;
466                 this.y += point.y;
467                 return this;
468         },
469
470         subtract: function (point) {
471                 return this.clone()._subtract(L.point(point));
472         },
473
474         _subtract: function (point) {
475                 this.x -= point.x;
476                 this.y -= point.y;
477                 return this;
478         },
479
480         divideBy: function (num) {
481                 return this.clone()._divideBy(num);
482         },
483
484         _divideBy: function (num) {
485                 this.x /= num;
486                 this.y /= num;
487                 return this;
488         },
489
490         multiplyBy: function (num) {
491                 return this.clone()._multiplyBy(num);
492         },
493
494         _multiplyBy: function (num) {
495                 this.x *= num;
496                 this.y *= num;
497                 return this;
498         },
499
500         round: function () {
501                 return this.clone()._round();
502         },
503
504         _round: function () {
505                 this.x = Math.round(this.x);
506                 this.y = Math.round(this.y);
507                 return this;
508         },
509
510         floor: function () {
511                 return this.clone()._floor();
512         },
513
514         _floor: function () {
515                 this.x = Math.floor(this.x);
516                 this.y = Math.floor(this.y);
517                 return this;
518         },
519
520         distanceTo: function (point) {
521                 point = L.point(point);
522
523                 var x = point.x - this.x,
524                     y = point.y - this.y;
525
526                 return Math.sqrt(x * x + y * y);
527         },
528
529         toString: function () {
530                 return 'Point(' +
531                         L.Util.formatNum(this.x) + ', ' +
532                         L.Util.formatNum(this.y) + ')';
533         }
534 };
535
536 L.point = function (x, y, round) {
537         if (x instanceof L.Point) {
538                 return x;
539         }
540         if (x instanceof Array) {
541                 return new L.Point(x[0], x[1]);
542         }
543         if (isNaN(x)) {
544                 return x;
545         }
546         return new L.Point(x, y, round);
547 };
548
549
550 /*
551  * L.Bounds represents a rectangular area on the screen in pixel coordinates.
552  */
553
554 L.Bounds = L.Class.extend({
555
556         initialize: function (a, b) {   //(Point, Point) or Point[]
557                 if (!a) { return; }
558
559                 var points = b ? [a, b] : a;
560
561                 for (var i = 0, len = points.length; i < len; i++) {
562                         this.extend(points[i]);
563                 }
564         },
565
566         // extend the bounds to contain the given point
567         extend: function (point) { // (Point)
568                 point = L.point(point);
569
570                 if (!this.min && !this.max) {
571                         this.min = point.clone();
572                         this.max = point.clone();
573                 } else {
574                         this.min.x = Math.min(point.x, this.min.x);
575                         this.max.x = Math.max(point.x, this.max.x);
576                         this.min.y = Math.min(point.y, this.min.y);
577                         this.max.y = Math.max(point.y, this.max.y);
578                 }
579                 return this;
580         },
581
582         getCenter: function (round) { // (Boolean) -> Point
583                 return new L.Point(
584                         (this.min.x + this.max.x) / 2,
585                         (this.min.y + this.max.y) / 2, round);
586         },
587
588         getBottomLeft: function () { // -> Point
589                 return new L.Point(this.min.x, this.max.y);
590         },
591
592         getTopRight: function () { // -> Point
593                 return new L.Point(this.max.x, this.min.y);
594         },
595
596         contains: function (obj) { // (Bounds) or (Point) -> Boolean
597                 var min, max;
598
599                 if (typeof obj[0] === 'number' || obj instanceof L.Point) {
600                         obj = L.point(obj);
601                 } else {
602                         obj = L.bounds(obj);
603                 }
604
605                 if (obj instanceof L.Bounds) {
606                         min = obj.min;
607                         max = obj.max;
608                 } else {
609                         min = max = obj;
610                 }
611
612                 return (min.x >= this.min.x) &&
613                        (max.x <= this.max.x) &&
614                        (min.y >= this.min.y) &&
615                        (max.y <= this.max.y);
616         },
617
618         intersects: function (bounds) { // (Bounds) -> Boolean
619                 bounds = L.bounds(bounds);
620
621                 var min = this.min,
622                     max = this.max,
623                     min2 = bounds.min,
624                     max2 = bounds.max,
625                     xIntersects = (max2.x >= min.x) && (min2.x <= max.x),
626                     yIntersects = (max2.y >= min.y) && (min2.y <= max.y);
627
628                 return xIntersects && yIntersects;
629         },
630
631         isValid: function () {
632                 return !!(this.min && this.max);
633         }
634 });
635
636 L.bounds = function (a, b) { // (Bounds) or (Point, Point) or (Point[])
637         if (!a || a instanceof L.Bounds) {
638                 return a;
639         }
640         return new L.Bounds(a, b);
641 };
642
643
644 /*
645  * L.Transformation is an utility class to perform simple point transformations through a 2d-matrix.
646  */
647
648 L.Transformation = L.Class.extend({
649         initialize: function (/*Number*/ a, /*Number*/ b, /*Number*/ c, /*Number*/ d) {
650                 this._a = a;
651                 this._b = b;
652                 this._c = c;
653                 this._d = d;
654         },
655
656         transform: function (point, scale) {
657                 return this._transform(point.clone(), scale);
658         },
659
660         // destructive transform (faster)
661         _transform: function (/*Point*/ point, /*Number*/ scale) /*-> Point*/ {
662                 scale = scale || 1;
663                 point.x = scale * (this._a * point.x + this._b);
664                 point.y = scale * (this._c * point.y + this._d);
665                 return point;
666         },
667
668         untransform: function (/*Point*/ point, /*Number*/ scale) /*-> Point*/ {
669                 scale = scale || 1;
670                 return new L.Point(
671                         (point.x / scale - this._b) / this._a,
672                         (point.y / scale - this._d) / this._c);
673         }
674 });
675
676
677 /*
678  * L.DomUtil contains various utility functions for working with DOM.
679  */
680
681 L.DomUtil = {
682         get: function (id) {
683                 return (typeof id === 'string' ? document.getElementById(id) : id);
684         },
685
686         getStyle: function (el, style) {
687
688                 var value = el.style[style];
689
690                 if (!value && el.currentStyle) {
691                         value = el.currentStyle[style];
692                 }
693
694                 if ((!value || value === 'auto') && document.defaultView) {
695                         var css = document.defaultView.getComputedStyle(el, null);
696                         value = css ? css[style] : null;
697                 }
698
699                 return value === 'auto' ? null : value;
700         },
701
702         getViewportOffset: function (element) {
703
704                 var top = 0,
705                     left = 0,
706                     el = element,
707                     docBody = document.body,
708                     pos,
709                     ie7 = L.Browser.ie7;
710
711                 do {
712                         top  += el.offsetTop  || 0;
713                         left += el.offsetLeft || 0;
714                         pos = L.DomUtil.getStyle(el, 'position');
715
716                         if (el.offsetParent === docBody && pos === 'absolute') { break; }
717
718                         if (pos === 'fixed') {
719                                 top  += docBody.scrollTop  || 0;
720                                 left += docBody.scrollLeft || 0;
721                                 break;
722                         }
723                         el = el.offsetParent;
724
725                 } while (el);
726
727                 el = element;
728
729                 do {
730                         if (el === docBody) { break; }
731
732                         top  -= el.scrollTop  || 0;
733                         left -= el.scrollLeft || 0;
734
735                         // webkit (and ie <= 7) handles RTL scrollLeft different to everyone else
736                         // https://code.google.com/p/closure-library/source/browse/trunk/closure/goog/style/bidi.js
737                         if (!L.DomUtil.documentIsLtr() && (L.Browser.webkit || ie7)) {
738                                 left += el.scrollWidth - el.clientWidth;
739
740                                 // ie7 shows the scrollbar by default and provides clientWidth counting it, so we
741                                 // need to add it back in if it is visible; scrollbar is on the left as we are RTL
742                                 if (ie7 && L.DomUtil.getStyle(el, 'overflow-y') !== 'hidden' &&
743                                            L.DomUtil.getStyle(el, 'overflow') !== 'hidden') {
744                                         left += 17;
745                                 }
746                         }
747
748                         el = el.parentNode;
749                 } while (el);
750
751                 return new L.Point(left, top);
752         },
753
754         documentIsLtr: function () {
755                 if (!L.DomUtil._docIsLtrCached) {
756                         L.DomUtil._docIsLtrCached = true;
757                         L.DomUtil._docIsLtr = L.DomUtil.getStyle(document.body, 'direction') === "ltr";
758                 }
759                 return L.DomUtil._docIsLtr;
760         },
761
762         create: function (tagName, className, container) {
763
764                 var el = document.createElement(tagName);
765                 el.className = className;
766
767                 if (container) {
768                         container.appendChild(el);
769                 }
770
771                 return el;
772         },
773
774         disableTextSelection: function () {
775                 if (document.selection && document.selection.empty) {
776                         document.selection.empty();
777                 }
778                 if (!this._onselectstart) {
779                         this._onselectstart = document.onselectstart;
780                         document.onselectstart = L.Util.falseFn;
781                 }
782         },
783
784         enableTextSelection: function () {
785                 if (document.onselectstart === L.Util.falseFn) {
786                         document.onselectstart = this._onselectstart;
787                         this._onselectstart = null;
788                 }
789         },
790
791         hasClass: function (el, name) {
792                 return (el.className.length > 0) &&
793                         new RegExp("(^|\\s)" + name + "(\\s|$)").test(el.className);
794         },
795
796         addClass: function (el, name) {
797                 if (!L.DomUtil.hasClass(el, name)) {
798                         el.className += (el.className ? ' ' : '') + name;
799                 }
800         },
801
802         removeClass: function (el, name) {
803
804                 function replaceFn(w, match) {
805                         if (match === name) { return ''; }
806                         return w;
807                 }
808
809                 el.className = el.className
810                         .replace(/(\S+)\s*/g, replaceFn)
811                         .replace(/(^\s+|\s+$)/, '');
812         },
813
814         setOpacity: function (el, value) {
815
816                 if ('opacity' in el.style) {
817                         el.style.opacity = value;
818
819                 } else if ('filter' in el.style) {
820
821                         var filter = false,
822                             filterName = 'DXImageTransform.Microsoft.Alpha';
823
824                         // filters collection throws an error if we try to retrieve a filter that doesn't exist
825                         try { filter = el.filters.item(filterName); } catch (e) {}
826
827                         value = Math.round(value * 100);
828
829                         if (filter) {
830                                 filter.Enabled = (value !== 100);
831                                 filter.Opacity = value;
832                         } else {
833                                 el.style.filter += ' progid:' + filterName + '(opacity=' + value + ')';
834                         }
835                 }
836         },
837
838         testProp: function (props) {
839
840                 var style = document.documentElement.style;
841
842                 for (var i = 0; i < props.length; i++) {
843                         if (props[i] in style) {
844                                 return props[i];
845                         }
846                 }
847                 return false;
848         },
849
850         getTranslateString: function (point) {
851                 // on WebKit browsers (Chrome/Safari/iOS Safari/Android) using translate3d instead of translate
852                 // makes animation smoother as it ensures HW accel is used. Firefox 13 doesn't care
853                 // (same speed either way), Opera 12 doesn't support translate3d
854
855                 var is3d = L.Browser.webkit3d,
856                     open = 'translate' + (is3d ? '3d' : '') + '(',
857                     close = (is3d ? ',0' : '') + ')';
858
859                 return open + point.x + 'px,' + point.y + 'px' + close;
860         },
861
862         getScaleString: function (scale, origin) {
863
864                 var preTranslateStr = L.DomUtil.getTranslateString(origin.add(origin.multiplyBy(-1 * scale))),
865                     scaleStr = ' scale(' + scale + ') ';
866
867                 return preTranslateStr + scaleStr;
868         },
869
870         setPosition: function (el, point, disable3D) { // (HTMLElement, Point[, Boolean])
871
872                 el._leaflet_pos = point;
873
874                 if (!disable3D && L.Browser.any3d) {
875                         el.style[L.DomUtil.TRANSFORM] =  L.DomUtil.getTranslateString(point);
876
877                         // workaround for Android 2/3 stability (https://github.com/CloudMade/Leaflet/issues/69)
878                         if (L.Browser.mobileWebkit3d) {
879                                 el.style.WebkitBackfaceVisibility = 'hidden';
880                         }
881                 } else {
882                         el.style.left = point.x + 'px';
883                         el.style.top = point.y + 'px';
884                 }
885         },
886
887         getPosition: function (el) {
888                 // this method is only used for elements previously positioned using setPosition,
889                 // so it's safe to cache the position for performance
890                 return el._leaflet_pos;
891         }
892 };
893
894
895 // prefix style property names
896
897 L.DomUtil.TRANSFORM = L.DomUtil.testProp(
898         ['transform', 'WebkitTransform', 'OTransform', 'MozTransform', 'msTransform']);
899
900 L.DomUtil.TRANSITION = L.DomUtil.testProp(
901         ['transition', 'webkitTransition', 'OTransition', 'MozTransition', 'msTransition']);
902
903 L.DomUtil.TRANSITION_END =
904         L.DomUtil.TRANSITION === 'webkitTransition' || L.DomUtil.TRANSITION === 'OTransition' ?
905         L.DomUtil.TRANSITION + 'End' : 'transitionend';
906
907
908 /*
909         CM.LatLng represents a geographical point with latitude and longtitude coordinates.
910 */
911
912 L.LatLng = function (rawLat, rawLng, noWrap) { // (Number, Number[, Boolean])
913         var lat = parseFloat(rawLat),
914             lng = parseFloat(rawLng);
915
916         if (isNaN(lat) || isNaN(lng)) {
917                 throw new Error('Invalid LatLng object: (' + rawLat + ', ' + rawLng + ')');
918         }
919
920         if (noWrap !== true) {
921                 lat = Math.max(Math.min(lat, 90), -90);                                 // clamp latitude into -90..90
922                 lng = (lng + 180) % 360 + ((lng < -180 || lng === 180) ? 180 : -180);   // wrap longtitude into -180..180
923         }
924
925         this.lat = lat;
926         this.lng = lng;
927 };
928
929 L.extend(L.LatLng, {
930         DEG_TO_RAD: Math.PI / 180,
931         RAD_TO_DEG: 180 / Math.PI,
932         MAX_MARGIN: 1.0E-9 // max margin of error for the "equals" check
933 });
934
935 L.LatLng.prototype = {
936         equals: function (obj) { // (LatLng) -> Boolean
937                 if (!obj) { return false; }
938
939                 obj = L.latLng(obj);
940
941                 var margin = Math.max(Math.abs(this.lat - obj.lat), Math.abs(this.lng - obj.lng));
942                 return margin <= L.LatLng.MAX_MARGIN;
943         },
944
945         toString: function (precision) { // -> String
946                 return 'LatLng(' +
947                         L.Util.formatNum(this.lat, precision) + ', ' +
948                         L.Util.formatNum(this.lng, precision) + ')';
949         },
950
951         // Haversine distance formula, see http://en.wikipedia.org/wiki/Haversine_formula
952         distanceTo: function (other) { // (LatLng) -> Number
953                 other = L.latLng(other);
954
955                 var R = 6378137, // earth radius in meters
956                     d2r = L.LatLng.DEG_TO_RAD,
957                     dLat = (other.lat - this.lat) * d2r,
958                     dLon = (other.lng - this.lng) * d2r,
959                     lat1 = this.lat * d2r,
960                     lat2 = other.lat * d2r,
961                     sin1 = Math.sin(dLat / 2),
962                     sin2 = Math.sin(dLon / 2);
963
964                 var a = sin1 * sin1 + sin2 * sin2 * Math.cos(lat1) * Math.cos(lat2);
965
966                 return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
967         }
968 };
969
970 L.latLng = function (a, b, c) { // (LatLng) or ([Number, Number]) or (Number, Number, Boolean)
971         if (a instanceof L.LatLng) {
972                 return a;
973         }
974         if (a instanceof Array) {
975                 return new L.LatLng(a[0], a[1]);
976         }
977         if (isNaN(a)) {
978                 return a;
979         }
980         return new L.LatLng(a, b, c);
981 };
982
983
984
985 /*
986  * L.LatLngBounds represents a rectangular area on the map in geographical coordinates.
987  */
988
989 L.LatLngBounds = L.Class.extend({
990         initialize: function (southWest, northEast) {   // (LatLng, LatLng) or (LatLng[])
991                 if (!southWest) { return; }
992
993                 var latlngs = northEast ? [southWest, northEast] : southWest;
994
995                 for (var i = 0, len = latlngs.length; i < len; i++) {
996                         this.extend(latlngs[i]);
997                 }
998         },
999
1000         // extend the bounds to contain the given point or bounds
1001         extend: function (obj) { // (LatLng) or (LatLngBounds)
1002                 if (typeof obj[0] === 'number' || obj instanceof L.LatLng) {
1003                         obj = L.latLng(obj);
1004                 } else {
1005                         obj = L.latLngBounds(obj);
1006                 }
1007
1008                 if (obj instanceof L.LatLng) {
1009                         if (!this._southWest && !this._northEast) {
1010                                 this._southWest = new L.LatLng(obj.lat, obj.lng, true);
1011                                 this._northEast = new L.LatLng(obj.lat, obj.lng, true);
1012                         } else {
1013                                 this._southWest.lat = Math.min(obj.lat, this._southWest.lat);
1014                                 this._southWest.lng = Math.min(obj.lng, this._southWest.lng);
1015
1016                                 this._northEast.lat = Math.max(obj.lat, this._northEast.lat);
1017                                 this._northEast.lng = Math.max(obj.lng, this._northEast.lng);
1018                         }
1019                 } else if (obj instanceof L.LatLngBounds) {
1020                         this.extend(obj._southWest);
1021                         this.extend(obj._northEast);
1022                 }
1023                 return this;
1024         },
1025
1026         // extend the bounds by a percentage
1027         pad: function (bufferRatio) { // (Number) -> LatLngBounds
1028                 var sw = this._southWest,
1029                     ne = this._northEast,
1030                     heightBuffer = Math.abs(sw.lat - ne.lat) * bufferRatio,
1031                     widthBuffer = Math.abs(sw.lng - ne.lng) * bufferRatio;
1032
1033                 return new L.LatLngBounds(
1034                         new L.LatLng(sw.lat - heightBuffer, sw.lng - widthBuffer),
1035                         new L.LatLng(ne.lat + heightBuffer, ne.lng + widthBuffer));
1036         },
1037
1038         getCenter: function () { // -> LatLng
1039                 return new L.LatLng(
1040                         (this._southWest.lat + this._northEast.lat) / 2,
1041                         (this._southWest.lng + this._northEast.lng) / 2);
1042         },
1043
1044         getSouthWest: function () {
1045                 return this._southWest;
1046         },
1047
1048         getNorthEast: function () {
1049                 return this._northEast;
1050         },
1051
1052         getNorthWest: function () {
1053                 return new L.LatLng(this._northEast.lat, this._southWest.lng, true);
1054         },
1055
1056         getSouthEast: function () {
1057                 return new L.LatLng(this._southWest.lat, this._northEast.lng, true);
1058         },
1059
1060         contains: function (obj) { // (LatLngBounds) or (LatLng) -> Boolean
1061                 if (typeof obj[0] === 'number' || obj instanceof L.LatLng) {
1062                         obj = L.latLng(obj);
1063                 } else {
1064                         obj = L.latLngBounds(obj);
1065                 }
1066
1067                 var sw = this._southWest,
1068                     ne = this._northEast,
1069                     sw2, ne2;
1070
1071                 if (obj instanceof L.LatLngBounds) {
1072                         sw2 = obj.getSouthWest();
1073                         ne2 = obj.getNorthEast();
1074                 } else {
1075                         sw2 = ne2 = obj;
1076                 }
1077
1078                 return (sw2.lat >= sw.lat) && (ne2.lat <= ne.lat) &&
1079                        (sw2.lng >= sw.lng) && (ne2.lng <= ne.lng);
1080         },
1081
1082         intersects: function (bounds) { // (LatLngBounds)
1083                 bounds = L.latLngBounds(bounds);
1084
1085                 var sw = this._southWest,
1086                     ne = this._northEast,
1087                     sw2 = bounds.getSouthWest(),
1088                     ne2 = bounds.getNorthEast(),
1089
1090                     latIntersects = (ne2.lat >= sw.lat) && (sw2.lat <= ne.lat),
1091                     lngIntersects = (ne2.lng >= sw.lng) && (sw2.lng <= ne.lng);
1092
1093                 return latIntersects && lngIntersects;
1094         },
1095
1096         toBBoxString: function () {
1097                 var sw = this._southWest,
1098                     ne = this._northEast;
1099
1100                 return [sw.lng, sw.lat, ne.lng, ne.lat].join(',');
1101         },
1102
1103         equals: function (bounds) { // (LatLngBounds)
1104                 if (!bounds) { return false; }
1105
1106                 bounds = L.latLngBounds(bounds);
1107
1108                 return this._southWest.equals(bounds.getSouthWest()) &&
1109                        this._northEast.equals(bounds.getNorthEast());
1110         },
1111
1112         isValid: function () {
1113                 return !!(this._southWest && this._northEast);
1114         }
1115 });
1116
1117 //TODO International date line?
1118
1119 L.latLngBounds = function (a, b) { // (LatLngBounds) or (LatLng, LatLng)
1120         if (!a || a instanceof L.LatLngBounds) {
1121                 return a;
1122         }
1123         return new L.LatLngBounds(a, b);
1124 };
1125
1126
1127 /*
1128  * L.Projection contains various geographical projections used by CRS classes.
1129  */
1130
1131 L.Projection = {};
1132
1133
1134
1135 L.Projection.SphericalMercator = {
1136         MAX_LATITUDE: 85.0511287798,
1137
1138         project: function (latlng) { // (LatLng) -> Point
1139                 var d = L.LatLng.DEG_TO_RAD,
1140                     max = this.MAX_LATITUDE,
1141                     lat = Math.max(Math.min(max, latlng.lat), -max),
1142                     x = latlng.lng * d,
1143                     y = lat * d;
1144
1145                 y = Math.log(Math.tan((Math.PI / 4) + (y / 2)));
1146
1147                 return new L.Point(x, y);
1148         },
1149
1150         unproject: function (point) { // (Point, Boolean) -> LatLng
1151                 var d = L.LatLng.RAD_TO_DEG,
1152                     lng = point.x * d,
1153                     lat = (2 * Math.atan(Math.exp(point.y)) - (Math.PI / 2)) * d;
1154
1155                 // TODO refactor LatLng wrapping
1156                 return new L.LatLng(lat, lng, true);
1157         }
1158 };
1159
1160
1161
1162 L.Projection.LonLat = {
1163         project: function (latlng) {
1164                 return new L.Point(latlng.lng, latlng.lat);
1165         },
1166
1167         unproject: function (point) {
1168                 return new L.LatLng(point.y, point.x, true);
1169         }
1170 };
1171
1172
1173
1174 L.CRS = {
1175         latLngToPoint: function (latlng, zoom) { // (LatLng, Number) -> Point
1176                 var projectedPoint = this.projection.project(latlng),
1177                     scale = this.scale(zoom);
1178
1179                 return this.transformation._transform(projectedPoint, scale);
1180         },
1181
1182         pointToLatLng: function (point, zoom) { // (Point, Number[, Boolean]) -> LatLng
1183                 var scale = this.scale(zoom),
1184                     untransformedPoint = this.transformation.untransform(point, scale);
1185
1186                 return this.projection.unproject(untransformedPoint);
1187         },
1188
1189         project: function (latlng) {
1190                 return this.projection.project(latlng);
1191         },
1192
1193         scale: function (zoom) {
1194                 return 256 * Math.pow(2, zoom);
1195         }
1196 };
1197
1198
1199
1200 L.CRS.Simple = L.extend({}, L.CRS, {
1201         projection: L.Projection.LonLat,
1202         transformation: new L.Transformation(1, 0, 1, 0)
1203 });
1204
1205
1206
1207 L.CRS.EPSG3857 = L.extend({}, L.CRS, {
1208         code: 'EPSG:3857',
1209
1210         projection: L.Projection.SphericalMercator,
1211         transformation: new L.Transformation(0.5 / Math.PI, 0.5, -0.5 / Math.PI, 0.5),
1212
1213         project: function (latlng) { // (LatLng) -> Point
1214                 var projectedPoint = this.projection.project(latlng),
1215                     earthRadius = 6378137;
1216                 return projectedPoint.multiplyBy(earthRadius);
1217         }
1218 });
1219
1220 L.CRS.EPSG900913 = L.extend({}, L.CRS.EPSG3857, {
1221         code: 'EPSG:900913'
1222 });
1223
1224
1225
1226 L.CRS.EPSG4326 = L.extend({}, L.CRS, {
1227         code: 'EPSG:4326',
1228
1229         projection: L.Projection.LonLat,
1230         transformation: new L.Transformation(1 / 360, 0.5, -1 / 360, 0.5)
1231 });
1232
1233
1234 /*
1235  * L.Map is the central class of the API - it is used to create a map.
1236  */
1237
1238 L.Map = L.Class.extend({
1239
1240         includes: L.Mixin.Events,
1241
1242         options: {
1243                 crs: L.CRS.EPSG3857,
1244
1245                 /*
1246                 center: LatLng,
1247                 zoom: Number,
1248                 layers: Array,
1249                 */
1250
1251                 fadeAnimation: L.DomUtil.TRANSITION && !L.Browser.android23,
1252                 trackResize: true,
1253                 markerZoomAnimation: L.DomUtil.TRANSITION && L.Browser.any3d
1254         },
1255
1256         initialize: function (id, options) { // (HTMLElement or String, Object)
1257                 options = L.setOptions(this, options);
1258
1259                 this._initContainer(id);
1260                 this._initLayout();
1261                 this._initHooks();
1262                 this._initEvents();
1263
1264                 if (options.maxBounds) {
1265                         this.setMaxBounds(options.maxBounds);
1266                 }
1267
1268                 if (options.center && options.zoom !== undefined) {
1269                         this.setView(L.latLng(options.center), options.zoom, true);
1270                 }
1271
1272                 this._initLayers(options.layers);
1273         },
1274
1275
1276         // public methods that modify map state
1277
1278         // replaced by animation-powered implementation in Map.PanAnimation.js
1279         setView: function (center, zoom) {
1280                 this._resetView(L.latLng(center), this._limitZoom(zoom));
1281                 return this;
1282         },
1283
1284         setZoom: function (zoom) { // (Number)
1285                 return this.setView(this.getCenter(), zoom);
1286         },
1287
1288         zoomIn: function (delta) {
1289                 return this.setZoom(this._zoom + (delta || 1));
1290         },
1291
1292         zoomOut: function (delta) {
1293                 return this.setZoom(this._zoom - (delta || 1));
1294         },
1295
1296         fitBounds: function (bounds) { // (LatLngBounds)
1297                 var zoom = this.getBoundsZoom(bounds);
1298                 return this.setView(L.latLngBounds(bounds).getCenter(), zoom);
1299         },
1300
1301         fitWorld: function () {
1302                 var sw = new L.LatLng(-60, -170),
1303                     ne = new L.LatLng(85, 179);
1304
1305                 return this.fitBounds(new L.LatLngBounds(sw, ne));
1306         },
1307
1308         panTo: function (center) { // (LatLng)
1309                 return this.setView(center, this._zoom);
1310         },
1311
1312         panBy: function (offset) { // (Point)
1313                 // replaced with animated panBy in Map.Animation.js
1314                 this.fire('movestart');
1315
1316                 this._rawPanBy(L.point(offset));
1317
1318                 this.fire('move');
1319                 return this.fire('moveend');
1320         },
1321
1322         setMaxBounds: function (bounds) {
1323                 bounds = L.latLngBounds(bounds);
1324
1325                 this.options.maxBounds = bounds;
1326
1327                 if (!bounds) {
1328                         this._boundsMinZoom = null;
1329                         return this;
1330                 }
1331
1332                 var minZoom = this.getBoundsZoom(bounds, true);
1333
1334                 this._boundsMinZoom = minZoom;
1335
1336                 if (this._loaded) {
1337                         if (this._zoom < minZoom) {
1338                                 this.setView(bounds.getCenter(), minZoom);
1339                         } else {
1340                                 this.panInsideBounds(bounds);
1341                         }
1342                 }
1343
1344                 return this;
1345         },
1346
1347         panInsideBounds: function (bounds) {
1348                 bounds = L.latLngBounds(bounds);
1349
1350                 var viewBounds = this.getBounds(),
1351                     viewSw = this.project(viewBounds.getSouthWest()),
1352                     viewNe = this.project(viewBounds.getNorthEast()),
1353                     sw = this.project(bounds.getSouthWest()),
1354                     ne = this.project(bounds.getNorthEast()),
1355                     dx = 0,
1356                     dy = 0;
1357
1358                 if (viewNe.y < ne.y) { // north
1359                         dy = ne.y - viewNe.y;
1360                 }
1361                 if (viewNe.x > ne.x) { // east
1362                         dx = ne.x - viewNe.x;
1363                 }
1364                 if (viewSw.y > sw.y) { // south
1365                         dy = sw.y - viewSw.y;
1366                 }
1367                 if (viewSw.x < sw.x) { // west
1368                         dx = sw.x - viewSw.x;
1369                 }
1370
1371                 return this.panBy(new L.Point(dx, dy, true));
1372         },
1373
1374         addLayer: function (layer) {
1375                 // TODO method is too big, refactor
1376
1377                 var id = L.stamp(layer);
1378
1379                 if (this._layers[id]) { return this; }
1380
1381                 this._layers[id] = layer;
1382
1383                 // TODO getMaxZoom, getMinZoom in ILayer (instead of options)
1384                 if (layer.options && !isNaN(layer.options.maxZoom)) {
1385                         this._layersMaxZoom = Math.max(this._layersMaxZoom || 0, layer.options.maxZoom);
1386                 }
1387                 if (layer.options && !isNaN(layer.options.minZoom)) {
1388                         this._layersMinZoom = Math.min(this._layersMinZoom || Infinity, layer.options.minZoom);
1389                 }
1390
1391                 // TODO looks ugly, refactor!!!
1392                 if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
1393                         this._tileLayersNum++;
1394             this._tileLayersToLoad++;
1395             layer.on('load', this._onTileLayerLoad, this);
1396                 }
1397
1398                 this.whenReady(function () {
1399                         layer.onAdd(this);
1400                         this.fire('layeradd', {layer: layer});
1401                 }, this);
1402
1403                 return this;
1404         },
1405
1406         removeLayer: function (layer) {
1407                 var id = L.stamp(layer);
1408
1409                 if (!this._layers[id]) { return; }
1410
1411                 layer.onRemove(this);
1412
1413                 delete this._layers[id];
1414
1415                 // TODO looks ugly, refactor
1416                 if (this.options.zoomAnimation && L.TileLayer && (layer instanceof L.TileLayer)) {
1417                         this._tileLayersNum--;
1418             this._tileLayersToLoad--;
1419             layer.off('load', this._onTileLayerLoad, this);
1420                 }
1421
1422                 return this.fire('layerremove', {layer: layer});
1423         },
1424
1425         hasLayer: function (layer) {
1426                 var id = L.stamp(layer);
1427                 return this._layers.hasOwnProperty(id);
1428         },
1429
1430         invalidateSize: function (animate) {
1431                 var oldSize = this.getSize();
1432
1433                 this._sizeChanged = true;
1434
1435                 if (this.options.maxBounds) {
1436                         this.setMaxBounds(this.options.maxBounds);
1437                 }
1438
1439                 if (!this._loaded) { return this; }
1440
1441                 var offset = oldSize._subtract(this.getSize())._divideBy(2)._round();
1442
1443                 if (animate === true) {
1444                         this.panBy(offset);
1445                 } else {
1446                         this._rawPanBy(offset);
1447
1448                         this.fire('move');
1449
1450                         clearTimeout(this._sizeTimer);
1451                         this._sizeTimer = setTimeout(L.bind(this.fire, this, 'moveend'), 200);
1452                 }
1453                 return this;
1454         },
1455
1456         // TODO handler.addTo
1457         addHandler: function (name, HandlerClass) {
1458                 if (!HandlerClass) { return; }
1459
1460                 this[name] = new HandlerClass(this);
1461
1462                 if (this.options[name]) {
1463                         this[name].enable();
1464                 }
1465
1466                 return this;
1467         },
1468
1469
1470         // public methods for getting map state
1471
1472         getCenter: function () { // (Boolean) -> LatLng
1473                 return this.layerPointToLatLng(this._getCenterLayerPoint());
1474         },
1475
1476         getZoom: function () {
1477                 return this._zoom;
1478         },
1479
1480         getBounds: function () {
1481                 var bounds = this.getPixelBounds(),
1482                     sw = this.unproject(bounds.getBottomLeft()),
1483                     ne = this.unproject(bounds.getTopRight());
1484
1485                 return new L.LatLngBounds(sw, ne);
1486         },
1487
1488         getMinZoom: function () {
1489                 var z1 = this.options.minZoom || 0,
1490                     z2 = this._layersMinZoom || 0,
1491                     z3 = this._boundsMinZoom || 0;
1492
1493                 return Math.max(z1, z2, z3);
1494         },
1495
1496         getMaxZoom: function () {
1497                 var z1 = this.options.maxZoom === undefined ? Infinity : this.options.maxZoom,
1498                     z2 = this._layersMaxZoom  === undefined ? Infinity : this._layersMaxZoom;
1499
1500                 return Math.min(z1, z2);
1501         },
1502
1503         getBoundsZoom: function (bounds, inside) { // (LatLngBounds, Boolean) -> Number
1504                 bounds = L.latLngBounds(bounds);
1505
1506                 var size = this.getSize(),
1507                     zoom = this.options.minZoom || 0,
1508                     maxZoom = this.getMaxZoom(),
1509                     ne = bounds.getNorthEast(),
1510                     sw = bounds.getSouthWest(),
1511                     boundsSize,
1512                     nePoint,
1513                     swPoint,
1514                     zoomNotFound = true;
1515
1516                 if (inside) {
1517                         zoom--;
1518                 }
1519
1520                 do {
1521                         zoom++;
1522                         nePoint = this.project(ne, zoom);
1523                         swPoint = this.project(sw, zoom);
1524
1525                         boundsSize = new L.Point(
1526                                 Math.abs(nePoint.x - swPoint.x),
1527                                 Math.abs(swPoint.y - nePoint.y));
1528
1529                         if (!inside) {
1530                                 zoomNotFound = boundsSize.x <= size.x && boundsSize.y <= size.y;
1531                         } else {
1532                                 zoomNotFound = boundsSize.x < size.x || boundsSize.y < size.y;
1533                         }
1534                 } while (zoomNotFound && zoom <= maxZoom);
1535
1536                 if (zoomNotFound && inside) {
1537                         return null;
1538                 }
1539
1540                 return inside ? zoom : zoom - 1;
1541         },
1542
1543         getSize: function () {
1544                 if (!this._size || this._sizeChanged) {
1545                         this._size = new L.Point(
1546                                 this._container.clientWidth,
1547                                 this._container.clientHeight);
1548
1549                         this._sizeChanged = false;
1550                 }
1551                 return this._size.clone();
1552         },
1553
1554         getPixelBounds: function () {
1555                 var topLeftPoint = this._getTopLeftPoint();
1556                 return new L.Bounds(topLeftPoint, topLeftPoint.add(this.getSize()));
1557         },
1558
1559         getPixelOrigin: function () {
1560                 return this._initialTopLeftPoint;
1561         },
1562
1563         getPanes: function () {
1564                 return this._panes;
1565         },
1566
1567         getContainer: function () {
1568                 return this._container;
1569         },
1570
1571
1572         // TODO replace with universal implementation after refactoring projections
1573
1574         getZoomScale: function (toZoom) {
1575                 var crs = this.options.crs;
1576                 return crs.scale(toZoom) / crs.scale(this._zoom);
1577         },
1578
1579         getScaleZoom: function (scale) {
1580                 return this._zoom + (Math.log(scale) / Math.LN2);
1581         },
1582
1583
1584         // conversion methods
1585
1586         project: function (latlng, zoom) { // (LatLng[, Number]) -> Point
1587                 zoom = zoom === undefined ? this._zoom : zoom;
1588                 return this.options.crs.latLngToPoint(L.latLng(latlng), zoom);
1589         },
1590
1591         unproject: function (point, zoom) { // (Point[, Number]) -> LatLng
1592                 zoom = zoom === undefined ? this._zoom : zoom;
1593                 return this.options.crs.pointToLatLng(L.point(point), zoom);
1594         },
1595
1596         layerPointToLatLng: function (point) { // (Point)
1597                 var projectedPoint = L.point(point).add(this._initialTopLeftPoint);
1598                 return this.unproject(projectedPoint);
1599         },
1600
1601         latLngToLayerPoint: function (latlng) { // (LatLng)
1602                 var projectedPoint = this.project(L.latLng(latlng))._round();
1603                 return projectedPoint._subtract(this._initialTopLeftPoint);
1604         },
1605
1606         containerPointToLayerPoint: function (point) { // (Point)
1607                 return L.point(point).subtract(this._getMapPanePos());
1608         },
1609
1610         layerPointToContainerPoint: function (point) { // (Point)
1611                 return L.point(point).add(this._getMapPanePos());
1612         },
1613
1614         containerPointToLatLng: function (point) {
1615                 var layerPoint = this.containerPointToLayerPoint(L.point(point));
1616                 return this.layerPointToLatLng(layerPoint);
1617         },
1618
1619         latLngToContainerPoint: function (latlng) {
1620                 return this.layerPointToContainerPoint(this.latLngToLayerPoint(L.latLng(latlng)));
1621         },
1622
1623         mouseEventToContainerPoint: function (e) { // (MouseEvent)
1624                 return L.DomEvent.getMousePosition(e, this._container);
1625         },
1626
1627         mouseEventToLayerPoint: function (e) { // (MouseEvent)
1628                 return this.containerPointToLayerPoint(this.mouseEventToContainerPoint(e));
1629         },
1630
1631         mouseEventToLatLng: function (e) { // (MouseEvent)
1632                 return this.layerPointToLatLng(this.mouseEventToLayerPoint(e));
1633         },
1634
1635
1636         // map initialization methods
1637
1638         _initContainer: function (id) {
1639                 var container = this._container = L.DomUtil.get(id);
1640
1641                 if (container._leaflet) {
1642                         throw new Error("Map container is already initialized.");
1643                 }
1644
1645                 container._leaflet = true;
1646         },
1647
1648         _initLayout: function () {
1649                 var container = this._container;
1650
1651                 container.innerHTML = '';
1652                 L.DomUtil.addClass(container, 'leaflet-container');
1653
1654                 if (L.Browser.touch) {
1655                         L.DomUtil.addClass(container, 'leaflet-touch');
1656                 }
1657
1658                 if (this.options.fadeAnimation) {
1659                         L.DomUtil.addClass(container, 'leaflet-fade-anim');
1660                 }
1661
1662                 var position = L.DomUtil.getStyle(container, 'position');
1663
1664                 if (position !== 'absolute' && position !== 'relative' && position !== 'fixed') {
1665                         container.style.position = 'relative';
1666                 }
1667
1668                 this._initPanes();
1669
1670                 if (this._initControlPos) {
1671                         this._initControlPos();
1672                 }
1673         },
1674
1675         _initPanes: function () {
1676                 var panes = this._panes = {};
1677
1678                 this._mapPane = panes.mapPane = this._createPane('leaflet-map-pane', this._container);
1679
1680                 this._tilePane = panes.tilePane = this._createPane('leaflet-tile-pane', this._mapPane);
1681                 panes.objectsPane = this._createPane('leaflet-objects-pane', this._mapPane);
1682                 panes.shadowPane = this._createPane('leaflet-shadow-pane');
1683                 panes.overlayPane = this._createPane('leaflet-overlay-pane');
1684                 panes.markerPane = this._createPane('leaflet-marker-pane');
1685                 panes.popupPane = this._createPane('leaflet-popup-pane');
1686
1687                 var zoomHide = ' leaflet-zoom-hide';
1688
1689                 if (!this.options.markerZoomAnimation) {
1690                         L.DomUtil.addClass(panes.markerPane, zoomHide);
1691                         L.DomUtil.addClass(panes.shadowPane, zoomHide);
1692                         L.DomUtil.addClass(panes.popupPane, zoomHide);
1693                 }
1694         },
1695
1696         _createPane: function (className, container) {
1697                 return L.DomUtil.create('div', className, container || this._panes.objectsPane);
1698         },
1699
1700         _initializers: [],
1701
1702         _initHooks: function () {
1703                 var i, len;
1704                 for (i = 0, len = this._initializers.length; i < len; i++) {
1705                         this._initializers[i].call(this);
1706                 }
1707         },
1708
1709         _initLayers: function (layers) {
1710                 layers = layers ? (layers instanceof Array ? layers : [layers]) : [];
1711
1712                 this._layers = {};
1713                 this._tileLayersNum = 0;
1714
1715                 var i, len;
1716
1717                 for (i = 0, len = layers.length; i < len; i++) {
1718                         this.addLayer(layers[i]);
1719                 }
1720         },
1721
1722
1723         // private methods that modify map state
1724
1725         _resetView: function (center, zoom, preserveMapOffset, afterZoomAnim) {
1726
1727                 var zoomChanged = (this._zoom !== zoom);
1728
1729                 if (!afterZoomAnim) {
1730                         this.fire('movestart');
1731
1732                         if (zoomChanged) {
1733                                 this.fire('zoomstart');
1734                         }
1735                 }
1736
1737                 this._zoom = zoom;
1738
1739                 this._initialTopLeftPoint = this._getNewTopLeftPoint(center);
1740
1741                 if (!preserveMapOffset) {
1742                         L.DomUtil.setPosition(this._mapPane, new L.Point(0, 0));
1743                 } else {
1744                         this._initialTopLeftPoint._add(this._getMapPanePos());
1745                 }
1746
1747                 this._tileLayersToLoad = this._tileLayersNum;
1748
1749                 var loading = !this._loaded;
1750                 this._loaded = true;
1751
1752                 this.fire('viewreset', {hard: !preserveMapOffset});
1753
1754                 this.fire('move');
1755
1756                 if (zoomChanged || afterZoomAnim) {
1757                         this.fire('zoomend');
1758                 }
1759
1760                 this.fire('moveend', {hard: !preserveMapOffset});
1761
1762                 if (loading) {
1763                         this.fire('load');
1764                 }
1765         },
1766
1767         _rawPanBy: function (offset) {
1768                 L.DomUtil.setPosition(this._mapPane, this._getMapPanePos().subtract(offset));
1769         },
1770
1771
1772         // map events
1773
1774         _initEvents: function () {
1775                 if (!L.DomEvent) { return; }
1776
1777                 L.DomEvent.on(this._container, 'click', this._onMouseClick, this);
1778
1779                 var events = ['dblclick', 'mousedown', 'mouseup', 'mouseenter',
1780                               'mouseleave', 'mousemove', 'contextmenu'],
1781                     i, len;
1782
1783                 for (i = 0, len = events.length; i < len; i++) {
1784                         L.DomEvent.on(this._container, events[i], this._fireMouseEvent, this);
1785                 }
1786
1787                 if (this.options.trackResize) {
1788                         L.DomEvent.on(window, 'resize', this._onResize, this);
1789                 }
1790         },
1791
1792         _onResize: function () {
1793                 L.Util.cancelAnimFrame(this._resizeRequest);
1794                 this._resizeRequest = L.Util.requestAnimFrame(
1795                         this.invalidateSize, this, false, this._container);
1796         },
1797
1798         _onMouseClick: function (e) {
1799                 if (!this._loaded || (this.dragging && this.dragging.moved())) { return; }
1800
1801                 this.fire('preclick');
1802                 this._fireMouseEvent(e);
1803         },
1804
1805         _fireMouseEvent: function (e) {
1806                 if (!this._loaded) { return; }
1807
1808                 var type = e.type;
1809
1810                 type = (type === 'mouseenter' ? 'mouseover' : (type === 'mouseleave' ? 'mouseout' : type));
1811
1812                 if (!this.hasEventListeners(type)) { return; }
1813
1814                 if (type === 'contextmenu') {
1815                         L.DomEvent.preventDefault(e);
1816                 }
1817
1818                 var containerPoint = this.mouseEventToContainerPoint(e),
1819                     layerPoint = this.containerPointToLayerPoint(containerPoint),
1820                     latlng = this.layerPointToLatLng(layerPoint);
1821
1822                 this.fire(type, {
1823                         latlng: latlng,
1824                         layerPoint: layerPoint,
1825                         containerPoint: containerPoint,
1826                         originalEvent: e
1827                 });
1828         },
1829
1830         _onTileLayerLoad: function () {
1831                 // TODO super-ugly, refactor!!!
1832                 // clear scaled tiles after all new tiles are loaded (for performance)
1833                 this._tileLayersToLoad--;
1834                 if (this._tileLayersNum && !this._tileLayersToLoad && this._tileBg) {
1835                         clearTimeout(this._clearTileBgTimer);
1836                         this._clearTileBgTimer = setTimeout(L.bind(this._clearTileBg, this), 500);
1837                 }
1838         },
1839
1840         whenReady: function (callback, context) {
1841                 if (this._loaded) {
1842                         callback.call(context || this, this);
1843                 } else {
1844                         this.on('load', callback, context);
1845                 }
1846                 return this;
1847         },
1848
1849
1850         // private methods for getting map state
1851
1852         _getMapPanePos: function () {
1853                 return L.DomUtil.getPosition(this._mapPane);
1854         },
1855
1856         _getTopLeftPoint: function () {
1857                 if (!this._loaded) {
1858                         throw new Error('Set map center and zoom first.');
1859                 }
1860
1861                 return this._initialTopLeftPoint.subtract(this._getMapPanePos());
1862         },
1863
1864         _getNewTopLeftPoint: function (center, zoom) {
1865                 var viewHalf = this.getSize()._divideBy(2);
1866                 // TODO round on display, not calculation to increase precision?
1867                 return this.project(center, zoom)._subtract(viewHalf)._round();
1868         },
1869
1870         _latLngToNewLayerPoint: function (latlng, newZoom, newCenter) {
1871                 var topLeft = this._getNewTopLeftPoint(newCenter, newZoom).add(this._getMapPanePos());
1872                 return this.project(latlng, newZoom)._subtract(topLeft);
1873         },
1874
1875         _getCenterLayerPoint: function () {
1876                 return this.containerPointToLayerPoint(this.getSize()._divideBy(2));
1877         },
1878
1879         _getCenterOffset: function (center) {
1880                 return this.latLngToLayerPoint(center).subtract(this._getCenterLayerPoint());
1881         },
1882
1883         _limitZoom: function (zoom) {
1884                 var min = this.getMinZoom(),
1885                     max = this.getMaxZoom();
1886
1887                 return Math.max(min, Math.min(max, zoom));
1888         }
1889 });
1890
1891 L.Map.addInitHook = function (fn) {
1892         var args = Array.prototype.slice.call(arguments, 1);
1893
1894         var init = typeof fn === 'function' ? fn : function () {
1895                 this[fn].apply(this, args);
1896         };
1897
1898         this.prototype._initializers.push(init);
1899 };
1900
1901 L.map = function (id, options) {
1902         return new L.Map(id, options);
1903 };
1904
1905
1906
1907 L.Projection.Mercator = {
1908         MAX_LATITUDE: 85.0840591556,
1909
1910         R_MINOR: 6356752.3142,
1911         R_MAJOR: 6378137,
1912
1913         project: function (latlng) { // (LatLng) -> Point
1914                 var d = L.LatLng.DEG_TO_RAD,
1915                     max = this.MAX_LATITUDE,
1916                     lat = Math.max(Math.min(max, latlng.lat), -max),
1917                     r = this.R_MAJOR,
1918                     r2 = this.R_MINOR,
1919                     x = latlng.lng * d * r,
1920                     y = lat * d,
1921                     tmp = r2 / r,
1922                     eccent = Math.sqrt(1.0 - tmp * tmp),
1923                     con = eccent * Math.sin(y);
1924
1925                 con = Math.pow((1 - con) / (1 + con), eccent * 0.5);
1926
1927                 var ts = Math.tan(0.5 * ((Math.PI * 0.5) - y)) / con;
1928                 y = -r2 * Math.log(ts);
1929
1930                 return new L.Point(x, y);
1931         },
1932
1933         unproject: function (point) { // (Point, Boolean) -> LatLng
1934                 var d = L.LatLng.RAD_TO_DEG,
1935                     r = this.R_MAJOR,
1936                     r2 = this.R_MINOR,
1937                     lng = point.x * d / r,
1938                     tmp = r2 / r,
1939                     eccent = Math.sqrt(1 - (tmp * tmp)),
1940                     ts = Math.exp(- point.y / r2),
1941                     phi = (Math.PI / 2) - 2 * Math.atan(ts),
1942                     numIter = 15,
1943                     tol = 1e-7,
1944                     i = numIter,
1945                     dphi = 0.1,
1946                     con;
1947
1948                 while ((Math.abs(dphi) > tol) && (--i > 0)) {
1949                         con = eccent * Math.sin(phi);
1950                         dphi = (Math.PI / 2) - 2 * Math.atan(ts *
1951                                     Math.pow((1.0 - con) / (1.0 + con), 0.5 * eccent)) - phi;
1952                         phi += dphi;
1953                 }
1954
1955                 return new L.LatLng(phi * d, lng, true);
1956         }
1957 };
1958
1959
1960
1961 L.CRS.EPSG3395 = L.extend({}, L.CRS, {
1962         code: 'EPSG:3395',
1963
1964         projection: L.Projection.Mercator,
1965
1966         transformation: (function () {
1967                 var m = L.Projection.Mercator,
1968                     r = m.R_MAJOR,
1969                     r2 = m.R_MINOR;
1970
1971                 return new L.Transformation(0.5 / (Math.PI * r), 0.5, -0.5 / (Math.PI * r2), 0.5);
1972         }())
1973 });
1974
1975
1976 /*
1977  * L.TileLayer is used for standard xyz-numbered tile layers.
1978  */
1979
1980 L.TileLayer = L.Class.extend({
1981         includes: L.Mixin.Events,
1982
1983         options: {
1984                 minZoom: 0,
1985                 maxZoom: 18,
1986                 tileSize: 256,
1987                 subdomains: 'abc',
1988                 errorTileUrl: '',
1989                 attribution: '',
1990                 zoomOffset: 0,
1991                 opacity: 1,
1992                 /* (undefined works too)
1993                 zIndex: null,
1994                 tms: false,
1995                 continuousWorld: false,
1996                 noWrap: false,
1997                 zoomReverse: false,
1998                 detectRetina: false,
1999                 reuseTiles: false,
2000                 */
2001                 unloadInvisibleTiles: L.Browser.mobile,
2002                 updateWhenIdle: L.Browser.mobile
2003         },
2004
2005         initialize: function (url, options) {
2006                 options = L.setOptions(this, options);
2007
2008                 // detecting retina displays, adjusting tileSize and zoom levels
2009                 if (options.detectRetina && L.Browser.retina && options.maxZoom > 0) {
2010
2011                         options.tileSize = Math.floor(options.tileSize / 2);
2012                         options.zoomOffset++;
2013
2014                         if (options.minZoom > 0) {
2015                                 options.minZoom--;
2016                         }
2017                         this.options.maxZoom--;
2018                 }
2019
2020                 this._url = url;
2021
2022                 var subdomains = this.options.subdomains;
2023
2024                 if (typeof subdomains === 'string') {
2025                         this.options.subdomains = subdomains.split('');
2026                 }
2027         },
2028
2029         onAdd: function (map) {
2030                 this._map = map;
2031
2032                 // create a container div for tiles
2033                 this._initContainer();
2034
2035                 // create an image to clone for tiles
2036                 this._createTileProto();
2037
2038                 // set up events
2039                 map.on({
2040                         'viewreset': this._resetCallback,
2041                         'moveend': this._update
2042                 }, this);
2043
2044                 if (!this.options.updateWhenIdle) {
2045                         this._limitedUpdate = L.Util.limitExecByInterval(this._update, 150, this);
2046                         map.on('move', this._limitedUpdate, this);
2047                 }
2048
2049                 this._reset();
2050                 this._update();
2051         },
2052
2053         addTo: function (map) {
2054                 map.addLayer(this);
2055                 return this;
2056         },
2057
2058         onRemove: function (map) {
2059                 map._panes.tilePane.removeChild(this._container);
2060
2061                 map.off({
2062                         'viewreset': this._resetCallback,
2063                         'moveend': this._update
2064                 }, this);
2065
2066                 if (!this.options.updateWhenIdle) {
2067                         map.off('move', this._limitedUpdate, this);
2068                 }
2069
2070                 this._container = null;
2071                 this._map = null;
2072         },
2073
2074         bringToFront: function () {
2075                 var pane = this._map._panes.tilePane;
2076
2077                 if (this._container) {
2078                         pane.appendChild(this._container);
2079                         this._setAutoZIndex(pane, Math.max);
2080                 }
2081
2082                 return this;
2083         },
2084
2085         bringToBack: function () {
2086                 var pane = this._map._panes.tilePane;
2087
2088                 if (this._container) {
2089                         pane.insertBefore(this._container, pane.firstChild);
2090                         this._setAutoZIndex(pane, Math.min);
2091                 }
2092
2093                 return this;
2094         },
2095
2096         getAttribution: function () {
2097                 return this.options.attribution;
2098         },
2099
2100         setOpacity: function (opacity) {
2101                 this.options.opacity = opacity;
2102
2103                 if (this._map) {
2104                         this._updateOpacity();
2105                 }
2106
2107                 return this;
2108         },
2109
2110         setZIndex: function (zIndex) {
2111                 this.options.zIndex = zIndex;
2112                 this._updateZIndex();
2113
2114                 return this;
2115         },
2116
2117         setUrl: function (url, noRedraw) {
2118                 this._url = url;
2119
2120                 if (!noRedraw) {
2121                         this.redraw();
2122                 }
2123
2124                 return this;
2125         },
2126
2127         redraw: function () {
2128                 if (this._map) {
2129                         this._map._panes.tilePane.empty = false;
2130                         this._reset(true);
2131                         this._update();
2132                 }
2133                 return this;
2134         },
2135
2136         _updateZIndex: function () {
2137                 if (this._container && this.options.zIndex !== undefined) {
2138                         this._container.style.zIndex = this.options.zIndex;
2139                 }
2140         },
2141
2142         _setAutoZIndex: function (pane, compare) {
2143
2144                 var layers = pane.getElementsByClassName('leaflet-layer'),
2145                     edgeZIndex = -compare(Infinity, -Infinity), // -Ifinity for max, Infinity for min
2146                     zIndex, i, len;
2147
2148                 for (i = 0, len = layers.length; i < len; i++) {
2149
2150                         if (layers[i] !== this._container) {
2151                                 zIndex = parseInt(layers[i].style.zIndex, 10);
2152
2153                                 if (!isNaN(zIndex)) {
2154                                         edgeZIndex = compare(edgeZIndex, zIndex);
2155                                 }
2156                         }
2157                 }
2158
2159                 this.options.zIndex = this._container.style.zIndex =
2160                         (isFinite(edgeZIndex) ? edgeZIndex : 0) + compare(1, -1);
2161         },
2162
2163         _updateOpacity: function () {
2164                 L.DomUtil.setOpacity(this._container, this.options.opacity);
2165
2166                 // stupid webkit hack to force redrawing of tiles
2167                 var i,
2168                     tiles = this._tiles;
2169
2170                 if (L.Browser.webkit) {
2171                         for (i in tiles) {
2172                                 if (tiles.hasOwnProperty(i)) {
2173                                         tiles[i].style.webkitTransform += ' translate(0,0)';
2174                                 }
2175                         }
2176                 }
2177         },
2178
2179         _initContainer: function () {
2180                 var tilePane = this._map._panes.tilePane;
2181
2182                 if (!this._container || tilePane.empty) {
2183                         this._container = L.DomUtil.create('div', 'leaflet-layer');
2184
2185                         this._updateZIndex();
2186
2187                         tilePane.appendChild(this._container);
2188
2189                         if (this.options.opacity < 1) {
2190                                 this._updateOpacity();
2191                         }
2192                 }
2193         },
2194
2195         _resetCallback: function (e) {
2196                 this._reset(e.hard);
2197         },
2198
2199         _reset: function (clearOldContainer) {
2200                 var tiles = this._tiles;
2201
2202                 for (var key in tiles) {
2203                         if (tiles.hasOwnProperty(key)) {
2204                                 this.fire('tileunload', {tile: tiles[key]});
2205                         }
2206                 }
2207
2208                 this._tiles = {};
2209                 this._tilesToLoad = 0;
2210
2211                 if (this.options.reuseTiles) {
2212                         this._unusedTiles = [];
2213                 }
2214
2215                 if (clearOldContainer && this._container) {
2216                         this._container.innerHTML = "";
2217                 }
2218
2219                 this._initContainer();
2220         },
2221
2222         _update: function (e) {
2223
2224                 if (!this._map) { return; }
2225
2226                 var bounds = this._map.getPixelBounds(),
2227                     zoom = this._map.getZoom(),
2228                     tileSize = this.options.tileSize;
2229
2230                 if (zoom > this.options.maxZoom || zoom < this.options.minZoom) {
2231                         return;
2232                 }
2233
2234                 var nwTilePoint = new L.Point(
2235                         Math.floor(bounds.min.x / tileSize),
2236                         Math.floor(bounds.min.y / tileSize)),
2237
2238                     seTilePoint = new L.Point(
2239                         Math.floor(bounds.max.x / tileSize),
2240                         Math.floor(bounds.max.y / tileSize)),
2241
2242                     tileBounds = new L.Bounds(nwTilePoint, seTilePoint);
2243
2244                 this._addTilesFromCenterOut(tileBounds);
2245
2246                 if (this.options.unloadInvisibleTiles || this.options.reuseTiles) {
2247                         this._removeOtherTiles(tileBounds);
2248                 }
2249         },
2250
2251         _addTilesFromCenterOut: function (bounds) {
2252                 var queue = [],
2253                     center = bounds.getCenter();
2254
2255                 var j, i, point;
2256
2257                 for (j = bounds.min.y; j <= bounds.max.y; j++) {
2258                         for (i = bounds.min.x; i <= bounds.max.x; i++) {
2259                                 point = new L.Point(i, j);
2260
2261                                 if (this._tileShouldBeLoaded(point)) {
2262                                         queue.push(point);
2263                                 }
2264                         }
2265                 }
2266
2267                 var tilesToLoad = queue.length;
2268
2269                 if (tilesToLoad === 0) { return; }
2270
2271                 // load tiles in order of their distance to center
2272                 queue.sort(function (a, b) {
2273                         return a.distanceTo(center) - b.distanceTo(center);
2274                 });
2275
2276                 var fragment = document.createDocumentFragment();
2277
2278                 // if its the first batch of tiles to load
2279                 if (!this._tilesToLoad) {
2280                         this.fire('loading');
2281                 }
2282
2283                 this._tilesToLoad += tilesToLoad;
2284
2285                 for (i = 0; i < tilesToLoad; i++) {
2286                         this._addTile(queue[i], fragment);
2287                 }
2288
2289                 this._container.appendChild(fragment);
2290         },
2291
2292         _tileShouldBeLoaded: function (tilePoint) {
2293                 if ((tilePoint.x + ':' + tilePoint.y) in this._tiles) {
2294                         return false; // already loaded
2295                 }
2296
2297                 if (!this.options.continuousWorld) {
2298                         var limit = this._getWrapTileNum();
2299
2300                         if (this.options.noWrap && (tilePoint.x < 0 || tilePoint.x >= limit) ||
2301                                                         tilePoint.y < 0 || tilePoint.y >= limit) {
2302                                 return false; // exceeds world bounds
2303                         }
2304                 }
2305
2306                 return true;
2307         },
2308
2309         _removeOtherTiles: function (bounds) {
2310                 var kArr, x, y, key;
2311
2312                 for (key in this._tiles) {
2313                         if (this._tiles.hasOwnProperty(key)) {
2314                                 kArr = key.split(':');
2315                                 x = parseInt(kArr[0], 10);
2316                                 y = parseInt(kArr[1], 10);
2317
2318                                 // remove tile if it's out of bounds
2319                                 if (x < bounds.min.x || x > bounds.max.x || y < bounds.min.y || y > bounds.max.y) {
2320                                         this._removeTile(key);
2321                                 }
2322                         }
2323                 }
2324         },
2325
2326         _removeTile: function (key) {
2327                 var tile = this._tiles[key];
2328
2329                 this.fire("tileunload", {tile: tile, url: tile.src});
2330
2331                 if (this.options.reuseTiles) {
2332                         L.DomUtil.removeClass(tile, 'leaflet-tile-loaded');
2333                         this._unusedTiles.push(tile);
2334
2335                 } else if (tile.parentNode === this._container) {
2336                         this._container.removeChild(tile);
2337                 }
2338
2339                 // for https://github.com/CloudMade/Leaflet/issues/137
2340                 if (!L.Browser.android) {
2341                         tile.src = L.Util.emptyImageUrl;
2342                 }
2343
2344                 delete this._tiles[key];
2345         },
2346
2347         _addTile: function (tilePoint, container) {
2348                 var tilePos = this._getTilePos(tilePoint);
2349
2350                 // get unused tile - or create a new tile
2351                 var tile = this._getTile();
2352
2353                 /*
2354                 Chrome 20 layouts much faster with top/left (verify with timeline, frames)
2355                 Android 4 browser has display issues with top/left and requires transform instead
2356                 Android 3 browser not tested
2357                 Android 2 browser requires top/left or tiles disappear on load or first drag
2358                 (reappear after zoom) https://github.com/CloudMade/Leaflet/issues/866
2359                 (other browsers don't currently care) - see debug/hacks/jitter.html for an example
2360                 */
2361                 L.DomUtil.setPosition(tile, tilePos, L.Browser.chrome || L.Browser.android23);
2362
2363                 this._tiles[tilePoint.x + ':' + tilePoint.y] = tile;
2364
2365                 this._loadTile(tile, tilePoint);
2366
2367                 if (tile.parentNode !== this._container) {
2368                         container.appendChild(tile);
2369                 }
2370         },
2371
2372         _getZoomForUrl: function () {
2373
2374                 var options = this.options,
2375                     zoom = this._map.getZoom();
2376
2377                 if (options.zoomReverse) {
2378                         zoom = options.maxZoom - zoom;
2379                 }
2380
2381                 return zoom + options.zoomOffset;
2382         },
2383
2384         _getTilePos: function (tilePoint) {
2385                 var origin = this._map.getPixelOrigin(),
2386                     tileSize = this.options.tileSize;
2387
2388                 return tilePoint.multiplyBy(tileSize).subtract(origin);
2389         },
2390
2391         // image-specific code (override to implement e.g. Canvas or SVG tile layer)
2392
2393         getTileUrl: function (tilePoint) {
2394                 this._adjustTilePoint(tilePoint);
2395
2396                 return L.Util.template(this._url, L.extend({
2397                         s: this._getSubdomain(tilePoint),
2398                         z: this._getZoomForUrl(),
2399                         x: tilePoint.x,
2400                         y: tilePoint.y
2401                 }, this.options));
2402         },
2403
2404         _getWrapTileNum: function () {
2405                 // TODO refactor, limit is not valid for non-standard projections
2406                 return Math.pow(2, this._getZoomForUrl());
2407         },
2408
2409         _adjustTilePoint: function (tilePoint) {
2410
2411                 var limit = this._getWrapTileNum();
2412
2413                 // wrap tile coordinates
2414                 if (!this.options.continuousWorld && !this.options.noWrap) {
2415                         tilePoint.x = ((tilePoint.x % limit) + limit) % limit;
2416                 }
2417
2418                 if (this.options.tms) {
2419                         tilePoint.y = limit - tilePoint.y - 1;
2420                 }
2421         },
2422
2423         _getSubdomain: function (tilePoint) {
2424                 var index = (tilePoint.x + tilePoint.y) % this.options.subdomains.length;
2425                 return this.options.subdomains[index];
2426         },
2427
2428         _createTileProto: function () {
2429                 var img = this._tileImg = L.DomUtil.create('img', 'leaflet-tile');
2430                 img.style.width = img.style.height = this.options.tileSize + 'px';
2431                 img.galleryimg = 'no';
2432         },
2433
2434         _getTile: function () {
2435                 if (this.options.reuseTiles && this._unusedTiles.length > 0) {
2436                         var tile = this._unusedTiles.pop();
2437                         this._resetTile(tile);
2438                         return tile;
2439                 }
2440                 return this._createTile();
2441         },
2442
2443         _resetTile: function (tile) {
2444                 // Override if data stored on a tile needs to be cleaned up before reuse
2445         },
2446
2447         _createTile: function () {
2448                 var tile = this._tileImg.cloneNode(false);
2449                 tile.onselectstart = tile.onmousemove = L.Util.falseFn;
2450                 return tile;
2451         },
2452
2453         _loadTile: function (tile, tilePoint) {
2454                 tile._layer  = this;
2455                 tile.onload  = this._tileOnLoad;
2456                 tile.onerror = this._tileOnError;
2457
2458                 tile.src     = this.getTileUrl(tilePoint);
2459         },
2460
2461     _tileLoaded: function () {
2462         this._tilesToLoad--;
2463         if (!this._tilesToLoad) {
2464             this.fire('load');
2465         }
2466     },
2467
2468         _tileOnLoad: function (e) {
2469                 var layer = this._layer;
2470
2471                 //Only if we are loading an actual image
2472                 if (this.src !== L.Util.emptyImageUrl) {
2473                         L.DomUtil.addClass(this, 'leaflet-tile-loaded');
2474
2475                         layer.fire('tileload', {
2476                                 tile: this,
2477                                 url: this.src
2478                         });
2479                 }
2480
2481                 layer._tileLoaded();
2482         },
2483
2484         _tileOnError: function (e) {
2485                 var layer = this._layer;
2486
2487                 layer.fire('tileerror', {
2488                         tile: this,
2489                         url: this.src
2490                 });
2491
2492                 var newUrl = layer.options.errorTileUrl;
2493                 if (newUrl) {
2494                         this.src = newUrl;
2495                 }
2496
2497         layer._tileLoaded();
2498     }
2499 });
2500
2501 L.tileLayer = function (url, options) {
2502         return new L.TileLayer(url, options);
2503 };
2504
2505
2506 L.TileLayer.WMS = L.TileLayer.extend({
2507
2508         defaultWmsParams: {
2509                 service: 'WMS',
2510                 request: 'GetMap',
2511                 version: '1.1.1',
2512                 layers: '',
2513                 styles: '',
2514                 format: 'image/jpeg',
2515                 transparent: false
2516         },
2517
2518         initialize: function (url, options) { // (String, Object)
2519
2520                 this._url = url;
2521
2522                 var wmsParams = L.extend({}, this.defaultWmsParams);
2523
2524                 if (options.detectRetina && L.Browser.retina) {
2525                         wmsParams.width = wmsParams.height = this.options.tileSize * 2;
2526                 } else {
2527                         wmsParams.width = wmsParams.height = this.options.tileSize;
2528                 }
2529
2530                 for (var i in options) {
2531                         // all keys that are not TileLayer options go to WMS params
2532                         if (!this.options.hasOwnProperty(i)) {
2533                                 wmsParams[i] = options[i];
2534                         }
2535                 }
2536
2537                 this.wmsParams = wmsParams;
2538
2539                 L.setOptions(this, options);
2540         },
2541
2542         onAdd: function (map) {
2543
2544                 var projectionKey = parseFloat(this.wmsParams.version) >= 1.3 ? 'crs' : 'srs';
2545                 this.wmsParams[projectionKey] = map.options.crs.code;
2546
2547                 L.TileLayer.prototype.onAdd.call(this, map);
2548         },
2549
2550         getTileUrl: function (tilePoint, zoom) { // (Point, Number) -> String
2551
2552                 var map = this._map,
2553                     crs = map.options.crs,
2554                     tileSize = this.options.tileSize,
2555
2556                     nwPoint = tilePoint.multiplyBy(tileSize),
2557                     sePoint = nwPoint.add(new L.Point(tileSize, tileSize)),
2558
2559                     nw = crs.project(map.unproject(nwPoint, zoom)),
2560                     se = crs.project(map.unproject(sePoint, zoom)),
2561
2562                     bbox = [nw.x, se.y, se.x, nw.y].join(','),
2563
2564                     url = L.Util.template(this._url, {s: this._getSubdomain(tilePoint)});
2565
2566                 return url + L.Util.getParamString(this.wmsParams) + "&bbox=" + bbox;
2567         },
2568
2569         setParams: function (params, noRedraw) {
2570
2571                 L.extend(this.wmsParams, params);
2572
2573                 if (!noRedraw) {
2574                         this.redraw();
2575                 }
2576
2577                 return this;
2578         }
2579 });
2580
2581 L.tileLayer.wms = function (url, options) {
2582         return new L.TileLayer.WMS(url, options);
2583 };
2584
2585
2586 L.TileLayer.Canvas = L.TileLayer.extend({
2587         options: {
2588                 async: false
2589         },
2590
2591         initialize: function (options) {
2592                 L.setOptions(this, options);
2593         },
2594
2595         redraw: function () {
2596                 var tiles = this._tiles;
2597
2598                 for (var i in tiles) {
2599                         if (tiles.hasOwnProperty(i)) {
2600                                 this._redrawTile(tiles[i]);
2601                         }
2602                 }
2603         },
2604
2605         _redrawTile: function (tile) {
2606                 this.drawTile(tile, tile._tilePoint, this._map._zoom);
2607         },
2608
2609         _createTileProto: function () {
2610                 var proto = this._canvasProto = L.DomUtil.create('canvas', 'leaflet-tile');
2611                 proto.width = proto.height = this.options.tileSize;
2612         },
2613
2614         _createTile: function () {
2615                 var tile = this._canvasProto.cloneNode(false);
2616                 tile.onselectstart = tile.onmousemove = L.Util.falseFn;
2617                 return tile;
2618         },
2619
2620         _loadTile: function (tile, tilePoint) {
2621                 tile._layer = this;
2622                 tile._tilePoint = tilePoint;
2623
2624                 this._redrawTile(tile);
2625
2626                 if (!this.options.async) {
2627                         this.tileDrawn(tile);
2628                 }
2629         },
2630
2631         drawTile: function (tile, tilePoint) {
2632                 // override with rendering code
2633         },
2634
2635         tileDrawn: function (tile) {
2636                 this._tileOnLoad.call(tile);
2637         }
2638 });
2639
2640
2641 L.tileLayer.canvas = function (options) {
2642         return new L.TileLayer.Canvas(options);
2643 };
2644
2645
2646 L.ImageOverlay = L.Class.extend({
2647         includes: L.Mixin.Events,
2648
2649         options: {
2650                 opacity: 1
2651         },
2652
2653         initialize: function (url, bounds, options) { // (String, LatLngBounds, Object)
2654                 this._url = url;
2655                 this._bounds = L.latLngBounds(bounds);
2656
2657                 L.setOptions(this, options);
2658         },
2659
2660         onAdd: function (map) {
2661                 this._map = map;
2662
2663                 if (!this._image) {
2664                         this._initImage();
2665                 }
2666
2667                 map._panes.overlayPane.appendChild(this._image);
2668
2669                 map.on('viewreset', this._reset, this);
2670
2671                 if (map.options.zoomAnimation && L.Browser.any3d) {
2672                         map.on('zoomanim', this._animateZoom, this);
2673                 }
2674
2675                 this._reset();
2676         },
2677
2678         onRemove: function (map) {
2679                 map.getPanes().overlayPane.removeChild(this._image);
2680
2681                 map.off('viewreset', this._reset, this);
2682
2683                 if (map.options.zoomAnimation) {
2684                         map.off('zoomanim', this._animateZoom, this);
2685                 }
2686         },
2687
2688         addTo: function (map) {
2689                 map.addLayer(this);
2690                 return this;
2691         },
2692
2693         setOpacity: function (opacity) {
2694                 this.options.opacity = opacity;
2695                 this._updateOpacity();
2696                 return this;
2697         },
2698
2699         // TODO remove bringToFront/bringToBack duplication from TileLayer/Path
2700         bringToFront: function () {
2701                 if (this._image) {
2702                         this._map._panes.overlayPane.appendChild(this._image);
2703                 }
2704                 return this;
2705         },
2706
2707         bringToBack: function () {
2708                 var pane = this._map._panes.overlayPane;
2709                 if (this._image) {
2710                         pane.insertBefore(this._image, pane.firstChild);
2711                 }
2712                 return this;
2713         },
2714
2715         _initImage: function () {
2716                 this._image = L.DomUtil.create('img', 'leaflet-image-layer');
2717
2718                 if (this._map.options.zoomAnimation && L.Browser.any3d) {
2719                         L.DomUtil.addClass(this._image, 'leaflet-zoom-animated');
2720                 } else {
2721                         L.DomUtil.addClass(this._image, 'leaflet-zoom-hide');
2722                 }
2723
2724                 this._updateOpacity();
2725
2726                 //TODO createImage util method to remove duplication
2727                 L.extend(this._image, {
2728                         galleryimg: 'no',
2729                         onselectstart: L.Util.falseFn,
2730                         onmousemove: L.Util.falseFn,
2731                         onload: L.bind(this._onImageLoad, this),
2732                         src: this._url
2733                 });
2734         },
2735
2736         _animateZoom: function (e) {
2737                 var map = this._map,
2738                     image = this._image,
2739                     scale = map.getZoomScale(e.zoom),
2740                     nw = this._bounds.getNorthWest(),
2741                     se = this._bounds.getSouthEast(),
2742
2743                     topLeft = map._latLngToNewLayerPoint(nw, e.zoom, e.center),
2744                     size = map._latLngToNewLayerPoint(se, e.zoom, e.center)._subtract(topLeft),
2745                     currentSize = map.latLngToLayerPoint(se)._subtract(map.latLngToLayerPoint(nw)),
2746                     origin = topLeft._add(size._subtract(currentSize)._divideBy(2));
2747
2748                 image.style[L.DomUtil.TRANSFORM] =
2749                         L.DomUtil.getTranslateString(origin) + ' scale(' + scale + ') ';
2750         },
2751
2752         _reset: function () {
2753                 var image   = this._image,
2754                     topLeft = this._map.latLngToLayerPoint(this._bounds.getNorthWest()),
2755                     size    = this._map.latLngToLayerPoint(this._bounds.getSouthEast())._subtract(topLeft);
2756
2757                 L.DomUtil.setPosition(image, topLeft);
2758
2759                 image.style.width  = size.x + 'px';
2760                 image.style.height = size.y + 'px';
2761         },
2762
2763         _onImageLoad: function () {
2764                 this.fire('load');
2765         },
2766
2767         _updateOpacity: function () {
2768                 L.DomUtil.setOpacity(this._image, this.options.opacity);
2769         }
2770 });
2771
2772 L.imageOverlay = function (url, bounds, options) {
2773         return new L.ImageOverlay(url, bounds, options);
2774 };
2775
2776
2777 L.Icon = L.Class.extend({
2778         options: {
2779                 /*
2780                 iconUrl: (String) (required)
2781                 iconSize: (Point) (can be set through CSS)
2782                 iconAnchor: (Point) (centered by default, can be set in CSS with negative margins)
2783                 popupAnchor: (Point) (if not specified, popup opens in the anchor point)
2784                 shadowUrl: (Point) (no shadow by default)
2785                 shadowSize: (Point)
2786                 shadowAnchor: (Point)
2787                 */
2788                 className: ''
2789         },
2790
2791         initialize: function (options) {
2792                 L.setOptions(this, options);
2793         },
2794
2795         createIcon: function () {
2796                 return this._createIcon('icon');
2797         },
2798
2799         createShadow: function () {
2800                 return this._createIcon('shadow');
2801         },
2802
2803         _createIcon: function (name) {
2804                 var src = this._getIconUrl(name);
2805
2806                 if (!src) {
2807                         if (name === 'icon') {
2808                                 throw new Error("iconUrl not set in Icon options (see the docs).");
2809                         }
2810                         return null;
2811                 }
2812
2813                 var img = this._createImg(src);
2814                 this._setIconStyles(img, name);
2815
2816                 return img;
2817         },
2818
2819         _setIconStyles: function (img, name) {
2820                 var options = this.options,
2821                     size = L.point(options[name + 'Size']),
2822                     anchor;
2823
2824                 if (name === 'shadow') {
2825                         anchor = L.point(options.shadowAnchor || options.iconAnchor);
2826                 } else {
2827                         anchor = L.point(options.iconAnchor);
2828                 }
2829
2830                 if (!anchor && size) {
2831                         anchor = size.divideBy(2, true);
2832                 }
2833
2834                 img.className = 'leaflet-marker-' + name + ' ' + options.className;
2835
2836                 if (anchor) {
2837                         img.style.marginLeft = (-anchor.x) + 'px';
2838                         img.style.marginTop  = (-anchor.y) + 'px';
2839                 }
2840
2841                 if (size) {
2842                         img.style.width  = size.x + 'px';
2843                         img.style.height = size.y + 'px';
2844                 }
2845         },
2846
2847         _createImg: function (src) {
2848                 var el;
2849
2850                 if (!L.Browser.ie6) {
2851                         el = document.createElement('img');
2852                         el.src = src;
2853                 } else {
2854                         el = document.createElement('div');
2855                         el.style.filter =
2856                                 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '")';
2857                 }
2858                 return el;
2859         },
2860
2861         _getIconUrl: function (name) {
2862                 return this.options[name + 'Url'];
2863         }
2864 });
2865
2866 L.icon = function (options) {
2867         return new L.Icon(options);
2868 };
2869
2870
2871
2872 L.Icon.Default = L.Icon.extend({
2873
2874         options: {
2875                 iconSize: new L.Point(25, 41),
2876                 iconAnchor: new L.Point(12, 41),
2877                 popupAnchor: new L.Point(1, -34),
2878
2879                 shadowSize: new L.Point(41, 41)
2880         },
2881
2882         _getIconUrl: function (name) {
2883                 var key = name + 'Url';
2884
2885                 if (this.options[key]) {
2886                         return this.options[key];
2887                 }
2888
2889                 var path = L.Icon.Default.imagePath;
2890
2891                 if (!path) {
2892                         throw new Error("Couldn't autodetect L.Icon.Default.imagePath, set it manually.");
2893                 }
2894
2895                 return path + '/marker-' + name + '.png';
2896         }
2897 });
2898
2899 L.Icon.Default.imagePath = (function () {
2900         var scripts = document.getElementsByTagName('script'),
2901             leafletRe = /\/?leaflet[\-\._]?([\w\-\._]*)\.js\??/;
2902
2903         var i, len, src, matches;
2904
2905         for (i = 0, len = scripts.length; i < len; i++) {
2906                 src = scripts[i].src;
2907                 matches = src.match(leafletRe);
2908
2909                 if (matches) {
2910                         return src.split(leafletRe)[0] + '/images';
2911                 }
2912         }
2913 }());
2914
2915
2916 /*
2917  * L.Marker is used to display clickable/draggable icons on the map.
2918  */
2919
2920 L.Marker = L.Class.extend({
2921
2922         includes: L.Mixin.Events,
2923
2924         options: {
2925                 icon: new L.Icon.Default(),
2926                 title: '',
2927                 clickable: true,
2928                 draggable: false,
2929                 zIndexOffset: 0,
2930                 opacity: 1,
2931                 riseOnHover: false,
2932                 riseOffset: 250
2933         },
2934
2935         initialize: function (latlng, options) {
2936                 L.setOptions(this, options);
2937                 this._latlng = L.latLng(latlng);
2938         },
2939
2940         onAdd: function (map) {
2941                 this._map = map;
2942
2943                 map.on('viewreset', this.update, this);
2944
2945                 this._initIcon();
2946                 this.update();
2947
2948                 if (map.options.zoomAnimation && map.options.markerZoomAnimation) {
2949                         map.on('zoomanim', this._animateZoom, this);
2950                 }
2951         },
2952
2953         addTo: function (map) {
2954                 map.addLayer(this);
2955                 return this;
2956         },
2957
2958         onRemove: function (map) {
2959                 this._removeIcon();
2960
2961                 this.fire('remove');
2962
2963                 map.off({
2964                         'viewreset': this.update,
2965                         'zoomanim': this._animateZoom
2966                 }, this);
2967
2968                 this._map = null;
2969         },
2970
2971         getLatLng: function () {
2972                 return this._latlng;
2973         },
2974
2975         setLatLng: function (latlng) {
2976                 this._latlng = L.latLng(latlng);
2977
2978                 this.update();
2979
2980                 this.fire('move', { latlng: this._latlng });
2981         },
2982
2983         setZIndexOffset: function (offset) {
2984                 this.options.zIndexOffset = offset;
2985                 this.update();
2986         },
2987
2988         setIcon: function (icon) {
2989                 if (this._map) {
2990                         this._removeIcon();
2991                 }
2992
2993                 this.options.icon = icon;
2994
2995                 if (this._map) {
2996                         this._initIcon();
2997                         this.update();
2998                 }
2999         },
3000
3001         update: function () {
3002                 if (!this._icon) { return; }
3003
3004                 var pos = this._map.latLngToLayerPoint(this._latlng).round();
3005                 this._setPos(pos);
3006         },
3007
3008         _initIcon: function () {
3009                 var options = this.options,
3010                     map = this._map,
3011                     animation = (map.options.zoomAnimation && map.options.markerZoomAnimation),
3012                     classToAdd = animation ? 'leaflet-zoom-animated' : 'leaflet-zoom-hide',
3013                     needOpacityUpdate = false;
3014
3015                 if (!this._icon) {
3016                         this._icon = options.icon.createIcon();
3017
3018                         if (options.title) {
3019                                 this._icon.title = options.title;
3020                         }
3021
3022                         this._initInteraction();
3023                         needOpacityUpdate = (this.options.opacity < 1);
3024
3025                         L.DomUtil.addClass(this._icon, classToAdd);
3026
3027                         if (options.riseOnHover) {
3028                                 L.DomEvent
3029                                         .on(this._icon, 'mouseover', this._bringToFront, this)
3030                                         .on(this._icon, 'mouseout', this._resetZIndex, this);
3031                         }
3032                 }
3033
3034                 if (!this._shadow) {
3035                         this._shadow = options.icon.createShadow();
3036
3037                         if (this._shadow) {
3038                                 L.DomUtil.addClass(this._shadow, classToAdd);
3039                                 needOpacityUpdate = (this.options.opacity < 1);
3040                         }
3041                 }
3042
3043                 if (needOpacityUpdate) {
3044                         this._updateOpacity();
3045                 }
3046
3047                 var panes = this._map._panes;
3048
3049                 panes.markerPane.appendChild(this._icon);
3050
3051                 if (this._shadow) {
3052                         panes.shadowPane.appendChild(this._shadow);
3053                 }
3054         },
3055
3056         _removeIcon: function () {
3057                 var panes = this._map._panes;
3058
3059                 if (this.options.riseOnHover) {
3060                         L.DomEvent
3061                             .off(this._icon, 'mouseover', this._bringToFront)
3062                             .off(this._icon, 'mouseout', this._resetZIndex);
3063                 }
3064
3065                 panes.markerPane.removeChild(this._icon);
3066
3067                 if (this._shadow) {
3068                         panes.shadowPane.removeChild(this._shadow);
3069                 }
3070
3071                 this._icon = this._shadow = null;
3072         },
3073
3074         _setPos: function (pos) {
3075                 L.DomUtil.setPosition(this._icon, pos);
3076
3077                 if (this._shadow) {
3078                         L.DomUtil.setPosition(this._shadow, pos);
3079                 }
3080
3081                 this._zIndex = pos.y + this.options.zIndexOffset;
3082
3083                 this._resetZIndex();
3084         },
3085
3086         _updateZIndex: function (offset) {
3087                 this._icon.style.zIndex = this._zIndex + offset;
3088         },
3089
3090         _animateZoom: function (opt) {
3091                 var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center);
3092
3093                 this._setPos(pos);
3094         },
3095
3096         _initInteraction: function () {
3097                 if (!this.options.clickable) {
3098                         return;
3099                 }
3100
3101                 var icon = this._icon,
3102                     events = ['dblclick', 'mousedown', 'mouseover', 'mouseout'];
3103
3104                 L.DomUtil.addClass(icon, 'leaflet-clickable');
3105                 L.DomEvent.on(icon, 'click', this._onMouseClick, this);
3106
3107                 for (var i = 0; i < events.length; i++) {
3108                         L.DomEvent.on(icon, events[i], this._fireMouseEvent, this);
3109                 }
3110
3111                 if (L.Handler.MarkerDrag) {
3112                         this.dragging = new L.Handler.MarkerDrag(this);
3113
3114                         if (this.options.draggable) {
3115                                 this.dragging.enable();
3116                         }
3117                 }
3118         },
3119
3120         _onMouseClick: function (e) {
3121                 var wasDragged = this.dragging && this.dragging.moved();
3122                 if (this.hasEventListeners(e.type) || wasDragged) {
3123                         L.DomEvent.stopPropagation(e);
3124                 }
3125                 if (wasDragged) { return; }
3126                 if (this._map.dragging && this._map.dragging.moved()) { return; }
3127                 this.fire(e.type, {
3128                         originalEvent: e
3129                 });
3130         },
3131
3132         _fireMouseEvent: function (e) {
3133                 this.fire(e.type, {
3134                         originalEvent: e
3135                 });
3136                 if (e.type !== 'mousedown') {
3137                         L.DomEvent.stopPropagation(e);
3138                 }
3139         },
3140
3141         setOpacity: function (opacity) {
3142                 this.options.opacity = opacity;
3143                 if (this._map) {
3144                         this._updateOpacity();
3145                 }
3146         },
3147
3148         _updateOpacity: function () {
3149                 L.DomUtil.setOpacity(this._icon, this.options.opacity);
3150                 if (this._shadow) {
3151                         L.DomUtil.setOpacity(this._shadow, this.options.opacity);
3152                 }
3153         },
3154
3155         _bringToFront: function () {
3156                 this._updateZIndex(this.options.riseOffset);
3157         },
3158
3159         _resetZIndex: function () {
3160                 this._updateZIndex(0);
3161         }
3162 });
3163
3164 L.marker = function (latlng, options) {
3165         return new L.Marker(latlng, options);
3166 };
3167
3168
3169 L.DivIcon = L.Icon.extend({
3170         options: {
3171                 iconSize: new L.Point(12, 12), // also can be set through CSS
3172                 /*
3173                 iconAnchor: (Point)
3174                 popupAnchor: (Point)
3175                 html: (String)
3176                 bgPos: (Point)
3177                 */
3178                 className: 'leaflet-div-icon'
3179         },
3180
3181         createIcon: function () {
3182                 var div = document.createElement('div'),
3183                     options = this.options;
3184
3185                 if (options.html) {
3186                         div.innerHTML = options.html;
3187                 }
3188
3189                 if (options.bgPos) {
3190                         div.style.backgroundPosition =
3191                                 (-options.bgPos.x) + 'px ' + (-options.bgPos.y) + 'px';
3192                 }
3193
3194                 this._setIconStyles(div, 'icon');
3195                 return div;
3196         },
3197
3198         createShadow: function () {
3199                 return null;
3200         }
3201 });
3202
3203 L.divIcon = function (options) {
3204         return new L.DivIcon(options);
3205 };
3206
3207
3208
3209 L.Map.mergeOptions({
3210         closePopupOnClick: true
3211 });
3212
3213 L.Popup = L.Class.extend({
3214         includes: L.Mixin.Events,
3215
3216         options: {
3217                 minWidth: 50,
3218                 maxWidth: 300,
3219                 maxHeight: null,
3220                 autoPan: true,
3221                 closeButton: true,
3222                 offset: new L.Point(0, 6),
3223                 autoPanPadding: new L.Point(5, 5),
3224                 className: ''
3225         },
3226
3227         initialize: function (options, source) {
3228                 L.setOptions(this, options);
3229
3230                 this._source = source;
3231         },
3232
3233         onAdd: function (map) {
3234                 this._map = map;
3235
3236                 if (!this._container) {
3237                         this._initLayout();
3238                 }
3239                 this._updateContent();
3240
3241                 var animFade = map.options.fadeAnimation;
3242
3243                 if (animFade) {
3244                         L.DomUtil.setOpacity(this._container, 0);
3245                 }
3246                 map._panes.popupPane.appendChild(this._container);
3247
3248                 map.on('viewreset', this._updatePosition, this);
3249
3250                 if (L.Browser.any3d) {
3251                         map.on('zoomanim', this._zoomAnimation, this);
3252                 }
3253
3254                 if (map.options.closePopupOnClick) {
3255                         map.on('preclick', this._close, this);
3256                 }
3257
3258                 this._update();
3259
3260                 if (animFade) {
3261                         L.DomUtil.setOpacity(this._container, 1);
3262                 }
3263         },
3264
3265         addTo: function (map) {
3266                 map.addLayer(this);
3267                 return this;
3268         },
3269
3270         openOn: function (map) {
3271                 map.openPopup(this);
3272                 return this;
3273         },
3274
3275         onRemove: function (map) {
3276                 map._panes.popupPane.removeChild(this._container);
3277
3278                 L.Util.falseFn(this._container.offsetWidth); // force reflow
3279
3280                 map.off({
3281                         viewreset: this._updatePosition,
3282                         preclick: this._close,
3283                         zoomanim: this._zoomAnimation
3284                 }, this);
3285
3286                 if (map.options.fadeAnimation) {
3287                         L.DomUtil.setOpacity(this._container, 0);
3288                 }
3289
3290                 this._map = null;
3291         },
3292
3293         setLatLng: function (latlng) {
3294                 this._latlng = L.latLng(latlng);
3295                 this._update();
3296                 return this;
3297         },
3298
3299         setContent: function (content) {
3300                 this._content = content;
3301                 this._update();
3302                 return this;
3303         },
3304
3305         _close: function () {
3306                 var map = this._map;
3307
3308                 if (map) {
3309                         map._popup = null;
3310
3311                         map
3312                             .removeLayer(this)
3313                             .fire('popupclose', {popup: this});
3314                 }
3315         },
3316
3317         _initLayout: function () {
3318                 var prefix = 'leaflet-popup',
3319                         containerClass = prefix + ' ' + this.options.className + ' leaflet-zoom-animated',
3320                         container = this._container = L.DomUtil.create('div', containerClass),
3321                         closeButton;
3322
3323                 if (this.options.closeButton) {
3324                         closeButton = this._closeButton =
3325                                 L.DomUtil.create('a', prefix + '-close-button', container);
3326                         closeButton.href = '#close';
3327                         closeButton.innerHTML = '&#215;';
3328
3329                         L.DomEvent.on(closeButton, 'click', this._onCloseButtonClick, this);
3330                 }
3331
3332                 var wrapper = this._wrapper =
3333                         L.DomUtil.create('div', prefix + '-content-wrapper', container);
3334                 L.DomEvent.disableClickPropagation(wrapper);
3335
3336                 this._contentNode = L.DomUtil.create('div', prefix + '-content', wrapper);
3337                 L.DomEvent.on(this._contentNode, 'mousewheel', L.DomEvent.stopPropagation);
3338
3339                 this._tipContainer = L.DomUtil.create('div', prefix + '-tip-container', container);
3340                 this._tip = L.DomUtil.create('div', prefix + '-tip', this._tipContainer);
3341         },
3342
3343         _update: function () {
3344                 if (!this._map) { return; }
3345
3346                 this._container.style.visibility = 'hidden';
3347
3348                 this._updateContent();
3349                 this._updateLayout();
3350                 this._updatePosition();
3351
3352                 this._container.style.visibility = '';
3353
3354                 this._adjustPan();
3355         },
3356
3357         _updateContent: function () {
3358                 if (!this._content) { return; }
3359
3360                 if (typeof this._content === 'string') {
3361                         this._contentNode.innerHTML = this._content;
3362                 } else {
3363                         while (this._contentNode.hasChildNodes()) {
3364                                 this._contentNode.removeChild(this._contentNode.firstChild);
3365                         }
3366                         this._contentNode.appendChild(this._content);
3367                 }
3368                 this.fire('contentupdate');
3369         },
3370
3371         _updateLayout: function () {
3372                 var container = this._contentNode,
3373                     style = container.style;
3374
3375                 style.width = '';
3376                 style.whiteSpace = 'nowrap';
3377
3378                 var width = container.offsetWidth;
3379                 width = Math.min(width, this.options.maxWidth);
3380                 width = Math.max(width, this.options.minWidth);
3381
3382                 style.width = (width + 1) + 'px';
3383                 style.whiteSpace = '';
3384
3385                 style.height = '';
3386
3387                 var height = container.offsetHeight,
3388                     maxHeight = this.options.maxHeight,
3389                     scrolledClass = 'leaflet-popup-scrolled';
3390
3391                 if (maxHeight && height > maxHeight) {
3392                         style.height = maxHeight + 'px';
3393                         L.DomUtil.addClass(container, scrolledClass);
3394                 } else {
3395                         L.DomUtil.removeClass(container, scrolledClass);
3396                 }
3397
3398                 this._containerWidth = this._container.offsetWidth;
3399         },
3400
3401         _updatePosition: function () {
3402                 if (!this._map) { return; }
3403
3404                 var pos = this._map.latLngToLayerPoint(this._latlng),
3405                     is3d = L.Browser.any3d,
3406                     offset = this.options.offset;
3407
3408                 if (is3d) {
3409                         L.DomUtil.setPosition(this._container, pos);
3410                 }
3411
3412                 this._containerBottom = -offset.y - (is3d ? 0 : pos.y);
3413                 this._containerLeft = -Math.round(this._containerWidth / 2) + offset.x + (is3d ? 0 : pos.x);
3414
3415                 //Bottom position the popup in case the height of the popup changes (images loading etc)
3416                 this._container.style.bottom = this._containerBottom + 'px';
3417                 this._container.style.left = this._containerLeft + 'px';
3418         },
3419
3420         _zoomAnimation: function (opt) {
3421                 var pos = this._map._latLngToNewLayerPoint(this._latlng, opt.zoom, opt.center);
3422
3423                 L.DomUtil.setPosition(this._container, pos);
3424         },
3425
3426         _adjustPan: function () {
3427                 if (!this.options.autoPan) { return; }
3428
3429                 var map = this._map,
3430                     containerHeight = this._container.offsetHeight,
3431                     containerWidth = this._containerWidth,
3432
3433                     layerPos = new L.Point(this._containerLeft, -containerHeight - this._containerBottom);
3434
3435                 if (L.Browser.any3d) {
3436                         layerPos._add(L.DomUtil.getPosition(this._container));
3437                 }
3438
3439                 var containerPos = map.layerPointToContainerPoint(layerPos),
3440                     padding = this.options.autoPanPadding,
3441                     size = map.getSize(),
3442                     dx = 0,
3443                     dy = 0;
3444
3445                 if (containerPos.x < 0) {
3446                         dx = containerPos.x - padding.x;
3447                 }
3448                 if (containerPos.x + containerWidth > size.x) {
3449                         dx = containerPos.x + containerWidth - size.x + padding.x;
3450                 }
3451                 if (containerPos.y < 0) {
3452                         dy = containerPos.y - padding.y;
3453                 }
3454                 if (containerPos.y + containerHeight > size.y) {
3455                         dy = containerPos.y + containerHeight - size.y + padding.y;
3456                 }
3457
3458                 if (dx || dy) {
3459                         map.panBy(new L.Point(dx, dy));
3460                 }
3461         },
3462
3463         _onCloseButtonClick: function (e) {
3464                 this._close();
3465                 L.DomEvent.stop(e);
3466         }
3467 });
3468
3469 L.popup = function (options, source) {
3470         return new L.Popup(options, source);
3471 };
3472
3473
3474 /*
3475  * Popup extension to L.Marker, adding openPopup & bindPopup methods.
3476  */
3477
3478 L.Marker.include({
3479         openPopup: function () {
3480                 if (this._popup && this._map) {
3481                         this._popup.setLatLng(this._latlng);
3482                         this._map.openPopup(this._popup);
3483                 }
3484
3485                 return this;
3486         },
3487
3488         closePopup: function () {
3489                 if (this._popup) {
3490                         this._popup._close();
3491                 }
3492                 return this;
3493         },
3494
3495         bindPopup: function (content, options) {
3496                 var anchor = L.point(this.options.icon.options.popupAnchor) || new L.Point(0, 0);
3497
3498                 anchor = anchor.add(L.Popup.prototype.options.offset);
3499
3500                 if (options && options.offset) {
3501                         anchor = anchor.add(options.offset);
3502                 }
3503
3504                 options = L.extend({offset: anchor}, options);
3505
3506                 if (!this._popup) {
3507                         this
3508                             .on('click', this.openPopup, this)
3509                             .on('remove', this.closePopup, this)
3510                             .on('move', this._movePopup, this);
3511                 }
3512
3513                 this._popup = new L.Popup(options, this)
3514                         .setContent(content);
3515
3516                 return this;
3517         },
3518
3519         unbindPopup: function () {
3520                 if (this._popup) {
3521                         this._popup = null;
3522                         this
3523                             .off('click', this.openPopup)
3524                             .off('remove', this.closePopup)
3525                             .off('move', this._movePopup);
3526                 }
3527                 return this;
3528         },
3529
3530         _movePopup: function (e) {
3531                 this._popup.setLatLng(e.latlng);
3532         }
3533 });
3534
3535
3536
3537 L.Map.include({
3538         openPopup: function (popup) {
3539                 this.closePopup();
3540
3541                 this._popup = popup;
3542
3543                 return this
3544                     .addLayer(popup)
3545                     .fire('popupopen', {popup: this._popup});
3546         },
3547
3548         closePopup: function () {
3549                 if (this._popup) {
3550                         this._popup._close();
3551                 }
3552                 return this;
3553         }
3554 });
3555
3556
3557 /*
3558  * L.LayerGroup is a class to combine several layers so you can manipulate the group (e.g. add/remove it) as one layer.
3559  */
3560
3561 L.LayerGroup = L.Class.extend({
3562         initialize: function (layers) {
3563                 this._layers = {};
3564
3565                 var i, len;
3566
3567                 if (layers) {
3568                         for (i = 0, len = layers.length; i < len; i++) {
3569                                 this.addLayer(layers[i]);
3570                         }
3571                 }
3572         },
3573
3574         addLayer: function (layer) {
3575                 var id = L.stamp(layer);
3576
3577                 this._layers[id] = layer;
3578
3579                 if (this._map) {
3580                         this._map.addLayer(layer);
3581                 }
3582
3583                 return this;
3584         },
3585
3586         removeLayer: function (layer) {
3587                 var id = L.stamp(layer);
3588
3589                 delete this._layers[id];
3590
3591                 if (this._map) {
3592                         this._map.removeLayer(layer);
3593                 }
3594
3595                 return this;
3596         },
3597
3598         clearLayers: function () {
3599                 this.eachLayer(this.removeLayer, this);
3600                 return this;
3601         },
3602
3603         invoke: function (methodName) {
3604                 var args = Array.prototype.slice.call(arguments, 1),
3605                     i, layer;
3606
3607                 for (i in this._layers) {
3608                         if (this._layers.hasOwnProperty(i)) {
3609                                 layer = this._layers[i];
3610
3611                                 if (layer[methodName]) {
3612                                         layer[methodName].apply(layer, args);
3613                                 }
3614                         }
3615                 }
3616
3617                 return this;
3618         },
3619
3620         onAdd: function (map) {
3621                 this._map = map;
3622                 this.eachLayer(map.addLayer, map);
3623         },
3624
3625         onRemove: function (map) {
3626                 this.eachLayer(map.removeLayer, map);
3627                 this._map = null;
3628         },
3629
3630         addTo: function (map) {
3631                 map.addLayer(this);
3632                 return this;
3633         },
3634
3635         eachLayer: function (method, context) {
3636                 for (var i in this._layers) {
3637                         if (this._layers.hasOwnProperty(i)) {
3638                                 method.call(context, this._layers[i]);
3639                         }
3640                 }
3641         }
3642 });
3643
3644 L.layerGroup = function (layers) {
3645         return new L.LayerGroup(layers);
3646 };
3647
3648
3649 /*
3650  * L.FeatureGroup extends L.LayerGroup by introducing mouse events and bindPopup method shared between a group of layers.
3651  */
3652
3653 L.FeatureGroup = L.LayerGroup.extend({
3654         includes: L.Mixin.Events,
3655
3656         statics: {
3657                 EVENTS: 'click dblclick mouseover mouseout mousemove contextmenu'
3658         },
3659
3660         addLayer: function (layer) {
3661                 if (this._layers[L.stamp(layer)]) {
3662                         return this;
3663                 }
3664
3665                 layer.on(L.FeatureGroup.EVENTS, this._propagateEvent, this);
3666
3667                 L.LayerGroup.prototype.addLayer.call(this, layer);
3668
3669                 if (this._popupContent && layer.bindPopup) {
3670                         layer.bindPopup(this._popupContent);
3671                 }
3672
3673                 return this.fire('layeradd', {layer: layer});
3674         },
3675
3676         removeLayer: function (layer) {
3677                 layer.off(L.FeatureGroup.EVENTS, this._propagateEvent, this);
3678
3679                 L.LayerGroup.prototype.removeLayer.call(this, layer);
3680
3681
3682                 if (this._popupContent) {
3683                         this.invoke('unbindPopup');
3684                 }
3685
3686                 return this.fire('layerremove', {layer: layer});
3687         },
3688
3689         bindPopup: function (content) {
3690                 this._popupContent = content;
3691                 return this.invoke('bindPopup', content);
3692         },
3693
3694         setStyle: function (style) {
3695                 return this.invoke('setStyle', style);
3696         },
3697
3698         bringToFront: function () {
3699                 return this.invoke('bringToFront');
3700         },
3701
3702         bringToBack: function () {
3703                 return this.invoke('bringToBack');
3704         },
3705
3706         getBounds: function () {
3707                 var bounds = new L.LatLngBounds();
3708
3709                 this.eachLayer(function (layer) {
3710                         bounds.extend(layer instanceof L.Marker ? layer.getLatLng() : layer.getBounds());
3711                 });
3712
3713                 return bounds;
3714         },
3715
3716         _propagateEvent: function (e) {
3717                 e.layer  = e.target;
3718                 e.target = this;
3719
3720                 this.fire(e.type, e);
3721         }
3722 });
3723
3724 L.featureGroup = function (layers) {
3725         return new L.FeatureGroup(layers);
3726 };
3727
3728
3729 /*
3730  * L.Path is a base class for rendering vector paths on a map. Inherited by Polyline, Circle, etc.
3731  */
3732
3733 L.Path = L.Class.extend({
3734         includes: [L.Mixin.Events],
3735
3736         statics: {
3737                 // how much to extend the clip area around the map view
3738                 // (relative to its size, e.g. 0.5 is half the screen in each direction)
3739                 // set it so that SVG element doesn't exceed 1280px (vectors flicker on dragend if it is)
3740                 CLIP_PADDING: L.Browser.mobile ?
3741                         Math.max(0, Math.min(0.5,
3742                                 (1280 / Math.max(window.innerWidth, window.innerHeight) - 1) / 2)) : 0.5
3743         },
3744
3745         options: {
3746                 stroke: true,
3747                 color: '#0033ff',
3748                 dashArray: null,
3749                 weight: 5,
3750                 opacity: 0.5,
3751
3752                 fill: false,
3753                 fillColor: null, //same as color by default
3754                 fillOpacity: 0.2,
3755
3756                 clickable: true
3757         },
3758
3759         initialize: function (options) {
3760                 L.setOptions(this, options);
3761         },
3762
3763         onAdd: function (map) {
3764                 this._map = map;
3765
3766                 if (!this._container) {
3767                         this._initElements();
3768                         this._initEvents();
3769                 }
3770
3771                 this.projectLatlngs();
3772                 this._updatePath();
3773
3774                 if (this._container) {
3775                         this._map._pathRoot.appendChild(this._container);
3776                 }
3777
3778                 map.on({
3779                         'viewreset': this.projectLatlngs,
3780                         'moveend': this._updatePath
3781                 }, this);
3782         },
3783
3784         addTo: function (map) {
3785                 map.addLayer(this);
3786                 return this;
3787         },
3788
3789         onRemove: function (map) {
3790                 map._pathRoot.removeChild(this._container);
3791
3792                 this._map = null;
3793
3794                 if (L.Browser.vml) {
3795                         this._container = null;
3796                         this._stroke = null;
3797                         this._fill = null;
3798                 }
3799
3800                 this.fire('remove');
3801
3802                 map.off({
3803                         'viewreset': this.projectLatlngs,
3804                         'moveend': this._updatePath
3805                 }, this);
3806         },
3807
3808         projectLatlngs: function () {
3809                 // do all projection stuff here
3810         },
3811
3812         setStyle: function (style) {
3813                 L.setOptions(this, style);
3814
3815                 if (this._container) {
3816                         this._updateStyle();
3817                 }
3818
3819                 return this;
3820         },
3821
3822         redraw: function () {
3823                 if (this._map) {
3824                         this.projectLatlngs();
3825                         this._updatePath();
3826                 }
3827                 return this;
3828         }
3829 });
3830
3831 L.Map.include({
3832         _updatePathViewport: function () {
3833                 var p = L.Path.CLIP_PADDING,
3834                     size = this.getSize(),
3835                     panePos = L.DomUtil.getPosition(this._mapPane),
3836                     min = panePos.multiplyBy(-1)._subtract(size.multiplyBy(p)._round()),
3837                     max = min.add(size.multiplyBy(1 + p * 2)._round());
3838
3839                 this._pathViewport = new L.Bounds(min, max);
3840         }
3841 });
3842
3843
3844 L.Path.SVG_NS = 'http://www.w3.org/2000/svg';
3845
3846 L.Browser.svg = !!(document.createElementNS && document.createElementNS(L.Path.SVG_NS, 'svg').createSVGRect);
3847
3848 L.Path = L.Path.extend({
3849         statics: {
3850                 SVG: L.Browser.svg
3851         },
3852
3853         bringToFront: function () {
3854                 var root = this._map._pathRoot,
3855                     path = this._container;
3856
3857                 if (path && root.lastChild !== path) {
3858                         root.appendChild(path);
3859                 }
3860                 return this;
3861         },
3862
3863         bringToBack: function () {
3864                 var root = this._map._pathRoot,
3865                     path = this._container,
3866                     first = root.firstChild;
3867
3868                 if (path && first !== path) {
3869                         root.insertBefore(path, first);
3870                 }
3871                 return this;
3872         },
3873
3874         getPathString: function () {
3875                 // form path string here
3876         },
3877
3878         _createElement: function (name) {
3879                 return document.createElementNS(L.Path.SVG_NS, name);
3880         },
3881
3882         _initElements: function () {
3883                 this._map._initPathRoot();
3884                 this._initPath();
3885                 this._initStyle();
3886         },
3887
3888         _initPath: function () {
3889                 this._container = this._createElement('g');
3890
3891                 this._path = this._createElement('path');
3892                 this._container.appendChild(this._path);
3893         },
3894
3895         _initStyle: function () {
3896                 if (this.options.stroke) {
3897                         this._path.setAttribute('stroke-linejoin', 'round');
3898                         this._path.setAttribute('stroke-linecap', 'round');
3899                 }
3900                 if (this.options.fill) {
3901                         this._path.setAttribute('fill-rule', 'evenodd');
3902                 }
3903                 this._updateStyle();
3904         },
3905
3906         _updateStyle: function () {
3907                 if (this.options.stroke) {
3908                         this._path.setAttribute('stroke', this.options.color);
3909                         this._path.setAttribute('stroke-opacity', this.options.opacity);
3910                         this._path.setAttribute('stroke-width', this.options.weight);
3911                         if (this.options.dashArray) {
3912                                 this._path.setAttribute('stroke-dasharray', this.options.dashArray);
3913                         } else {
3914                                 this._path.removeAttribute('stroke-dasharray');
3915                         }
3916                 } else {
3917                         this._path.setAttribute('stroke', 'none');
3918                 }
3919                 if (this.options.fill) {
3920                         this._path.setAttribute('fill', this.options.fillColor || this.options.color);
3921                         this._path.setAttribute('fill-opacity', this.options.fillOpacity);
3922                 } else {
3923                         this._path.setAttribute('fill', 'none');
3924                 }
3925         },
3926
3927         _updatePath: function () {
3928                 var str = this.getPathString();
3929                 if (!str) {
3930                         // fix webkit empty string parsing bug
3931                         str = 'M0 0';
3932                 }
3933                 this._path.setAttribute('d', str);
3934         },
3935
3936         // TODO remove duplication with L.Map
3937         _initEvents: function () {
3938                 if (this.options.clickable) {
3939                         if (L.Browser.svg || !L.Browser.vml) {
3940                                 this._path.setAttribute('class', 'leaflet-clickable');
3941                         }
3942
3943                         L.DomEvent.on(this._container, 'click', this._onMouseClick, this);
3944
3945                         var events = ['dblclick', 'mousedown', 'mouseover',
3946                                       'mouseout', 'mousemove', 'contextmenu'];
3947                         for (var i = 0; i < events.length; i++) {
3948                                 L.DomEvent.on(this._container, events[i], this._fireMouseEvent, this);
3949                         }
3950                 }
3951         },
3952
3953         _onMouseClick: function (e) {
3954                 if (this._map.dragging && this._map.dragging.moved()) { return; }
3955
3956                 this._fireMouseEvent(e);
3957         },
3958
3959         _fireMouseEvent: function (e) {
3960                 if (!this.hasEventListeners(e.type)) { return; }
3961
3962                 var map = this._map,
3963                     containerPoint = map.mouseEventToContainerPoint(e),
3964                     layerPoint = map.containerPointToLayerPoint(containerPoint),
3965                     latlng = map.layerPointToLatLng(layerPoint);
3966
3967