function YandexClusterer(map,markers,opts){this.map=map;this.bounds=null;this.clusters=[];this.markers=markers||[];this.zoom=null;this.map.YandexClusterer=this;this.om=new YMaps.ObjectManager();this.map.addOverlay(this.om);opts=opts||{};this.max_zoom=opts.max_zoom||0;this.grid=opts.grid||60;this.min_size=opts.min_size||2;this.centered=opts.centered||false;this.batch=opts.batch||400;this.style=opts.style||{icon:'http://gmaps-utility-library.googlecode.com/svn/trunk/markerclusterer/images/m1.png',height:52,width:53,offset:[0,0],textColor:'#000000',textSize:11,printable:false};if(this.map._yandexClustererEvents){for(event in this.map._yandexClustererEvents){this.map._yandexClustererEvents[event].cleanup();}}
this.map._yandexClustererEvents={};this.map._yandexClustererEvents['update']=YMaps.Events.observe(this.map,this.map.Events.Update,function(){this.repaint();},this);this.map._yandexClustererEvents['move']=YMaps.Events.observe(this.map,this.map.Events.MoveEnd,function(){this.redraw();},this);this.repaint();}
YandexClusterer.prototype.setMarkers=function(markers){this.markers=markers;}
YandexClusterer.prototype.clearMarkers=function(){var l=this.markers.length;while(l--){var marker=this.markers[l];marker.isAdded=false;if(marker.isOnMap){marker.isOnMap=false;}}
this.om.removeAll();l=this.clusters.length;while(l--){this.clusters[l].remove();}
this.clusters=[];}
YandexClusterer.prototype.repaint=function(){this.clearMarkers();this.redraw();}
YandexClusterer.prototype.redraw=function(){this.createClusters(0);}
YandexClusterer.prototype.createClusters=function(iter){var self=this,i=iter,length=this.markers.length,iter_last=Math.min(iter+this.batch,length);if(iter===0){this.bounds=this.extendBounds(this.map.getBounds());this.zoom=this.map.getZoom();if(typeof this.timerRefStatic!=="undefined"){clearTimeout(this.timerRefStatic);delete this.timerRefStatic;}}
for(i;i<iter_last;i++){var marker=this.markers[i];if(!marker.isAdded&&this.bounds.contains(marker.getCoordPoint())){this.addToCluster(marker);}}
if(iter_last<length){this.timerRefStatic=setTimeout(function(){self.createClusters(iter_last);},0);}else{delete this.timerRefStatic;setTimeout(function(){self.updateClusterIcons();},0);}}
YandexClusterer.prototype.extendBounds=function(bounds){var converter=this.map.converter,rtPix,lbPix,ne,sw;rtPix=converter.coordinatesToMapPixels(bounds.getRightTop());rtPix.x+=this.grid;rtPix.y-=this.grid;lbPix=converter.coordinatesToMapPixels(bounds.getLeftBottom());lbPix.x-=this.grid;lbPix.y+=this.grid;ne=converter.mapPixelsToCoordinates(rtPix);sw=converter.mapPixelsToCoordinates(lbPix);return new YMaps.GeoBounds(sw,ne);}
YandexClusterer.prototype.addToCluster=function(marker){var d,cluster,center,l=this.clusters.length;var distance=40000;var clusterToAddTo=null;while(l--){cluster=this.clusters[l];if(cluster.center){d=this.distanceBetween(cluster.center,marker.getCoordPoint());if(d<distance){distance=d;clusterToAddTo=cluster;}}}
if(clusterToAddTo&&clusterToAddTo.isMarkerInBounds(marker)){clusterToAddTo.addMarker(marker);}else{cluster=new YandexCluster(this);cluster.addMarker(marker);this.clusters.push(cluster);}}
YandexClusterer.prototype.distanceBetween=function(p1,p2){var R=6371;var dLat=(p2.getLat()-p1.getLat())*Math.PI/180;var dLon=(p2.getLng()-p1.getLng())*Math.PI/180;var a=Math.sin(dLat/2)*Math.sin(dLat/2)+
Math.cos(p1.getLat()*Math.PI/180)*Math.cos(p2.getLat()*Math.PI/180)*Math.sin(dLon/2)*Math.sin(dLon/2);var c=2*Math.atan2(Math.sqrt(a),Math.sqrt(1-a));var d=R*c;return d;};YandexClusterer.prototype.updateClusterIcons=function(){var self=this,l=this.clusters.length,max_zoom=this.getMaxZoom(),max_map_zoom=this.map.getZoom();while(l--){var cluster=this.clusters[l];count=cluster.markers.length;if(max_map_zoom>=max_zoom){var i=cluster.markers.length;while(i--){var marker=cluster.markers[i];if(!marker.isOnMap){this.om.add(marker,this.zoom,this.zoom);marker.isOnMap=true;}}
continue;}
else if(count<this.min_size){var i=cluster.markers.length;while(i--){var marker=cluster.markers[i];if(!marker.isOnMap){this.om.add(marker,this.zoom,this.zoom);marker.isOnMap=true;}}
continue;}
else{var i=cluster.markers.length;while(i--){var marker=cluster.markers[i];this.map.removeOverlay(marker);marker.isOnMap=false;}}
cluster.showIcon();}}
YandexClusterer.prototype.getMaxZoom=function(){return this.max_zoom||this.map.getMaxZoom();};function YandexCluster(mc){this.mc=mc;this.om=mc.om;this.map=mc.map;this.center=null;this.markers=[];this.bounds=null;this.min_size=mc.min_size;this.style=mc.style;this.point=null;}
YandexCluster.prototype.addMarker=function(marker){var count,i,zoom=this.map.getZoom();if(marker.isAdded)return false;if(!this.center){this.center=marker.getCoordPoint();this.bounds=this.calculateBounds();}else if(this.mc.centered){var l=this.markers.length+1,m_coord=marker.getCoordPoint();var lat=(this.center.getLat()*(l-1)+m_coord.getLat())/l;var lng=(this.center.getLng()*(l-1)+m_coord.getLng())/l;this.center=new YMaps.GeoPoint(lng,lat);this.bounds=this.calculateBounds();}
marker.isAdded=true;this.markers.push(marker);return true;}
YandexCluster.prototype.calculateBounds=function(){var bounds=new YMaps.GeoCollectionBounds(this.center);return this.mc.extendBounds(bounds);};YandexCluster.prototype.isMarkerInBounds=function(marker){return this.bounds.contains(marker.getCoordPoint());}
YandexCluster.prototype.showIcon=function(){var self=this,position=new YMaps.GeoPoint(this.center.getLng(),this.center.getLat());if(this.point){this.om.remove(this.point);}
if(this.style.printable){var template=new YMaps.Template('<div style="cursor:pointer;position:relative;line-height:$[style.iconStyle.size.y]px;height:$[style.iconStyle.size.y]px;width:$[style.iconStyle.size.x]px;text-align:center"><img src="$[style.iconStyle.href]" alt="" style="position:absolute;left:0;top:0"><span style="position:relative;font-weight:bold;font-size:$[textSize|12]px;color:$[textColor|#000]">$[markersCount|0]</span></div>');}else{var template=new YMaps.Template('<div style="cursor:pointer;position:relative;line-height:$[style.iconStyle.size.y]px;height:$[style.iconStyle.size.y]px;width:$[style.iconStyle.size.x]px;text-align:center;background:url($[style.iconStyle.href]) no-repeat;font-weight:bold;font-size:$[textSize|12]px;color:$[textColor|#000]">$[markersCount|0]</div>');}
var style=new YMaps.Style();style.iconStyle=new YMaps.IconStyle(template);style.iconStyle.href=this.style.icon;style.iconStyle.size=new YMaps.Point(this.style.width,this.style.height);style.iconStyle.offset=new YMaps.Point(this.style.offset);this.point=new YMaps.Placemark(position,{style:style,hasBalloon:false});this.point.markersCount=this.markers.length;this.point.textSize=this.style.textSize;this.point.textColor=this.style.textColor;YMaps.Events.observe(this.point,this.point.Events.Click,function(){this.map.setBounds(this.bounds);if(this.mc.getMaxZoom()<this.map.getZoom()){this.map.setZoom(this.mc.getMaxZoom()+1);}},this);var zoom=this.map.getZoom();this.om.add(this.point,zoom,zoom);}
YandexCluster.prototype.remove=function(){this.markers=[];if(this.point){this.point=null;}};;function GeoMap(elid,points){this.elid=elid;this.gmap_el=document.getElementById(elid);this.points=points;this.map_zoom=0;this.map_center=0;this.min_zoom=10;this.max_zoom=16;this.minLat=0;this.minLon=0;this.maxLat=0;this.maxLon=0;this.points_on_map=0;this.map=null;this.marker_cluster=null;this.show_title_link=true;this.dirty=true;}
GeoMap.prototype.show=function(){if(this.map){this.map.redraw();}
if(this.dirty){if(this.gmap_el.innerHTML==''){this.init(true);}else{this.init(false);}}
$(this.gmap_el).removeClass('hidden');};GeoMap.prototype.hide=function(){$(this.gmap_el).addClass('hidden');};GeoMap.prototype.init=function(start){var that=this;if(start){var first_step=0;for(i in this.points){if(this.points[i].COORD[0]&&this.points[i].COORD[1]&&!this.points[i].hidden){var lat=Number(this.points[i].COORD[0]);var lon=Number(this.points[i].COORD[1]);if(first_step===0){first_step=i;this.minLat=lat;this.minLon=lon;this.maxLat=lat;this.maxLon=lon;}else{this.minLat=(lat>this.minLat)?lat:this.minLat;this.minLon=(lon<this.minLon)?lon:this.minLon;this.maxLat=(lat<this.maxLat)?lat:this.maxLat;this.maxLon=(lon>this.maxLon)?lon:this.maxLon;}
this.points_on_map++;}}
this.minLat+=0.004;this.minLon-=0.008;this.maxLat-=0.004;this.maxLon+=0.008;this.map=new YMaps.Map($(this.gmap_el));this.map.addControl(new YMaps.TypeControl());this.map.addControl(new YMaps.Zoom());this.map.enableScrollZoom();if(this.points_on_map>1){var bounds=new YMaps.GeoCollectionBounds([new YMaps.GeoPoint(this.minLon,this.minLat),new YMaps.GeoPoint(this.maxLon,this.maxLat)]);this.map.setBounds(bounds);}
else if(this.points_on_map==1){this.map_center=new YMaps.GeoPoint(Number(this.points[first_step].COORD[1]),Number(this.points[first_step].COORD[0]));this.map.setCenter(this.map_center,7);this.map.setZoom(this.map.getMaxZoom()-2);}
this.addPoints();}
else{this.updatePoints();}
this.dirty=false;};GeoMap.prototype.createMarker=function(lat,lng,point){var icon=new YMaps.Style();icon.iconStyle=new YMaps.IconStyle();icon.iconStyle.href=template_url+'/i/gmarker.png';icon.iconStyle.size=new YMaps.Point(26,30);icon.iconStyle.offset=new YMaps.Point(-13,-27);icon.iconStyle.shadow=new YMaps.IconShadowStyle();icon.iconStyle.shadow.href=template_url+'/i/gmarker-shadow.png';icon.iconStyle.shadow.size=new YMaps.Point(26,30);icon.iconStyle.shadow.offset=new YMaps.Point(-13,-27);var position=new YMaps.GeoPoint(Number(lng),Number(lat));var marker=new YMaps.Placemark(position,{style:icon,hasBalloon:false});if(this.show_title_link){YMaps.Events.observe(marker,marker.Events.Click,function(){point_click_on_map(point.ID);});}
return marker;};GeoMap.prototype.addPoints=function(){var gpoints=[];for(i in this.points){var point=this.points[i];if(point.COORD[0]&&point.COORD[1]&&!point.hidden){gpoints.push(this.createMarker(point.COORD[0],point.COORD[1],point));}}
var msie=$.browser.msie;var opts={grid:msie?65:50,centered:msie?false:true,style:{icon:template_url+'/i/gmarker-multiple.png',height:33,width:20,offset:[-10,-16],textColor:'#fff',textSize:11}};this.marker_cluster=new YandexClusterer(this.map,gpoints,opts);this.map.redraw();};GeoMap.prototype.updatePoints=function(){var gpoints=[];for(i in this.points){var point=this.points[i];if(point.COORD[0]&&point.COORD[1]&&!point.hidden){gpoints.push(this.createMarker(point.COORD[0],point.COORD[1],point));}}
this.marker_cluster.clearMarkers();this.marker_cluster.setMarkers(gpoints);this.marker_cluster.repaint();};;$('#point-list-table').click(function(e){e.stopPropagation();e=e||window.event();var target=e.target||e.srcElement;if($(target).hasClass('ptitle')){$.fancybox({'orig':$(target),'padding':0,'scrolling':'no','overlayColor':'#000','overlayOpacity':0.5,'href':$(target).attr('href'),'transitionIn':'elastic','transitionOut':'elastic','onComplete':function(){init_point_page();}});}
return false;});$(function(){if($('#branches-region-select').length){region_list_from_select('branches-region-select','branches-region-list','Выберите город для просмотра '+popup_postfix_type+':',SEF_FOLDER,'',5,true);$('#geo-list-trigger').fancybox({'padding':15,'titleShow':false});}
$('#print-trigger-card').live('click',function(){$('#point-card').jqprint();return false;});});function show_map_trigger(){if(!trigger_list)trigger_list=document.getElementById('list-trigger');if(!trigger_map)trigger_map=document.getElementById('map-trigger');var map_spinner=$('#map-loading');map_spinner.removeAttr('style');$('#list-trigger:not(.current)').live('click',function(){map_spinner.removeClass('hidden');setTimeout(function(){points_table.style.display='block';map_spinner.addClass('hidden');},0);list_map.hide();trigger_list.className='current';trigger_map.className='';return false;});$('#map-trigger:not(.current):not(.disabled)').live('click',function(){map_spinner.removeClass('hidden');points_table.style.display='none';trigger_map.className='current';trigger_list.className='';setTimeout(function(){list_map.show();map_spinner.addClass('hidden');},0);return false;});}
function point_click_on_map(pid){$.fancybox({'padding':0,'scrolling':'no','overlayColor':'#000','overlayOpacity':0.5,'href':$('#row-'+pid+' .ptitle').attr('href')+'?ajax=Y','transitionIn':'elastic','transitionOut':'elastic','onComplete':function(){init_point_map();}});}
