google.load("maps", "2.x");
google.setOnLoadCallback(initializeMap);

defaultCoordX = 47.78069900512695;
defaultCoordY = 52.02873286421382;
defaultZoom = 12;

function setupBalakovoMap(map, bBMZoomControl)
{
	map.setCenter(new google.maps.LatLng(defaultCoordY, defaultCoordX), defaultZoom);
	
	//map.setUIToDefault();
	if (bBMZoomControl)
		map.addControl(getBMZoomControl(map));
	else
		map.addControl(new GSmallMapControl());
	
	//addLayer(map, 'landscape');
	
	
	var copyCollection = new GCopyrightCollection('');
	var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)), 13,"© balakovomaps.ru");
	copyCollection.addCopyright(copyright);
	
	var tilelayers = [
	                  getTileLayer(copyCollection, 'all'),
	                  getCopyrightLayer(copyCollection)/*,
	                  getTileLayer(copyCollection, 'landscape'),
	                  getTileLayer(copyCollection, 'roads'),
	                  getTileLayer(copyCollection, 'bridges'),
	                  getTileLayer(copyCollection, 'highways'),
	                  getTileLayer(copyCollection, 'fencings'),
	                  getTileLayer(copyCollection, 'outskirts'),
	                  getTileLayer(copyCollection, 'builts'),
	                  getTileLayer(copyCollection, 'stops'),
	                  getTileLayer(copyCollection, 'lights'),*/
	                 ];
	
	balakovomap = new GMapType(tilelayers, G_NORMAL_MAP.getProjection(), "Карта Балаково", {errorMessage:"Нет данных"});
	//balakovomap.hasOverlay = function () {return true;};
	map.addMapType(balakovomap);
	map.setMapType(balakovomap);
	
	map.removeMapType(G_NORMAL_MAP);
	map.removeMapType(G_HYBRID_MAP);
	map.removeMapType(G_SATELLITE_MAP);
	map.removeMapType(G_PHYSICAL_MAP);
	
	
	var mapControl = new GHierarchicalMapTypeControl();

	// Set up map type menu relationships
	mapControl.clearRelationships();
	mapControl.addRelationship(balakovomap, tilelayers[0], "Crosshairs");

	// Add control after you've specified the relationships
	map.addControl(mapControl);

	
}

function getTileLayer(copyCollection, name)
{
	var tileLayer = new GTileLayer(copyCollection, 12, 16);
	
	tileLayer.getTileUrl = function(a,b){
		var v=0;
		if ((b==7)&&(a.x>79)&&(a.x<82)&&(a.y>41)&&(a.y<43)) {v=1;}
		if ((b==8)&&(a.x>160)&&(a.x<163)&&(a.y>83)&&(a.y<85)) {v=1;}
		if ((b==9)&&(a.x>322)&&(a.x<325)&&(a.y>167)&&(a.y<170)) {v=1;}
		if ((b==10)&&(a.x>646)&&(a.x<649)&&(a.y>336)&&(a.y<339)) {v=1;}
		if ((b==11)&&(a.x>1293)&&(a.x<1297)&&(a.y>674)&&(a.y<678)) {v=1;}
		if ((b==12)&&(a.x>2588)&&(a.x<2594)&&(a.y>1349)&&(a.y<1355)) {v=1;}
		if ((b==13)&&(a.x>5178)&&(a.x<5187)&&(a.y>2700)&&(a.y<2709)) {v=1;}
		if ((b==14)&&(a.x>10358)&&(a.x<10373)&&(a.y>5402)&&(a.y<5417)) {v=1;}
		if ((b==15)&&(a.x>20718)&&(a.x<20746)&&(a.y>10806)&&(a.y<10834)) {v=1;}
		if ((b==16)&&(a.x>41437)&&(a.x<41491)&&(a.y>21613)&&(a.y<21667)) {v=1;}
		
		if (v==1) {
			var z = /*17 -*/ b;
			var f = "http://www.balakovomaps.ru/img/map1/" + name + "/" + z +"_"+a.x+"_"+a.y+".png";
			return f;
		} else {
			return null;// G_NORMAL_MAP.getTileLayers()[0].getTileUrl(a,b);
		}
	};
	
	tileLayer.isPng = function() {return true;};
	
	tileLayer.getOpacity = function() { return 1.0; };
	
	return tileLayer;
}

