var map = null;
var geocoder = null;
var icon = new GIcon();
icon.image = "marker3.png";
icon.iconSize = new GSize(42, 45);
icon.iconAnchor = new GPoint(0, 20);
icon.infoWindowAnchor = new GPoint(135,0) //(115, 53);

markersList=[];
lats=[];
lngs=[]
gebouw=[];
foto=[];
naaminfo=[];
adres=[];
postbus=[]
onderdelen=[];
openingstijden=[];
loc=-1;
lang="nl";
infoLoc=-1;
realInfoLength=0;
singleSelected=true;
oneToShow=-1;
zoomfactor=14;
sideBarDiv = document.createElement("div");
sideBarDiv.style.display="none";
sideBarDiv.id="sideBarDiv";
sortBy="az";
shownOne=-1;
showIds=false


function load() {

document.getElementById("sortForm").reset()
  if (GBrowserIsCompatible()) {

    	map = new GMap2(document.getElementById("map"));
		
	map.setCenter(new GLatLng(52.156822,4.481634),zoomfactor);
	var extLargeMapControl = new ExtLargeMapControl();
       map.addControl(extLargeMapControl);
	   map.addControl(new GMapTypeControl());
	 
    //create randomnumber and retrieve xml file
    var randomnumber=Math.floor(Math.random()*11111)
    GDownloadUrl("mapdata3.xml?random="+randomnumber, function(data, responseCode) {
    var xml = GXml.parse(data);

    //store markers in markers array
	var markers = xml.documentElement.getElementsByTagName("marker");
	allMarkers=markers.length
			
	infoLength=markers[0].childNodes.length
			
	for (q=0; q<infoLength;q++) {
	if (markers[0].childNodes[q].nodeType==1) realInfoLength++
	}

	infogebieden=[]
	for (z=0;z < (realInfoLength+1); z++) {
	infogebieden[z]=[]
	}

markerCounter=0;
b=0;
c=1;
var d;

points=[]
theIds=[]


for (y=0;y<allMarkers;y++) {
markersList[y]=-1
}
	while (b<(infoLength+1)) {
	if (markers[markerCounter].childNodes[b].nodeType==1) {
		theValue=markers[markerCounter].childNodes[b].childNodes[0].nodeValue
		//if (markers[markerCounter].childNodes[b].tagName=="id") {d=theValue}
	infogebieden[c].push(theValue)
		//if (c==0) {theId=theValue}
	
	if (loc==theValue) {oneToShow=loc}
	if (markers[markerCounter].childNodes[b].tagName=="coordinaten") {
		latlng=markers[markerCounter].childNodes[b].childNodes[0].nodeValue+""
		comma=latlng.indexOf(",")
		lat=latlng.substring(0,comma)
		lng=latlng.substring(comma+1)
		point=new GLatLng(lat, lng)
		points.push(point);		
	}
	c++
		}
	b++;
	if (b==infoLength) {	
		markerCounter++;
		b=0;c=1;
		if (markerCounter==allMarkers) {break}
		}
		
			}
		
		for (q=0;q<infogebieden[1].length;q++) {
		infogebieden[0].push(q)
		}	
	if (oneToShow!=-1) {
		//map.addOverlay(createMarker(points[oneToShow],oneToShow,icon));
		processClick(oneToShow)
		}
gebouwnamen=[]
for (h=0;h<infogebieden[0].length;h++) {
gebouwnamen.push(new Array(2))
gebouwnamen[h][0]=(lang=="nl"
	? infogebieden[2][h].replace(/\[en\](.*)/gi,"") 
	: infogebieden[2][h].replace(/.*\[en\]/gi,""))
	.replace(/<br>/gi,"")
gebouwnamen[h][1]=infogebieden[1][h]
}
gebouwnamen=gebouwnamen.sort()
toonAlleGebouwen()
 }
        ); //close GDownloadUrl
  } //close GBrowserIsCompatible

if (window.location.href.search(/(&|\?)lang=/gi)!=-1) {
lang=window.location.href
.replace(/(.*(?=lang))/gi,"")
.replace(/lang=/gi,"")
.replace(/&(.)*/g,"")
}

if (window.location.href.search(/(&|\?)sortby=/gi)!=-1) {
sortBy=window.location.href
.replace(/(.*(?=sortby))/gi,"")
.replace(/sortby=/gi,"")
.replace(/&(.)*/g,"")

}

if (window.location.href.search(/(&|\?)loc=/gi)!=-1) {
loc=window.location.href
.replace(/(.*(?=loc))/gi,"")
.replace(/loc=/gi,"")
.replace(/&(.)*/g,"")
}

if (window.location.href.search(/(&|\?)showIds/gi)!=-1) {
showIds=true
}

titleText=document.createTextNode((lang=="nl" ? "Locaties Universiteit leiden" : "Locations Leiden University"))
document.getElementById("title").appendChild(titleText)

mapChSpan=document.createElement("span");
document.getElementById("banner").appendChild(mapChSpan);
mapChText=document.createTextNode((lang=="nl" ? "In English" : "Nederlands"))
mapChSpan.setAttribute("id","mapCh");

mapChLink=document.createElement("a")
curAddress=window.location.href;
divider="&";

if(curAddress.search("loc=")==-1 && curAddress.search("sortby=") ==-1) {divider="?"}
mapChLinkAddress=""

if(curAddress.search("lang=")==-1) {mapChLinkAddress=curAddress+divider+"lang="+(lang=="nl" ? "en" : "nl")}
else mapChLinkAddress=curAddress.replace(/lang=(\D{2})/gi,function(a){return("lang="+(lang=="nl" ? "en" : "nl"))})

mapChLink.appendChild(mapChText)

mapChSpan.appendChild(mapChLink)
mapChLink.setAttribute("href",mapChLinkAddress)

	} //close load


