var map=null;
var geocoder=null;
var marker;
var icon=new GIcon();
icon.image="http://wns.ru/images/hotspot.png";
icon.shadow="http://wns.ru/images/hotspot_shadow.png";
icon.iconSize=new GSize(28,26);
icon.shadowSize=new GSize(41,26);
icon.iconAnchor=new GPoint(12,28);
icon.infoWindowAnchor=new GPoint(13,23);
icon.infoShadowAnchor=new GPoint(13,23);

var iconCurrent=new GIcon();
iconCurrent.image="http://wns.ru/images/hotspot0.png";
iconCurrent.shadow="http://wns.ru/images/hotspot_shadow.png";
iconCurrent.iconSize=new GSize(28,26);
iconCurrent.shadowSize=new GSize(41,26);
iconCurrent.iconAnchor=new GPoint(12,28);
iconCurrent.infoWindowAnchor=new GPoint(13,23);
iconCurrent.infoShadowAnchor=new GPoint(13,23);

function showAddress(address,zoom,popupWin,showMarker) {
	if(geocoder) {
		geocoder.getLatLng(address,function(point) {
			if(!point) {
				alert('Адрес &laquo;'+address+'&raquo; не найден');
			} else {
				map.setCenter(point,zoom);
				if(showMarker){
					var marker=new GMarker(point);
					map.addOverlay(marker);
				}
			
				if(popupWin) marker.openInfoWindowHtml(address);
			}
		});
	}
}

function jumpToAddress(address,zoom) {
	var point=null;
	geocoder=new GClientGeocoder();
	geocoder.getLatLng(address,function(point){
		if(!point){
			alert(address+" not found")
		} else {
			map.setCenter(point,zoom);
		}
	});
}


function showAddressOnPreview(address) {
	if(geocoder && address!='') {
		geocoder.getLatLng(address,function(point){
			$('add-map-overflow').addClass('noDisplay');
			if(!point){
				point=new GLatLng($('spotLat').value,$('spotLong').value);
				zoomC = 13;
			}
			else{
				map.clearOverlays(marker);
				zoomC = 16;
			}
			map.setCenter(point,zoomC);
			
			var marker=new GMarker(point,{draggable:true});
			GEvent.addListener(marker,"dragend",function(){
				$('spotLong').value=marker.getPoint().lng();
				$('spotLat').value=marker.getPoint().lat();
			});
			map.addOverlay(marker);
			marker.openExtInfoWindow(map,"estetic_window",'<div style="font:16px bold Verdana;">   ,    .<br/><div style="color: gray; font:12px normal Verdana;">       .</div></div>',{beakOffset:3});
			$('spotLong').value=marker.getPoint().lng();
			$('spotLat').value=marker.getPoint().lat();
		});
	}
}

function createMarker(point,index){
	var marker=new GMarker(point,icon);
	GEvent.addListener(marker,"click",function(){
		marker.openExtInfoWindow(map,"estetic_window",index,{beakOffset:3});
	});
	return marker;
}

function createCurrentMarker(point,index){
	var marker=new GMarker(point,icon);
	GEvent.addListener(marker,'click',function(){
		marker.openExtInfoWindow(map,"estetic_window",index,{beakOffset:3});
	});
	return marker
}

