var map = null;
var hideBanner = true;

$(function(){
	if ($.browser.msie)
	{
		initIeMap();
	}
	else
	{
		if (GBrowserIsCompatible()) {initMap();};
	}
	prepareZoom();
    
    

    $('.bookletLink').click(function(){        
        hideBanner = false;
    });
	$('#bannerOverlay').click(function(){
        if(hideBanner==true)
        {
            $(this).hide();
        }
        hideBanner = true;
	});    
});

function initIeMap()
{
	if (window.points === undefined)
	{
		setTimeout(initIeMap, 500);
	}
	else
	{
		initMap();
	}
}

function initMap()
{
	map = new GMap2(document.getElementById("mapCanvas"));
	map.setCenter(new GLatLng(56.648889, 23.406389), 7);
	map.disableScrollWheelZoom();
	map.enableContinuousZoom();
	map.addControl(
		new GSmallZoomControl3D(),
		new GControlPosition(0, new GSize(10, 90)) 	
	);
	
	for (var i = 0; i < points.length; i++)
	{        
        var baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.image = '/images/map-marker-'+points[i].iconType+'.png';
        baseIcon.shadow = '/images/map-shadow-'+points[i].iconType+'.png';
        if(points[i].iconType == 1)
        {
            baseIcon.iconSize = new GSize(32, 46);
            baseIcon.shadowSize = new GSize(32, 46);
            baseIcon.iconAnchor = new GPoint(12, 46);
            baseIcon.infoWindowAnchor = new GPoint(16, 210);
            baseIcon.imageMap = [12, 47, 24, 33, 24, 10, 19, 4, 12, 0, 5, 4, 0, 10, 0, 33];
        }
        else
        {
            baseIcon.iconSize = new GSize(53, 47);
            baseIcon.shadowSize = new GSize(53, 50);
            baseIcon.iconAnchor = new GPoint(2, 44);
            baseIcon.infoWindowAnchor = new GPoint(3, 210);
            baseIcon.imageMap = [0, 47, 0, 0, 53, 0, 53, 33, 8, 33, 8, 47];
        }
    
		spawnMarker(points[i], baseIcon);
	}
}

function spawnMarker(point, baseIcon)
{
	var marker = new GMarker(point.point, {icon: baseIcon});

	GEvent.addListener(marker, "click", function() {
        this.openExtInfoWindow(
			map,
			"simple_example_window",
			null,
			{
				ajaxUrl: point.ajaxUrl
			}
        );
	});
	
	map.addOverlay(marker);
}

function prepareZoom()
{
	$('#zoomLatvia').click(function(e){
		e.preventDefault();
		$('#zoomRiga').removeClass('active');
		$(this).addClass('active');
		map.closeExtInfoWindow();
		map.setCenter(new GLatLng(56.948889, 24.106389), 7);
	});
	$('#zoomRiga').click(function(e){
		e.preventDefault();
		$('#zoomLatvia').removeClass('active');
		$(this).addClass('active');
		map.closeExtInfoWindow();
		map.setCenter(new GLatLng(56.948889, 24.106389), 12);
	});
	$('#mapCanvas .close').live('click', function(){
		map.closeExtInfoWindow();
	});
}
