
var allowedBounds = new GLatLngBounds(new GLatLng(53.7876,20.6872), new GLatLng(56.5776,26.9824));
var directions;
var map;
var mapClicked = false;
var layer = Array();
var layersData = {
	foto: {'url':'com.panoramio.all', 'index':'foto', 'name':'Nuotraukos'},
	video: {'url':'com.youtube.all', 'index':'video', 'name':'Video'},
	wiki: {'url':'org.wikipedia.lt', 'index':'wiki', 'name':'Wikipedija'}
};
gmap = {
	init: function(id,lat,lng,zoom) {
		if (GBrowserIsCompatible()) {
		var mapOptions = {
			googleBarOptions : {
			  style : "new"
			}
		  }

		map = new GMap2(document.getElementById(id));
		map.setCenter(new GLatLng(lat, lng), zoom);
		map.setUIToDefault();
	//	map.enableGoogleBar();

        map.addControl(new GOverviewMapControl());
  //      map.addMapType(G_SATELLITE_3D_MAP);
		//map.checkResize() // po div resaizinimo iskvieciam sita f-ja
		map.enableScrollWheelZoom();
		_mPreferMetric=true;
		map.hideControls();
		$('#coords').hide();
		GEvent.addListener(map, "mouseover", function(){
			map.showControls();
			$('#coords').show();
		});
		GEvent.addListener(map, "mouseout", function(){
			map.hideControls();
			$('#coords').hide();
		}); 
        GEvent.addListener(map, "mousemove", function(point) {
            if ( ! mapClicked) {
                var latLng = 'http://www.turizmokatalogas.lt/lietuvos-zemelapis.html?gps='+point.lat().toFixed(6) + ',' + point.lng().toFixed(6);
                $('#coords').attr("value", latLng);
            }
        });
		GEvent.addListener(map, "click", function() {
            if (mapClicked) {
                mapClicked = false;
            }
			else {
                mapClicked = true;
                $("#coords").select();
            }
        });
		GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", gmap.wheelevent);
		map.getContainer().onmousewheel = gmap.wheelevent; 
		var minMapScale = 7;
		var maxMapScale = 17;
		var mapTypes = map.getMapTypes();
		for (var i=0; i<mapTypes.length; i++) {
			mapTypes[i].getMinimumResolution = function() {return minMapScale;}
			mapTypes[i].getMaximumResolution = function() {return maxMapScale;}
		}
		GEvent.addListener(map, "move", function() {
			gmap.checkBounds();
		});
		
		$.each( layersData, function(){
			$('#layerPicker').html($('#layerPicker').html()+"<input type='checkbox' id='"+this.index+"' name='"+this.url+"' class='layerPickerCheckbox'/> <label for='"+this.index+"'>"+this.name+"</label> ");
			
		});
		$(".layerPickerCheckbox").change(function() {
				if(this.checked==true) {
					layer[this.id] = new GLayer(this.name);
					map.addOverlay(layer[this.id]);
				}
				if(this.checked!=true) {
					if (typeof(layer[this.id]) == 'object') {
						map.removeOverlay(layer[this.id]);
					}
				} 
			});
		
		
		$('#mapinfodiv').hide();
		}
		else {
			$('#mapinfodiv').html('Atleiskite, Jūsų naršyklė nesuderinama su šiuo žemėlapiu.');
		}
	},
	wheelevent: function(e){
		if (!e){
			e = window.event
		}
		if (e.preventDefault){
			e.preventDefault()
		}
		e.returnValue = false;
	},
	markerAdd: function(glat, glng){
		var latlng = new GLatLng(glat, glng);
		pointer = new GMarker(new GLatLng(latlng.lat().toFixed(6), latlng.lng().toFixed(6)), {draggable: false});
		map.addOverlay(pointer);
	},
	markerClear: function(){
		map.removeOverlay(pointer); 
	},
	checkBounds: function(){
		 // Perform the check and return if OK
        if (allowedBounds.contains(map.getCenter())) {
          return;
        }
        // It`s not OK, so find the nearest allowed point and move there
        var C = map.getCenter();
        var X = C.lng();
        var Y = C.lat();

        var AmaxX = allowedBounds.getNorthEast().lng();
        var AmaxY = allowedBounds.getNorthEast().lat();
        var AminX = allowedBounds.getSouthWest().lng();
        var AminY = allowedBounds.getSouthWest().lat();

        if (X < AminX) {X = AminX;}
        if (X > AmaxX) {X = AmaxX;}
        if (Y < AminY) {Y = AminY;}
        if (Y > AmaxY) {Y = AmaxY;}
        //alert ("Restricting "+Y+" "+X);
        map.setCenter(new GLatLng(Y,X));
	},
	resized: function() {
        if (typeof(map) == 'object' && map != null) {
            center = map.getCenter();
            map.checkResize();
            map.panTo(center);
        }
    }
	/*
	,
	localSearch: function (q) {
        ls = new google.search.LocalSearch();
        ls.setCenterPoint(map);
        ls.setSearchCompleteCallback(null, function () {
            if (!ls.results || typeof(ls.results[0]) != "object") {
                alert('Nieko neradau!');
                $('input[name=s]').val('');
                $('input[name=s]').focus();
                return false;
            }
            if (typeof(ls.results[1]) == 'object') {
                var p = ls.results[0];
             //   p.tpl_type = 0;
                p.id = timestamp();
            //    p.html = gMap.html_get(p);
              //  gMap.marker_add(p);
                map.setCenter(new GLatLng(p.lat, p.lng));
            } else {
                var p = ls.results[0];
                Ajax.add_place(p);
                p.tpl_type = 0;
                p.id = timestamp();
            //    p.html = gMap.html_get(p);
              //  gMap.marker_add(p);
                layer[p.id].openInfoWindow(p.html);
                map.setCenter(new GLatLng(p.lat, p.lng));
            };
            cLayout.rightOpen();
        });
        ls.execute(q);
    }
*/
};

$(window).unload( function () { GUnload(); } );