function getCopyrightLayer(copyCollection)
{
	var tileLayer = new GTileLayer(copyCollection, 12, 16);
	
	tileLayer.getTileUrl = function(a,b){
		var v=0;
		if ((b==7)&&(a.x>79)&&(a.x<82)&&(a.y>41)&&(a.y<43)) {v=1;}
		if ((b==8)&&(a.x>160)&&(a.x<163)&&(a.y>83)&&(a.y<85)) {v=1;}
		if ((b==9)&&(a.x>322)&&(a.x<325)&&(a.y>167)&&(a.y<170)) {v=1;}
		if ((b==10)&&(a.x>646)&&(a.x<649)&&(a.y>336)&&(a.y<339)) {v=1;}
		if ((b==11)&&(a.x>1294)&&(a.x<1297)&&(a.y>674)&&(a.y<677)) {v=1;}
		if ((b==12)&&(a.x>2589)&&(a.x<2593)&&(a.y>1350)&&(a.y<1354)) {v=1;}
		if ((b==13)&&(a.x>5180)&&(a.x<5186)&&(a.y>2702)&&(a.y<2708)) {v=1;}
		if ((b==14)&&(a.x>10362)&&(a.x<10371)&&(a.y>5405)&&(a.y<5415)) {v=1;}
		if ((b==15)&&(a.x>20725)&&(a.x<20741)&&(a.y>10811)&&(a.y<10830)) {v=1;}
		if ((b==16)&&(a.x>41451)&&(a.x<41481)&&(a.y>21624)&&(a.y<21660)) {v=1;}
		
		if (v==1) {
			var f = "http://www.balakovomaps.ru/img/map/copyright.png";
			return f;
		} else {
			return G_NORMAL_MAP.getTileLayers()[0].getTileUrl(a,b);
		}
	};
	
	tileLayer.isPng = function() {return true;};
	
	//tileLayer.getOpacity = function() { return 0.6; };
	
	return tileLayer;
}

function addLayer(map, name)
{
	// custom maps
	var copyCollection = new GCopyrightCollection(name);
	var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)), 7, "©2010 balakovomaps.ru");
	copyCollection.addCopyright(copyright);

	var tilelayers = [new GTileLayer(copyCollection, 7, 16)];
	tilelayers[0].getTileUrl = function(a,b){
		var v=0;
		if ((b==7)&&(a.x>79)&&(a.x<82)&&(a.y>41)&&(a.y<43)) {v=1;}
		if ((b==8)&&(a.x>160)&&(a.x<163)&&(a.y>83)&&(a.y<85)) {v=1;}
		if ((b==9)&&(a.x>322)&&(a.x<325)&&(a.y>167)&&(a.y<170)) {v=1;}
		if ((b==10)&&(a.x>646)&&(a.x<649)&&(a.y>336)&&(a.y<339)) {v=1;}
		if ((b==11)&&(a.x>1294)&&(a.x<1297)&&(a.y>674)&&(a.y<677)) {v=1;}
		if ((b==12)&&(a.x>2589)&&(a.x<2593)&&(a.y>1350)&&(a.y<1354)) {v=1;}
		if ((b==13)&&(a.x>5180)&&(a.x<5186)&&(a.y>2702)&&(a.y<2708)) {v=1;}
		if ((b==14)&&(a.x>10362)&&(a.x<10371)&&(a.y>5405)&&(a.y<5415)) {v=1;}
		if ((b==15)&&(a.x>20725)&&(a.x<20741)&&(a.y>10811)&&(a.y<10830)) {v=1;}
		if ((b==16)&&(a.x>41451)&&(a.x<41481)&&(a.y>21624)&&(a.y<21660)) {v=1;}
		
		if (v==1) {
			var z = /*17 -*/ b;
			var f = "http://www.balakovomaps.ru/img/map/" + name + "/" + z +"_"+a.x+"_"+a.y+".png";
			return f;
		} else {
			return G_NORMAL_MAP.getTileLayers()[0].getTileUrl(a,b);
		}
	};
	tilelayers[0].isPng = function() { return true;};
	tilelayers[0].getOpacity = function() { return 1.0; };
	
	overlay = new GTileLayerOverlay( tilelayers[0] );
    map.addOverlay(overlay);
    
    /*
	var custommap = new GMapType(tilelayers, G_NORMAL_MAP.getProjection(), "Roads", {errorMessage:"No chart data available"});
	map.addMapType(custommap);
	map.setMapType(custommap);
	*/
}

