var COLORS = [["red", "#ff0000"], ["orange", "#ff8800"], ["green","#008000"],
              ["blue", "#000080"], ["purple", "#800080"]];
var options = {};
var map;
var lineCounter_ = 0;
var shapeCounter_ = 0;
var markerCounter_ = 0;
var allCounter_ = 0;

//google.load("maps", "2", {callback: initialize});


// маркер существующих мест
var iconRed = new GIcon(); 
iconRed.image = '/bitrix/templates/xteam2/images/map/ico_o.png';
iconRed.shadow = '/bitrix/templates/xteam2/images/map/ico_sh.png';
iconRed.iconSize = new GSize(25, 30);
iconRed.shadowSize = new GSize(35, 30);
iconRed.iconAnchor = new GPoint(7, 30);
iconRed.infoWindowAnchor = new GPoint(7, 30);

var iconGreen = new GIcon(); 
iconGreen.image = '/bitrix/templates/xteam2/images/map/ico_g.png';
iconGreen.shadow = '/bitrix/templates/xteam2/images/map/ico_sh.png';
iconGreen.iconSize = new GSize(25, 30);
iconGreen.shadowSize = new GSize(35, 30);
iconGreen.iconAnchor = new GPoint(7, 30);
iconGreen.infoWindowAnchor = new GPoint(7, 30);

var iconSt = new GIcon(G_DEFAULT_ICON); 
iconSt.image = '/bitrix/templates/xteam2/images/map/ico_st.png';

var customIcons = [];
customIcons["travel_g"] = iconGreen;
customIcons["travel_o"] = iconRed;
customIcons["standart"] = iconSt;


var iconBg_w = new GIcon(); 
iconBg_w.image = '/bitrix/templates/xteam2/images/map/img_marker_bg.png';
iconBg_w.shadow = '/bitrix/templates/xteam2/images/map/img_marker_bg.png';
iconBg_w.transparent = '/bitrix/templates/xteam2/images/map/img_marker_spacer.png';
iconBg_w.iconSize = new GSize(34, 39);
iconBg_w.shadowSize = new GSize(34, 39);
iconBg_w.iconAnchor = new GPoint(9, 26);
iconBg_w.infoWindowAnchor = new GPoint(17, 0);

var iconBg_o = new GIcon(iconBg_w); 
iconBg_o.image = '/bitrix/templates/xteam2/images/map/img_marker_bg_o.png';
iconBg_o.shadow = '/bitrix/templates/xteam2/images/map/img_marker_bg_o.png';

var photo_icon = new GIcon(); 
photo_icon.iconSize = new GSize(28, 28);
photo_icon.shadowSize = new GSize(28, 28);
photo_icon.transparent = '/bitrix/templates/xteam2/images/map/img_marker_spacer.png';
photo_icon.iconAnchor = new GPoint(6, 23);
photo_icon.infoWindowAnchor = new GPoint(18, 7);

customIcons["iconBg_w"] = iconBg_w;
customIcons["iconBg_o"] = iconBg_o;
customIcons["photo_icon"] = photo_icon;

var markerGroups = {"travel_g": [], "travel_o": [], "standart": [], "iconBg_w": [], "iconBg_o": [], "photo_icon": [], "place_polygon": []};



//квадратик на углах линий
var iconBg_cor = new GIcon(); 
iconBg_cor.image = '/bitrix/templates/xteam2/images/map/cor_9c3515.gif';
iconBg_cor.shadow = '/bitrix/templates/xteam2/images/map/cor_tr.png';
iconBg_cor.transparent = '/bitrix/templates/xteam2/images/map/cor_tr.png';
iconBg_cor.iconSize = new GSize(9, 9);
iconBg_cor.shadowSize = new GSize(9, 9);
iconBg_cor.iconAnchor = new GPoint(4, 4);
customIcons["iconBg_cor"] = iconBg_cor;



function initialize() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		
		map.setCenter(new GLatLng(55.732523, 37.625384), 13);
		map_type_set(2);

		map.clearOverlays();
		
		GEvent.addListener(map, 'zoomend', function() {
			map_zoom_scroll_act();
		});
		map_zoom_scroll_act();
		
		/*arTypes = map.getMapTypes();
		map.setMapType(arTypes[0]);*/
	}
}




