DOMAssistant.DOMReady(loadFunctions);

function loadFunctions() {
var x=$(document).elmsByAttribute("type", "checkbox", "input");
x.each(function () {
$(this).addEvent("click", changeLayer);
});
}

function changeLayer() {
window.ajaxstatus=getBusyOverlay($$("map"),{opacity:0.5,text:"Loading..."});
var layerID=$(this).name.replace("layerfilt[","");
layerID=layerID.replace("]","");
if (window.layers[layerID]!=undefined) removeLayer(layerID);
else addLayer(layerID);
}

function removeLayer(layerID) {
for (key in window.marker)
   {
   tempmarker=window.marker[key];
   if (tempmarker["l"]==layerID)
      {
      tempmarker.remove();
      tempmarker["i"]=null; tempmarker["l"]=null;
      }
   }
window.layers[layerID]=null;
setCookie("layerfilt["+layerID+"]","1","-90");
window.ajaxstatus.remove();
}

function addLayer(layerID) {
$(document).get("ajax/requestlayer.php?layerID="+layerID, displayLayer);
window.layers[layerID]=1;
}

function displayLayer(response) {
var line, lat, long, title, icon, url, layerID, info, tooltip, testno;
response=response.split("|~\n");
for (i=0;i<(response.length-1);i++)
   {
   line=response[i];
   line=line.split("|*");
   lat=line[0]; long=line[1];
   title=line[2]; icon=line[3];
   url=line[4]; layerID=line[5];
   info=line[6];
   window.point[window.lastpoint]=new GLatLng(lat,long);
   window.marker[window.lastpoint]=new PdMarker(window.point[window.lastpoint],window.icon[layerID]);
   window.marker[window.lastpoint]["l"]=layerID;
   tooltip="<strong>"+title+"</strong>";
   if (info) tooltip=tooltip+"<br /><em>Click for more information</em>";
   window.marker[window.lastpoint].setTooltip(tooltip);
   window.map.addOverlay(window.marker[window.lastpoint]);
   infowindow='<div class="infotxt">';
   if (url) infowindow=infowindow+'<a href="'+url+'" title="'+title+'">';
   infowindow=infowindow+'<strong>'+title+'</strong>';
   if (url) infowindow=infowindow+'</a>';
   infowindow=infowindow+'<br />'+info.replace("\n","<br />");
   infowindow=infowindow+'</div>';
   window.marker[window.lastpoint]["i"]=infowindow;
   GEvent.addListener(window.marker[window.lastpoint], 'click', function() {this.openInfoWindowHtml(this["i"]);} );
   window.lastpoint++;
   }
setCookie("layerfilt["+layerID+"]","1","90");
window.ajaxstatus.remove();
}

function setCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}