function getBMZoomControl()
{
	//A TextualZoomControl is a GControl that displays textual "Zoom In"
	//and "Zoom Out" buttons (as opposed to the iconic buttons used in
	//Google Maps).

	//We define the function first
	function BMZoomControl() {
	}

	//To "subclass" the GControl, we set the prototype object to
	//an instance of the GControl object
	BMZoomControl.prototype = new google.maps.Control();

	//Creates a one DIV for each of the buttons and places them in a container
	//DIV which is returned as our control element. We add the control to
	//to the map container and return the element for the map class to
	//position properly.
	BMZoomControl.prototype.initialize = function(map) {
		var container = document.createElement("div");
		
		var moveUp = document.createElement("a");
		moveUp.className = 'move-up';
		moveUp.title = 'Вверх';
		container.appendChild(moveUp);
		moveUp.appendChild(document.createTextNode("Вверх"));
		GEvent.addDomListener(moveUp, "click", function() {
			map.panBy(new GSize(0,200));
		});
		
		var moveRight = document.createElement("a");
		moveRight.className = 'move-right';
		container.appendChild(moveRight);
		moveRight.appendChild(document.createTextNode("Вправо"));
		GEvent.addDomListener(moveRight, "click", function() {
			map.panBy(new GSize(-200,0));
		});
		
		var moveDown = document.createElement("a");
		moveDown.className = 'move-bottom';
		container.appendChild(moveDown);
		moveDown.appendChild(document.createTextNode("Вниз"));
		GEvent.addDomListener(moveDown, "click", function() {
			map.panBy(new GSize(0,-200));
		});
		
		var moveLeft = document.createElement("a");
		moveLeft.className = 'move-left';
		container.appendChild(moveLeft);
		moveLeft.appendChild(document.createTextNode("Влево"));
		GEvent.addDomListener(moveLeft, "click", function() {
			map.panBy(new GSize(200,0));
		});
		
		var zoomIn = document.createElement("a");
		zoomIn.className = 'zoom-in';
		container.appendChild(zoomIn);
		zoomIn.appendChild(document.createTextNode("Приблизить"));
		GEvent.addDomListener(zoomIn, "click", function() {
			map.zoomIn();
		});
		
		var zoomOut = document.createElement("a");
		zoomOut.className = 'zoom-out';
		container.appendChild(zoomOut);
		zoomOut.appendChild(document.createTextNode("Отдалить"));
		GEvent.addDomListener(zoomOut, "click", function() {
			map.zoomOut();
		});
		
		var container1 = document.createElement("div");
		container1.className = 'map-controls';
		container1.appendChild(container);
		
		map.getContainer().appendChild(container1);
		return container1;
	};

	//By default, the control will appear in the top left corner of the
	//map with 7 pixels of padding.
	BMZoomControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
	};
	
	return new BMZoomControl();
}


