﻿var map;
var zoomLevel = 1;

function LoadMaps()
{
	map = new VEMap("courseMap");
	countryLayer = new VEShapeLayer();

	map.onLoadMap = openCoursesMap;
	map.SetDashboardSize(VEDashboardSize.Tiny);
	map.AttachEvent("onmousewheel", function() { return true; });
	map.LoadMap(new VELatLong(0, 0), zoomLevel, VEMapStyle.Road);

	map.AttachEvent("onclick", Map_Click);
}

function openCoursesMap()
{
	map.SetMouseWheelZoomToCenter(false);
	for(var i=0; i<countryArray.length;i++)
	{
		var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(countryArray[i].lat, countryArray[i].long));
		shape.SetCustomIcon('<img src="/_images/icons/pin_blue.png" />');
		shape.SetTitle('<div style="text-align:center;padding-bottom:10px"><a style="text-decoration:underline;color:#0000CC" href="' + countryArray[i].link + '">' + countryArray[i].name + '</a></div>');
		shape.ourLink = ('<a style="text-decoration:underline;color:#0000CC" href="' + countryArray[i].link + '">' + countryArray[i].name + '</a>');
		shape.countryLink = countryArray[i].link;
		countryLayer.AddShape(shape);
	}
	map.AddShapeLayer(countryLayer);
	
    var options_country =  new VEClusteringOptions();

    options_country.Callback = clusterCallback;

    var customCourseIcon = new VECustomIconSpecification();
    customCourseIcon.Image = "/_images/icons/pin_blue_c.png";
    options_country.Icon = customCourseIcon;

	countryLayer.SetClusteringConfiguration(VEClusteringType.Grid, options_country);

}
function clusterCallback(items)
{
	for(var i=0; i<items.length; i++)
	{
		var cluster = items[i]; 
		var clusterShape = cluster.GetClusterShape(); 
		clusterShape.SetTitle(cluster.Shapes.length + " countrys in this area.");
		clusterShape.SetDescription("<span>" + clusterShape.GetDescription() + "</span>");
		clusterShape.isCluster = true;
		for(var j=0; j<cluster.Shapes.length; j++)
			clusterShape.SetDescription(clusterShape.GetDescription() + '<div style="padding-top:3px;">' + cluster.Shapes[j].ourLink + "</div>"); 
	}
}

function Map_Click(e)
{
	zoomLevel = map.GetZoomLevel();
	if (e.elementID != null)
	{
		zoomLevel++;
		var shape = map.GetShapeByID(e.elementID);
		if(shape.isCluster)
			map.SetCenterAndZoom(shape.GetPoints()[0], zoomLevel);
		else
			location.href = shape.countryLink;
	}
}

Ekina.Events.register(window, "onload", function() { LoadMaps(); });
