// JavaScript Document
// JavaScript Document
var map;
var mgr;
var mapZoomLevel = 11;
function initialiseMap(){
	  map = new GMap2(document.getElementById("map"));
	  //map.setCenter(new GLatLng(52.647, -8.477), 6);
	  map.setCenter(new GLatLng(53.40298, -7.8222), 6);
	  map.setUIToDefault();
}

var layers = [];
var layerStatus = [];

function togglePOILayer(id){
	//Get the markers associated with the Layer with the given ID
	var markerArray = layers[id];
	//Get the current visibility status of the Layer with the given ID
	var isHidden = layerStatus[id];
	if(isHidden){
		//Layer is hidden
		//-1- Set hidden as FALSE
		//-2- Show each marker
		layerStatus[id]=false;
		for (var i = 0; i < markerArray.length; i++) {
			markerArray[i].show();
		}
	}else{
		//Layer is !hidden
		//-1- Set hidden as TRUE
		//-2- Hide each marker
		layerStatus[id]=true;
		for (var i = 0; i < markerArray.length; i++) {
			markerArray[i].hide();
		}
	}
	//mgr.refresh();
}

/**
Add GMarkers to the map and link them to the checkbox in the Category Menu
**/
function addPOILayer(markers, id, size, layerID, idPackage){
	fullLayerID = "layer_"+layerID
	//check if selected layer is already showing or added
	checkBoxes = document.getElementById("categoryCheckboxes");
	html = checkBoxes.innerHTML;
	
    html = html + '<div id="chkbx-table">' ;      
    html = html + '<table cellspacing="0" cellpadding="0px" style="table-layout: fixed;">';
    html = html + '<tr>';

	//if(layers[id] == undefined){
		for(i = 0; i < markers.length;i++){
			map.addOverlay(markers[i]);
			markers[i].show();
			markers[i].layerName = id;
		}
		layers[fullLayerID] = markers;
		layerStatus[fullLayerID] = false;

        html = html + '<td class="column1">';
   	    html = html + "<label id=\""+fullLayerID+"\" for=\"checkbox_"+fullLayerID+"\"><strong>"+id+"</strong></label>";
        html = html + '</td>';
        html = html + '<td class="column2">';
		html = html + "<label for=\"checkbox_"+fullLayerID+"\"><strong> ("+size+")</strong></label>";
        html = html + '</td>';
        html = html + '<td class="column3">';
		html = html + "<input id=\"checkbox_"+fullLayerID+"\" type=\"checkbox\" value=\""+id+"\" onclick=\"togglePOILayer('"+fullLayerID+"');\" checked />";
        html = html + '</td>';
        html = html + '<td class="column4">';
		html = html + "<label for=\"checkbox_"+fullLayerID+"\"><img src=\""+rootImageDirectory+"menuIcons/"+idPackage+".png\" style=\"padding-bottom:1px;\"></label><br>\n";
        html = html + '</td>';

//	}

    html = html + '</tr>';
    html = html + '</table>';
    html = html + '</div>';

    checkBoxes.innerHTML = html;

}

/**
Add GMarkers to the map for the root POI, assign it to a disabled checkbox
**/
function addRootPOILayer(marker, id){
	//check if selected layer is already showing or added
	if(layers[id] == undefined){
		map.addOverlay(marker);
		marker.show();
		marker.layerName = id;
		
		layers[id] = marker;
		layerStatus[id] = false;
	}
}
