var MEDIA_URL=window.MEDIA_URL||'/static/';YandexMap=(function(){var AllMaps={};function SampleBalloonLayout(){this.element=YMaps.jQuery('<div class="address-popup"><a class="close">[X]</a><div class="t">&nbsp;</div><div class="c"><div class="frame"></div></div><div class="b">&nbsp;</div></div>');this.close=this.element.find("a.close");this.content=this.element.find(".frame");this.disableClose=function(){this.close.unbind("click").css("display","none");};this.enableClose=function(callback){this.close.bind("click",callback).css("display","");return false;};this.onAddToParent=function(parentNode){YMaps.jQuery(parentNode).append(this.element.hide().slideDown(700));};this.onRemoveFromParent=function(){this.element.remove();};this.setContent=function(content){content.onAddToParent(this.content[0]);};this.update=function(){};};var sampleBalloonTemplate=new YMaps.LayoutTemplate(SampleBalloonLayout);function YandexMap(id,mapdata){this.container=$(id);this.mapdata=mapdata;this.init();AllMaps[id]=this;}
YandexMap.prototype.init=function(){this.POINT_STYLES={'Default':new YMaps.Style()};this.map=new YMaps.Map(this.container);this.POINT_STYLES.Default.balloonStyle={template:sampleBalloonTemplate};this.POINT_STYLES.Default.iconStyle=new YMaps.IconStyle();this.POINT_STYLES.Default.iconStyle.href=MEDIA_URL+"images/yandex-point.png";this.POINT_STYLES.Default.iconStyle.size=new YMaps.Point(31,43);this.POINT_STYLES.Default.iconStyle.offset=new YMaps.Point(-15,-42);this.POINT_STYLES.Default.iconStyle.shadow=new YMaps.IconShadowStyle();this.POINT_STYLES.Default.iconStyle.shadow.href=MEDIA_URL+"images/yandex-point-shadow.png";this.POINT_STYLES.Default.iconStyle.shadow.size=new YMaps.Point(53,48);this.POINT_STYLES.Default.iconStyle.shadow.offset=new YMaps.Point(-15,-42);this.map.addControl(new YMaps.ToolBar());this.map.addControl(new YMaps.Zoom());this.map.addControl(new YMaps.ScaleLine());if(!this.mapdata){this.map.setCenter(new YMaps.GeoPoint(37.64,55.76),10);return this;}
this.map.setCenter(new YMaps.GeoPoint(this.mapdata.center[0],this.mapdata.center[1]),this.mapdata.zoom);this.addPoints();return this;}
YandexMap.prototype.redraw=function(){this.map.redraw();return this;}
YandexMap.prototype.eachPoint=function(points,cb){for(var i=0,l=points.length;i<l;i++){if(!points[i])continue;cb.call(this,points[i],i);}}
YandexMap.prototype.addPoints=function(){this.filter(this.mapdata.points,function(points){this.map.removeAllOverlays();this.eachPoint(points,function(point){var placemark,current,options={style:this.POINT_STYLES[point.style||'Default']};if(!point['balloon']){options['hasBalloon']=false;}
placemark=new YMaps.Placemark(new YMaps.GeoPoint(point.center[0],point.center[1]),options);if(point['balloon']){placemark.setBalloonContent($(point.balloon).html());}
if(point['link']){YMaps.Events.observe(placemark,placemark.Events.Click,function(){document.location.href=point.link;});}
this.map.addOverlay(placemark);});});}
YandexMap.prototype.setFilter=function(cb){this.filter=cb;return this;}
YandexMap.prototype.filter=function(points,cb){cb.call(this,this.sort(points));}
YandexMap.prototype.setSort=function(cb){this.sort=cb;return this;}
YandexMap.prototype.sort=function(points){return points.sort(function(a,b){return(a&&b)?b.center[1]-a.center[1]:a?-1:1;});}
YandexMap.prototype.geoCode=function(address,cb,go){var geocoder=new YMaps.Geocoder(address,{results:1,boundedBy:this.map.getBounds()}),that=this;YMaps.Events.observe(geocoder,geocoder.Events.Load,function(){if(go)that.map.panTo(this.get(0).getGeoPoint());cb.call(that,this.length()?this.get(0):null);});}
YandexMap.prototype.nearest=function(points,gpoint){var ret=[];if(gpoint)
this.eachPoint(points,function(point){if(new YMaps.GeoPoint(point.center[0],point.center[1]).distance(gpoint.getGeoPoint())<4000){ret.push(point);}});else return points;return ret;}
YandexMap.get=function(id){return AllMaps[id]||null;}
YandexMap.redrawAll=function(){for(var i in AllMaps){AllMaps[i].redraw();}}
return YandexMap;})();
