var map = null;

var mapLayers = new Array();

//var siteUrl = 'http://localhost/greenkey/site/';

var siteUrl = 'http://www.snowdoniagreenkey.co.uk/';



function createMap() {

	map = new VEMap('myMap');

    map.LoadMap(new VELatLong(53.02552230330848, -3.9440917968750013), 10 ,'r' ,false);

}



function createLayer(layerid) {

	//create a layer and add it to the array

	var layer = new VEShapeLayer();

	var veLayerSpec = new VEShapeSourceSpecification(VEDataType.GeoRSS, siteUrl + 'georss.asp?id=' + layerid, layer);

    map.ImportShapeLayerData(veLayerSpec, afterLoadLayer, false);

    mapLayers['cat' + layerid] = layer;	

	layer.Hide();	

}





function afterLoadLayer(layer) {

	//set custom icons

	var numShapes = layer.GetShapeCount();

    for(var i=0; i < numShapes; ++i)

    {

        var s = layer.GetShapeByIndex(i);

        if (s.IconId != "") {
			var mySplitResult = String(s.IconId).split(":");
	        
        	s.SetCustomIcon("<img src='" + siteUrl + "images/mapicons/" + mySplitResult[0] + "'/>");
        	 if (s.GetType() == VEShapeType.Polyline) {
	        	 var mySplitColor = String(mySplitResult[1]).split(" ");
	        	 var color = new VEColor(mySplitColor[0],mySplitColor[1],mySplitColor[2],0.8)
	        	 s.SetLineColor(color);
        	}
        	//s.SetCustomIcon("<img src='" + siteUrl + "images/mapicons/" + s.IconId + "'/>");

        }

    }

}



/*

function addPointToLayer(layerid, shapeid, latitude, longitude, strTitle, strDescription, strIcon) {

	//add a point to the layer

	var ll = new VELatLong(latitude, longitude);

	var point = new VEShape(VEShapeType.Pushpin, ll);            

    point.SetTitle(strTitle);

    point.SetDescription(strDescription);

    point.SetCustomIcon('images/mapicons/' + strIcon);

    point.ShowIcon();

    //point.SetMoreInfoURL('<a href="http://localhost/greenkey/site/mapdetail.asp?shapeid=' + shapeid + '">More Info</a>');

    mapLayers['cat' + layerid].AddShape(point);

}

*/



function AddPoint(latitude,longitude,strTitle,strIcon) {

	var ll = new VELatLong(latitude, longitude);

	var point = new VEShape(VEShapeType.Pushpin, ll);            

    point.SetTitle(strTitle);

    point.SetCustomIcon('images/mapicons/' + strIcon);

    point.ShowIcon();

    map.AddShape(point);

    

    return point;

}



String.prototype.trim = function() {

	return this.replace(/^\s+|\s+$/g,"");

}



function AddLine(points,strTitle,strIcon) {

	var arrPoints = String(points).trim().split(" ");

	arrLL = new Array();

	for(var i=0; i < arrPoints.length; i=i+2)

    {

	    //alert('made LL ' + arrPoints[i] + ' ' + arrPoints[i+1]);

        arrLL[arrLL.length] = new VELatLong(parseFloat(arrPoints[i]), parseFloat(arrPoints[i+1]));

    }

	

	

	var line = new VEShape(VEShapeType.Polyline,arrLL);   

	//line.SetPoints(arrLL); 

    line.SetTitle(strTitle);

    line.SetCustomIcon('images/mapicons/' + strIcon);

    line.ShowIcon();

    try {

    	map.AddShape(line);

	} catch (e) {

		alert (e.message);

	}

	

	return line;

}



function centreMap(lat, long, zoom) {

	map.SetCenterAndZoom(new VELatLong(lat,long), zoom);

}



function addLine(layerid) {

	//add a line to the layer	

}



function toggleLayer(layerid) {

	//show or hide the layer

	var layer = mapLayers['cat' + layerid];

	var obj = document.getElementById('togglebutton' + layerid);

	

	if (layer.IsVisible()) {

		

		layer.Hide()

		obj.setAttribute('class','maplayerbutton_off');	

		obj.setAttribute('className','maplayerbutton_off');	

	} else {

		layer.Show()

		obj.setAttribute('class','maplayerbutton_on');	

		obj.setAttribute('className','maplayerbutton_on');	

	}

}