function initialize_OLD(){
	if(GBrowserIsCompatible()){
		if($("map_canvas")){
			map=new GMap2($("map_canvas"));
			clusterer = new Clusterer(map);
			map.removeMapType(G_HYBRID_MAP);
			map.setCenter(new GLatLng(parseFloat($('map_center_cont_lat').innerHTML),parseFloat($('map_center_cont_long').innerHTML)),parseFloat($('map_zoom_cont').innerHTML));
			var mapControl=new GMapTypeControl();
			map.addControl(mapControl);
			if(!$('map_canvas').hasClass('add-map')) map.addControl(new GOverviewMapControl());
			map.addControl(new GScaleControl());
			$('zoomIn').addEvent('click',function(event){
				event.stop();map.zoomIn();
			});
			$('zoomOut').addEvent('click',function(event){
				event.stop();map.zoomOut();
				});
			paintAllSpots('all');
			if($('jumpToAddress')){
				jumpToAddress($('jumpToAddress').innerHTML,15);
			}
			geocoder=new GClientGeocoder();
		}
	}
}
function initializeSmall(){
	if(GBrowserIsCompatible()){
		if($('smallMap')) {
			mapSmall=new GMap2(document.getElementById("smallMap"));
			mapSmall.removeMapType(G_HYBRID_MAP);
			$('zoomInSmall').addEvent('click',function(event){
				event.stop();
				mapSmall.zoomIn();
			});
			$('zoomOutSmall').addEvent('click',function(event){
				event.stop();
				mapSmall.zoomOut();
			});
			
			var mapControl=new GMapTypeControl();
			mapSmall.addControl(mapControl);
			mapSmall.setCenter(new GLatLng(parseFloat($('map_center_cont_lat').innerHTML),parseFloat($('map_center_cont_long').innerHTML)),parseFloat($('map_zoom_cont').innerHTML));
			
			/*
			new GLatLng(parseFloat($('map_center_cont_lat').innerHTML),parseFloat($('map_center_cont_long').innerHTML))
			if(!geocoder) geocoder=new GClientGeocoder();
			var point=new GLatLng($('spotLat').value,$('spotLong').value);
			
			var marker=new GMarker(point,{draggable:true});
			GEvent.addListener(marker,"dragend",function(){
				$('spotLong').value=marker.getPoint().lng();
				$('spotLat').value=marker.getPoint().lat();
			});*/
			mapSmall.addOverlay(marker);
		}
	}
}

function createMarkerOfWF(point, index, type) {
	if(type == '1') var marker = new GMarker(point, iconCurrent);	
	else var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() { 
		extWinEst(map, marker, index);
	});
	return marker;
}

function extWinEst(map, marker, index) {
	marker.openExtInfoWindow(
		  map,
		  "estetic_window",
		  "<div id=\"spotPopupData"+index+"\"></div>",
		  {beakOffset: 3}
		); 
		if($('spotPopupData'+index).innerHTML == '') {
			$('spotPopupData'+index).innerHTML = $('ewz_'+index).innerHTML; //'kj '+index;
		}
	}

function extWinEst_OLD(map, marker, index) {
	marker.openExtInfoWindow(
		  map,
		  "estetic_window",
		  "<div id=\"spotPopupData"+index+"\"></div>",
		  {beakOffset: 3}
		); 
		
		if($('spotPopupData'+index).innerHTML == '') {
			var req = new Request({
			method: 'get',
			url: url2spotInfo + index,
			onComplete: function(responseText) { 
				$('spotPopupData'+index).innerHTML = responseText;
				}
			}).send();
		}
	}
	
function showSpotsInterval() {
 	for (i = 0; i < resp.length; i ++) {
		if(resp[i]) {
                                                                var f = resp[i].split('-');
			var mr = createMarkerOfWF(new GLatLng(f[1], f[2]), f[0], f[3]);
			freeSpots[i] = mr;
			clusterer.AddMarker(mr, 'ttt');
			
			if(curMarkerId == f[0] && curMarkerId != '0') extWinEst(map, mr, curMarkerId);
		}
	}
	if($('bottomActions')) $('bottomActions').innerHTML = 'Точки загружены. Всего в городе&nbsp;&mdash; ' + (allSpots - 1) + '.';
}

function hidePaidMarker() {
	for(i = 0; i < freeSpots.length; i ++) {
		clusterer.RemoveMarker(freeSpots[i]); 
	}
}

function toggleMarkers(this_) {
	hidePaidMarker();
	$('bottomActions').innerHTML = 'Подождите, идет загрузка точек...';
	if(curClusterState == 'all') {
		curClusterState = 'free';
		paintAllSpots('free');
		this_.innerHTML = 'показать платные точки';
	} else {
		curClusterState = 'all';
		paintAllSpots('all');
		this_.innerHTML = 'скрыть платные точки';
	}
	return false;
}

var curClusterState = 'all';
var resp = new Array;
var showPer = 700;
var allSpots = 0;
var ttt = 0;
var freeSpots = new Array;
var allSpots = new Array;

function paintAllSpots(type, test) {
                    
if(type == 'free') divid = 'coords'; //'coords_free';
	else divid = 'coords';

	resp = test.split(':');

                     allSpots = resp.length;
	showSpotsInterval();
}

function paintAllSpots_OLD(type) {
	if(type == 'free') getUrl = url2spots + '&type=free';
	else getUrl = url2spots;
	var req = new Request({
		method: 'get',
		url: getUrl,
		onComplete: function(responseText) { 
			resp = responseText.split(":");
			allSpots = resp.length;
			showSpotsInterval();
		}
	}).send();
}