function startShape(pts, name, color, id, index) {
	var color = color;
	if(color=="") color="#ff0000";
	var polygon = new GPolygon(pts, color, 3, 0.7, color, 0.2);
	++shapeCounter_;
	++allCounter_;
	startDrawing(polygon, name, color, 'polygon', shapeCounter_, true, id, index);
}


function startLine(pts, name, color, id) {
	var color = color;
	if(color=="") color="#ff0000";
	//var line = new GPolyline(pts, color, 3);
	var line = new BDCCPolyline(pts,color,3,1,name,"dash");
	/*for ( var k in pts ){
		var marker = new GMarker(pts[k], customIcons["iconBg_cor"]);
		map.addOverlay(marker);
	}*/
	++lineCounter_;
	++allCounter_;
	startDrawing(line, name, color, 'line', lineCounter_, true, id);
}
function startDrawing(poly, name, color, typeOb, count, load, id, index){
	map.addOverlay(poly);
	poly.setStrokeStyle({color: color});
	
	GEvent.addListener(poly, "click", function(latlng, index) {
		htmlform = "<div class='map_cloud'>"+name+"</div>";
		map.openInfoWindowHtml(latlng,htmlform);
	});
}




function placeMarker(latlng, name, index, id){
	if(org_com)
		type = "travel_o";
	else
		type = "travel_g";
	
	var marker = new GMarker(latlng, {icon:customIcons["standart"], draggable: false});
	markerGroups[type].push(marker);
	
	++markerCounter_;
	++allCounter_;
	
	var countm = markerCounter_;
	
	GEvent.addListener(marker, 'click', function() {
		htmlform = name;
		marker.openInfoWindowHtml(htmlform);
	});
	map.addOverlay(marker);
}




function place_to_travel(latlng, name, pts, color, jsParams){
	++allCounter_;
	var html = "";
	
	if (jsParams["photo30"]!="" && jsParams["photo110"]!=""){
		src = jsParams["photo110"];
		src_icon = jsParams["photo30"];
	}else{
		src = '/bitrix/templates/xteam2/images/z_travel_map.jpg';
		src_icon = '/bitrix/templates/xteam2/images/z_travel_mini.jpg';
	}
	var html = "";
	html += '<table cellspacing="0" cellpadding="0" class="map_cloud">';
	html += '	<tr>';
	html += '		<td width="122"><a href="/places/detail.php?ID=' + jsParams["ID"] + '"><img src="'+src+'" alt="'+name+'" width="110" height="83" border="0"></a></td>';
	html += '		<td>';
	html += '			<div class="title"><a href="/places/detail.php?ID=' + jsParams["ID"] + '">'+name+'</a></div>';
	
	if(jsParams["category"]=='Профессиональный'){
		html += '			<div class="map_cloud_p_com">Профессиональные услуги</div>';
		type_icon = "iconBg_o";
	}else{
		html += '			<div class="map_cloud_p_notcom"></div>';
		type_icon = "iconBg_w";
	}
	
	if (jsParams["USER_ID"]>0){
	
		var class_logo_com = "company_td";
	
		if (jsParams["USER_photomini"]=="") jsParams["USER_photomini"]="/bitrix/templates/xteam2/images/z_user_30.gif";
		
		org_com = jsParams["USER_COM"];
		org_id = jsParams["USER_ID"];
		org_photomini = jsParams["USER_photomini"];
		org_login = jsParams["USER_LOGIN"];
		org_com_logo = jsParams["USER_WORK_LOGO"];
		org_com_id = jsParams["USER_COMPANY_ID"];
		org_com_name = jsParams["USER_COMPANY_NAME"];
	
		html += '			<div class="map_cloud_org_title">Автор:</div>';
		
		html += '<div class="org_timer">';
		html += '	<table cellspacing="0" cellpadding="0" class="list_item_org_left"><tr>';
		
		if (org_com_id>0 && org_com=="Y"){
			html += '		<td class="avatar_td">';
			html += '		<a href="/timer/user/'+org_id+'/"><img src="'+org_photomini+'" alt="'+org_login+'" title="'+org_login+'" width="30" height="30"></a>';
			html += '		</td>';
		}else{
			html += '		<td class="'+class_logo_com+'">';
			html += '		<a href="/timer/user/'+org_id+'/">'+org_com_logo+'</a>';
			html += '		</td>';
		}
		
		if (org_com_id>0 && org_com=="Y"){
			html += '	<td class="sp_td"><img src="/bitrix/templates/xteam2/images/spacer.gif" alt="" width="1" height="1" /></td>';
			html += '	<td class="'+class_logo_com+'"><a href="/timer/company/'+org_com_id+'/">'+org_com_logo+'</a></td>';
		}
		
		html += '	</tr></table>';
		
		if (org_com_id>0 && org_com=="Y")
			var classtd = "best_item";
		else
			var classtd = 'not_company';
		
		html += '	<div class="'+classtd+'">';
			html += '	<a href="/timer/user/'+org_id+'/">'+org_login+'</a>';
			if (org_com_id>0 && org_com=="Y"){
				html += '<br/><a href="/timer/company/'+org_com_id+'/">'+org_com_name+'</a>';
			}
		html += '	</div>';
		
		html += '</div>';
		
		
		
	}
	
	html += '		</td>';
	html += '	</tr>';
	html += '</table>';
	
	if(pts && color){
		var polygon = createPolygon(pts, color, html);
		var marker = createMarkeredLabel(latlng, type_icon, src_icon, html, polygon);
	}else{
		var marker = createMarkeredLabel(latlng, type_icon, src_icon, html);
	}
}