//Create marker and set up event window
  function createMarker(point,markerCounter,icon){
  
  	var marker = new GMarker(point,icon); 
	markersList[markerCounter]=marker
	
		GEvent.addListener(marker, "click", function() {
	shownOne=markerCounter;
		marker.openExtInfoWindow( map, 
              "infoBubble",
	 "<div class='title'>"+(lang=="nl"
	? infogebieden[2][markerCounter].replace(/\[en\](.*)/gi,"") 
	: infogebieden[2][markerCounter].replace(/.*\[en\](<br>)*/gi,""))
	+"</div>"

	+ "<div class='section'><h4>" + (lang=="nl" ? "Bezoekadres" : "Address")+"</h4><p>"+infogebieden[6][markerCounter]+"</p>"
	+ (infogebieden[4][markerCounter] !="" ? "<h4>" +(lang=="nl" ? "Gevestigd op deze locatie" : "At this location")+"</h4><p>"+ (lang=="nl" ? infogebieden[4][markerCounter].replace(/(<br(\/)*>)*\[en\](.*)/gi,"") : infogebieden[4][markerCounter].replace(/.*\[en\](<br(\/)*>)*/gi,"")) : "") 
 	+ "</p></div>",
	
              {beakOffset: 3}
            ); 
        });

        return marker;
  }

function toonAlleGebouwen() {
/*
masterButton=document.createElement("input")
masterButton.setAttribute("type","button")
masterButton.setAttribute("value",(lang=="nl" ? "Lijst van alle universitaire locaties" : "List of all university locations"))
masterButton.id="masterButton"
*/

showAllLink=document.createElement("a")
showAllLinkText=document.createTextNode(lang=="nl" ? "Alle universitaire locaties" : "All university locations")
showAllLink.appendChild(showAllLinkText)
showAllLink.href="javascript:void(0)"
showAllLink.id="showAllLink"

sortHeaderText=document.createTextNode(lang=="nl" ? "Sorteer locaties op:" : "Sort locations by:")
azLabelText=document.createTextNode(lang=="nl" ? "Alfabet" : "Alphabet")
facLabelText=document.createTextNode(lang=="nl" ? "Faculteit" : "Faculty")
document.getElementById("sortHeader").appendChild(sortHeaderText)
document.getElementById("facLabel").appendChild(facLabelText)
document.getElementById("azLabel").appendChild(azLabelText)

masterCheckbox=document.createElement("input")
masterCheckbox.setAttribute("type","checkbox")
masterCheckboxLabel=document.createElement("label")
nodeText=(lang=="nl" ? "Vink alles aan/uit" : "(De)Select all")
masterCheckboxLabelText=document.createTextNode(nodeText)
masterCheckboxLabel.appendChild(masterCheckboxLabelText)
masterCheckboxLabel.style.margin="0px";
masterCheckbox.id="masterCheckbox"
masterCheckboxLabel.id="masterCheckboxLabel"
masterCheckboxLabel.setAttribute("htmlFor","masterCheckbox") //IE 7
masterCheckboxLabel.setAttribute("for","masterCheckbox")

masterCheckbox.onclick=function() {
singleSelected=false;
allInputElements=sideBarDiv.getElementsByTagName("input")
for (v =0; v< allInputElements.length;v++) {
allInputElements[v].checked=masterCheckbox.checked
if (masterCheckbox.checked) {
		x=-1;
		boxId=allInputElements[v].id
		processClick(boxId)
	}
else {
for (g=0;g<markersList.length;g++) {if (markersList[g]!=-1) markersList[g].hide()};
map.closeExtInfoWindow()
}
}
singleSelected=true;
}


document.getElementById('sideBar').appendChild(showAllLink);
document.getElementById('sortForm').appendChild(masterCheckbox);
document.getElementById('sortForm').appendChild(masterCheckboxLabel);
document.getElementById('sideBar').appendChild(sideBarDiv);
getHeights()
sideBarDiv.style.height=document.getElementById("sideBar").style.height=sideHeight+"px"
document.getElementById("map").style.height=mapHeight+"px"

window.onresize=function() {
getHeights()
if (sideHeight>200) {
window.status=sideHeight
sideBarDiv.style.height=document.getElementById("sideBar").style.height=sideHeight+"px"
}

if (mapHeight>330) {
document.getElementById("map").style.height=mapHeight+"px"
}

}


if (loc==-1) {
document.getElementById("sortForm").style.display="block";
showAllLink.style.display="none";
sideBarDiv.style.display="block"
masterCheckbox.style.display="block";
masterCheckboxLabel.style.display="block";
document.getElementById("sort").style.display="block"
}

else {showAllLink.onclick=function() {
sideBarDiv.style.display = "block";
document.getElementById("sortForm").style.display="block"
this.style.display = "none";
masterCheckbox.style.display="block";
masterCheckboxLabel.style.display="block";
loc=-1;
getHeights()
sideBarDiv.style.height=document.getElementById("sideBar").style.height=sideHeight+"px"
if (document.getElementById(oneToShow)) {document.getElementById(oneToShow).checked=true};
}
}

if (sortBy=="az") {
	document.getElementById("az").checked=true
	 showAZ()
	}

else {
	document.getElementById("fac").checked=true
	 showFac()
	}

if (showIds) showLocationIds()
}

