google.load("maps", "2");
google.setOnLoadCallback(function(){
	var gMap;
	var gDirections;
	var gB1Marker;
	var gB2Marker;
	var gMargers = [];
	
	var b1Place = new google.maps.LatLng(55.729223, 24.312787);
	var b2Place = new google.maps.LatLng(55.729696, 24.308195);
	var bauhofPlace = new google.maps.LatLng(55.73046917259266, 24.299848079681396);
	var techCenterPlace = new google.maps.LatLng(55.728004258878, 24.29898977279663);
	var koneskoPlace = new google.maps.LatLng(55.72566002989725, 24.297616481781006);
	var sostenaPlace = new google.maps.LatLng(55.731266611358116, 24.294413924217224);

	var BBControl = function() {};
	
	BBControl.prototype = new GControl();
	BBControl.prototype.initialize = function(gMap) {
		this.container = document.createElement("div");
	
		this.addRestoreButton();
		
		this.addTownButton('Ryga', 'Iš Rygos');
		this.addTownButton('Siauliai', 'Iš Šiaulių');
		this.addTownButton('Kaunas', 'Iš Kauno');
		this.addTownButton('Vilnius', 'Iš Vilniaus');

		gMap.getContainer().appendChild(this.container);
		return this.container;
	};
	
	BBControl.prototype.addTownButton = function(from, label) {
		var fromRyga = document.createElement("div");
		this.container.appendChild(fromRyga);
		$(fromRyga).addClass('g-map-button');
		fromRyga.appendChild(document.createTextNode(label));
		GEvent.addDomListener(fromRyga, "click", function() {
			gDirections.load('from: ' + from +'; to: Panevezys, Klaipėdos gatvė, 150', {'locale': 'lt_LT'});
			for(var i = 0; i < gMargers.length; ++i) {
				gMargers[i].hide();
			}
		});
	};
	
	BBControl.prototype.addRestoreButton = function() {
		var btn = document.createElement("div");
		this.container.appendChild(btn);
		$(btn).addClass('g-map-button');
		btn.appendChild(document.createTextNode("Vieta mieste"));
		GEvent.addDomListener(btn, "click", function() {
			gDirections.clear();
			for(var i = 0; i < gMargers.length; ++i) {
				gMargers[i].show();
			}
			gMap.setCenter(b1Place, 13);
		});
	};
	
	BBControl.prototype.getDefaultPosition = function() {
		return new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(7, 7));
	};

	gMap = new google.maps.Map2(document.getElementById("g-map"));
	gMap.setCenter(b1Place, 13);
	gMap.addControl(new GLargeMapControl3D());
	gMap.addControl(new BBControl());
	gMap.enableScrollWheelZoom();
	
	var blueIcon = new GIcon(G_DEFAULT_ICON);
	blueIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";	
	
	gDirections = new GDirections(gMap);

	// Shops
	gB1Marker = new GMarker(b1Place, {'icon': blueIcon});
	gMap.addOverlay(gB1Marker);
	GEvent.addListener(gB1Marker, "click", function() {
		gB1Marker.openInfoWindowHtml('<div style="height: 225px; width: 400px;"><img src="client/img/gmap/babilonas-I-logo.gif" alt="" /><br/><img src="client/img/gmap/babilonas-I-photo.jpg" alt="" /></div>');
	});
	
	gB2Marker = new GMarker(b2Place, {'icon': blueIcon});
	gMap.addOverlay(gB2Marker);
	GEvent.addListener(gB2Marker, "click", function() {
		gB2Marker.openInfoWindowHtml('<div style="height: 230px; width: 400px;"><img src="client/img/gmap/babilonas-II-logo.gif" alt="" /><br/><img src="client/img/gmap/babilonas-II-photo.jpg" alt="" /></div>');
	});

	var bauhofMarker = new GMarker(bauhofPlace, {'icon': blueIcon});
	gMap.addOverlay(bauhofMarker);
	GEvent.addListener(bauhofMarker, "click", function() {
		bauhofMarker.openInfoWindowHtml('<div style="height: 210px; width: 400px;"><img src="client/img/gmap/bauhof-logo.gif" alt="" /><br/><img src="client/img/gmap/bauhof-photo.jpg" alt="" /></div>');
	});
	
	var techMarker = new GMarker(techCenterPlace, {'icon': blueIcon});
	gMap.addOverlay(techMarker);
	GEvent.addListener(techMarker, "click", function() {
		techMarker.openInfoWindowHtml('<div style="height: 230px; width: 400px;"><img src="client/img/gmap/tech-centras-logo.gif" alt="" /><br/><img src="client/img/gmap/tech-centras-photo.jpg" alt="" /></div>');
	});
	
	var koneskoMarker = new GMarker(koneskoPlace, {'icon': blueIcon});
	gMap.addOverlay(koneskoMarker);
	GEvent.addListener(koneskoMarker, "click", function() {
		koneskoMarker.openInfoWindowHtml('<div style="height: 260px; width: 400px;"><img src="client/img/gmap/konekesko-logo.gif" alt="" /><br/><img src="client/img/gmap/konekesko-photo.jpg" alt="" /></div>');
	});	
	
	var sostenaMarker = new GMarker(sostenaPlace, {'icon': blueIcon});
	gMap.addOverlay(sostenaMarker);
	GEvent.addListener(sostenaMarker, "click", function() {
		sostenaMarker.openInfoWindowHtml('<div style="height: 250px; width: 400px;"><img src="client/img/gmap/sostena-logo.gif" alt="" /><br/><img src="client/img/gmap/sostena-photo.jpg" alt="" /></div>');
	});		
	
	
	// Places
	var addPlace = function(place, name) {
		var marker = new GMarker(place);
		gMargers.push(marker);
		gMap.addOverlay(marker);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(name);
		});
	};
	
	addPlace(new GLatLng(55.735441, 24.367526), 'Respublikinė ligoninė');
	addPlace(new GLatLng(55.727702, 24.364983), 'Autobusų stotis');
	addPlace(new GLatLng(55.728590, 24.363191), 'Miltinio dramos teatras');
	addPlace(new GLatLng(55.727660, 24.368942), 'Centrinis miesto turgus');
	addPlace(new GLatLng(55.728765, 24.339910), 'Cido arena');
	
	function createPolygon(list) {
		var polygon = new GPolygon(list, "#666666", 1.0, 1, "#ebe6dc", 1.0);
		gMap.addOverlay(polygon);
	}
	
	createPolygon([new GLatLng(55.729889206895784, 24.311500310723204),
                   new GLatLng(55.729647551979234, 24.314374923706055),
                   new GLatLng(55.72863258499561, 24.3142032623291),
                   new GLatLng(55.728922578254796, 24.311134815216064),
                   new GLatLng(55.729889206895784, 24.311500310723204)]);
	
	createPolygon([new GLatLng(55.73017919082118, 24.308452606201172),
                   new GLatLng(55.730046281789406, 24.309825897216797),
                   new GLatLng(55.72956297240504, 24.309546947479248),
                   new GLatLng(55.72961130361273, 24.30898904800415),
                   new GLatLng(55.729309232583134, 24.30798053741455),
                   new GLatLng(55.72907965703752, 24.30776596069336),
                   new GLatLng(55.72935756410493, 24.30553436279297),
                   new GLatLng(55.72998586844357, 24.30602788925171),
                   new GLatLng(55.72986504147143, 24.30697202682495),
                   new GLatLng(55.73017919082118, 24.308452606201172)]);
	
	createPolygon([new GLatLng(55.731097459041756, 24.299139976501465),
	               new GLatLng(55.73080748193709, 24.30145883525256),
	               new GLatLng(55.729695883082215, 24.300684928894043),
	               new GLatLng(55.730058364447366, 24.298326015123166),
	               new GLatLng(55.731097459041756, 24.299139976501465)]);
	
	createPolygon([new GLatLng(55.72863258499561, 24.29901123046875),
	               new GLatLng(55.728460086344245, 24.299955368041992),
	               new GLatLng(55.727340914720124, 24.29901123046875),
	               new GLatLng(55.727437583567494, 24.298067092895508),
	               new GLatLng(55.72863258499561, 24.29901123046875)]);
	
	createPolygon([new GLatLng(55.72622254873297, 24.29755210876465),
	               new GLatLng(55.72605337369513, 24.298410415649414),
	               new GLatLng(55.72501413952495, 24.297809600830078),
	               new GLatLng(55.725231655942296, 24.29677963256836),
	               new GLatLng(55.72622254873297, 24.29755210876465)]);
	
	createPolygon([new GLatLng(55.73160491379209, 24.294163226732053),
	               new GLatLng(55.73145992739365, 24.295063018798828),
	               new GLatLng(55.73090414121129, 24.294676780700684),
	               new GLatLng(55.73104912967388, 24.293818473815918),
	               new GLatLng(55.73160491379209, 24.294163226732053)]);

	var polyline = new GPolyline([new GLatLng(55.7268572431032, 24.305250161743164),
	                              new GLatLng(55.72735809012119, 24.29600715637207)],
	                              "#444444", 1.5);
	gMap.addOverlay(polyline);

	var polyline = new GPolyline([new GLatLng(55.731300441585944, 24.302186965942383),
	                              new GLatLng(55.72494464937324, 24.298453330993652)],
	                              "#444444", 1.5);
	gMap.addOverlay(polyline);
});