//маркер - фотка
function createMarkeredLabel(point,type,src,html,polygon) {
	marker_bg = new GMarker(point,customIcons[type]);
	map.addOverlay(marker_bg);
	
	customIcons['photo_icon'].image = src;
	var marker = new GMarker(point,customIcons['photo_icon']);
	GEvent.addListener(marker, "click", function(LatLng) {
		map.openInfoWindowHtml(LatLng, html);
	});
	map.addOverlay(marker);
	
	if(polygon){
		GEvent.addListener(marker, "mouseover", function() {
			polygon.setStrokeStyle({opacity: 1});
			polygon.setFillStyle({opacity: 0.25});
		});
		GEvent.addListener(marker, "mouseout", function() {
			polygon.setStrokeStyle({opacity: 0.3});
			polygon.setFillStyle({opacity: 0.1});
		});
	}
	
	return marker;
}


//создать маркер
function createMarker(point, name, address, type) {
	var marker = new GMarker(point, customIcons[type]);
	markerGroups[type].push(marker);
	var html = "<b>" + name + "</b> <br/>" + address;
	GEvent.addListener(marker, 'click', function(LatLng) {
		map.openInfoWindowHtml(LatLng, html);
	});
	return marker;
}




function createPolygon(pts, color, html){
	var polygon = new GPolygon(pts, color, 3, 0.3, color, 0.1);
	map.addOverlay(polygon);
	GEvent.addListener(polygon, "click", function(latlng, index) {
		map.openInfoWindowHtml(latlng,html);
	});
	return polygon;
}





function map_zoom_plus(){
	map.zoomIn();
}
function map_zoom_min(){
	map.zoomOut();
}
function map_zoom_set(zoom){
	map.setZoom(zoom);
}
function map_zoom_scroll_act(){
	var zoom = map.getZoom();
	var flag=0;
	for (i=0; i<=19; i++){
		if (i > zoom) flag=1;
		if (flag == 0)
			document.getElementById("map_zoom_level_"+i).className = "map_zoom_level_a";
		else
			document.getElementById("map_zoom_level_"+i).className = "map_zoom_level_n";
	}
}

function map_type_set(type){
	arTypes = map.getMapTypes();
	map.setMapType(arTypes[type]);
	for (i=0; i<=2; i++){
		if (i==type)
			document.getElementById("map_type_"+i).className = "map_type_item_a";
		else
			document.getElementById("map_type_"+i).className = "map_type_item_n";
	}
}









function viewObject(name) 
{ 
var obj = eval(name), i; 

if(!obj) 
{ 
alert("\""+name+"\" ia not an object"); 
return; 
} 

var w_Test = open("","Test","width=600,height=500,scrollbars=1"); 

if(!w_Test) 
{ 
alert("Cannot open window for "+name); 
return; 
} 

w_Test.document.open(); 

for(i in obj) 
w_Test.document.write(name+"."+i+"="+obj[i]+"<br>"); 

w_Test.document.close(); 
} 