function getHeights() {
if (loc==-1) {sideHeight=document.documentElement.clientHeight-330}
else {sideHeight=document.documentElement.clientHeight-194}
mapHeight=document.documentElement.clientHeight-199.5
}

function showAZ() {
if (loc==-1) closeAll()
allNodes= (sideBarDiv.childNodes.length)
for (r=allNodes-1;r>-1;r--) {
eraseMe=sideBarDiv.childNodes[r]
eraseMe.parentNode.removeChild(eraseMe)
}
currentLetter=""
for (g=0;g<infogebieden[0].length;g++) {
	
	letter=gebouwnamen[g][0].substring(0,1)
	letterText=document.createTextNode(letter)
	
	sideBarP = document.createElement("p");
	sideBarText=document.createTextNode(gebouwnamen[g][0])
	sideBarA= document.createElement("a");
	sideBarSpan= document.createElement("span");
	sideBarA.appendChild(sideBarSpan);	
	sideBarSpan.appendChild(sideBarText);

	sideBarCheckbox=document.createElement("input")
	sideBarCheckbox.setAttribute("type","checkbox")
	
	if (letter!=currentLetter) {
	letterHeader=document.createElement("h5")
	if (g==0) letterHeader.id="first" 
	
	sideBarDiv.appendChild(letterHeader)
	letterHeader.appendChild(letterText)
	currentLetter=letter
	}

	sideBarP.appendChild(sideBarCheckbox);
	sideBarP.appendChild(sideBarA);
	
	sideBarDiv.appendChild(sideBarP);
	sideBarCheckbox.id=gebouwnamen[g][1];
	
	sideBarCheckbox.onclick=function() {
			x=0;
			for (a=0;a<infogebieden[0].length;a++) {
			if (infogebieden[1][a]==this.id) {x=a;break}
			}
			if (this.checked) {
							if (markersList[x]==-1) {
				map.addOverlay(createMarker(points[x],x,icon))
				}
			else if (markersList[x]!=-1){
				markersList[x].show()
					}
			map.setCenter(points[x]);			
		} else {
		markersList[x].hide();
		if (x==shownOne) map.closeExtInfoWindow();
		}
	}
	sideBarA.setAttribute("href","javascript:processClick("+gebouwnamen[g][1]+")");
	} 
}

function showFac() {
if (loc==-1) closeAll();
allNodes= (sideBarDiv.childNodes.length)
for (r=allNodes-1;r>-1;r--) {
eraseMe=sideBarDiv.childNodes[r]
eraseMe.parentNode.removeChild(eraseMe)
}

arc=[]
arc.id=(lang=="nl" ? "Archeologie" : "Archaeology")

fsw=[]
fsw.id=(lang=="nl" ? "Sociale Wetenschappen" : "Social and Behavioural Sciences")

hum=[]
hum.id=(lang=="nl" ? "Geesteswetenschappen" : "Humanities")

gk=[]
gk.id=(lang=="nl" ? "Geneeskunde" : "Medicine")

sci=[]
sci.id=(lang=="nl" ? "Wiskunde en Natuurwetenschappen" : "Science")

law=[]
law.id=(lang=="nl" ? "Rechtsgeleerdheid" : "Law")

cdh=[]
cdh.id=(lang=="nl" ? "Campus Den Haag" : "Campus The Hague")

facArrays=(lang=="nl" ? new Array(arc,cdh,hum,gk,law,fsw,sci) : new Array(arc,cdh,hum,law,gk,sci,fsw))
arcIndex=(lang=="nl" ? 0 : 0)
humIndex=(lang=="nl" ? 2 : 2)
gkIndex=(lang=="nl" ? 3 : 4)
lawIndex=(lang=="nl" ? 4 : 3)
fswIndex=(lang=="nl" ? 5 : 6)
sciIndex=(lang=="nl" ? 6 : 5)
cdhIndex=(lang=="nl" ? 1 : 1)

for (g=0;g<infogebieden[0].length;g++) {
var a = infogebieden[3][g];
if (a.search(arc.id)!=-1) {addFac(g,facArrays[arcIndex])}
if (a.search(fsw.id)!=-1) {addFac(g,facArrays[fswIndex])}
if (a.search(hum.id)!=-1) {addFac(g,facArrays[humIndex])}
if (a.search(gk.id)!=-1) {addFac(g,facArrays[gkIndex])}
if (a.search(sci.id)!=-1) {addFac(g,facArrays[sciIndex])}
if (a.search(law.id)!=-1) {addFac(g,facArrays[lawIndex])}
if (a.search(cdh.id)!=-1) {addFac(g,facArrays[cdhIndex])}
}

for (g=0;g<facArrays.length;g++) {
facArrayElement=facArrays[g]
if (facArrayElement.length>0) {
facName=facArrayElement.id
facText=document.createTextNode(facName)
facHeader=document.createElement("h5")
if (g==0) facHeader.id="first"
facHeader.appendChild(facText)
sideBarDiv.appendChild(facHeader)

for (q=0;q<facArrayElement.length;q++) {
	sideBarP = document.createElement("p");
	sideBarText=document.createTextNode(
	(lang=="nl"
	? infogebieden[2][facArrayElement[q][2]].replace(/\[en\](.*)/gi,"") 
	: infogebieden[2][facArrayElement[q][2]].replace(/.*\[en\](<br>)*/gi,""))
	.replace(/<br>/gi,"")
	)
	sideBarA= document.createElement("a");
	sideBarSpan= document.createElement("span");
	sideBarA.appendChild(sideBarSpan);	
	sideBarSpan.appendChild(sideBarText);
	
	sideBarCheckbox=document.createElement("input")
	sideBarCheckbox.setAttribute("type","checkbox")
	
	sideBarP.appendChild(sideBarCheckbox);
	sideBarP.appendChild(sideBarA);
	sideBarDiv.appendChild(sideBarP);
	
	sideBarCheckbox.id=facArrayElement[q][1];
	
	sideBarCheckbox.onclick=function() {
		for (a=0;a<infogebieden[0].length;a++) {
			if (infogebieden[1][a]==this.id) {x=a;break}
			}
		if (this.checked) {
				if (markersList[this.id]==-1) {
				map.addOverlay(createMarker(points[x],x,icon))
				}
			else if (markersList[x]!=-1){
				markersList[x].show()
				}
			map.setCenter(points[x]);			
		} else {
		markersList[x].hide();
		if (x==shownOne) map.closeExtInfoWindow();
		}
	}
	
		sideBarA.setAttribute("href","javascript:processClick("+facArrayElement[q][1]+")");
}
}
}
}

function closeAll() {
masterCheckbox.checked=false;
allInputElements=sideBarDiv.getElementsByTagName("input")
for (v=0;v<allInputElements.length;v++) {
if (allInputElements[v].type=="checkbox") (allInputElements[v].checked=false)
}

for (g=0;g<markersList.length;g++) {if (markersList[g]!=-1) markersList[g].hide()};
map.closeExtInfoWindow()
loc=-1;
}

function addFac(g,fac) {
x=[]
x[0]=infogebieden[2][g]
x[1]=infogebieden[1][g]
x[2]=g
fac.push(x)
fac.sort()
}

  function processClick(spot) {
  
    infoLoc=spot;
	if (sideBarDiv.style.display=="block" && document.getElementById(spot)) {document.getElementById(spot).checked=true}
	
	for (a=0;a<infogebieden[0].length;a++) {
	if (infogebieden[1][a]==spot) {spot=a;break}
	}
	
	if (markersList[spot]==-1) {
	
	map.addOverlay(createMarker(points[spot],spot,icon))
	} else {
	markersList[spot].show()
	}	
	
	if (singleSelected) {
	GEvent.trigger(markersList[spot], "click");
	map.setCenter(points[spot]);
	}
	}
	
	function showLocationIds() {
	alert(gebouwnamen.join('\n').replace(/,/gi,": ")); 
	}
	
	
